2020-06-16 09:32:48 +00:00
|
|
|
interface ColorRGB {
|
|
|
|
r: number;
|
|
|
|
g: number;
|
|
|
|
b: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ColorHSV {
|
|
|
|
h: number;
|
|
|
|
s: number;
|
|
|
|
v: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
export enum ColorFmt {
|
|
|
|
RGB, // rgb(R,G,B)
|
|
|
|
HEX, // #RRGGBB
|
|
|
|
}
|
|
|
|
|
|
|
|
function hsvToRgb({ h, s, v }: ColorHSV): ColorRGB {
|
2020-06-17 13:16:54 +00:00
|
|
|
const i = Math.floor(h * 6);
|
|
|
|
const f = h * 6 - i;
|
|
|
|
const p = v * (1 - s);
|
|
|
|
const q = v * (1 - f * s);
|
|
|
|
const t = v * (1 - (1 - f) * s);
|
2020-06-16 09:32:48 +00:00
|
|
|
|
|
|
|
switch (i % 6) {
|
|
|
|
case 0:
|
2020-06-17 15:44:50 +00:00
|
|
|
return { r: v, g: t, b: p };
|
2020-06-16 09:32:48 +00:00
|
|
|
case 1:
|
2020-06-17 15:44:50 +00:00
|
|
|
return { r: q, g: v, b: p };
|
2020-06-16 09:32:48 +00:00
|
|
|
case 2:
|
2020-06-17 15:44:50 +00:00
|
|
|
return { r: p, g: v, b: t };
|
2020-06-16 09:32:48 +00:00
|
|
|
case 3:
|
2020-06-17 15:44:50 +00:00
|
|
|
return { r: p, g: q, b: v };
|
2020-06-16 09:32:48 +00:00
|
|
|
case 4:
|
2020-06-17 15:44:50 +00:00
|
|
|
return { r: t, g: p, b: v };
|
2020-06-16 09:32:48 +00:00
|
|
|
case 5:
|
2020-06-17 15:44:50 +00:00
|
|
|
return { r: v, g: p, b: q };
|
2020-06-17 13:16:54 +00:00
|
|
|
default:
|
|
|
|
throw new Error("unreacheable");
|
2020-06-16 09:32:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function rgbToHsv({ r, g, b }: ColorRGB): ColorHSV {
|
2020-06-17 13:16:54 +00:00
|
|
|
const max = Math.max(r, g, b);
|
|
|
|
const min = Math.min(r, g, b);
|
|
|
|
const v = max;
|
2020-06-16 09:32:48 +00:00
|
|
|
|
2020-06-17 13:16:54 +00:00
|
|
|
const d = max - min;
|
|
|
|
const s = max === 0 ? 0 : d / max;
|
2020-06-16 09:32:48 +00:00
|
|
|
|
2020-06-17 13:16:54 +00:00
|
|
|
let h;
|
|
|
|
if (max === min) {
|
2020-06-16 09:32:48 +00:00
|
|
|
h = 0; // achromatic
|
|
|
|
} else {
|
|
|
|
switch (max) {
|
|
|
|
case r:
|
|
|
|
h = (g - b) / d + (g < b ? 6 : 0);
|
|
|
|
break;
|
|
|
|
case g:
|
|
|
|
h = (b - r) / d + 2;
|
|
|
|
break;
|
|
|
|
case b:
|
|
|
|
h = (r - g) / d + 4;
|
|
|
|
break;
|
2020-06-17 13:16:54 +00:00
|
|
|
default:
|
|
|
|
throw new Error("unreacheable");
|
2020-06-16 09:32:48 +00:00
|
|
|
}
|
|
|
|
h /= 6;
|
|
|
|
}
|
|
|
|
|
|
|
|
return { h, s, v };
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hacky way to get RGB values FOR SURE!
|
|
|
|
function nameToRGB(name: string): ColorRGB {
|
|
|
|
// Create fake div
|
2020-06-17 13:16:54 +00:00
|
|
|
const fakeDiv = document.createElement("div");
|
2020-06-16 09:32:48 +00:00
|
|
|
fakeDiv.style.color = name;
|
|
|
|
document.body.appendChild(fakeDiv);
|
|
|
|
|
|
|
|
// Get color of div
|
2020-06-17 13:16:54 +00:00
|
|
|
const cs = window.getComputedStyle(fakeDiv);
|
|
|
|
const pv = cs.getPropertyValue("color");
|
2020-06-16 09:32:48 +00:00
|
|
|
|
|
|
|
// Remove div after obtaining desired color value
|
|
|
|
document.body.removeChild(fakeDiv);
|
|
|
|
|
|
|
|
return parseColor(pv);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If you also wonder "What the fuck", go here:
|
|
|
|
// https://stackoverflow.com/questions/11068240/what-is-the-most-efficient-way-to-parse-a-css-color-in-javascript
|
|
|
|
function parseColor(input: string): ColorRGB {
|
|
|
|
// Hex format
|
2020-06-17 13:16:54 +00:00
|
|
|
if (input.substr(0, 1) === "#") {
|
|
|
|
const collen = (input.length - 1) / 3;
|
|
|
|
const fact = [17, 1, 0.062272][collen - 1];
|
2020-06-16 09:32:48 +00:00
|
|
|
return {
|
|
|
|
r: Math.round(parseInt(input.substr(1, collen), 16) * fact) / 256,
|
|
|
|
g:
|
|
|
|
Math.round(parseInt(input.substr(1 + collen, collen), 16) * fact) / 256,
|
|
|
|
b:
|
|
|
|
Math.round(parseInt(input.substr(1 + 2 * collen, collen), 16) * fact) /
|
|
|
|
256,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (input.startsWith("rgb")) {
|
|
|
|
// RGB() format
|
|
|
|
const vals = input
|
|
|
|
.split("(")[1]
|
|
|
|
.split(")")[0]
|
|
|
|
.split(",")
|
|
|
|
.map((i) => parseInt(i, 10))
|
|
|
|
.map(Math.round);
|
|
|
|
return { r: vals[0] / 256, g: vals[1] / 256, b: vals[2] / 256 };
|
|
|
|
}
|
|
|
|
// Fuck, well.. normalize it and put it into an element and get back to me
|
|
|
|
return nameToRGB(input);
|
|
|
|
}
|
|
|
|
|
|
|
|
function serializeColor(col: ColorRGB, format: ColorFmt): string {
|
|
|
|
const r = Math.round(col.r * 255);
|
|
|
|
const g = Math.round(col.g * 255);
|
|
|
|
const b = Math.round(col.b * 255);
|
|
|
|
switch (format) {
|
|
|
|
case ColorFmt.RGB:
|
|
|
|
return `rgb(${r}, ${g}, ${b})`;
|
|
|
|
case ColorFmt.HEX: {
|
2020-06-17 13:16:54 +00:00
|
|
|
const rhex = `00${r.toString(16)}`.slice(-2);
|
|
|
|
const ghex = `00${g.toString(16)}`.slice(-2);
|
|
|
|
const bhex = `00${b.toString(16)}`.slice(-2);
|
|
|
|
return `#${rhex}${ghex}${bhex}`;
|
2020-06-16 09:32:48 +00:00
|
|
|
}
|
2020-06-17 13:16:54 +00:00
|
|
|
default:
|
|
|
|
return "#000";
|
2020-06-16 09:32:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function darken(color: string, format: ColorFmt): string {
|
|
|
|
const col = parseColor(color);
|
|
|
|
const hsl = rgbToHsv(col);
|
|
|
|
if (hsl.s < 0.15) {
|
2020-06-16 10:51:24 +00:00
|
|
|
hsl.h = 0.6;
|
2020-06-16 09:32:48 +00:00
|
|
|
hsl.s = 0.5;
|
|
|
|
hsl.v = Math.max(0.2, 1 - hsl.v);
|
2020-06-17 13:16:54 +00:00
|
|
|
} else if (hsl.v > 0.5) {
|
|
|
|
hsl.v = 0.4;
|
|
|
|
if (hsl.s > 0.2) {
|
|
|
|
hsl.s = Math.min(1, hsl.s + 0.2);
|
2020-06-16 09:32:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
const out = hsvToRgb(hsl);
|
|
|
|
return serializeColor(out, format);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function lighten(color: string, format: ColorFmt): string {
|
|
|
|
const col = parseColor(color);
|
|
|
|
const hsl = rgbToHsv(col);
|
|
|
|
if (hsl.v < 0.5) {
|
|
|
|
hsl.v = 0.8;
|
|
|
|
}
|
|
|
|
if (hsl.s > 0.7) {
|
|
|
|
hsl.s = Math.min(1, hsl.s - 0.3);
|
|
|
|
hsl.v = 1;
|
|
|
|
}
|
|
|
|
// Blue is shit to read, make it cyan
|
|
|
|
if (Math.abs(hsl.h - 0.666) < 0.1) {
|
|
|
|
hsl.h -= 0.13;
|
|
|
|
hsl.v = Math.min(1, hsl.v + 0.1);
|
|
|
|
}
|
|
|
|
const out = hsvToRgb(hsl);
|
|
|
|
return serializeColor(out, format);
|
|
|
|
}
|