changing hue

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
slydc
Prole
Posts: 11
Joined: Mon Jul 09, 2012 12:37 pm
Contact:

changing hue

Post by slydc »

Hi folks,

This is my first post and i need some help for a simulator i'm trying to do. The problem is that i'm trying to simulate a hue potentiometer
(like on old TV's). The closest thing i have found is a random color background that changes randomly in the Pop game by VeeThree.

I don't want the colors at random, just something simple as like if i press "1", the hue goes from 0 to 360 (potentiometer clockwise)
and press "2", hue goes from 360 to 0 (potentiometer counterclockwise).

Can someone help me before i go bald and insane ?! ;)
--- Sly DC ---
Bannana97
Citizen
Posts: 77
Joined: Wed May 16, 2012 2:49 pm

Re: changing hue

Post by Bannana97 »

You could try tinting the screen with a transparent color...
Won't work well, however.

For images, you can use love.graphics.setColor to set a tint for them, or RGB 255,255,255 to reset it back to normal.
User avatar
SimonLarsen
Party member
Posts: 100
Joined: Thu Mar 31, 2011 4:47 pm
Location: Denmark
Contact:

Re: changing hue

Post by SimonLarsen »

I'm not sure what you want to do, but you can use the HSL function on the wiki.

Here's quick demo for you:

Code: Select all

function HSL(h, s, l, a)
    if s<=0 then return l,l,l,a end
    h, s, l = h/256*6, s/255, l/255
    local c = (1-math.abs(2*l-1))*s
    local x = (1-math.abs(h%2-1))*c
    local m,r,g,b = (l-.5*c), 0,0,0
    if h < 1     then r,g,b = c,x,0
    elseif h < 2 then r,g,b = x,c,0
    elseif h < 3 then r,g,b = 0,c,x
    elseif h < 4 then r,g,b = 0,x,c
    elseif h < 5 then r,g,b = x,0,c
    else              r,g,b = c,0,x
    end return (r+m)*255,(g+m)*255,(b+m)*255,a
end

function love.load()
	hue = 0
	direction = 1 -- increasing hue
end

function love.update(dt)
	hue = hue + 32*dt*direction

	if hue > 255 then hue = 0
	elseif hue < 0 then hue = 255 end
end

function love.draw()
	love.graphics.setColor(HSL(hue,255,128))
	love.graphics.rectangle("fill",0,0,800,600)

	love.graphics.setColor(255,255,255,255)
	love.graphics.print("Hue: "..hue, 16,16)
	love.graphics.print(direction == 1 and "increasing" or "decreasing",16,32)
end

function love.keypressed(k,uni)
	if k == "1" then
		direction = 1
	elseif k == "2" then
		direction = -1
	end
end
slydc
Prole
Posts: 11
Joined: Mon Jul 09, 2012 12:37 pm
Contact:

Re: changing hue

Post by slydc »

Thank you so much SimonLarsen! This is what i was looking for! :awesome:

But your code increases or decreases the hue automatically, so i sightly modified it to change the hue manually like this:

Code: Select all

function HSL(h, s, l, a)
    if s<=0 then return l,l,l,a end
    h, s, l = h/256*6, s/255, l/255
    local c = (1-math.abs(2*l-1))*s
    local x = (1-math.abs(h%2-1))*c
    local m,r,g,b = (l-.5*c), 0,0,0
    if h < 1     then r,g,b = c,x,0
    elseif h < 2 then r,g,b = x,c,0
    elseif h < 3 then r,g,b = 0,c,x
    elseif h < 4 then r,g,b = 0,x,c
    elseif h < 5 then r,g,b = x,0,c
    else              r,g,b = c,0,x
    end return (r+m)*255,(g+m)*255,(b+m)*255,a
end

function love.load()
	hue = 0
end

function love.update(dt)
	if hue > 255 then hue = 0
	elseif hue < 0 then hue = 255 end
	if love.keyboard.isDown('1') then hue = hue +10*dt end   
	if love.keyboard.isDown('2') then hue = hue -10*dt end   
end

function love.draw()
	love.graphics.setColor(HSL(hue,255,128))
	love.graphics.rectangle("fill",0,0,800,600)
	love.graphics.setColor(255,255,255,255)
	love.graphics.print("Hue: "..hue, 16,16)
end
So thanks to you, i won't go bald & insane...LOL!!! ^^
--- Sly DC ---
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 6 guests