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

Function rgbToHsl

apps/sim/lib/colors/convert.ts:31–58  ·  view source on GitHub ↗
(r: number, g: number, b: number)

Source from the content-addressed store, hash-verified

29
30/** Convert RGB (0-255) to HSL (h: 0-360, s: 0-1, l: 0-1). */
31export function rgbToHsl(r: number, g: number, b: number): { h: number; s: number; l: number } {
32 const rn = r / 255
33 const gn = g / 255
34 const bn = b / 255
35 const max = Math.max(rn, gn, bn)
36 const min = Math.min(rn, gn, bn)
37 const l = (max + min) / 2
38 let h = 0
39 let s = 0
40
41 if (max !== min) {
42 const d = max - min
43 s = l > 0.5 ? d / (2 - max - min) : d / (max + min)
44 switch (max) {
45 case rn:
46 h = ((gn - bn) / d + (gn < bn ? 6 : 0)) * 60
47 break
48 case gn:
49 h = ((bn - rn) / d + 2) * 60
50 break
51 case bn:
52 h = ((rn - gn) / d + 4) * 60
53 break
54 }
55 }
56
57 return { h, s, l }
58}
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 } {

Callers 7

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

Calls

no outgoing calls

Tested by

no test coverage detected