Recent

Author Topic: [Solved] FpPDF SetColor and TColor  (Read 1170 times)

Nono Reading Activity

  • Jr. Member
  • **
  • Posts: 56
[Solved] FpPDF SetColor and TColor
« on: March 14, 2024, 04:17:40 pm »
Hello
I want to add some pictures in the PDF and fill the space around them with the same color as the top left pixel.
Code: Pascal  [Select][+][-]
  1. procedure ChargeImage(Niveau: Integer);
  2. begin
  3.     Picture  := TPicture.Create;
  4.     Bitmap   := TBitmap.Create;
  5.     try
  6.       Path   :=GetCurrentDir+ConstCheminImageNiveau+InttoStr(Niveau)+'.PNG';
  7.       Picture.LoadFromFile(Path);
  8.       Bitmap.Assign(Picture.graphic);
  9.       ListImage.Add(Bitmap, nil);
  10.       ColorLoc := Bitmap.Canvas.Pixels[1, 1];
  11.       ColorList[Niveau] := ColorLoc;
  12.     finally
  13.       Picture.Free;
  14.       Bitmap.Free;
  15.     end;
  16. end;  
  17.  
it work for the Form, but with FpPDF, i have to initialize SetColor before using DrawRect
i can't find how to convert a TColor to the TARGBColor (Cardinal ?) SetColor want.
i tried some of the TColor conversion, but i can't find the one i need to use (or if i need to use something else)
thanks in advance
« Last Edit: March 15, 2024, 06:19:10 am by Nono Reading Activity »

wp

  • Hero Member
  • *****
  • Posts: 13565
Re: FpPDF SetColor and TColor
« Reply #1 on: March 14, 2024, 05:14:07 pm »
Try this (untested):
Code: Pascal  [Select][+][-]
  1. function ColorToARGB(AColor: TColor): TARGBColor;
  2. type
  3.   TColorRec = record r,g,b,a: byte; end;
  4.   TARGBColorRec = record a,r,g,b: byte; end;
  5. begin
  6.   with TARGBColorRec(Result) do
  7.   begin
  8.     a := 0;
  9.     r := TColorRec(AColor).r;
  10.     g := TColorRec(AColor).g;
  11.     b := TColorRec(AColor).b;
  12.   end;
  13. end;

Unit fppdf defines some standard colors:
Code: Pascal  [Select][+][-]
  1. Const
  2.   { Some popular predefined colors. Channel format is: RRGGBB }
  3.   clBlack   = $000000;
  4.   clWhite   = $FFFFFF;
  5.   clBlue    = $0000FF;
  6.   clGreen   = $008000;
  7.   clRed     = $FF0000;
  8.   clAqua    = $00FFFF;
  9.   clMagenta = $FF00FF;
  10.   clYellow  = $FFFF00;
  11.   clLtGray  = $C0C0C0;
  12.   clMaroon  = $800000;
  13.   clOlive   = $808000;
  14.   clDkGray  = $808080;
  15.   clTeal    = $008080;
  16.   clNavy    = $000080;
  17.   clPurple  = $800080;
  18.   clLime    = $00FF00;
  19.   clWaterMark = $F0F0F0;  
As you can see, these constants have the same names as those in the Graphics unit. So, be careful, when using clXXXX constants and you are having both fppdf and graphics in the same unit. Ideally, fully-qualify these identifiers by prepending them with the unit name, e.g. Graphics.clRed vs. fppdf.clRed.

I am not sure about the "a" in the TARGBColor values. If it is the "alpha channel" (opacity) then a=0 would mean: fully transparent, and the clXXXX colors in the fppdf unit would not be visible. Maybe some special handling is needed to mask out and replace the "a" byte when transparency is needed. (I did not use fppdf so far, so, just guessing...)

« Last Edit: March 14, 2024, 05:20:03 pm by wp »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12901
  • FPC developer.
Re: FpPDF SetColor and TColor
« Reply #2 on: March 14, 2024, 05:58:15 pm »
(IIRC GDI and other systems like opengl have different opinions on where the color channels should be )

Nono Reading Activity

  • Jr. Member
  • **
  • Posts: 56
Re: FpPDF SetColor and TColor
« Reply #3 on: March 14, 2024, 06:00:24 pm »
thanks it helped me a lot.
i modified the order to make it work (from a,r,g,b to b,g,r,a)
i'm wondering why it's called ARGB when it BGRA  %)

Code: Pascal  [Select][+][-]
  1. function ColorToARGB(AColor: TColor): TARGBColor;
  2. type
  3.   TColorRec = record r,g,b,a: byte; end;
  4.   TARGBColorRec = record b,g,r,a: byte; end;
  5. begin
  6.   with TARGBColorRec(Result) do
  7.   begin
  8.     b := TColorRec(AColor).b;
  9.     g := TColorRec(AColor).g;
  10.     r := TColorRec(AColor).r;
  11.     a := 0;
  12.   end;
  13. end;
  14.  

wp

  • Hero Member
  • *****
  • Posts: 13565
Re: FpPDF SetColor and TColor
« Reply #4 on: March 14, 2024, 06:57:29 pm »
i'm wondering why it's called ARGB when it BGRA  %)
Because it refers to the order of bytes written in code: clRed = $FF0000 = $00FF0000. The first 00 is A, the FF is R, next 00 is G, and the last 00 is B = A-R-G-B. In memory, however, in little-endian machines, the bytes are stored in reverse order: B is the byte at the smaller address and the A is byte at the higher address = B-G-R-A.

 

TinyPortal © 2005-2018