mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-10-06 05:26:54 +08:00
added hex to hsl func
This commit is contained in:
parent
482a2e7856
commit
6593de3470
1 changed files with 47 additions and 0 deletions
|
@ -159,3 +159,50 @@ function kogasa(cov) {
|
|||
100 * (1 - Math.tanh(cov + Math.pow(cov, 3) / 3 + Math.pow(cov, 5) / 5))
|
||||
);
|
||||
}
|
||||
|
||||
function hexToHSL(H) {
|
||||
// Convert hex to RGB first
|
||||
let r = 0,
|
||||
g = 0,
|
||||
b = 0;
|
||||
if (H.length == 4) {
|
||||
r = "0x" + H[1] + H[1];
|
||||
g = "0x" + H[2] + H[2];
|
||||
b = "0x" + H[3] + H[3];
|
||||
} else if (H.length == 7) {
|
||||
r = "0x" + H[1] + H[2];
|
||||
g = "0x" + H[3] + H[4];
|
||||
b = "0x" + H[5] + H[6];
|
||||
}
|
||||
// Then to HSL
|
||||
r /= 255;
|
||||
g /= 255;
|
||||
b /= 255;
|
||||
let cmin = Math.min(r, g, b),
|
||||
cmax = Math.max(r, g, b),
|
||||
delta = cmax - cmin,
|
||||
h = 0,
|
||||
s = 0,
|
||||
l = 0;
|
||||
|
||||
if (delta == 0) h = 0;
|
||||
else if (cmax == r) h = ((g - b) / delta) % 6;
|
||||
else if (cmax == g) h = (b - r) / delta + 2;
|
||||
else h = (r - g) / delta + 4;
|
||||
|
||||
h = Math.round(h * 60);
|
||||
|
||||
if (h < 0) h += 360;
|
||||
|
||||
l = (cmax + cmin) / 2;
|
||||
s = delta == 0 ? 0 : delta / (1 - Math.abs(2 * l - 1));
|
||||
s = +(s * 100).toFixed(1);
|
||||
l = +(l * 100).toFixed(1);
|
||||
|
||||
return {
|
||||
hue: h,
|
||||
sat: s,
|
||||
lgt: l,
|
||||
string: "hsl(" + h + "," + s + "%," + l + "%)",
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue