Python Forum

Full Version: Python function to get colorwheel RGB values from compass degree?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm trying to translate compass values (1 - 360) to the RGB values of a color wheel. For example, looking at this image:

https://www.timvandevall.com/wp-content/...ate-02.png

1 degree would be RGB(255,0,0), 180 degrees would be 0,255,255, etc.

Any anyone think of a way to convert each compass degree to an RGB value programatically?
Yeah. Each part stays FF for 120 degrees, shifts to 00 over 60 degrees, stays 00 for 120 degrees, and shifts to FF over 60 degrees. Map those arcs and calculate from there.
> Yeah. Each part stays FF for 120 degrees, shifts to 00 over 60 degrees, stays 00 for 120 degrees, and shifts to FF over 60 degrees. Map those arcs and calculate from there.

Can you think of a function that would build that?
I could write a function that could do that, but I'm not going to do it for you. The way it works here is you try to write the code, if it fails you post the code and a clear description of the problem, and we help you fix the problem.
Ok thanks.

I don't know where to start, so if anyone else has any ideas, I'd love to see them.
For anyone else who comes down this path, HSV color makes this easy, since the hue is just a color degree on the wheel. So:

def compass_to_rgb(h, s=1, v=1):
    h = float(h)
    s = float(s)
    v = float(v)
    h60 = h / 60.0
    h60f = math.floor(h60)
    hi = int(h60f) % 6
    f = h60 - h60f
    p = v * (1 - s)
    q = v * (1 - f * s)
    t = v * (1 - (1 - f) * s)
    r, g, b = 0, 0, 0
    if hi == 0: r, g, b = v, t, p
    elif hi == 1: r, g, b = q, v, p
    elif hi == 2: r, g, b = p, v, t
    elif hi == 3: r, g, b = p, q, v
    elif hi == 4: r, g, b = t, p, v
    elif hi == 5: r, g, b = v, p, q
    r, g, b = int(r * 255), int(g * 255), int(b * 255)
    return r, g, b


print compass_to_rgb(0)
(255, 0, 0)