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.
72 lines
1.5 KiB
72 lines
1.5 KiB
export interface ColorRGB {
|
|
red: number
|
|
green: number
|
|
blue: number
|
|
}
|
|
|
|
export interface ColorXY {
|
|
x: number,
|
|
y: number
|
|
}
|
|
|
|
export interface ColorHS {
|
|
hue: number,
|
|
sat: number,
|
|
}
|
|
|
|
export interface Color {
|
|
rgb?: ColorRGB
|
|
xy?: ColorXY
|
|
hs?: ColorHS
|
|
k?: number
|
|
}
|
|
|
|
export enum ColorFlags {
|
|
CFlagXY = 1 << 0,
|
|
CFlagRGB = 1 << 1,
|
|
CFlagHS = 1 << 2,
|
|
CFlagHSK = 1 << 3,
|
|
CFlagKelvin = 1 << 4,
|
|
}
|
|
|
|
export function parseColor(s?: string | null): Color | null {
|
|
if (!s) {
|
|
return null;
|
|
}
|
|
|
|
const [kind, value] = s.split(":");
|
|
const [x,y,z] = value.split("|")[0].split(",").map(v => parseFloat(v));
|
|
|
|
switch (kind) {
|
|
case "hsk": return { hs: { hue: x, sat: y }, k: z };
|
|
case "hs": return { hs: { hue: x, sat: y } };
|
|
case "rgb": return { rgb: { red: x, green: y, blue: z } };
|
|
case "k": return { k: x };
|
|
case "xy": return { xy: {x, y} };
|
|
default: throw new Error(`Unknown color: ${s}`);
|
|
}
|
|
}
|
|
|
|
export function stringifyColor(c?: Color | null): string | null {
|
|
if (c?.hs && c?.k) {
|
|
return `hsk:${c.hs.hue},${c.hs.sat},${c.k}`;
|
|
} else if (c?.hs) {
|
|
return `hs:${c.hs.hue},${c.hs.sat}`;
|
|
} else if (c?.rgb) {
|
|
return `rgb:${c.rgb.red},${c.rgb.green},${c.rgb.blue}`;
|
|
} else if (c?.xy) {
|
|
return `xy:${c.xy.x},${c.xy.y}`;
|
|
} else if (c?.k) {
|
|
return `k:${c.k}`
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export function rgbString({red,green,blue}: ColorRGB) {
|
|
return `rgb(${red*255},${green*255},${blue*255})`
|
|
}
|
|
|
|
export function rgb(red: number, green: number, blue: number): ColorRGB {
|
|
return {red, green, blue};
|
|
}
|