# Hue to RGB

This stems from my attempts to generate random changing colours. Using (R,G,B) creates 256 x 256 x 256 possibilities, many of which didn’t suit what I wanted, which was punchy saturated colours. By looking at RGB values when Saturation & Brightness are both 100% for different Hues, I mapped it down to 256. The code is pretty self explanatory, and function Hue(0-255) will generate appropriate R, G, B, Hue values in text window and on screen.

But I’m sure there is probably a MUCH more efficient way of coding it that this… is there?

``````function setup()
h = 0
parameter.integer("h",0,255,125)
end

function Hue(h)

if h < 43 then
r,g = 255,0
b = 6*h
end

if h > 42 and h < 86 then
g,b = 0,255
r = 510 - 6*h
end

if h > 85 and h < 128 then
r,b = 0,255
g = 6*(h-85)
end

if h > 127 and h < 171 then
r,g= 0,255
b = 1020 - 6*h
end

if h > 170 and h < 213 then
g,b = 255,0
r = 6*h -1020
end

if h > 212 then
r,b = 255,0
g = 1530 - 6*h
end

end

function draw()

Hue(h)

print(r, g, b, h)
background(r,g,b)

end
``````

Just reading @Ignatz 's Lua for beginners on combining multiple if tests, which is very appropriate for this…

``````function setup()
h = 0
parameter.integer("h",0,255,125)
end

function Hue(h)

if h < 43 then r,g,b = 255,0,6*h

elseif h < 86 then r,g,b = 510-6*h,0,255

elseif h < 128 then r,g,b = 0,6*(h-85),255

elseif h < 171 then r,g,b = 0,255,1020-6*h

elseif h < 213 then r,g,b =  6*h-1020,255,0

elseif h > 212 then r,g,b = 255,1530-6*h,0 end

end

function draw()

Hue(h)

print(r, g, b, h)
background(r,g,b)

end
``````

Think that’s as concise as is possible.

@David. you’re practicing or implementing the application of colors (RGBA) by parameter.integer. it shows well. but you want to display a random color? or something like that

@David - when creating helper functions like Hue, I suggest you make the rgb variables local, then return them to the calling function, ie

``````function Hue(h)
local r,g,b
-- the rest of your code
return r,g,b
end

--then in draw...
print(Hue(h))
``````

This makes your Hue function more portable and self contained.

@David Or if you want to avoid “local” try this

``````
function setup()
parameter.integer("h",0,255,125)
end

function Hue(h)
if     h < 43  then return 255,0,6*h
elseif h < 86  then return 510-6*h,0,255
elseif h < 128 then return 0,6*(h-85),255
elseif h < 171 then return 0,255,1020-6*h
elseif h < 213 then return 6*h-1020,255,0
elseif h > 212 then return 255,1530-6*h,0 end
end

function draw()
background(Hue(h))
end

``````

When combining the conditionals into a series of `elseif`s then the last condition, `h > 212` is an empty test. We already know that `h` is not less than `213` so (as it is an integer) it must be greater than `212`.

@Luismi Yes, that is the idea, as well as cycling through the values, so something like

``````h = math.random(0,255)
---
draw something
---
h = h+0.1
if h>255 then h=0 end
``````

I wrote a simple curve stitching program which I’ll post, and it looks quite pretty cycling through the colours.

@Ignatz & @dave1707 I don’t understand the significance of local and what it means to be portable or self contained… is that covered in the eBooks? My only understanding is that a global variable can be used anywhere and local can only be used within the function that declared it. As with all this stuff, I’m keen to learn and know more.

@Andrew_Stacey Thanks, and yeah that makes sense, but for some reason when I took it out, the rgb values get screwed up at the high end…

``````elseif h < 213 then r,g,b =  6*h-1020,255,0

end

r,g,b = 255,1530-6*h,0

end
``````

or the low end…

``````elseif h < 213 then r,g,b =  6*h-1020,255,0

r,g,b = 255,1530-6*h,0 end

end
``````

I’m obviously doing something silly but not yet figured out what?

@dave1707 Also not sure how your code puts the rgb values in the variables or did you miss that bit out?

@David, dave1707’s code returns the value, so if you would call the function
Hue(h) then you’d get 3 vars returned, well 3 values actually, which yous still have to put in vars

so you can do something like

``````a, b, c = Hue(h)
``````

