PDA

View Full Version : How to get a palette color



masonwheeler
22-07-2007, 04:43 PM
I asked a question about this, didn't get any answers, and finally did some hacking around until I figured it out on my own. Here's a routine to get a specific color from a palette referenced by a HPALETTE handle. (I know BMPs and PNGs use this method. Not sure what other graphics formats do.)



function getPaletteColor(const image:TPngObject; const color: byte): TColor;
var
theColor: array[0..0] of TPaletteEntry;
begin
GetPaletteEntries(image.Palette, 0, 1, theColor);
result := theColor[0].peRed;
inc(result, (theColor[0].peGreen * $100));
inc(result, (theColor[0].peBlue * $10000));
end;


I wrote this routine for a PNG object, but it will work for BMPs or any other graphics file type with a property named "palette" of type HPALETTE, simply by modifying the data type in the function header.

Feel free to use this in your projects.

Mason