RGB and HSP 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:
P R = 0.299 P G = 0.587 P B = 0.114 \begin{align*} P_{R} &= 0.299 \\ P_{G} &= 0.587 \\ P_{B} &= 0.114 \end{align*} P R P G P B = 0.299 = 0.587 = 0.114 This algorithm is similar to the one Photoshop uses when converting images to greyscale.
V = m a x ( R , G , B ) C = V − m i n ( R , G , B ) H = { 0 if C = 0 G − B C if V = R B − R C + 2 if V = G R − G C + 4 if V = B S = { 0 if V = 0 V C otherwise P = R 2 ⋅ P R + G 2 ⋅ P G + B 2 ⋅ P B \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*} V C H S P = ma x ( R , G , B ) = V − min ( R , G , B ) = ⎩ ⎨ ⎧ 0 C G − B C B − R + 2 C R − G + 4 if C = 0 if V = R if V = G if V = B = { 0 C V if V = 0 otherwise = R 2 ⋅ P R + G 2 ⋅ P G + B 2 ⋅ P B HSP to RGB H ′ = H 60 S 0 = 1 − S H 0 = { H ′ if H ′ < 1 − H ′ + 2 if 1 ≤ H ′ < 2 H ′ − 2 if 2 ≤ H ′ < 3 − H ′ + 4 if 3 ≤ H ′ < 4 H ′ − 4 if 4 ≤ H ′ < 5 − H ′ + 6 if 5 ≤ H ′ < 6 f a ( x ) = x S 0 f b ( x ) = x ⋅ S 0 f c ( x , y ) = y + H 0 ⋅ ( x − y ) f d ( x , y , z ) = P P x S 0 2 + P y ⋅ ( 1 + H 0 + 1 S 0 − 1 ) 2 + P z f e ( x , y ) = P 2 P x + P y ⋅ H 0 2 R = { f a ( B ) if S 0 > 0 and H ′ < 1 f c ( G , B ) if S 0 > 0 and 1 ≤ H ′ < 2 f d ( R , B , G ) if S 0 > 0 and 2 ≤ H ′ < 3 f a ( B , R , G ) if S 0 > 0 and 3 ≤ H ′ < 4 f c ( B , G ) if S 0 > 0 and 4 ≤ H ′ < 5 f a ( G ) if S 0 > 0 and 5 ≤ H ′ < 6 f e ( R , G ) if S 0 = 0 and H ′ < 1 f b ( G ) if S 0 = 0 and 1 ≤ H ′ < 2 0 if S 0 = 0 and 2 ≤ H ′ < 4 f b ( B ) if S 0 = 0 and 4 ≤ H ′ < 5 f e ( R , B ) if S 0 = 0 and 5 ≤ H ′ < 6 G = { f c ( R , B ) if S 0 > 0 and H ′ < 1 f a ( B ) if S 0 > 0 and 1 ≤ H ′ < 2 f a ( R ) if S 0 > 0 and 2 ≤ H ′ < 3 f c ( B , R ) if S 0 > 0 and 3 ≤ H ′ < 4 f d ( B , R , G ) if S 0 > 0 and 4 ≤ H ′ < 5 f d ( R , B , G ) if S 0 > 0 and 5 ≤ H ′ < 6 f b ( R ) if S 0 = 0 and H ′ < 1 f e ( G , R ) if S 0 = 0 and 1 ≤ H ′ < 2 f e ( R , G ) if S 0 = 0 and 2 ≤ H ′ < 3 f b ( B ) if S 0 = 0 and 3 ≤ H ′ < 4 0 if S 0 = 0 and 4 ≤ H ′ < 6 B = { f a ( B ) if S 0 > 0 and H ′ < 1 f d ( G , R , B ) if S 0 > 0 and 1 ≤ H ′ < 2 f c ( G , R ) if S 0 > 0 and 2 ≤ H ′ < 3 f a ( R ) if S 0 > 0 and 3 ≤ H ′ < 4 f a ( G ) if S 0 > 0 and 4 ≤ H ′ < 5 f b ( R , G ) if S 0 > 0 and 5 ≤ H ′ < 6 0 if S 0 = 0 and H ′ < 2 f b ( G ) if S 0 = 0 and 2 ≤ H ′ < 3 f e ( B , G ) if S 0 = 0 and 3 ≤ H ′ < 4 f e ( B , R ) if S 0 = 0 and 4 ≤ H ′ < 5 f b ( R ) if S 0 = 0 and 5 ≤ H ′ < 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*} H ′ S 0 H 0 f a ( x ) f b ( x ) f c ( x , y ) f d ( x , y , z ) f e ( x , y ) R G B = 60 H = 1 − S = ⎩ ⎨ ⎧ H ′ − H ′ + 2 H ′ − 2 − H ′ + 4 H ′ − 4 − H ′ + 6 if H ′ < 1 if 1 ≤ H ′ < 2 if 2 ≤ H ′ < 3 if 3 ≤ H ′ < 4 if 4 ≤ H ′ < 5 if 5 ≤ H ′ < 6 = S 0 x = x ⋅ S 0 = y + H 0 ⋅ ( x − y ) = S 0 2 P x P + P y ⋅ ( 1 + H 0 + S 0 − 1 1 ) 2 + P z = P x + P y ⋅ H 0 2 P 2 = ⎩ ⎨ ⎧ f a ( B ) f c ( G , B ) f d ( R , B , G ) f a ( B , R , G ) f c ( B , G ) f a ( G ) f e ( R , G ) f b ( G ) 0 f b ( B ) f e ( R , B ) if S 0 > 0 and H ′ < 1 if S 0 > 0 and 1 ≤ H ′ < 2 if S 0 > 0 and 2 ≤ H ′ < 3 if S 0 > 0 and 3 ≤ H ′ < 4 if S 0 > 0 and 4 ≤ H ′ < 5 if S 0 > 0 and 5 ≤ H ′ < 6 if S 0 = 0 and H ′ < 1 if S 0 = 0 and 1 ≤ H ′ < 2 if S 0 = 0 and 2 ≤ H ′ < 4 if S 0 = 0 and 4 ≤ H ′ < 5 if S 0 = 0 and 5 ≤ H ′ < 6 = ⎩ ⎨ ⎧ f c ( R , B ) f a ( B ) f a ( R ) f c ( B , R ) f d ( B , R , G ) f d ( R , B , G ) f b ( R ) f e ( G , R ) f e ( R , G ) f b ( B ) 0 if S 0 > 0 and H ′ < 1 if S 0 > 0 and 1 ≤ H ′ < 2 if S 0 > 0 and 2 ≤ H ′ < 3 if S 0 > 0 and 3 ≤ H ′ < 4 if S 0 > 0 and 4 ≤ H ′ < 5 if S 0 > 0 and 5 ≤ H ′ < 6 if S 0 = 0 and H ′ < 1 if S 0 = 0 and 1 ≤ H ′ < 2 if S 0 = 0 and 2 ≤ H ′ < 3 if S 0 = 0 and 3 ≤ H ′ < 4 if S 0 = 0 and 4 ≤ H ′ < 6 = ⎩ ⎨ ⎧ f a ( B ) f d ( G , R , B ) f c ( G , R ) f a ( R ) f a ( G ) f b ( R , G ) 0 f b ( G ) f e ( B , G ) f e ( B , R ) f b ( R ) if S 0 > 0 and H ′ < 1 if S 0 > 0 and 1 ≤ H ′ < 2 if S 0 > 0 and 2 ≤ H ′ < 3 if S 0 > 0 and 3 ≤ H ′ < 4 if S 0 > 0 and 4 ≤ H ′ < 5 if S 0 > 0 and 5 ≤ H ′ < 6 if S 0 = 0 and H ′ < 2 if S 0 = 0 and 2 ≤ H ′ < 3 if S 0 = 0 and 3 ≤ H ′ < 4 if S 0 = 0 and 4 ≤ H ′ < 5 if S 0 = 0 and 5 ≤ H ′ < 6