etheryo/src/lib/ts/color.ts

28 lines
No EOL
716 B
TypeScript

type RGB = {
r: number;
g: number;
b: number;
};
export function hexTorgb(hex: string) {
return {
r: parseInt(hex.substring(0, 2), 16),
g: parseInt(hex.substring(2, 4), 16),
b: parseInt(hex.substring(4, 6), 16),
}
}
/* should be type {r,g,b} */
export function rgbTohex(rgb: RGB) {
return (rgb.r.toString(16) + rgb.g.toString(16) + rgb.b.toString(16));
}
export function darkenHexTorgb(hex: string, darken: number) {
let rgb = hexTorgb(hex);
console.log(hex);
console.log(rgb);
rgb.r = Math.floor(Math.round(rgb.r * darken));
rgb.g = Math.floor(Math.round(rgb.g * darken));
rgb.b = Math.floor(Math.round(rgb.b * darken));
return rgb;
}