From: Jay Dee on
I have an application that stores colour text to file currently as 4
bytes for each character (Char, Red, Green, Blue). As you can imagine,
this creates rather large files and is not ideal.

The required selection of colours is minimal and the base 16 colours
available as standard in something like MS Paint would be sufficient.

Is there a simple way to store a colour as 1 byte rather than 3?

I understand that a rounding factor would have to be applied so the
colour out may not be exactly the same as the colour in and would end
up the nearest match and don’t see this as a problem.

Finally I then intend to store a number of consecutive matching bytes,
e.g. if all text is black for the first 100 characters store the
colour black once along with the number 100 as integer or something.
I don’t se this being a problem eather.

What I am asking is for some help with the rounding formula required
for storing a colour as a single byte if possible.

public byte ColorToByte(Color color)
{
// TODO
// alpha value douse not need to be considered
}

public Color ByteToColor (byte byteIn)
{
// TODO
// alpha value douse not need to be considered
}

Many thanks to anyone with some input.

Jay Dee

From: Jeff Gaines on
On 07/12/2009 in message
<04ebc10f-1f3d-4678-8b90-ba6e6a3cd8be(a)v19g2000vbk.googlegroups.com> Jay
Dee wrote:

>I have an application that stores colour text to file currently as 4
>bytes for each character (Char, Red, Green, Blue). As you can imagine,
>this creates rather large files and is not ideal.
>
>The required selection of colours is minimal and the base 16 colours
>available as standard in something like MS Paint would be sufficient.
>
>Is there a simple way to store a colour as 1 byte rather than 3?
>
>I understand that a rounding factor would have to be applied so the
>colour out may not be exactly the same as the colour in and would end
>up the nearest match and don�t see this as a problem.
>
>Finally I then intend to store a number of consecutive matching bytes,
>e.g. if all text is black for the first 100 characters store the
>colour black once along with the number 100 as integer or something.
>I don�t se this being a problem eather.
>
>What I am asking is for some help with the rounding formula required
>for storing a colour as a single byte if possible.
>
>public byte ColorToByte(Color color)
>{
> // TODO
> // alpha value douse not need to be considered
>}
>
>public Color ByteToColor (byte byteIn)
>{
> // TODO
> // alpha value douse not need to be considered
>}
>
>Many thanks to anyone with some input.
>
>Jay Dee

That sounds like fun :-)

A single byte would give a value from 0 to 255. Could you use a lookup
table then use the lower 4 bits (0 to 15) as a colour code?

--
Jeff Gaines Dorset UK
Indecision is the key to flexibility
From: Alberto Poblacion on
"Jay Dee" <first180(a)gmail.com> wrote in message
news:04ebc10f-1f3d-4678-8b90-ba6e6a3cd8be(a)v19g2000vbk.googlegroups.com...
> Is there a simple way to store a colour as 1 byte rather than 3?

Normally this is done by means of a color table. A small table is stored
in the file that contains the 3 bytes for the few colors that the file
needs. Then, every time that a color is needed, you just store a single byte
which is the offset into the color table where the actual color is stored.
GIF files use this method.

From: Uwe Hercksen on


Jay Dee schrieb:

> What I am asking is for some help with the rounding formula required
> for storing a colour as a single byte if possible.
>
> public byte ColorToByte(Color color)
> {
> // TODO
> // alpha value douse not need to be considered
> }
>
> public Color ByteToColor (byte byteIn)
> {
> // TODO
> // alpha value douse not need to be considered
> }
>
Hello,

the RGB color uses 3 bytes to store the color, 24 bits alltogether.
You may take the 2 most significant bits from the red, green and blue
value and pack those 6 bits into one byte for 64 different colors.

Are you familiar with bit masking and shifting using C#?
The operators >> for right shift, << for left shift, & for bitwise and,
| for bitwise or.

color.R gives you the value of the red component, color.G the green and
color.B the blue. With color.FromArgb(int r, int g, int b) you get the
color from the values for red, green and blue.

Hope that helps.

Bye

From: Uwe Hercksen on


Uwe Hercksen schrieb:

> You may take the 2 most significant bits from the red, green and blue
> value and pack those 6 bits into one byte for 64 different colors.

Hello again,

for 256 different colors, you may take the most significant 3 bits from
the red and green value and only 2 bits for blue, that are 8 bits
altogether. The resolution for green and red is better than for blue.

Bye