[SDL] How to gray out a button image?
Will Langford
unfies at gmail.com
Wed Apr 9 08:38:45 PDT 2008
> Thanks for the information. I had no idea that there should be such
> magic numbers in the calculation. I would have just used the
> (unweighted) average, like David Olsen wrote. Is there a link with the
> rationale for those particular numbers?
Actually it all has to do with color theory. If ya glanced at the
link included with examples from other languages/platforms, I believe
they called one of the 'grayed' component values 'luminosity' (or
similar). And, if you think about it in just plain old experience --
green is a much "brighter" color than red or blue, etc. A quick
example would be to draw three large max-intensity red, green, and
blue rectangles in your favorite graphics editor... and just note how
vibrant the green appears. Similarly, if your art program can do grey
scale, you can convert these three rectangles to gray and notice
brightness difference.... and if it offers histogram stuffs or other
analytical tools, they might show you as well. Some more research
into the HSV color space might help explain things as well.
> I was actually expecting to not do the gray scale conversion myself. I
> was thinking that we would not be the first SDL based project that uses
I imagine there are some third party lib's (similar to sdl_image,
sdl_mixer, sdl_net, sdl_gfx, sdl_font, etc) that might offer gray
scaling. Inclusion of a gray scale conversion function in SDL is
probably debatable as to the core / meaning of what SDL is. You can
convert an image from one surface format to another, but I'm unsure
how SDL would react if you tried to convert a color surface into an
8bit palletized surface consisting of 256 shades of gray... it'd be
kind of interesting to find out though :).
> pictorial buttons that can be disabled. I was hoping that there was some
> library function that could do it and take advantage of special hardware
> features on different platforms (such as MMX).
The last time I did anything with MMX in assembly was with the
original MMX spec. I don't believe it offered anything for color
conversion of this type. Adding without overflow and similar, sure...
but... not too sure about colorspace conversion. Newer MMX/SSE stuff
might be able to handle it, though. Some graphics cards might be able
to do the colorspace conversion into gray scale, but I doubt it....
As suggested by someone else, you can try creating your grayscale
surfaces before actively using them... this way it's just a plane ole
regular blit. If you really want something real time, you might try
creating R G B lookup tables of each component so you don't have to do
the math (and instead its just an array index lookup. have your code
generate the lookup on startup or something?). On a 233mhz system I
was able to get decent real time gray scale conversion on a non-SDL
project... so... ya might be in for a surprise.
-Will
More information about the SDL
mailing list