MCPcopy
hub / github.com/darkreader/darkreader / hslToRGB

Function hslToRGB

src/utils/color.ts:59–78  ·  view source on GitHub ↗
({h, s, l, a = 1}: HSLA)

Source from the content-addressed store, hash-verified

57
58// https://en.wikipedia.org/wiki/HSL_and_HSV
59export function hslToRGB({h, s, l, a = 1}: HSLA): RGBA {
60 if (s === 0) {
61 const [r, b, g] = [l, l, l].map((x) => Math.round(x * 255));
62 return {r, g, b, a};
63 }
64
65 const c = (1 - Math.abs(2 * l - 1)) * s;
66 const x = c * (1 - Math.abs((h / 60) % 2 - 1));
67 const m = l - c / 2;
68 const [r, g, b] = (
69 h < 60 ? [c, x, 0] :
70 h < 120 ? [x, c, 0] :
71 h < 180 ? [0, c, x] :
72 h < 240 ? [0, x, c] :
73 h < 300 ? [x, 0, c] :
74 [c, 0, x]
75 ).map((n) => Math.round((n + m) * 255));
76
77 return {r, g, b, a};
78}
79
80// https://en.wikipedia.org/wiki/HSL_and_HSV
81export function rgbToHSL({r: r255, g: g255, b: b255, a = 1}: RGBA): HSLA {

Callers 3

modifyColorWithCacheFunction · 0.90
color.tests.tsFile · 0.90
parseHSLFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected