How to use the alcalzone-shared/math.roundTo function in alcalzone-shared

To help you get started, we’ve selected a few alcalzone-shared examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github AlCalzone / node-tradfri-client / build / lib / conversions.js View on Github external
h = 60 * (0 + (g - b) / (max - min));
    }
    else if (max === g) {
        h = 60 * (2 + (b - r) / (max - min));
    }
    else if (max === b) {
        h = 60 * (4 + (r - g) / (max - min));
    }
    h = Math.round(h); // h is defined, we checked all possibilities
    if (h < 0)
        h += 360;
    if (max === 0) {
        s = 0;
    }
    else {
        s = math_1.roundTo((max - min) / max, 3);
    }
    return { h, s, v };
}
function rgbFromHSV(h, s, v) {
github AlCalzone / node-tradfri-client / src / lib / conversions.ts View on Github external
function rgbToHSV(r: number, g: number, b: number) {
	// transform [0..255] => [0..1]
	[r, g, b] = [r, g, b].map(c => c / 255);
	const max = Math.max(r, g, b);
	const min = Math.min(r, g, b);
	let h: number;
	let s: number;
	const v: number = roundTo(max, 2);
	if (r === g && g === b) {
		h = 0;
	} else if (max === r) {
		h = 60 * (0 + (g - b) / (max - min));
	} else if (max === g) {
		h = 60 * (2 + (b - r) / (max - min));
	} else if (max === b) {
		h = 60 * (4 + (r - g) / (max - min));
	}
	h = Math.round(h!); // h is defined, we checked all possibilities
	if (h < 0) h += 360;

	if (max === 0) {
		s = 0;
	} else {
		s = roundTo((max - min) / max, 3);
github AlCalzone / node-tradfri-client / build / lib / conversions.js View on Github external
const colorTemperature_out = (value) => {
    const [min, max] = predefined_colors_1.colorTemperatureRange;
    // extrapolate 0-100% to [min..max]
    value = math_1.clamp(value, 0, 100);
    return math_1.roundTo(min + value / 100 * (max - min), 0);
};
// interpolate from [250..454] to [0..100%]
github AlCalzone / node-tradfri-client / build / lib / conversions.js View on Github external
function rgbToHSV(r, g, b) {
    // transform [0..255] => [0..1]
    [r, g, b] = [r, g, b].map(c => c / 255);
    const max = Math.max(r, g, b);
    const min = Math.min(r, g, b);
    let h;
    let s;
    const v = math_1.roundTo(max, 2);
    if (r === g && g === b) {
        h = 0;
    }
    else if (max === r) {
        h = 60 * (0 + (g - b) / (max - min));
    }
    else if (max === g) {
        h = 60 * (2 + (b - r) / (max - min));
    }
    else if (max === b) {
        h = 60 * (4 + (r - g) / (max - min));
    }
    h = Math.round(h); // h is defined, we checked all possibilities
    if (h < 0)
        h += 360;
    if (max === 0) {
github AlCalzone / node-tradfri-client / build / lib / conversions.js View on Github external
const brightness_in = (value) => {
    value = math_1.clamp(value, 0, 254);
    if (value === 0)
        return 0;
    value = value / 254 * 100;
    return math_1.roundTo(value, 1);
};
// ===========================
github AlCalzone / node-tradfri-client / build / lib / conversions.js View on Github external
const hue_in = (value /*, light: Light*/) => {
    value = math_1.clamp(value / predefined_colors_1.MAX_COLOR, 0, 1);
    return math_1.roundTo(value * 360, 1);
};
// interpolate saturation from [0..100%] to [0..COLOR_MAX]
github AlCalzone / node-tradfri-client / build / lib / conversions.js View on Github external
const saturation_out = (value, light) => {
    if (light != null && light.spectrum !== "rgb")
        return null; // hue is not supported
    value = math_1.clamp(value, 0, 100);
    return math_1.roundTo(value / 100 * predefined_colors_1.MAX_COLOR, 0);
};
// interpolate saturation from [0..COLOR_MAX] to [0..100%]
github AlCalzone / node-tradfri-client / src / lib / conversions.ts View on Github external
if (r === g && g === b) {
		h = 0;
	} else if (max === r) {
		h = 60 * (0 + (g - b) / (max - min));
	} else if (max === g) {
		h = 60 * (2 + (b - r) / (max - min));
	} else if (max === b) {
		h = 60 * (4 + (r - g) / (max - min));
	}
	h = Math.round(h!); // h is defined, we checked all possibilities
	if (h < 0) h += 360;

	if (max === 0) {
		s = 0;
	} else {
		s = roundTo((max - min) / max, 3);
	}
	return {h, s, v};
}
github AlCalzone / node-tradfri-client / src / lib / conversions.ts View on Github external
const colorTemperature_out: PropertyTransformKernel = (value) => {
	const [min, max] = colorTemperatureRange;
	// extrapolate 0-100% to [min..max]
	value = clamp(value, 0, 100);
	return roundTo(min + value / 100 * (max - min), 0);
};
// interpolate from [250..454] to [0..100%]
github AlCalzone / node-tradfri-client / src / lib / conversions.ts View on Github external
const hue_in: PropertyTransformKernel = (value /*, light: Light*/) => {
	value = clamp(value / MAX_COLOR, 0, 1);
	return roundTo(value * 360, 1);
};