Board index » delphi » Render a monochrome bitmap into 2 colors with the palette

Render a monochrome bitmap into 2 colors with the palette

OK, I have a monochrome bitmap that needs displaying in two colours (not
black and white!).

One of the two colours is most likely one of the 20 system colours but the
other is most likely not.
How can I do this?

Do I mess about in the on-paint?
Do I mess about with the palette change?

How do I change the palette values for a bitmap?

I'm tired of messing about with brushcopy on a 24 bit bitmap to translate
colours, and even that doesn't always work!

--
Sam Liddicott                   |   Nothing I say is to be attributed as |
Campbell Scientific Ltd.        | a company statement or representation. |
Campbell Park, 80 Hathern Road, *----------------------------------------+
Shepshed, Leic. United Kingdom. LE12 9AL        Phone: +44 (0) 1509 601141
Email: s...@campbellsci.co.uk                    Fax:   +44 (0) 1509 601091

 

Re:Render a monochrome bitmap into 2 colors with the palette


OK, I have got a lot to work since then, but not all.

I'll explain the problem.  I want to have a nice background to Frabrice
Deville's TExplorer button.

MS makes theirs have the main color as cl3DObjects or clBtnFace, and the
other colour is calculated according to some fould formula I have managed
to work out (shown below)

Now in 24bit mode, I get exactly the same colours as explorer does, but in
256 colour mode I do not always.
I am sure it is because I don't know how to change the palette, or to add
values to a palette, or even which palette to add the values too...

In 256 colour mode sometimes my color is nicer than the one explorer ends
up with but sometimes not.

On the whole I am satisfied, but I feel the customers may have something to
say in 256 color mode if the colour is wrong.  At least if I match explorer
exectly they can't complain.

Oh - I'm using Delphi 1.0.

{PaintBackDrop is a PaintBox}
procedure TPC10Main.PaintBackDropPaint(Sender: TObject);
var
       FillColor : TColor;
  BackColor : Longint;
  Red, Blue, Green : Byte;

  Procedure Fix(var Color : Byte);
  begin
    Color:=Translate[Color];
  end;

begin
  BackColor:=ColorToRGB(clbtnFace);
  Red:=GetRValue(BackColor);
  Blue:=GetBValue(BackColor);
  Green:=GetGValue(BackColor);
  Fix(Red);
  Fix(Blue);
  Fix(Green);
  FillColor:=RGB(Red,Green,Blue) or $2000000;

  if (FCacheBitmap=nil) or (FillColor<>OldFillColor) or (BackColor<>OldBackC
olor) then begin
    If FCacheBitmap=nil then FCachebitmap:=TBitmap.Create;
    FCacheBitmap.Assign(BackDrop.Picture.Bitmap); { this is a black and
white 24 bit bitmap with the pattern on}
    FCacheBitMap.Canvas.Brush.Style:=bsSolid;
    FCacheBitmap.Canvas.Brush.Color:=BackColor;
    FCacheBitmap.Canvas.FillRect(ClientRect);
    DrawTransparent(FCacheBitmap,0,0,BackDrop.Picture.Bitmap,clBlack);
  end;
  With PaintBackDrop do begin
    Canvas.Brush.Style:=bsSolid;
    Canvas.Brush.Color:=FillColor;
    Canvas.FillRect(ClientRect);

    Canvas.BrushCopy(ClientRect,FCacheBitmap,ClientRect,clWhite);
  end;
end;

And Translate has these values, they were monitored by trail and error and
all match what eplorer currently does.

  const
    Translate : Array[0..255] of byte=(
      0,1,2,3,4,5,6,7,8,10,11,12,13,14,15,16,17,19,20,21,22,23,24,25,26,27,2
9,30,31,32,33,
      34,35,36,38,39,40,41,42,43,44,45,47,48,49,50,51,52,53,54,55,57,58,59,6
0,61,62,63,64,66,67,68,69,
      70,71,72,73,75,76,77,78,79,80,81,82,83,85,86,87,88,89,90,91,92,94,95,9
6,97,98,99,100,101,103,104,105,
      106,107,108,109,110,111,113,114,115,116,117,118,119,120,122,123,124,12
5,126,127,128,129,131,132,133,134,135,136,137,138,139,141,
      142,143,144,145,147,147,148,160,151,152,153,154,155,156,157,159,160,16
1,162,163,164,165,166,167,169,170,171,172,173,174,175,176,
      178,179,180,181,182,183,184,185,187,188,189,190,191,192,193,194,195,19
7,198,199,200,201,202,203,204,206,207,208,209,210,211,212,
      213,215,216,217,218,219,220,221,222,223,225,226,227,228,229,230,231,23
2,234,235,236,237,238,239,240,241,242,244,245,246,247,248,
      249,250,251,253,254,255,0,1,2,3,4,6,7,8,9,10,11,12,13,14,16,18,22,19,2
0,21,22,23,25,26,27,28,29);

Sam Liddicott                   |   Nothing I say is to be attributed as |
Campbell Scientific Ltd.        | a company statement or representation. |
Campbell Park, 80 Hathern Road, *----------------------------------------+
Shepshed, Leic. United Kingdom. LE12 9AL        Phone: +44 (0) 1509 601141
Email: s...@campbellsci.co.uk                    Fax:   +44 (0) 1509 601091

Other Threads