Color transparency

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
ejulius
Prole
Posts: 3
Joined: Sat Apr 03, 2010 4:13 am

Color transparency

Post 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?
User avatar
bmelts
Party member
Posts: 380
Joined: Fri Jan 30, 2009 3:16 am
Location: Wiscönsin
Contact:

Re: Color transparency

Post 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.
ejulius
Prole
Posts: 3
Joined: Sat Apr 03, 2010 4:13 am

Re: Color transparency

Post by ejulius »

What color in a png format image would be transparent?
User avatar
bmelts
Party member
Posts: 380
Joined: Fri Jan 30, 2009 3:16 am
Location: Wiscönsin
Contact:

Re: Color transparency

Post 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.
User avatar
zachwlewis
Citizen
Posts: 70
Joined: Fri Mar 19, 2010 7:58 am
Location: Huntsville, AL
Contact:

Re: Color transparency

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

Re: Color transparency

Post 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.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 68 guests