Search code examples
pythonnumpymathcolorshue

Transforming rectangular (cartesian) to polar coordinates


Before asking the actual question, I want to mention that I know how to convert between the 2 coordinate systems. The thing that I need to understand is a picture in Wikipedia related to colors. This is the picture:

pic

Now, the formulas are as follows:

Formulas

So, using python, it's pretty easy to follow along and get this:

import numpy as np
R, G, B = 1/5, 3/5, 4/5

a = (2 * R - G - B) * 0.5
b = (np.sqrt(3) / 2) * (G - B)

H2 = np.arctan2(b, a)
C2 = np.sqrt(a*a + b*b)

The problem is that in this case H2 is -2.808119481337961, which is in radians. Even if I convert this to degrees using np.rad2deg(), I get -160.8933946491309. I tried everything I could think of which includes using math instead of numpy (just in case), all sorts of other calculations, using external calculators as reference and I always get these results.

So, my question is how do they get H2=199.1° in the picture? What am I missing? All other calculations are fine (a, b, C2).


Solution

  • The angles -160 and 199 are the same.

    >>> math.degrees(H2)
    -160.8933946491309
    >>> math.degrees(H2) % 360
    199.1066053508691