Set a color to transparent

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
User avatar
cyse
Prole
Posts: 2
Joined: Wed Aug 15, 2012 10:05 am
Location: Brussels

Set a color to transparent

Post by cyse »

Hello guys,

I can't find how I tell löve X color should be transparent!

I want brown (106, 76, 48): Image to be displayed as transparent.

I added in love.load()

Code: Select all

	love.graphics.setBackgroundColor(106, 76, 48)
But it doesn't work:
Image

I would like my little miner and the rocks without brown, just on a the grass.

Should I use:
- PixelEffect ? viewtopic.php?f=4&t=10112&hilit=transparent -> I don't understand how I could do what I want :(
- Convert my PNG's to 8 bit colors and replace brown (6A4C30) by alpha: How could I do this in a batch? I have too many files to do this one by one :(

Any idea?

thx
User avatar
Jasoco
Inner party member
Posts: 3726
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Set a color to transparent

Post by Jasoco »

Yes. You need to use a PNG with transparency. Löve doesn't have the "use a specific color as a transparent mask" feature that I know of. We just use PNG's with transparent backgrounds. Convert all your images now. And get used to using transparency in the future. I haven't used a colored mask since my DOOM editing days. Soooo much magenta!!!
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Set a color to transparent

Post by kikito »

I'd also like to point out that PNGs allows for partial transparency on the alpha channel, which can make your borders look better (unless you are going for a "pixelated" effect, of course)
When I write def I mean function.
User avatar
Kadoba
Party member
Posts: 399
Joined: Mon Jan 10, 2011 8:25 am
Location: Oklahoma

Re: Set a color to transparent

Post by Kadoba »

You can load your image as image data first and then use ImageData:mapPixel() to alter a specific color to have zero alpha.

Code: Select all

local function loadTransparent(imagePath, transR, transG, transB)

   local imageData = love.graphics.newImageData( imagePath )

    local function mapFunction(r,g,b,a)
        if r == transR and g == transG and b == trangB then a = 0 end
        return r,g,b,a
    end

    imageData:mapPixel( mapFunction )

    return love.graphics.newImage( ImageData )

end
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: Set a color to transparent

Post by Ref »

Can remove backfround using pixel effects (much faster than mapPixel) but as pointed out before the results will most likely be unsatisfactory because of how the image was originally created (edge shading).
As always, best to start with quality rather than trying to patch.
Attachments
remove_background.love
Shader to create transparent background
(10.31 KiB) Downloaded 214 times
Zeliarden
Party member
Posts: 139
Joined: Tue Feb 28, 2012 4:40 pm

Re: Set a color to transparent

Post by Zeliarden »

here is a fixed ver of Kadoba code

Code: Select all

function loadTransparent(imagePath, transR, transG, transB)
	imageData = love.image.newImageData( imagePath )
	function mapFunction(x, y, r, g, b, a)
		if r == transR and g == transG and b == transB then a = 0 end
		return r,g,b,a
	end
	imageData:mapPixel( mapFunction )
	return love.graphics.newImage( imageData )
end
User avatar
josefnpat
Inner party member
Posts: 955
Joined: Wed Oct 05, 2011 1:36 am
Location: your basement
Contact:

Re: Set a color to transparent

Post by josefnpat »

If you use gimp, you can also use the "Select by Color" tool to get all of the brown in a spritesheet.
7ShGV.png
7ShGV.png (128.14 KiB) Viewed 980 times
Missing Sentinel Software | Twitter

FORCIBLY IGNORED.
<leafo> when in doubt delete all of your code
<bartbes> git rm -r *
<bartbes> git commit -m "Fixed all bugs"
<bartbes> git push
User avatar
cyse
Prole
Posts: 2
Joined: Wed Aug 15, 2012 10:05 am
Location: Brussels

Re: Set a color to transparent

Post by cyse »

thank you all for the answers! I'll try each solution until I'm happy with the result ;)

Removing the brown is effectively unsatisfactory but josefnpat's version seems good though: I'll try it with gimp.

thanks all - I'll post the result (in a few months - I won't get that much free time before that).
Post Reply

Who is online

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