# Hue to RGB? (Or any range of number to RGB)

### » Wed May 18, 2011 8:26 pm

I need to change the color filter of a sprite, based on HUE or even any range of number (i.e. 1-1000).

I know I can do this with the HUE shader effect, but my netbook doesn't like it, and I'd like to stay away from pixel shaders (for performance)

How can I get RGB from a range of numbers?
B
34
S
14
G
4
Posts: 251
Reputation: 4,074

### » Wed May 18, 2011 8:38 pm

Right click on the bar above the color, select use expression, then use rgb(r, g, b) with r, g, and b being values from 0-255.
Moderator
B
96
S
34
G
33
Posts: 3,007
Reputation: 27,921

### » Wed May 18, 2011 9:21 pm

I know I can do that, what I need is to find out the rgb from a hue.
B
34
S
14
G
4
Posts: 251
Reputation: 4,074

### » Wed May 18, 2011 10:53 pm

Oh, sorry, misunderstood. Not sure how to do that.
Moderator
B
96
S
34
G
33
Posts: 3,007
Reputation: 27,921

### » Wed May 18, 2011 11:22 pm

That are two different demands.

1) Hue
Will never stand alone, it is part of some models that represent RGB in another geometry. You will find 3 models, HSV, HSL, HSI (Hue Saturation [Value, Lightness, Intensity]). In case you need to convert from one of the first two models, Python is the easiest way:
[code:3prbcqlu]import colorsys
my_rgb = colorsys.hsv_to_rgb(0.25, 0.5, 0.4)
r = my_rgb[0] * 255
g = my_rgb[1] * 255
b = my_rgb[2] * 255[/code:3prbcqlu]

A more useful implementation

1. Put the following script as a subevent of 'Start of layout':
[code:3prbcqlu]import colorsys

class FromHSV(object):
def __init__(self):
self.__h = 0
self.__s = 0
self.__v = 0

def set_hue(self, hue):
self.__h = hue

def set_saturation(self, saturation):
self.__s = saturation

def set_value(self, value):
self.__v = value

def get_red(self):
return colorsys.hsv_to_rgb(self.__h, self.__s, self.__v)[0] * 255

def get_green(self):
return colorsys.hsv_to_rgb(self.__h, self.__s, self.__v)[1] * 255

def get_blue(self):
return colorsys.hsv_to_rgb(self.__h, self.__s, self.__v)[2] * 255

Convert = FromHSV()[/code:3prbcqlu]

Then, as soon as you have the HSV values, use any of these to fill the class:
[code:3prbcqlu]Convert.set_hue(yourhuehere)
Convert.set_saturation(yoursathere)
Conert.set_value(yourvalhere)[/code:3prbcqlu]

And finally, whereever you need the rgb, use the functions get_red, get_green or get_blue. In the rgb expression it would be:
[code:3prbcqlu]RGB(Python("Convert.get_red()"), Python("Convert.get_green()"), Python("Convert.get_blue()"))[/code:3prbcqlu]

2) From any range of numbers
I'm not sure what you mean. Basically, red, green and blue are expressed as values in the range [0, 255]. If you have values in another range, just normalize them and then map them to [0, 255]. For example, v in range [0, 1000] would become v / 1000 * 255, v in range [3, 20] would become (v - 3) / 17 * 255, etc.
B
24
S
8
G
10
Posts: 1,821
Reputation: 8,289

### » Thu May 19, 2011 12:24 am

Thanks, I'll look at that.

But just to clarify, the Sat & Val are always 100. I just need to retrieve the RGB with the HUE being the only changing variable.

Is there a simpler way? (preferably without using Python?)
B
34
S
14
G
4
Posts: 251
Reputation: 4,074

### » Thu May 19, 2011 6:35 am

I couldn't think of anything easier than calling a function "hsv_to_rgb"
If you don't want to use Python, you can always do your own conversion events. Converting between color models involves some math, here is one example code (or adapt the one from the shader):
B
24
S
8
G
10
Posts: 1,821
Reputation: 8,289

### » Thu May 19, 2011 1:58 pm

If anyone is interested I figured it out:

[code:xnwb0n3t]RGB(Max(510*Abs(Cos([ANGLE]))-255, 0), Max(510*Abs(Cos([ANGLE]+120))-255, 0), Max(510*Abs(Cos([ANGLE]+240))-255, 0)) [/code:xnwb0n3t]

Thanks for the help.
B
34
S
14
G
4
Posts: 251
Reputation: 4,074

### » Thu May 19, 2011 4:19 pm

tulamide your python class works great when setting the values if i set them directly like -

Convert.set_hue(0.25)
Convert.set_saturation(0.5)
Convert.set_value(0.4)

however when i do this to have EditBox input -

Convert.set_hue(EditBox.Value)
Convert.set_saturation(EditBox2.Value)
Convert.set_value(EditBox3.Value)

I am getting errors, so i am guessing this might be the wrong way to get EditBox values. Is there a way to make the EditBox inputs work?
B
2
S
2
G
1
Posts: 121
Reputation: 1,092

### » Fri May 20, 2011 3:14 am

The edit box only knows text. You access its content with 'Get text' (Editbox.Text). But Python does not autoconvert the text to a number. So use the Python-built-in function float(), e.g.:
[code:2v0yez47]Convert.set_hue(float(EditBox.Text))[/code:2v0yez47]
B
24
S
8
G
10
Posts: 1,821
Reputation: 8,289

Next