#### Archived

This topic is now archived and is closed to further replies.

# RGB=====>256 colors

This topic is 6108 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

how to convert rgb values to a 256-color value and vice versa ?? and what is the range of these rgb values? ...Xeee

##### Share on other sites
You mean indexed colours with 256 colour palette?
I remember I did once in DOS time.

First make palette. There are some algorithms to do this.
Then for each colour in the image select palette-index that matches best with the original color.

struct color {     unsigned char r,g,b; };color palette[256]; // rgb-palette// match value, the smaller the better.int match(color a,color b){    return (a.r-b.r)^2 + (a.g-b.g)^2 + (a.b-b.b)^2;}color rgb_image[width*height];unsigned char indexed_image[width*height];for(each pixel in rgb_image){   then just find best match from palette for current rgb_image pixel and put it to indexed_image}

This works for any random palette.
If palette is for example general RGB = 323.
Then it''s much faster, just shifting.

##### Share on other sites
i think that''s not what i want ,
i need a function which takes RGB values and return the corresponding color in 256 color mode
something like mkcolor(float r,float g,float b)
and then i can use this function with graphics.h setcolor() function for example
setcolor(mkcolor(0.1,0.8,0));

...Xeee

##### Share on other sites
with floats its more complicated! rgb are 3 bytes! 256 = 3 bit r / 2 bit g / 3 bit b! you just need to divide each 8 bit rgb by 8/3or2(?) so that they fit into the 3 or 2 bytes! is this right?

##### Share on other sites
hey Zeusel , would you please explain more
the function recieves 3 bytes , r , g and b , the first thing i wanna ask about is what is the range of these values 0-??? .
i have written the function prototype with floats just because i''ve seen that it is so in openGL , so how to do it with three integers??
did u mean to take the left 3 bits in the r and the middle 2 bits in the g and the right 3 bits in the b ?? it can b done using shifting not division (if you mean so!!).

...Xeee

##### Share on other sites
It all depends on how you have setup your 8-bit palette

-
"I do not approve of anything that tampers with natural ignorance. Ignorance is like a delicate exotic fruit. Touch it and the bloom is gone."
Lady Bracknell in "The Importance of being Earnest" by Oscar Wilde

##### Share on other sites

Something like this, I wrote it from scratch

#define R8G8B8_TO_R3G2B3(r,g,b) \
(((unsigned char)(r>>5))<<5) | \
(((unsigned char)(g>>6))<<3) | \
((unsigned char)(b>>6))

##### Share on other sites
Or:

#define R8G8B8_TO_R3G2B3(r,g,b) ((unsigned char)(r)&0xE0) | ((unsigned char)(g)&0x18) | ((unsigned char)(b)&0x07)

Edited by - stefu on December 29, 2001 3:28:54 PM

##### Share on other sites

BTW, to address one of the issues:

Just multiply each argument by a scaling factor... If you want to convert from a number ranging from 0.0 to 1.0 to a number ranging from 0 to 255, the answer is obvious. Your scaling factor is 255, so you multiply each of your components by 255 and cast each to an 8-bit integer. That gives you real RGB values in the correct 0...255 range to convert to 256 color mode. Then just pack them together into a Uint32 using shifting and &-masking as shown in other responses. Voila! You have a 32-bit pixel created from floating data.

Take it easy!

##### Share on other sites
function unRGB(Color : TColor) : RGBColor;
begin
unRGB.r := Color and 255;
unRGB.g := Color shr 8 and 255;
unRGB.b := Color shr 16 and 255;
end;

delphi already has the RGB(r,g,b) function

or c++

int32 pack(int r,g,b)
{
return r >> 16 | g >> 8 | b
}

1. 1
2. 2
3. 3
Rutin
18
4. 4
JoeJ
14
5. 5

• 14
• 10
• 23
• 9
• 47
• ### Forum Statistics

• Total Topics
632636
• Total Posts
3007573
• ### Who's Online (See full list)

There are no registered users currently online

×