Hey guys,
Im new to Löve and i want to draw a rectangle with the function "love.graphics.rectangle( "line", 10, 10, 50, 50 )" and now i want to change the color of it. Is that possible?
Change Color of Rectangle
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: Change Color of Rectangle
use love.graphics.setColor(R,G,B[,Alpha]) to alter the colour love draws in.
For example to make love draw in red you would do
For example to make love draw in red you would do
Code: Select all
love.graphics.setColor(255,0,0)
--Drawing stuff
love.graphics.setColor(255,255,255) -- reset colours
Your screen is very zoomed in...
- Jasoco
- Inner party member
- Posts: 3727
- Joined: Mon Jun 22, 2009 9:35 am
- Location: Pennsylvania, USA
- Contact:
Re: Change Color of Rectangle
The love.graphics.setColor() function is what you use.
Once you set the color, it stays that color until you change it.
When you want to print text or a shape in a color, set the color right before it. But don't worry about setting it every time. Just set it once and it'll remain that color until you change it. For instance, to draw an image which would be set to 255,255,255 (white) to draw normally. But for other things like shapes and text, use whatever color you want.
Colors are in RGBA format. Red, Green, Blue, Alpha. And go from 0 to 255. If you don't know what numbers you need to use a specific color, Google for RGB tools and there's probably numerous sources on the internet for letting you adjust colors and get the numbers.
Alpha is opacity. 0 is completely transparent and 255 is completely opaque (Solid). Alpha is also optional. You either set it or your don't.
If you have questions, just ask! Fake edit: Beaten but who cares.
Once you set the color, it stays that color until you change it.
When you want to print text or a shape in a color, set the color right before it. But don't worry about setting it every time. Just set it once and it'll remain that color until you change it. For instance, to draw an image which would be set to 255,255,255 (white) to draw normally. But for other things like shapes and text, use whatever color you want.
Code: Select all
love.graphics.setColor(255,0,0) --Red
love.graphics.rectangle( "line", 10, 10, 50, 50 )
Alpha is opacity. 0 is completely transparent and 255 is completely opaque (Solid). Alpha is also optional. You either set it or your don't.
If you have questions, just ask! Fake edit: Beaten but who cares.
Re: Change Color of Rectangle
Changing the drawing color is a bit of a performance penalization (opengl guys have yelled at me, that I should be drawing in batches before changing color/texture), I generally use this with Love:
That way I can simply wrap rectange drawing like so:
And then use it like so:
Code: Select all
Graphics = {}
function Graphics:setColor(c)
if self.lastColor ~= c then
self.lastColor = c
love.graphics.setColor(c)
end
return self
end
WHITE = { 255, 255, 255 }
Code: Select all
function Graphics:drawRect( x, y, w, h, c )
if c then self:setColor(c) end
love.graphics.rectangle( "fill", x, y, w, h )
return self
end
Code: Select all
Graphics:drawRect( 5, 10, 50, 25, WHITE )
-- or method chaining
Graphics:setColor( WHITE ):drawRect( 5, 10, 50, 25 )
Who is online
Users browsing this forum: Bing [Bot] and 4 guests