Skip to main content

RGB and HSV

Red, Green, Blue ⇄ Hue, Saturation, Value

RGB to HSV

C references chroma.

V=max(R,G,B)C=Vmin(R,G,B)S={0 if V=0CV otherwise H={0 if V=0GBC if V=RBRC if V=GRGC if V=B\begin{align*} V &= max(R,G,B) \\ \:\\ C &= V - min(R,G,B) \\ \:\\ S &= \begin{cases} 0 & \text{ if } V = 0 \\ \frac{C}{V} & \text{ otherwise } \end{cases} \\ \:\\ H &= \begin{cases} 0 & \text{ if } V = 0 \\ \frac{G - B}{C} & \text{ if } V = R \\ \frac{B - R}{C} & \text{ if } V = G \\ \frac{R - G}{C} & \text{ if } V = B \\ \end{cases} \end{align*}

HSV to RGB

p=V(1S)q=V(1SH)t=V(1S(1H))(R,G,B)={(V,V,V) if S=0(V,t,p) if H<1(q,V,p) if 1<H2(p,V,t) if 2<H3(p,q,V) if 3<H4(t,p,V) if 4<H5(V,p,q) otherwise \begin{align*} p &= V \cdot (1 - S) \\ q &= V \cdot (1 - S \cdot H) \\ t &= V \cdot (1 - S \cdot (1 - H)) \\ \:\\ (R,G,B) &= \begin{cases} (V,V,V) & \text{ if } S = 0 \\ (V,t,p) & \text{ if } H < 1 \\ (q,V,p) & \text{ if } 1 < H \leq 2 \\ (p,V,t) & \text{ if } 2 < H \leq 3 \\ (p,q,V) & \text{ if } 3 < H \leq 4 \\ (t,p,V) & \text{ if } 4 < H \leq 5 \\ (V,p,q) & \text{ otherwise } \\ \end{cases} \end{align*}