Change Color of Rectangle

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
Manner
Prole
Posts: 3
Joined: Tue Jun 12, 2012 5:28 pm

Change Color of Rectangle

Post by Manner »

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?
User avatar
mickeyjm
Party member
Posts: 237
Joined: Thu Dec 29, 2011 11:41 am

Re: Change Color of Rectangle

Post by mickeyjm »

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

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...
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Change Color of Rectangle

Post by Jasoco »

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.

Code: Select all

love.graphics.setColor(255,0,0) --Red
love.graphics.rectangle( "line", 10, 10, 50, 50 )
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.
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: Change Color of Rectangle

Post by Inny »

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:

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 }
That way I can simply wrap rectange drawing like so:

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
And then use it like so:

Code: Select all

Graphics:drawRect( 5, 10, 50, 25, WHITE )

-- or method chaining
Graphics:setColor( WHITE ):drawRect( 5, 10, 50, 25 )
Manner
Prole
Posts: 3
Joined: Tue Jun 12, 2012 5:28 pm

Re: Change Color of Rectangle

Post by Manner »

thanks :)
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 4 guests