[SDL] How to gray out a button image?

Will Langford unfies at gmail.com
Tue Apr 8 19:19:24 PDT 2008


The formula for converting something to grey scale is something similar to

G * 59%
B * 11 %
R * 30 %

Those are pulled off my head from a project about 7 years ago, so they
might be wrong.... but they should get ya darn close :).

You'll just have to optionally lock the surface, and alter the pixels.
 I'd suggest copying the surface to a new surface and modifying the
secondary surface.

So, you'll get something at the pixel level similar to:

grayed = (source_pixel_red * 0.10) + (source_pixel_green * 0.61) +
(source_pixel_blue * 0.19);

destination_pixel_red = grayed;
destination_pixel_green = grayed;
destination_pixel_blue = grayed;

Sorry for the lack of real code, but if you can do put/get pixel or
other pixel manipulation stuff, the psuedo code should give ya the
idea/concept.

Actually, a quick google gives some concept code in other languages,
and appears my numbers were correct, lucky memory guess heh:

http://www.bobpowell.net/grayscale.htm

-Will

On Tue, Apr 8, 2008 at 8:20 PM, David Olsen <jolynsbass at gmail.com> wrote:
> If I haven't misunderstood, you're asking how to take an image and turn it
>  into just plain grayscale, to indicate a state of un-clickable. An easy way
>  to do this would be to scan the image with the Get_Pixel routine(loops in
>  the x and y directions) included in the documentation, and then average the
>  rgb values for each pixel into a new image. Viola, grayscale. (If that's
>  what you are looking for, that is)
>  psuedocode:
>  SDL_Surface * button_regular = SDL_LoadBMP("regular.bmp");
>  SDL_Surface * button_gray = SDL_CreateRGBSurface(make image of appropriate
>  size);
>  for (int y=0; y<button_regular->h; y++)
>   for (int x=0; x<button_regular->w; x++)
>   {
>    Uint 8 r,g,b,a;
>    Uint32 pixel = Get_Pixel(button_regular,x,y);
>    SDL_GetRGB(pixel, button_regular->fmt, &r, &g, &b);
>    r = g = b = (r+g+b)/3;
>    pixel = SDL_MapRGB(button_gray->fmt, r, g, b);
>    Put_Pixel(button_gray,x,y,pixel);
>   }
>  //button_gray should now look just like button_regular, but in grayscale.
>
>  the get_pixel and put_pixel routines can be found in the SDL Documentation
>  under the examples 2-4 and 2-5.
>  Hope that helps,
>  -Dave Olsen
>
>
>
>  ----- Original Message -----
>  From: "Erik" <esigra at gmail.com>
>  To: "A list for developers using the SDL library. (includes SDL-announce)"
>
> <sdl at lists.libsdl.org>
>  Sent: Tuesday, April 08, 2008 6:26 PM
>  Subject: [SDL] How to gray out a button image?
>
>
>
>
> > We need to gray out[1] buttons with images on in our toolkit. I just
>  > wonder what is the best way to gray out the image. Has anyone done that
>  > before? Does it need SDL_gfx or just SDL?
>  >
>  > [1] [http://en.wikipedia.org/wiki/Grayed_out]
>  > _______________________________________________
>  > SDL mailing list
>  > SDL at lists.libsdl.org
>  > http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
>
>  _______________________________________________
>  SDL mailing list
>  SDL at lists.libsdl.org
>  http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
>


More information about the SDL mailing list