Skip to main content

RGB and HSL

Red, Green, Blue ⇄ Hue, Saturation, Lightness

RGB to HSL

C references chroma.

V=max(R,G,B)C=Vmin(R,G,B)L=VC2S={0 if L=0 or L=1VLmin(L,1L) otherwise H={0 if C=0GBC if V=RBRC+2 if V=GRGC+4 if V=B\begin{align*} V &= max(R,G,B) \\ \:\\ C &= V - min(R,G,B) \\ \:\\ L &= V - \frac{C}{2} \:\\ S &= \begin{cases} 0 & \text{ if } L = 0 \text{ or } L = 1 \\ \frac{V-L}{min(L,1-L)} & \text{ otherwise } \end{cases} \\ \:\\ H &= \begin{cases} 0 & \text{ if } C = 0 \\ \frac{G - B}{C} & \text{ if } V = R \\ \frac{B - R}{C} + 2 & \text{ if } V = G \\ \frac{R - G}{C} + 4 & \text{ if } V = B \\ \end{cases} \end{align*}

HSL to RGB

C=(12L1)Sx=C(1H mod 21)(R1,G1,B1)={(0,0,0) if Hundefined(C,x,0) if 0<H1(x,C,0) if 1<H2(0,C,x) if 2<H3(0,x,C) if 3<H4(x,0,C) if 4<H5(C,0,x) if 5<H6m=LC2(R,G,B)=(R1+m,G1+m,B1+m)\begin{align*} C &= (1 - |2L - 1|) \cdot S \\ \:\\ x &= C \cdot (1 - |H \text{ mod } 2-1|) \\ \:\\ (R_{1},G_{1},B_{1}) &= \begin{cases} (0,0,0) & \text{ if } H \: \mathrm{undefined} \\ (C,x,0) & \text{ if } 0 < H \leq 1 \\ (x,C,0) & \text{ if } 1 < H \leq 2 \\ (0,C,x) & \text{ if } 2 < H \leq 3 \\ (0,x,C) & \text{ if } 3 < H \leq 4 \\ (x,0,C) & \text{ if } 4 < H \leq 5 \\ (C,0,x) & \text{ if } 5 < H \leq 6 \\ \end{cases} \\ \:\\ m &= L - \frac{C}{2} \\ \:\\ (R,G,B) &= (R_1 + m, G_1 + m, B_1 + m) \end{align*}