Reducing the saturation of a bitmap lazarus / 31-LCL ---------------------------------------------------------------------- MarkMLl · 2025-04-24 18:22 I'm transferring a glyph (drawn with a saturated primary colour, or dark grey) from a TImageList onto a TBitmap, then placing a small amount of black text to its right. I repeat this several times, resulting in different-coloured glyphs and accompanying text. I'm then transferring the TBitmap to the canvas of a TPaintbox where it's visible. The TBitmap remains buffered so that other redraws don't cause flickering. Is there an easy way to reduce the saturation of both the glyph and the text before buffering it? This is largely cosmetic, and I definitely don't want to get into anything like putting additional non-saturated colours into the TImageList. MarkMLl ---------------------------------------------------------------------- MarkMLl · 2025-04-25 10:13 Does anybody have any thoughts on this please? I anticipate that I'm going to have to work via an intermediate bitmap but the detail eludes me. MarkMLl ---------------------------------------------------------------------- jamie · 2025-04-25 11:03 R u referring to taking the contrast out of rhe image? ---------------------------------------------------------------------- Handoko · 2025-04-25 11:35 Not exactly what MarkMLI needs, but has the code for doing color shifting: https://forum.lazarus.freepascal.org/index.php/topic,37251.msg249962.h tml#msg249962 Then, we need to change the formula for reducing saturation. These info could be helpful: https://sot.com.sg/hsl/ https://stackoverflow.com/questions/31627303/calculate-new-rgb-by- saturation ---------------------------------------------------------------------- MarkMLl · 2025-04-25 11:51 That's another way of looking at it, but I want to leave the background (assumed to be transparent) unchanged. Alternatively decreasing alpha. I messed up the attached screenshot, but basically I want to de- emphasise a marker in the bottom part of the window if it's substantially to one side of the runway (i.e. in a parking area etc.). MarkMLl ---------------------------------------------------------------------- MarkMLl · 2025-04-25 11:53 Quote from: Handoko on April 25, 2025, 01:35:16 pmNot exactly what MarkMLI needs, but has the code for doing color shifting: https://forum.lazarus.freepascal.org/index.php/topic,37251.msg249962.h tml#msg249962 Then, we need to change the formula for reducing saturation. These info could be helpful: https://sot.com.sg/hsl/ https://stackoverflow.com/questions/31627303/calculate-new-rgb-by- saturation Thanks, looks helpful. Alternatively doing something with the pixels in a RawImage... I'm being cautious because of the warnings regarding poor performance on pixel-by-pixel operations on top of X11. Considering https://wiki.freepascal.org/Developing_with_Graphics#Creat ing_and_drawing_a_transparent_bitmap_for_a_TImage I suspect that I might need to be careful with the drawing mode... or at least check that it's what's needed. MarkMLl ---------------------------------------------------------------------- Ally · 2025-04-25 12:59 Hello MarkMLl, here is a small sample program. The interesting part for you is the unit “rhsBitmapDesaturate.pas”. Best regards Roland ---------------------------------------------------------------------- MarkMLl · 2025-04-25 13:33 Quote from: Ally on April 25, 2025, 02:59:08 pmhere is a small sample program. The interesting part for you is the unit “rhsBitmapDesaturate.pas”. Now that looks like just about the job: thanks very much indeed :-) MarkMLl ---------------------------------------------------------------------- Handoko · 2025-04-25 15:05 I'm late. But here is my code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Graphics, Dialogs, StdCtrls, ExtCtrls, LCLType,  ComCtrls, Controls; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    Button2: TButton;    Image1: TImage;    Image2: TImage;  OpenDialog1: TOpenDialog;    TrackBar1: TTrackBar;    procedure Button1Click(Sender: TObject);    procedure Button2Click(Sender: TObject);    procedure FormCreate(Sender: TObject);    procedure FormResize(Sender: TObject);  end; var  Form1: TForm1; implementation const  RedShift = 40;  GreenShift = -40;  BlueShift = -40; {$R *.lfm} { TForm1 } procedure TForm1.Button1Click(Sender: TObject);var  AJpg:  TJPEGImage;begin  if not(OpenDialog1.Execute) then Exit;  AJpg := TJpegImage.Create;  AJpg.LoadFromFile(OpenDialog1.FileName);  Image1.Picture.Bitmap.Assign(AJpg);  AJpg.Free;  Button2.Enabled := True;end; function inRange(S: Single): Byte;begin  if S <= 0 then  begin    Result := 0;    Exit;  end;  if S >= 255 then  begin    Result := 255;    Exit;  end;  Result := Round(S);end; procedure ChangeSaturation(Data: PRGBQUAD; Shift: Byte);var  Luminance:  Single;  ValueRed:   Byte;  ValueGreen: Byte;  ValueBlue:  Byte;begin  ValueRed       := Data^.rgbRed;  ValueGreen     := Data^.rgbGreen;  ValueBlue      := Data^.rgbBlue;  Luminance      := (ValueRed+ValueGreen+ValueBlue) / 3;  Data^.rgbRed   := inRange((Shift/127*(ValueRed-Luminance)/Luminance + 1) *Luminance);  Data^.rgbGreen := inRange((Shift/127*(ValueGreen- Luminance)/Luminance+1) *Luminance);  Data^.rgbBlue  := inRange((Shift/127*(ValueBlue-Luminance)/Luminance+1)  *Luminance);end;  procedure TForm1.Button2Click(Sender: TObject);var  ScanData: PRGBQuad;  X, Y: Integer;begin  Image2.Picture.Clear;  Image2.Picture.Assign(Image1.Picture);  Image2.Picture.Bitmap.BeginUpdate;  for Y := 0 to (Image1.Picture.Bitmap.Height-1) do  begin    ScanData := Image2.Picture.Bitmap.ScanLine[Y];    for X:= 0 to (Image1.Picture.Bitmap.Width-1) do    begin      ChangeSaturation(ScanData, TrackBar1.Position);      Inc(ScanData);  end;  end;  Image2.Picture.Bitmap.EndUpdate;end; procedure TForm1.FormCreate(Sender: TObject);begin  Constraints.MinHeight := 200;  Constraints.MinWidth  := 360;  TrackBar1.Anchors := [akTop, akLeft, akRight];  Image1.Anchors    := [akTop, akBottom, akLeft];  Image2.Anchors    := [akTop, akBottom, akLeft];end; procedure TForm1.FormResize(Sender: TObject);var  i: Integer;begin  i := (Width - 60) div 2;  Image1.Width := i;  Image2.Left  := i + 40;  Image2.Width := i;end; end. ---------------------------------------------------------------------- MarkMLl · 2025-04-25 18:37 Quote from: Handoko on April 25, 2025, 05:05:23 pmI'm late. But here is my code: Thanks, tinkering continues. I've backtracked a little and am investigating using the alpha colour component of the text that's being generated on-the-fly, that might possibly be a bit "jumpy" which might depend on the widgetset. /If/ that works then all I'll have to do is fiddle with the glyph which is small (typically 12x12) so "cheap" in computation terms, otherwise I can put both glyph and text into a temporary bitmap (term used loosely) for manipulation. With reference to the image I posted earlier, what I'd like is for a plane doing an overhead crossfield pass to fade into and then out of visibility as it passes above the runway, or for planes parked near but not on the runway to appear as "ghosts". Hopefully I'll get plenty of test data tomorrow since the local pilots will be out in force. I've been working on this on and off since 2023... MarkMLl ---------------------------------------------------------------------- MarkMLl · 2025-04-28 09:11 What needs to be done to use the alpha component of e.g. a colour when drawing into a bitmap or transferring to a canvas, or is this sufficiently implementation-specific as to be best avoided? If I do something like this Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  (* Draw a border on the outermost pixels, the background colour should    approximate that of the map so that the same coloured symbols representing    aircraft show up.  *)  procedure border;   begin//    buffer.BeginUpdate;    with buffer.Canvas do begin      Pen.Color := $00f0f0f0;      Pen.Style := psSolid;      Pen.Width := 1;      Brush.Color := $00f0f0f0;      Rectangle(0, 0, bmW, bmH);      Pen.Color := clBlack;       Line(0, 0, bmW, 0);               (* Draw right to edge                   *)      Line(bmW - 1, 0, bmW - 1, bmH);      Line(bmW - 1, bmH - 1, 0, bmH - 1);      Line(0, bmH - 1, 0, 0);       if hud in [HudT, HudB] then begin        Line(0, 0, bmW, 0);             (* Draw right to edge                   *)        Line(0, bmH - 1, bmW, bmH - 1)      end else begin        Line(0, 0, 0, bmH);             (* Draw right to edge                   *)      Line(bmW - 1, 0, bmW - 1, bmH)      end;Font.Color := TColor(clBlack + qword($00 << 24));TextOut(15, 15, 'X') ;Font.Color := TColor(clBlack + qword($40 << 24));TextOut(30, 30, 'X') ;Font.Color := TColor(clBlack + qword($80 << 24));TextOut(45, 45, 'X') ;Font.Color := TColor(clBlack + qword($c0 << 24));TextOut(60, 60, 'X') ;Font.Color := TColor(clBlack + qword($ff << 24));TextOut(75, 75, 'X')      end;//    buffer.EndUpdate  end { border } ;  where buffer is a TBitmap and bmH etc. is initialised to its container's size then Xs with an alpha component <= $7f appear OK but the others are invisible. If at the point of the bitmap's creation I do Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     hudBmps[hud] := TBitmap.Create;    hudBmps[hud].PixelFormat := pf32bit;  nothing appears at all when the bitmap is transferred to a GUI component's canvas using Draw()... I suspect because the component's pixelformat hasn't been set. So, is the transparency of text etc. easily set, or is this best avoided? MarkMLl