Board index » delphi » 16 million to 16 colors

16 million to 16 colors

Hello,

I have a TColor object - basically a 24 bit color value.
I need to translate that to an old DOS color (4 bit 16 color type).
Are there already routines to do such a thing?

Thanks for any info on this one ..................

 

Re:16 million to 16 colors


Quote
mattk wrote:

> Hello,

> I have a TColor object - basically a 24 bit color value.
> I need to translate that to an old DOS color (4 bit 16 color type).
> Are there already routines to do such a thing?

Take a look at the color reduation examples from efg.
i.e.
http://www.efg2.com/Lab/Graphics/Colors/ShowDemoOne.htm
http://www.efg2.com/Lab/Graphics/Colors/ShowDemoMany.htm
http://www.efg2.com/Lab/Graphics/Colors/CombinePf4bit.htm

--
Charles Hacker
Lecturer in Electronics and Computing
School of Engineering
Griffith University - Gold Coast
Australia

Re:16 million to 16 colors


How would I take a single pixel and see what color 'value' it is?
- as I am not really interested in manipulating bitmaps but I am interested
in what the equivalent 4 bit color is.

Quote
"Charles Hacker" <reply...@borland.newsgroups> wrote in message

news:3EF2319A.17334E22@borland.newsgroups...
Quote
> mattk wrote:

> > Hello,

> > I have a TColor object - basically a 24 bit color value.
> > I need to translate that to an old DOS color (4 bit 16 color type).
> > Are there already routines to do such a thing?

> Take a look at the color reduation examples from efg.
> i.e.
> http://www.efg2.com/Lab/Graphics/Colors/ShowDemoOne.htm
> http://www.efg2.com/Lab/Graphics/Colors/ShowDemoMany.htm
> http://www.efg2.com/Lab/Graphics/Colors/CombinePf4bit.htm

> --
> Charles Hacker
> Lecturer in Electronics and Computing
> School of Engineering
> Griffith University - Gold Coast
> Australia

Re:16 million to 16 colors


Quote
"mattk" <mattkreuter@kmc-controls> wrote in message

news:3ef302c4$1@newsgroups.borland.com...

Quote
> How would I take a single pixel and see what color 'value' it is?
> - as I am not really interested in manipulating bitmaps but I am
interested
> in what the equivalent 4 bit color is.

The term "equivalent" can be deceptive, because there is no way for a
16-color palette to support the full range of 24-bit colors. But if you want
to know the closest, then you can determine how far it is from each of the
16 colors and use the one with the least difference. You can do this by
decomposing the image colors and 16 basic colors into the three color bytes
and subtracting.

The 16 basic colors in Delphi are clBlack, clMaroon, clGreen, clOlive,
clNavy, clPurple, clTeal, clGray, clSilver, clRed, clLime, clYellow, clBlue,
clFuchsia, clAqua, and clWhite.

Cheers,
  Ignacio

Re:16 million to 16 colors


The Win32 API GetNearestColor will perform color reduction for you.  Create
a device context with a 16 color palette and then call GetNearestColor using
that DC and the 24 bit color you want reduced.

-Danny

Quote
"mattk" <mattkreuter@kmc-controls> wrote in message

news:3ef22725$1@newsgroups.borland.com...
Quote
> Hello,

> I have a TColor object - basically a 24 bit color value.
> I need to translate that to an old DOS color (4 bit 16 color type).
> Are there already routines to do such a thing?

> Thanks for any info on this one ..................

Re:16 million to 16 colors


Quote
> The Win32 API GetNearestColor will perform color reduction for you.
>  Create a device context with a 16 color palette and then call
> GetNearestColor using that DC and the 24 bit color you want reduced.

Could you give me a small code example how to do this? I am looking for a
similar solution, mapping 24bit colors to a fixed 16 color palette. I am not
familiar with the notion of device context though.

Thanks a lot
Matthias

Re:16 million to 16 colors


Well, if you're going to be doing Windows color manipulation, you need to
read up a bit on color palettes and device contexts.

Untested code off the top of my head:

procedure TForm1.Button1Click(Sender: TObject);
var
  LP: TLogPalette;
  HP: HPalette;
  BM: TBitmap;
begin
  BM := TBitmap.Create;
  <initialize LP fields with color info>
  BM.Palette := CreatePalette(LP);

  AColorFromThePalette := GetNearestColor(BM.Canvas.Handle, A24bitColor),
8);
end;

You'll have to initialize the fields of LP appropriately.  See
Graphics.pas:CreateSystemPalette for hints.

The resulting AColorFromThePalette will still be a 24 bit RGB value, but it
will be one of the values you placed into the palette.  If you limit the
palette to 16 or fewer entries, then you have a 24 bit to 4 bit color
reduction.

If you're wanting to reduce an entire bitmap from 24 bits to 4 bits, don't
do it one pixel at a time.  Load the 24 bit image into one bitmap object,
construct your 16 color (4 bit) palette, and assign it to the bitmap's
Palette property.  In Delphi 5 and later, the bitmap will re-render its
pixels to conform to the new palette.

-Danny

Quote
"Matthias Matting" <matt...@matting.de> wrote in message

news:bd6one$7qj$00$1@news.t-online.com...
Quote
> > The Win32 API GetNearestColor will perform color reduction for you.
> >  Create a device context with a 16 color palette and then call
> > GetNearestColor using that DC and the 24 bit color you want reduced.

> Could you give me a small code example how to do this? I am looking for a
> similar solution, mapping 24bit colors to a fixed 16 color palette. I am
not
> familiar with the notion of device context though.

> Thanks a lot
> Matthias

Re:16 million to 16 colors


Quote
> Untested code off the top of my head:

Thanks :)

Matthias

Other Threads