Python Forum
Python function to get colorwheel RGB values from compass degree?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python function to get colorwheel RGB values from compass degree?
#1
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?
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
> 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?
Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Ok thanks.

I don't know where to start, so if anyone else has any ideas, I'd love to see them.
Reply
#6
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Adding values with reduce() function from the list of tuples kinimod 10 2,632 Jan-24-2023, 08:22 AM
Last Post: perfringo
  function accepts infinite parameters and returns a graph with those values edencthompson 0 855 Jun-10-2022, 03:42 PM
Last Post: edencthompson
  Function - Return multiple values tester_V 10 4,435 Jun-02-2021, 05:34 AM
Last Post: tester_V
  Function parameters and values as string infobound 1 1,753 Jul-24-2020, 04:28 AM
Last Post: scidam
  Function to return list of all the INDEX values of a defined ndarray? pjfarley3 2 1,958 Jul-10-2020, 04:51 AM
Last Post: pjfarley3
  Return values for use outside of function willowman 1 1,673 Apr-13-2020, 07:00 AM
Last Post: buran
  Function with many arguments, with some default values medatib531 3 2,575 Mar-14-2020, 02:39 AM
Last Post: medatib531
  auto supply values to a python script(function) from text file metro17 4 2,527 Oct-26-2019, 01:25 AM
Last Post: ichabod801
  How do I get the tangent of a degree SheeppOSU 3 4,556 Oct-24-2019, 02:13 PM
Last Post: ichabod801
  Custom Function to Return Database Values rm78 0 1,773 Sep-05-2019, 01:01 PM
Last Post: rm78

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020