Not logged inGosu Forums
Forum back to libgosu.org Help Search Register Login
Up Topic Gosu / Gosu Exchange / How to set transparent colour of Image?
- - By Levi Date 2011-02-15 02:09 Edited 2011-02-15 02:41
I've been going through the ruby gosu documentation, but I can't seem to find anything that lets me choose/convert the transparent color of an image.  I have a bunch of PNG's with the traditional ugly pink background that I would like to use with gosu.

Any ideas, or is my only option to open them up in an image editor and convert the pink into a transparent color?
Parent - - By banister Date 2011-02-15 03:47
This is trivial in texplay:

image.clear  :dest_select => MagicPink

Where MagicPink is the special pink color (expressed in rgba)
Parent - - By Levi Date 2011-02-15 04:22 Edited 2011-02-15 04:34
This looks perfect!

Unfortunately I can't seem to get the texplay gem loaded for some reason.  I'll play around with it a bit.  Thanks, this library actually looks like it'll solve a bunch of my future worries as well!  :)

Edit:  Ahah, I had a ruby 1.9 installation somewhere gumming up the works.  This worked great!  :)
Edit2:  I notice that the dest_select option doesn't seem to be in the ruby docs(or at least not the one I have).  Any other useful ones that might come in handy but aren't documented?
Parent - By banister Date 2011-02-15 05:00
yeah there's a bunch of things i haven't documented :( If you look on the texplay thread here on the forums under "ext" you'll find most things  :)
Parent - - By RunnerPack Date 2011-02-15 05:11
If you only want it to be done once, instead of each time anyone runs your game/app, you can use ImageMagick (http://www.imagemagick.org/) like so:

mogrify -format png -transparent magenta *.png

You'll probably want to specify a different output folder to prevent overwriting, or run it on a copy of the images, just in case something goes wrong. Just check the docs.
Parent - - By banister Date 2011-02-15 05:16
nice. Alternately, he can also save the images using gosu (new functionality, pretty cool :))

image.clear  :dest_select => MagicPink
image.save "fixed.png"
Parent - By RavensKrag Date 2011-02-21 01:41
Cool, didn't think it'd be this simple.  That's quite a convenient addition.  However, perhaps it would be better to call the method #dump, to be consistent with Ruby's serialization of objects through the YAML library.

Calling it #save makes sense, but it could also be interpreted as you are saving the current state of the image in memory.
Parent - - By jlnr (dev) Date 2011-02-15 13:48
Gosu automatically turns pink pixels into transparent pixels. This also one advantage that I'm not sure the other options have: When Gosu makes pixels transparent, it assigns them the average color value of the surrounding pixels. This is important if you want to stretch or rotate your images later. For example, if you draw an image at 2x its size, it may draw the average between your image pixel and a now transparent pixel. If the transparent pixel is still pink, then the image will "bleed" pink. If the pixel is black (Gosu::Color::NONE), it will "bleed" black.

You should be able to use Gosu itself to do the conversion:
  img = Image.new("with_pink.png")
  img.save "with_pink.bmp"
  img = Image.new("with_pink.bmp")  # Transparency is automatically applied here.
  img.save "proper_png.png"
Parent - - By RunnerPack Date 2011-02-15 19:34
D'oh! I was even looking stuff up in the Gosu doc around the time I posted my answer, and I completely missed:

"new (window, filename_or_rmagick_image, tileable)

Loads an image from a given filename that can be drawn onto the given window.

This constructor can handle PNG and BMP images. A color key of ff00ff is automatically applied to BMP type images. For more flexibility, use PNG files."

Of course, I wasn't actually looking up the new method, but it was probably on the screen multiple times! Does it apply in all cases, i.e. "load_tiles" and the extended "new" syntax, too? If so, maybe a "see also" or mention of the chroma-key effect in their descriptions would be in order?

Also, a small bug report for the forum: I'm posting this in the "QuickReply" box, and there is a missing button image at the top. It's the "Go to parent post" button, and it should be at: http://www.libgosu.org/cgi-bin/mwf/default/nav_up.png but it isn't.
Parent - - By jlnr (dev) Date 2011-02-16 00:11

> Of course, I wasn't actually looking up the new method, but it was probably on the screen multiple times! Does it apply in all cases, i.e. "load_tiles" and the extended "new" syntax, too?


It applies to load_tiles and everything else where you can use a BMP file.
Actually, that thing about the new syntax is a good question (Image.new(filename_or_rmagick, options_hash={})). Having a :color_key=> option sounds reasonable, but if it would default to fuchsia, wouldn't one expect it to be applied to PNG files too? I have to think about this. ;)

> If so, maybe a "see also" or mention of the chroma-key effect in their descriptions would be in order?


I'd love to have something like that, since basically every single parameter in Gosu appears a dozen times. But I don't really know how to efficiently do that sort of stuff with Rdoc.

I could still add a single file about the color key, but that's like all the stuff I want to write down, I haven't found time for it yet. Contributions welcome!
Parent - By banister Date 2011-02-16 00:35
time to start using YARD...rdoc has gone nowhere in years
Parent - - By erisdiscord Date 2011-02-16 13:17
Well, can you determine from libpng whether a PNG image has an alpha channel or not? Is it actually possible for a PNG to not have the channel, or is it there whether it's used or not?

I think the default color key should be applied only to images completely lacking an alpha channel if this is possible.
Parent - - By RunnerPack Date 2011-02-17 00:29
I agree about the default application of the color key.

The alpha channel is definitely optional, but there's also the "tRNS" chunk that specifies transparency for palette-based images (it's a set of alpha values for one or more colors in the "PLTE" chunk). Is this method handled transparently (pun acknowledged) by libpng? If not, I wouldn't fault gosu for not supporting it. Many paint programs don't even support it beyond emulating the gif89a method of setting one color in the palette as fully transparent.

I would assume that it's quite easy to find out if transparency is present. At the very least you could check for alpha values lower than 255 in the buffer.
Parent - - By erisdiscord Date 2011-02-17 01:30
Aha, I see now after looking at the PNG specification that alpha is explicitly optional. I wonder, though, if most software that saves PNGs actually supports saving them without it? There are so many variations, I'm kind of not surprised that support for the format is frequently broken or incomplete.
Parent - - By RunnerPack Date 2011-02-17 02:19
Actually, I would assume that there are more apps that don't support alpha at all than ones that force superfluous alpha channels…

Anyway, the two apps I use most—IrfanView and Paint Shop Pro 8.10 (an oldie but a goodie ;)—both have pretty good PNG support, including alpha-less images (in fact, IV only supports the "single transparent color" mode, even for truecolor images, except maybe in batch conversions... I need to check that).

PSP's is actually quite nice, supporting interlacing, gamma value setting, and many different transparency modes. IV doesn't seem to handle greyscale+alpha images correctly, but it's otherwise indispensable and highly recommended.
Parent - By erisdiscord Date 2011-02-17 17:49
Oh yeah, I'm pretty familiar with irfanview; I used it quite a bit when I was using Windows. I generally use ImageMagick for my image conversion needs on my Mac, but then I kind of live in the terminal.

I do kinda miss Paint Shop Pro 8 from Windows. Tried it under Wine and it just wasn't the same. Old versions of Photoshop for Mac are hard to come by for whatever reason, and the new version is terrible.
Up Topic Gosu / Gosu Exchange / How to set transparent colour of Image?

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill