42 lines
960 B
TypeScript
42 lines
960 B
TypeScript
|
import { RGB } from "@watsonb8/wiz-lib";
|
||
|
|
||
|
export const colorTempToRgb = (kelvin: number): RGB => {
|
||
|
var temp = kelvin / 100;
|
||
|
var red, green, blue;
|
||
|
if (temp <= 66) {
|
||
|
red = 255;
|
||
|
green = temp;
|
||
|
green = 99.4708025861 * Math.log(green) - 161.1195681661;
|
||
|
|
||
|
if (temp <= 19) {
|
||
|
blue = 0;
|
||
|
} else {
|
||
|
blue = temp - 10;
|
||
|
blue = 138.5177312231 * Math.log(blue) - 305.0447927307;
|
||
|
}
|
||
|
} else {
|
||
|
red = temp - 60;
|
||
|
red = 329.698727446 * Math.pow(red, -0.1332047592);
|
||
|
|
||
|
green = temp - 60;
|
||
|
green = 288.1221695283 * Math.pow(green, -0.0755148492);
|
||
|
|
||
|
blue = 255;
|
||
|
}
|
||
|
return {
|
||
|
r: clamp(red, 0, 255),
|
||
|
g: clamp(green, 0, 255),
|
||
|
b: clamp(blue, 0, 255),
|
||
|
};
|
||
|
};
|
||
|
|
||
|
const clamp = (x: number, min: number, max: number) => {
|
||
|
if (x < min) {
|
||
|
return min;
|
||
|
}
|
||
|
if (x > max) {
|
||
|
return max;
|
||
|
}
|
||
|
return x;
|
||
|
};
|