You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
954 B
39 lines
954 B
// @ts-ignore
|
|
import kelvinToRgb from "kelvin-to-rgb";
|
|
import {ColorValue} from "../models/Colors";
|
|
import iro from "@jaames/iro";
|
|
|
|
export function kelvinToColorValue(rawKelvin: number): ColorValue {
|
|
const kelvin = normalizeKelvin(rawKelvin);
|
|
|
|
return kelvinTable[kelvin];
|
|
}
|
|
|
|
export function kelvinToRgbHex(rawKelvin: number): string {
|
|
const kelvin = normalizeKelvin(rawKelvin);
|
|
|
|
const c = new iro.Color();
|
|
c.kelvin = kelvin;
|
|
|
|
return c.hexString;
|
|
}
|
|
|
|
function normalizeKelvin(kelvin: number): number {
|
|
if (kelvin < 1000) {
|
|
return 1000;
|
|
} else if (kelvin > 10000) {
|
|
return 10000;
|
|
} else {
|
|
return kelvin - (kelvin % 100);
|
|
}
|
|
}
|
|
|
|
const kelvinTable: {[key: number]: ColorValue} = {};
|
|
for (let kelvin = 1000; kelvin <= 10000; kelvin += 100) {
|
|
const c = new iro.Color();
|
|
c.kelvin = kelvin;
|
|
|
|
const [h, s] = [c.hsv.h, (c.hsv.s as number) / 100];
|
|
|
|
kelvinTable[kelvin] = {h, s, kelvin};
|
|
}
|