Board index » delphi » Color palettes in 256 color modes

Color palettes in 256 color modes

I didn't think we were asking too much to be able to define a very small
set (~24) of colors for our application which would be 'realize'd when the
active application was activated, and approximated when inactive. The
application is written in Delphi 1.0 and is being developed on machines
running Windows 95 (mentioned in case Windows 95 behaves differently with
respect to color and 16-bit apps).

We have been trying to solve this problem on and off for some time. I have
recently had some success when the application contains no child controls
which need to use the same palette as the form.

The following test code illustrates the essentials of our problem. The
intention is for the code below to draw a rectangle on to the
form's canvas with color 1 from 'MyPalette', and for 'Shape1' to use color
1 from 'MyPalette'. When the form becomes active, this is indeed the case.
Even if other windows are placed over the form and removed, the colors are
fine. However, as soon as the form is taken off screen, the colors are
lost. Interesting point #1: if Shape1.Brush.Color is set to an absolute RGB
e.g. $00f7f7f7, the palette colors hold correctly. Point #2: the colors are
lost again if the line Canvas.Brush.Handle:=0 is removed. Can anyone
explain these  points?

I have tried overriding the dynamic method: function GetPalette:HPalette
with no luck at all.

Whether what we're asking is impossible, or if there is a solution, please
put us out of our misery. Essentially, we would like to know if it is
possible to define a palette in Delphi 1, for a 256 color mode, which is
shared amongst all the forms and their child controls such that the colors
are accurately rendered, necessarily, only for the active form. (Also, how
to go about doing it!).

Thanks in advance,

Kosta.

************ Test Code Begins ***********

unit ColorTest;

interface

uses
  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, StdCtrls, Lblext, ExtCtrls;

type
  TForm1 = class(TForm)
    Shape1: TShape;
    procedure FormPaint(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClick(Sender: TObject);
  private
    { Private declarations }
    MyPalette:HPalette;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation
{$R *.DFM}

procedure TForm1.FormPaint(Sender: TObject);
var loop:integer;
begin
  SelectPalette(Canvas.Handle,MyPalette,False);
  RealizePalette(Canvas.Handle);
  Shape1.Brush.Color:=$01000001;    {Set Shape1 color to palette entry 1}
  Canvas.Brush.Handle:=0;                {Why does this seem to be
necessary?}
  Canvas.Brush.Color:=$01000001;
  Canvas.Rectangle(10,10,100,100);
end;

procedure TForm1.FormCreate(Sender: TObject);
const Colors:Array [0..2] of TPalet{*word*249}try=((peRed:  0; peGreen:  0;
peBlue:  0; peFlags:  0),
                                            (peRed:$35; peGreen:$79;
peBlue:$f7; peFlags:$00),
                                            (peRed:$ff; peGreen:$ff;
peBlue:$ff; peFlags:$00));
var Palette:^TLogPalette;
    loop:integer;
    pe:TPalet{*word*249}try;
    PalSize:word;
begin
  PalSize:=sizeof(TLogPalette)+3*sizeof(TPalet{*word*249}try);
  GetMem(Palette,PalSize);
  Palette^.palVersion:=$300;
  Palette^.palNumEntries:=3;

  for loop:=0 to 2 do
    Palette^.palPalEntry[loop]:=Colors[loop];

  MyPalette:=CreatePalette(Palette^);
  FreeMem(Palette,PalSize)
end;

end.

 

Re:Color palettes in 256 color modes


On 23 Dec 1997 18:29:08 GMT, "Kosta" <ko...@sci-clone.co.uk> wrote:

Quote
>I didn't think we were asking too much to be able to define a very small
>set (~24) of colors for our application which would be 'realize'd when the
>active application was activated, and approximated when inactive. The
>application is written in Delphi 1.0

You need to intercept the Windows palette messages.
Check my home page for a good example:

www-afsc.saia.af.mil/~englere/

I made this example after going crazy myself
over this whole matter!

Eric

Other Threads