or more in your way of doing

``````r, g, b = Hue(h)
``````

the return function returns a value, in order to save that value, you need to ‘save that function’ into a var, it doesn’t ‘save the function’ but saves all values that are returned, but because the function return 3 values, you need 3 vars to store it to @David - the significance of local is that if you share this code as it is, then you are defining global variables r,g,b. The person using your code may also be using these variables somewhere else, creating errors.

It is safest to use local variables - which only exist in your function and not outside - and then return the three r,g,b values as dave1707 as shown. Then you won’t mess up someone else’s code if they use your function.

(I’m sure I put this in the Lua ebook somewhere)

Thanks guys… I understand it a lot more now but have run into a problem implementing it. Without posting the entire code, this should hopefully illustrate…

I’m already using variables b and h in my code. Not figured out how I can use ‘h’ here… function Hue (local h) maybe? So I avoided using it.
But I’m also finding that b is affecting the variable outside the function too.

This is the Hue function as suggested…

``````function Hue(hue)
local r,g,b
if hue < 43 then return 255,0,6*hue
elseif hue < 86 then return 510-6*hue,0,255
elseif hue < 128 then return 0,6*(hue-85),255
elseif hue < 171 then return 0,255,1020-6*hue
elseif hue < 213 then return 6*hue-1020,255,0
elseif hue > 212 then return 255,1530-6*hue,0 end

end
``````

and this is just a part of the program that calls it…

``````function outsideCurves2()

for a = 0, w, pixelSize do
b = w-a

local r,g,b = Hue(hueO2)
stroke(r,g,b,255)

line(0, a, a/2, w)      -- top left 1
line(b, w, 0, b/2+h)    -- top left 2
line(w, a, b/2+h, w)    -- top right 1
line(a, w, w, b/2+h)    -- top right 2
line(0, b, a/2, 0)      -- bottom left 1
line(b, 0, 0,a/2)       -- bottom left 2
line(w, b, b/2+h, 0)    -- bottom right 1
line(a, 0, w, a/2)      -- bottom right 2

hueO2 = hueO2 + 0.02
if hueO2 > 255 then hueO2 = 0 end

end

end
``````

So basically how do I keep the ‘b’ in rgb local and still use it outside the function to set the stroke colour?

@david, you set the b for hue after you set the other b, so the function uses the last know value of b, which is in this case, the hue value…

So you should do something like

``````function outsideCurves2()

for a = 0, w, pixelSize do

local r,g,b = Hue(hueO2)
stroke(r,g,b,255)

b = w - a

line(0, a, a/2, w)      -- top left 1
line(b, w, 0, b/2+h)    -- top left 2
line(w, a, b/2+h, w)    -- top right 1
line(a, w, w, b/2+h)    -- top right 2
line(0, b, a/2, 0)      -- bottom left 1
line(b, 0, 0,a/2)       -- bottom left 2
line(w, b, b/2+h, 0)    -- bottom right 1
line(a, 0, w, a/2)      -- bottom right 2

hueO2 = hueO2 + 0.02
if hueO2 > 255 then hueO2 = 0 end

end

end
``````

or even use another var for the hue, it doesn’t have to be local r,g,b it can also be local r,g,v or so…

Typically, it’s not just hue by itself, but HSL (Hue Saturation Lightness) or HSV (Hue Saturation Value)…

@SkyTheCoder Yes The initial post explains that.

@stevon8ter Thanks - I’ll try that…

In the meantime I’ve posted a new thread ‘Curve Stitching’ It works fine but doesn’t use local variables. So that will be the next step…

ahhh thanks @stevon8ter - that now works properly. I just need to get my head around precisely why… this is what I think is happening…

local r,g,b keeps the variables local to the function. The function ‘returns’ 3 values, and the very next line - in this case stroke - assigns these three values in order to r,g,b. Then it immediately reverts back to global, so the line b = w - a will use global variables. And if the next line was say w = b - a, the second use of variable b here would be global, right?

@David i don’t completely get what you mean, but this is how it works:

• you set the vars local to the function Hue, this will not affect any global values with the same name…
• you don’t actually use the global var when doing soemthing like
``````local r,g,b = hue()
b = w - a
``````

What you do is change the value of the local var…

If you have a global var, b, it still will not be affected by this

I hope that makes sense :s

Ahhhhh gotcha! That makes sense now. ty!