[help] Dynamically generate gradient

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
soullessbr
Prole
Posts: 11
Joined: Sat Jun 01, 2013 1:18 am

[help] Dynamically generate gradient

Post by soullessbr »

how can I draw a gradient at runtime?

Thank you ...
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: [help] Dynamically generate gradient

Post by Plu »

Prep a canvas, select a starting color, an ending color, make a for loop and use the love.graphics.line function with slowly changing colors. You can calculate how fast the colors need to change by getting the difference between start and end and dividing by the length of the gradient. (ie from 0 to 100 in 400 lines means you add 0.25 to the color in each loop cycle)
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: [help] Dynamically generate gradient

Post by T-Bone »

I guess a shader can be an option, depends a little on how you want to use it. If you want to change the colors every frame, a shader will work better. If it should only be drawn once (or a few times) then a Canvas is better.
User avatar
Jasoco
Inner party member
Posts: 3726
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: [help] Dynamically generate gradient

Post by Jasoco »

I think the hardest part is doing the color transition.
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: [help] Dynamically generate gradient

Post by T-Bone »

It doesn't have to be all that hard. Just do a weighted average of all three components, and let the weight of the first color go from 1 to 0 linearly while the second colors weight goes from 0 to 1.
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: [help] Dynamically generate gradient

Post by vrld »

You can (ab)use ImageData:

Code: Select all

local function Gradient(from, to, orientation)
	orientation = orientation or 'horizontal'

	local d = 1
	if orientation == 'vertical' then
		d = 0
	elseif orientation ~= 'horizontal' then
		error('invalid orientation: ' .. tostring(orientation))
	end

	local img = love.image.newImageData(1+d, 2-d)
	print(orientation, d, 1-d, 1+d, 1+(d-1))
	img:setPixel(0,0, from[1], from[2], from[3], from[4] or 255)
	img:setPixel(d,1-d, to[1], to[2], to[3], to[4] or 255)

	img = love.graphics.newImage(img)
	img:setFilter('linear', 'linear')
	return {
		draw = function(x,y, w,h, r, ox,oy, kx,ky)
			ox = (ox or 0) / w
			oy = (oy or 0) / h
			love.graphics.draw(img, x,y, r or 0, w/(1+d), h/(2-d), ox,oy, kx,ky)
		end
	}
end

function love.load()
	gradient = Gradient({40,85,175}, {140,218,234})
	gradient2 = Gradient({200,100,30}, {80,200,50}, 'vertical')
end

function love.draw()
	gradient.draw( 10,10, love.graphics.getWidth()-10,100)
	gradient2.draw(10,120, 150,150)
end
Result:
gradient.jpg
gradient.jpg (5.37 KiB) Viewed 5010 times
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
wesleylucas
Prole
Posts: 12
Joined: Thu May 09, 2013 11:08 pm
Location: Brazil

Re: [help] Dynamically generate gradient

Post by wesleylucas »

I just converted an old script for RPG Maker:

Code: Select all

function newColor(r,g,b,a)
 local c = {}
 c.red = r
 c.green = g
 c.blue = b
 if a == nil then
  c.alpha = 255
 else
  c.alpha = a
 end
 return c
end

function makeGradient(width,height,startColor,endColor)
 local rcalc = endColor.red   - startColor.red
 local gcalc = endColor.green - startColor.green
 local bcalc = endColor.blue  - startColor.blue
 local acalc = endColor.alpha - startColor.alpha
 local i = 0
 local r
 local g
 local b
 local a
 local color = love.graphics.getColor()
 local gradient = love.graphics.newCanvas(width,height)
 love.graphics.setCanvas(gradient)
 while i < width do
  r = startColor.red   + (i * rcalc / width)
  g = startColor.green + (i * gcalc / width)
  b = startColor.blue  + (i * bcalc / width)
  a = startColor.alpha + (i * acalc / width)
  love.graphics.setColor(r,g,b,a)
  love.graphics.rectangle('fill',i, 0, 1, height)
  i = i + 1
 end
 love.graphics.setCanvas()
 return gradient
end
Example:

Code: Select all

function love.load()
 rrt = makeGradient(800,600,newColor(255,0,0),newColor(0,255,0))
end

function love.draw()
 love.graphics.draw(rrt)
end
Original script: http://forums.mundorpgmaker.com.br/inde ... ic=18164.0 (PT-BR)
def or function doesn't matter, you understand what I mean anyway.
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: [help] Dynamically generate gradient

Post by Ref »

Never would have guessed that you could use 'setPixel' this way.
Thanks vrid!
Demo just confirming that I got the logic correct.
Attachments
Modified Gradient.love
Slight mod of vrid's script
(905 Bytes) Downloaded 200 times
soullessbr
Prole
Posts: 11
Joined: Sat Jun 01, 2013 1:18 am

Re: [help] Dynamically generate gradient

Post by soullessbr »

Very good! first thank you all! And two other questions, I put the static assets within the draw function, but if I'm not mistaken it is to draw all the time or not?

Because I want the gradient to be done only when the level was loaded, since it does not change anymore.

Thank you!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 2 guests