Recent

Author Topic: Sahding an Image to a color - [Solved]  (Read 1276 times)

zxandris

  • Full Member
  • ***
  • Posts: 170
Sahding an Image to a color - [Solved]
« on: December 18, 2024, 11:49:40 am »
I have a project where I would like to shade an existing image a certain color.  Not replace colors, but shade if you see what I mean.  I currently don't have a clue on this one, if anyone would be willing to help I would be grateful.  In theory i go through the pixels and add with a certain tolerance, right?  But I am completely unsure how to actually go about this.

They would likely be quite small images, up to about 256 so I would imagine something like the old scanline might help?  Theory would help a great deal, and examples would be awesome :)

CJ
« Last Edit: December 18, 2024, 03:19:53 pm by zxandris »

Dzandaa

  • Hero Member
  • *****
  • Posts: 531
  • From C# to Lazarus
Re: Sahding an Image to a color
« Reply #1 on: December 18, 2024, 02:42:00 pm »
Hi,

Something like this?

B->
« Last Edit: December 18, 2024, 02:54:55 pm by Dzandaa »
Regards,
Dzandaa

zxandris

  • Full Member
  • ***
  • Posts: 170
Re: Sahding an Image to a color
« Reply #2 on: December 18, 2024, 03:19:39 pm »
Yes, that's just the job, mate! I just tweaked it a tiny bit to preserve the alpha channel. For the purposes of my little app, I also made it into a function

Thank you so much!

Code: Pascal  [Select][+][-]
  1. function BgraColorise(var pictOrig : TBgraBitmap; const clrBlend : TColor; const boolBW : Boolean) : TBgraBitmap;
  2. var
  3.         Pxo, Pxd: PBGRAPixel;
  4.         CSelect: TBGRAPixel;
  5.         ColBW: Integer;
  6.         i: integer;
  7.         PictDest : TBgraBitmap;
  8. begin
  9.      CSelect := ColorToBgra(clrBlend);
  10.      if(PictOrig = nil) then exit;
  11.      if(PictDest <> nil) then FreeAndNil(PictDest);
  12.      PictDest := TBGRABitmap.Create(PictOrig.Width, PictOrig.Height, BGRABlack);
  13.      Pxo := PictOrig.Data;
  14.      Pxd := PictDest.Data;
  15.      for i := PictOrig.NbPixels-1 downto 0 do
  16.      begin
  17.                         if(boolBW) then
  18.                         begin
  19.                              ColBW := Round(0.299 * Pxo^.red + 0.587 * Pxo^.green + 0.114 * Pxo^.blue);
  20.                              Pxd^.red := Round((ColBW + CSelect.red) / 2.0);
  21.                              Pxd^.green := Round((ColBW + CSelect.green) / 2.0);
  22.                              Pxd^.blue := Round((ColBW + CSelect.blue) / 2.0);
  23.                              pxd^.alpha := pxo^.alpha;
  24.                         end else
  25.                         begin
  26.                              Pxd^.red := Round((Pxo^.red + CSelect.red) / 2.0);
  27.                              Pxd^.green := Round((Pxo^.green + CSelect.green) / 2.0);
  28.                              Pxd^.blue := Round((Pxo^.blue + CSelect.blue) / 2.0);
  29.                              pxd^.alpha := pxo^.alpha;
  30.                         end;
  31.                         inc(Pxo);
  32.                         inc(Pxd);
  33.      end;
  34.      result := pictDest;
  35. end;
  36.  

Hi,

Something like this?

B->

 

TinyPortal © 2005-2018