Skip to main content
gamedev.net
🔒 Locked

de/encoding 15bit color

Started by ehmdjii Aug 21, 2006 at 4:16 AM 6 replies 1.1k views
Original Post
ehmdjii
ehmdjii
hello, i have an integer id running from 0 to 32768 (short). now i want to de/encode a 5 Bit RGB color into that range. since 2^5 is 32 and 32*32*32 is 32768 this should be possible. :) i was thinking about using the first 5 bits for red, the next 5 bits for green and the last 5 bits for blue. unfortunately i am extremly bad with bitshifts and bitmasks so i have no idead how to code that and would be very thankful if anyone could provide me with these function: color getColorFromId(short id) { ... } short getIdFromColor(byte red, byte green, byte blue) { ... }
ToohrVyk
ToohrVyk
My guess would be along these lines:

#define R(i) = ((i >> 10) & ((1 << 6) - 1))#define G(i) = ((i >> 5) & ((1 << 6) - 1))#define B(i) = (i & ((1 << 6) - 1))#define RGB(r,g,b) = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3)


Alternatively, you could look for some A1R5G5B5 or X1R5G5B5 conversion methods online, since they're somewhat common formats.
ehmdjii
ehmdjii
thanks a lot, but something doesn't work.

if i input the number 10000, after conversion to RGB and back it is 1250.

edit: it is probably the >> 3, so i guess it should be

#define RGB(r,g,b) = (r << 10) | (g << 5) | b

thanks.

[Edited by - ehmdjii on August 21, 2006 5:07:34 AM]
Necator
Necator
It depends on how you store each color component. It's quite convenient to use the highest 5 bits (in the 15 bit case) since you can easily convert between formats.

If you later on decides that you want to use 8 bits instead, you can just alter the code to.

#define RGB(r,g,b) = ((r >> 5) << 5) | ((g >> 5) << 2) | (b >> 6)

which will still work without any need to change the allowed ranges in the code. The same for using 24 bits instead, just alter the RGB macro and it works without any other code changes.
ToohrVyk
ToohrVyk
Quote:
Original post by ehmdjii
thanks a lot, but something doesn't work.


Sorry, wasn't paying attention. There's an additional <<3 after each of the R(), G() and B() definitions, which is necessary to move the colors back into the 0-256 range (as opposed to 0-32 range).
AbandonedAccount
AbandonedAccount
Quote:
Original post by ToohrVyk
My guess would be along these lines:

#define R(i) = ((i >> 10) & ((1 << 6) - 1))#define G(i) = ((i >> 5) & ((1 << 6) - 1))#define B(i) = (i & ((1 << 6) - 1))#define RGB(r,g,b) = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3)


It is standard to use hex constants (0x1F) rather than shift and decrement. Shift and decrement needs an extra bit, which causes it to be prone to bugs. Also hex constants are much more readable to those who deal with binary manipulations.

Quote:
Alternatively, you could look for some A1R5G5B5 or X1R5G5B5 conversion methods online, since they're somewhat common formats.


This is extremely good advice if you do not have a reason to ignore it. You are trying to reinvent the wheel that Nintendo DS currently uses. If you do not have support for these formats in your environment, perhaps you should check your current graphics libraries are the appropriate choice for 16-bit code.
ToohrVyk
ToohrVyk
Quote:
Original post by richardurich
It is standard to use hex constants (0x1F) rather than shift and decrement. Shift and decrement needs an extra bit, which causes it to be prone to bugs. Also hex constants are much more readable to those who deal with binary manipulations.


Agreed.
CloudNine
CloudNine
Bitmasking does look like it's quite hard to work out, but I get a pencil and piece of paper, and do something like this:

0|111 11|11 111|1 1111


and work out the hexademical values for each section, if you will. So for the furtherest on the right - you'll have a bitmask of 0x1F. (Hint: shift, then mask).

In this case, it's easy though, so just shift 0, 5 and 10, since the size is constant, and mask 0x1F after each.

Are you sure you'd like to store colour like this though? What exactly do you need to store it for?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.