MCPcopy Index your code
hub / github.com/simstudioai/sim / hslToRgb

Function hslToRgb

apps/sim/lib/colors/convert.ts:61–89  ·  view source on GitHub ↗
(h: number, s: number, l: number)

Source from the content-addressed store, hash-verified

59
60/** Convert HSL (h: 0-360, s: 0-1, l: 0-1) to RGB (0-255). */
61export function hslToRgb(h: number, s: number, l: number): { r: number; g: number; b: number } {
62 h = ((h % 360) + 360) % 360
63 s = Math.max(0, Math.min(1, s))
64 l = Math.max(0, Math.min(1, l))
65
66 if (s === 0) {
67 const v = Math.round(l * 255)
68 return { r: v, g: v, b: v }
69 }
70
71 const hueToRgb = (p: number, q: number, t: number): number => {
72 if (t < 0) t += 1
73 if (t > 1) t -= 1
74 if (t < 1 / 6) return p + (q - p) * 6 * t
75 if (t < 1 / 2) return q
76 if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6
77 return p
78 }
79
80 const q = l < 0.5 ? l * (1 + s) : l + s - l * s
81 const p = 2 * l - q
82 const hNorm = h / 360
83
84 return {
85 r: Math.round(hueToRgb(p, q, hNorm + 1 / 3) * 255),
86 g: Math.round(hueToRgb(p, q, hNorm) * 255),
87 b: Math.round(hueToRgb(p, q, hNorm - 1 / 3) * 255),
88 }
89}
90
91/**
92 * Render a resolved color + alpha as a CSS color string: the bare hex when

Callers 8

applyLumModFunction · 0.90
applyLumOffFunction · 0.90
applySatModFunction · 0.90
applyHueModFunction · 0.90
applyHueOffFunction · 0.90
applySatOffFunction · 0.90
resolveColorUncachedFunction · 0.90
convert.test.tsFile · 0.90

Calls 1

hueToRgbFunction · 0.85

Tested by

no test coverage detected