blue tint in 16-bit?

Started by
12 comments, last by xtrmntr 23 years, 8 months ago
Anonymous: i tried both of your macros and the first one makes my colors look wacked while the second one makes all the colors alot darker then the way i drew them. can you explain a little more about why i would choose yours over mine?

Once I thought i was wrong but i was mistaken...
Advertisement
What colour values are you supplying? The macro that the above Anonymous Poster (not me gave you expects a value of 0-255, with 0 being dark and 255 being maximum brightness. The one you have is just plain weird, it will have maximum brightness at 31,63,95,127,.... etc. or 63,127... depending on whether it is green or not. If you supply values of 127 for example, his macro will be medium brightness (correct) yours will be full brightness (wrong)
Something bugs me about those macros. Aren't they grabbing the lowest bits? I'd think you'd want the highest bits to make the closest match. For example:
DWORD red = green = blue = 0x7F;  // In 24-bit, this is 0x7F7F7F = medium grey  // (Used DWORD to avoid possible type-casting problems)    


#define RGB16_555(r,g,b)((b & 31) + ((g & 31) << 5) + ((r & 31) << 10))    


Now, let's take a look at this:

When the following is declared:

DWORD rgb555 = RGB16_555(red,green,blue);    


This makes:

rgb555 = ((blue & 31) + ((green & 31) << 5) + ((red & 31) << 10))rgb555 = ((0x7F & 0x1F) + ((0x7F & 0x1F) << 5) + ((0x7F & 31) << 10))rgb555 = ((0x1F) + ((0x1F) << 5) + (0x1F) << 10))    


Note that each component is 0x1F, the maximum intensity for 5-bit values! This creates white, not medium gray.

Now, if you take the highest five bits instead:

#define RGB16_555(r,g,b) = (((b & 248) >> 3) + ((g & 248) << 2) + ((r & 248) << 7))    


This makes:

rgb555 = (((0x7F & 0xF8) >> 3) + ((0x7F & 0xF8) >> 2) + ((0x7F & 0xF8) << 7))rgb555 = ((0x78 >> 3) + (0x78 << 2) + (0x78 << 7))    


Since (0x78 >> 3) = 0x0F, let's rewrite the above statement so that the components are obvious:

rgb555 = ((0x0F) + (0x0F << 5) + (0x0F << 10));    


Each component is 0x0F, which is medium intensity. This creates medium gray.
(Of course, the bottom 3 bits of the original values were lost, so this is
equivalent to 24-bit 0x7C7C7C, but its as close as it gets without
rounding.)

I typed this up just now so I haven't debugged it, but my current library runs
on the same algorithm--take the highest five bits of the value, not the lowest.

Does this make any sense, or am I just spewing math out of my ass?
I hope not--my wrists hurt.

Edited by - SkyDruid on September 4, 2000 4:32:25 PM
I just realized that someone else said the same thing in fewer words.

No offense intended to Mr. LaMothe, but this wasn''t the only code I had to
rewrite from Tricks of the Windows Game Programming Gurus.

This topic is closed to new replies.

Advertisement