RGB and HSI RGB to HSI I = R + G + B 3 S = { 1 − m i n ( R , G , B ) I if I > 0 0 otherwise H = { a t a n 2 ( 3 2 ( G − B ) , 1 2 ( 2 R − G − B ) ) if I > 0 0 otherwise \begin{align*} I &= \frac{R + G + B}{3} \\ \\ S &= \begin{cases} 1 - \frac{min(R, G, B)}{I} & \text{ if } I > 0 \\ 0 & \text{ otherwise } \\ \end{cases} \\ \\ H &= \begin{cases} \mathrm{atan2}(\frac{\sqrt{3}}{2}(G - B), \frac{1}{2}(2R - G - B)) & \text{ if } I > 0 \\ 0 & \text{ otherwise } \end{cases} \end{align*} I S H = 3 R + G + B = { 1 − I min ( R , G , B ) 0 if I > 0 otherwise = { atan2 ( 2 3 ( G − B ) , 2 1 ( 2 R − G − B )) 0 if I > 0 otherwise a t a n 2 \mathrm{atan2} atan2 determines the counterclockwise angle in radians between the positive X axis and the point (x,y). The output range is [ − π , π ] [-\pi,\pi] [ − π , π ] .
a t a n 2 ( y , x ) = { a t a n ( y x ) if x > 0 a t a n ( y x ) + π if x < 0 and y ≥ 0 a t a n ( y x ) − π if x < 0 and y < 0 π 2 if x = 0 and y > 0 − π 2 if x = 0 and y < 0 undefined if x = 0 and y = 0 \mathrm{atan2}(y,x) = \begin{cases} \mathrm{atan}(\frac{y}{x}) & \text{ if } x > 0 \\ \mathrm{atan}(\frac{y}{x}) + \pi & \text{ if } x < 0 \text{ and } y \geq 0 \\ \mathrm{atan}(\frac{y}{x}) - \pi & \text{ if } x < 0 \text{ and } y < 0 \\ \frac{\pi}{2} & \text{ if } x = 0 \text{ and } y > 0 \\ -\frac{\pi}{2} & \text{ if } x = 0 \text{ and } y < 0 \\ \text{undefined} & \text{ if } x = 0 \text{ and } y = 0 \end{cases} atan2 ( y , x ) = ⎩ ⎨ ⎧ atan ( x y ) atan ( x y ) + π atan ( x y ) − π 2 π − 2 π undefined if x > 0 if x < 0 and y ≥ 0 if x < 0 and y < 0 if x = 0 and y > 0 if x = 0 and y < 0 if x = 0 and y = 0 HSI to RGB f ( a ) = cos ( ( H − a ) π 180 ) cos ( 60 − ( H − a ) ) π 180 R = { I ⋅ ( 1 + S ⋅ f ( 0 ) ) if 0 ≤ H < 120 I ⋅ ( 1 − S ) if 120 ≤ H < 240 3 I − G − B if 240 ≤ H < 360 G = { 3 I − R − B if 0 ≤ H < 120 I ⋅ ( 1 + S ⋅ f ( 120 ) ) if 120 ≤ H < 240 I ⋅ ( 1 − S ) if 240 ≤ H < 360 B = { I ⋅ ( 1 − S ) if 0 ≤ H < 120 3 I − R − G if 120 ≤ H < 240 I ⋅ ( 1 + S ⋅ f ( 240 ) ) if 240 ≤ H < 360 \begin{align*} f(a) &= \frac{\cos((H-a)\frac{\pi}{180})}{\cos(60 - (H - a))\frac{\pi}{180}} \\ \\ R &= \begin{cases} I \cdot (1 + S \cdot f(0)) & \text{ if } 0 \leq H < 120 \\ I \cdot (1 - S) & \text{ if } 120 \leq H < 240 \\ 3I - G - B & \text{ if } 240 \leq H < 360 \end{cases} \\ \\ G &= \begin{cases} 3I - R - B & \text{ if } 0 \leq H < 120 \\ I \cdot (1 + S \cdot f(120)) & \text{ if } 120 \leq H < 240 \\ I \cdot (1 - S) & \text{ if } 240 \leq H < 360 \end{cases} \\ \\ B &= \begin{cases} I \cdot (1 - S) & \text{ if } 0 \leq H < 120 \\ 3I - R - G & \text{ if } 120 \leq H < 240 \\ I \cdot (1 + S \cdot f(240)) & \text{ if } 240 \leq H < 360 \end{cases} \\ \\ \end{align*} f ( a ) R G B = cos ( 60 − ( H − a )) 180 π cos (( H − a ) 180 π ) = ⎩ ⎨ ⎧ I ⋅ ( 1 + S ⋅ f ( 0 )) I ⋅ ( 1 − S ) 3 I − G − B if 0 ≤ H < 120 if 120 ≤ H < 240 if 240 ≤ H < 360 = ⎩ ⎨ ⎧ 3 I − R − B I ⋅ ( 1 + S ⋅ f ( 120 )) I ⋅ ( 1 − S ) if 0 ≤ H < 120 if 120 ≤ H < 240 if 240 ≤ H < 360 = ⎩ ⎨ ⎧ I ⋅ ( 1 − S ) 3 I − R − G I ⋅ ( 1 + S ⋅ f ( 240 )) if 0 ≤ H < 120 if 120 ≤ H < 240 if 240 ≤ H < 360