Skip to main content

RGB and HSP

Red, Green, Blue ⇄ Hue, Saturation, Perceived Brightness

RGB to HSP

Where P is perceived brightness. PR, PG, and PB are weights that can be adjusted as you will, such that PR + PG + PB= 1

If no Pi values are passed to the method, the default weight for P is as follows:

PR=0.299PG=0.587PB=0.114\begin{align*} P_{R} &= 0.299 \\ P_{G} &= 0.587 \\ P_{B} &= 0.114 \end{align*}

This algorithm is similar to the one Photoshop uses when converting images to greyscale.

V=max(R,G,B)C=Vmin(R,G,B)H={0 if C=0GBC if V=RBRC+2 if V=GRGC+4 if V=BS={0 if V=0VC otherwise P=R2PR+G2PG+B2PB\begin{align*} V &= max(R,G,B) \\ C &= V - min(R,G,B) \\ \:\\ 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} \\ \:\\ S &= \begin{cases} 0 & \text{ if } V=0 \\ \frac{V}{C} & \text{ otherwise } \end{cases} \\ \:\\ P &= \sqrt{R^2 \cdot P_{R} + G^2 \cdot P_{G} + B^2 \cdot P_{B}} \\ \end{align*}

HSP to RGB

H=H60S0=1SH0={H if H<1H+2 if 1H<2H2 if 2H<3H+4 if 3H<4H4 if 4H<5H+6 if 5H<6fa(x)=xS0fb(x)=xS0fc(x,y)=y+H0(xy)fd(x,y,z)=PPxS02+Py(1+H0+1S01)2+Pzfe(x,y)=P2Px+PyH02R={fa(B) if S0>0 and H<1fc(G,B) if S0>0 and 1H<2fd(R,B,G) if S0>0 and 2H<3fa(B,R,G) if S0>0 and 3H<4fc(B,G) if S0>0 and 4H<5fa(G) if S0>0 and 5H<6fe(R,G) if S0=0 and H<1fb(G) if S0=0 and 1H<20 if S0=0 and 2H<4fb(B) if S0=0 and 4H<5fe(R,B) if S0=0 and 5H<6G={fc(R,B) if S0>0 and H<1fa(B) if S0>0 and 1H<2fa(R) if S0>0 and 2H<3fc(B,R) if S0>0 and 3H<4fd(B,R,G) if S0>0 and 4H<5fd(R,B,G) if S0>0 and 5H<6fb(R) if S0=0 and H<1fe(G,R) if S0=0 and 1H<2fe(R,G) if S0=0 and 2H<3fb(B) if S0=0 and 3H<40 if S0=0 and 4H<6B={fa(B) if S0>0 and H<1fd(G,R,B) if S0>0 and 1H<2fc(G,R) if S0>0 and 2H<3fa(R) if S0>0 and 3H<4fa(G) if S0>0 and 4H<5fb(R,G) if S0>0 and 5H<60 if S0=0 and H<2fb(G) if S0=0 and 2H<3fe(B,G) if S0=0 and 3H<4fe(B,R) if S0=0 and 4H<5fb(R) if S0=0 and 5H<6\begin{align*} H' &= \frac{H}{60} \\ S_{0} &= 1 - S \\ H_{0} &= \begin{cases} H' & \text{ if } H' < 1 \\ -H'+2 & \text{ if } 1 \leq H' < 2 \\ H'-2 & \text{ if } 2 \leq H' < 3 \\ -H'+4 & \text{ if } 3 \leq H' < 4 \\ H'-4 & \text{ if } 4 \leq H' < 5 \\ -H'+6 & \text{ if } 5 \leq H' < 6 \\ \end{cases} \\ \:\\ f_{a}(x) &= \frac{x}{S_{0}} \\ f_{b}(x) &= x \cdot S_{0} \\ f_{c}(x,y) &= y + H_{0} \cdot (x - y) \\ f_{d}(x,y,z) &= \frac {P} {\sqrt{\frac{P_{x}}{S_{0}^2}}} + P_{y} \cdot (1 + H_{0} + \frac{1}{S_{0}-1})^2 + P_{z}\\ f_{e}(x,y) &= \sqrt{ \frac{P^2} {P_{x} + P_{y} \cdot H_{0}^2} } \\ \:\\ R &= \begin{cases} f_{a}(B) & \text{ if } S_{0} > 0 \text{ and } H' < 1 \\ f_{c}(G,B) & \text{ if } S_{0} > 0 \text{ and } 1 \leq H' < 2 \\ f_{d}(R,B,G) & \text{ if } S_{0} > 0 \text{ and } 2 \leq H' < 3 \\ f_{a}(B,R,G) & \text{ if } S_{0} > 0 \text{ and } 3 \leq H' < 4 \\ f_{c}(B,G) & \text{ if } S_{0} > 0 \text{ and } 4 \leq H' < 5 \\ f_{a}(G) & \text{ if } S_{0} > 0 \text{ and } 5 \leq H' < 6 \\ f_{e}(R,G) & \text{ if } S_{0} = 0 \text{ and } H' < 1 \\ f_{b}(G) & \text{ if } S_{0} = 0 \text{ and } 1 \leq H' < 2 \\ 0 & \text{ if } S_{0} = 0 \text{ and } 2 \leq H' < 4 \\ f_{b}(B) & \text{ if } S_{0} = 0 \text{ and } 4 \leq H' < 5 \\ f_{e}(R,B) & \text{ if } S_{0} = 0 \text{ and } 5 \leq H' < 6 \\ \end{cases} \\ \:\\ G &= \begin{cases} f_{c}(R,B) & \text{ if } S_{0} > 0 \text{ and } H' < 1 \\ f_{a}(B) & \text{ if } S_{0} > 0 \text{ and } 1 \leq H' < 2 \\ f_{a}(R) & \text{ if } S_{0} > 0 \text{ and } 2 \leq H' < 3 \\ f_{c}(B,R) & \text{ if } S_{0} > 0 \text{ and } 3 \leq H' < 4 \\ f_{d}(B,R,G) & \text{ if } S_{0} > 0 \text{ and } 4 \leq H' < 5 \\ f_{d}(R,B,G) & \text{ if } S_{0} > 0 \text{ and } 5 \leq H' < 6 \\ f_{b}(R) & \text{ if } S_{0} = 0 \text{ and } H' < 1 \\ f_{e}(G,R) & \text{ if } S_{0} = 0 \text{ and } 1 \leq H' < 2 \\ f_{e}(R,G) & \text{ if } S_{0} = 0 \text{ and } 2 \leq H' < 3 \\ f_{b}(B) & \text{ if } S_{0} = 0 \text{ and } 3 \leq H' < 4 \\ 0 & \text{ if } S_{0} = 0 \text{ and } 4 \leq H' < 6 \\ \end{cases} \\ \:\\ B &= \begin{cases} f_{a}(B) & \text{ if } S_{0} > 0 \text{ and } H' < 1 \\ f_{d}(G,R,B) & \text{ if } S_{0} > 0 \text{ and } 1 \leq H' < 2 \\ f_{c}(G,R) & \text{ if } S_{0} > 0 \text{ and } 2 \leq H' < 3 \\ f_{a}(R) & \text{ if } S_{0} > 0 \text{ and } 3 \leq H' < 4 \\ f_{a}(G) & \text{ if } S_{0} > 0 \text{ and } 4 \leq H' < 5 \\ f_{b}(R,G) & \text{ if } S_{0} > 0 \text{ and } 5 \leq H' < 6 \\ 0 & \text{ if } S_{0} = 0 \text{ and } H' < 2 \\ f_{b}(G) & \text{ if } S_{0} = 0 \text{ and } 2 \leq H' < 3 \\ f_{e}(B,G) & \text{ if } S_{0} = 0 \text{ and } 3 \leq H' < 4 \\ f_{e}(B,R) & \text{ if } S_{0} = 0 \text{ and } 4 \leq H' < 5 \\ f_{b}(R) & \text{ if } S_{0} = 0 \text{ and } 5 \leq H' < 6 \\ \end{cases} \end{align*}