• C

How do colours work with respect to bits per pixel

Hi guys,

I'm a complete newbie on this topic.  How are various colour depths ecoded exactly?  For example, I believe 8 bit/pixel is 3 bits for red and green and 2 for blue?  What about 16bit, and 32bit?  Are they just scaled up versions of 8 bit (ie with less bits allocated for the blue part)?   What about the RGB value, is that 24 bit?  Is there a way to convert between the various colour depths?  

I know this isn't exactly a C question, but I need to know it for a C project and I strongly suspect 99% of C programmers reading this will know the answer ;)

Thanks for any help,

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Generally 8 bit color uses a pallete, so the number is simply a index into a color table, which is an array of 256 24 bit values.

16 bit color is usually stored in a 5-6-5 format, giving one more bit to the green component. (64 shades vs. 32 for red and blue)

32 bit usually uses the "extra" byte for either an alpha channel (transparency) or sometimes represents color as CMYK for printing.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
There are generally 2 forms of pixel data.
Chunky pixel and Planar graphics.

Chunky pixel (as used on Windows Operating systems) stores each pixel in either 8, 16 or 32bits.  In 8 bit mode, the number stored in those 8 bits refers to a pallete entery.  If palette entry 1 is blue, then 000000001 will display blue when drawn.  Changing the pallete color forces all colors of that pixel value to change as well.  Maximum of 2^8 colors (256 colors)
16bit works as described by catbutt, the 16bits are divided up giving a slight advantage to the green range of colors.
32bit is usually represented in ARGB format (depending on the format you choose, theres many others)
A = Alpha
R = Red
G = Green
B = Blue

The alpha "channel" is rarely used, but modern graphics cards will use this as a sort of blend factor, allowing transparencies etc. and overlayed images.

You'll rarely come across planar format, but it works a bit different.  Each "plane" of pixels are stacked on top of each other.  To visualize an 8bit display, take 8 sheets of paper and write 1s and 0s all across them, then stack them on top of each other.  Reading each corrosponding bit on each sheet with the same position on the next sheet gives you your 8bit number. (top sheet to bottom sheet)

Planar graphics have the advantage of taking up less memory as opposed to chunky graphics.  If you have a 24bit display, you only need 24 planes of data.  The smallest data that will accomodate a 24bit display in chunky format is a 32bit number.  So you don't save any memory between the them when using chunky pixel graphics.

Hope that helps   I'm not big on graphics manipulation so if I'm talking out of my butt here, feel free to correct me :)
DawkinsAuthor Commented:
Thanks a lot guys.  I guess I should accept catbutts comment as an answer since he was first, but thanks a lot for expanding a bit more cybeonix  :)
DawkinsAuthor Commented:
Thanks a lot guys.  I guess I should accept catbutts comment as an answer since he was first, but thanks a lot for expanding a bit more cybeonix  :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.