Posts: 10
Threads: 5
Joined: Sep 2017
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?
Posts: 4,220
Threads: 97
Joined: Sep 2016
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.
Posts: 10
Threads: 5
Joined: Sep 2017
> 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?
Posts: 4,220
Threads: 97
Joined: Sep 2016
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.
Posts: 10
Threads: 5
Joined: Sep 2017
Ok thanks.
I don't know where to start, so if anyone else has any ideas, I'd love to see them.
Posts: 10
Threads: 5
Joined: Sep 2017
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
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 )
|
|