MCPcopy Create free account
hub / github.com/NativeScript/NativeScript / convertHSLToRGBColor

Function convertHSLToRGBColor

packages/core/css/parser.ts:97–133  ·  view source on GitHub ↗
(hue: number, saturation: number, lightness: number)

Source from the content-addressed store, hash-verified

95}
96
97export function convertHSLToRGBColor(hue: number, saturation: number, lightness: number): { r: number; g: number; b: number } {
98 // Per formula it will be easier if hue is divided to 60° and saturation to 100 beforehand
99 // https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_RGB
100 hue /= 60;
101 lightness /= 100;
102
103 const chroma = ((1 - Math.abs(2 * lightness - 1)) * saturation) / 100,
104 X = chroma * (1 - Math.abs((hue % 2) - 1));
105 // Add lightness match to all RGB components beforehand
106 let { m: r, m: g, m: b } = { m: lightness - chroma / 2 };
107
108 if (0 <= hue && hue < 1) {
109 r += chroma;
110 g += X;
111 } else if (hue < 2) {
112 r += X;
113 g += chroma;
114 } else if (hue < 3) {
115 g += chroma;
116 b += X;
117 } else if (hue < 4) {
118 g += X;
119 b += chroma;
120 } else if (hue < 5) {
121 r += X;
122 b += chroma;
123 } else if (hue < 6) {
124 r += chroma;
125 b += X;
126 }
127
128 return {
129 r: Math.round(r * 0xff),
130 g: Math.round(g * 0xff),
131 b: Math.round(b * 0xff),
132 };
133}
134
135export function parseColorKeyword(value, start: number, keyword = parseKeyword(value, start)): Parsed<Color> {
136 const parseColor = keyword && getKnownColor(keyword.value);

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected