Inverting the Hue calculation

37 Views Asked by At

From Wikipedia, the calculation of a hue from an RGB color input is given as:

$$h_{rgb} = \mathrm{atan}\left( \frac{\sqrt{3} \cdot (G - B)}{ 2 \cdot R - G - B} \right).$$

Where R, G and B are in the range [0, 1], and h ranges from [0, 2π]

I'm trying to reverse this calculation. As hues are split in 60 degree portions, some information is known in advance for each of the 6 portions:

  • From 0-60 degrees, R = 1, B = 0
  • From 60-120 degrees, G = 1, B = 0
  • From 120-180 degrees, R = 0, G = 1
  • From 180-240 degrees, R = 0, B = 1
  • From 240-300 degrees, G = 0, B = 1
  • From 300-360 degrees, R = 1, G = 0

For some reason however, when I fill in the known variables in an inverse formula, I end up with a constant value for the unknown variable. For example, solving for G in the 0-60 degrees portion I get:

G = tan(h) * -2 tan(h) / (-tan(h) * tan(h))

All the tangents eliminate each other and I end up with G = 2. An online Math solver also comes to this conclusion, leading me to believe the formula on Wikipedia is incorrect somehow or I am making a wrong assumption somewhere.