Page 1 of 1

Color transparency

Posted: Sat Apr 03, 2010 11:58 pm
by ejulius
I'm in the beginning stages of my game. I was wondering on how do I create something transparent in an image? Is there a specific color that makes LOVE ignore that color and make it transparent?

Re: Color transparency

Posted: Sun Apr 04, 2010 12:22 am
by bmelts
No, LÖVE doesn't have any built-in support for that sort of thing. The easiest way is to make those pixels actually transparent in your images in the first place - use PNG, or some other file format that supports transparency.

Re: Color transparency

Posted: Sun Apr 04, 2010 12:32 am
by ejulius
What color in a png format image would be transparent?

Re: Color transparency

Posted: Sun Apr 04, 2010 1:30 am
by bmelts
A pixel with an alpha value of 0 is fully transparent. A format like PNG supports having pixels that are transparent, as opposed to a format like JPEG, which does not. Nearly all modern graphics editors support transparency (with the exception of, uh, Paint).

If you're having trouble getting an image to have transparent pixels in it, you can write some code that will do it for you. Let's say you've got a file called "hero.png" with a nice magenta color (RGB value: 255, 0, 255) as its background that you want to make transparent. First, you'd put this function somewhere in your code:

Code: Select all

function makeTransparent(x, y, r, g, b, a)
   if r == 255 and g == 0 and b == 255 then
     a = 0
   end
   return r,g,b,a
end
Then, when you want to load the image (in love.load, say), do this:

Code: Select all

local herodata = love.image.newImageData("hero.png")
herodata:mapPixel(makeTransparent)
hero = love.graphics.newImage(herodata)
And you would then have an Image called "hero", with all the magenta parts of the image turned transparent.

However, I still recommend making the image itself transparent – that way, you don't have to use any of the code above, and can just create an Image from the file directly without worrying.

Re: Color transparency

Posted: Mon Apr 05, 2010 3:19 pm
by zachwlewis
There is no one color that is transparent. In the image editor, you should be able to specify the transparency level of a color and paint with that.

Re: Color transparency

Posted: Mon Apr 05, 2010 9:53 pm
by Jasoco
Many graphics programs these days support alpha transparency and even layers so you should be fine with anything. I think newer versions of Paint? PhotoShop does and if you can't get PhotoShop, get Gimp. There are many others. Use Google.