ソースを参照

some refactorings

Ugochukwu Mmaduekwe 5 年 前
コミット
cb28ca7fe2

+ 10 - 28
QRCodeGenLib/src/Interfaces/QlpIQrCode.pas

@@ -5,16 +5,6 @@ unit QlpIQrCode;
 interface
 
 uses
-{$IF DEFINED(VCL)}
-  Vcl.Graphics,
-  Vcl.Imaging.jpeg, // for VCL JPEG Support
-  Vcl.Imaging.pngimage, // for VCL PNG Support
-{$ELSEIF DEFINED(FMX)}
-  FMX.Graphics,
-  System.UITypes,
-{$ELSEIF DEFINED(LCL)}
-  Graphics,
-{$IFEND}
   QlpQRCodeGenLibTypes;
 
 type
@@ -29,19 +19,13 @@ type
     property Mask: Int32 read GetMask;
     function GetModules: TQRCodeGenLibInt32Array;
     property Modules: TQRCodeGenLibInt32Array read GetModules;
-    function GetBackgroundColor:
-{$IFNDEF FMX}TColor{$ELSE}TAlphaColor{$ENDIF FMX};
-    procedure SetBackgroundColor(const AColor:
-{$IFNDEF FMX}TColor{$ELSE}TAlphaColor{$ENDIF FMX});
-    property BackgroundColor:
-{$IFNDEF FMX}TColor{$ELSE}TAlphaColor{$ENDIF FMX} read GetBackgroundColor
+    function GetBackgroundColor: TQRCodeGenLibColor;
+    procedure SetBackgroundColor(const AColor: TQRCodeGenLibColor);
+    property BackgroundColor: TQRCodeGenLibColor read GetBackgroundColor
       write SetBackgroundColor;
-    function GetForegroundColor:
-{$IFNDEF FMX}TColor{$ELSE}TAlphaColor{$ENDIF FMX};
-    procedure SetForegroundColor(const AColor:
-{$IFNDEF FMX}TColor{$ELSE}TAlphaColor{$ENDIF FMX});
-    property ForegroundColor:
-    {$IFNDEF FMX}TColor{$ELSE}TAlphaColor{$ENDIF FMX} read GetForegroundColor
+    function GetForegroundColor: TQRCodeGenLibColor;
+    procedure SetForegroundColor(const AColor: TQRCodeGenLibColor);
+    property ForegroundColor: TQRCodeGenLibColor read GetForegroundColor
       write SetForegroundColor;
 
     /// <summary>
@@ -89,9 +73,9 @@ type
     /// <b>The caller is responsible for the lifetime of the returned image
     /// object.</b>
     /// </remarks>
-    function ToBmpImage(AScale, ABorder: Int32): TBitmap;
+    function ToBmpImage(AScale, ABorder: Int32): TQRCodeGenLibBitmap;
 
-    {$IFNDEF FMX}
+{$IFNDEF FMX}
     /// <summary>
     /// Returns a jpeg image depicting this QR Code, with the specified
     /// module scale and border modules. For example, ToJpegImage(scale=10,
@@ -118,7 +102,7 @@ type
     /// <b>The caller is responsible for the lifetime of the returned image
     /// object.</b>
     /// </remarks>
-    function ToJpegImage(AScale, ABorder: Int32): TJPEGImage;
+    function ToJpegImage(AScale, ABorder: Int32): TQRCodeGenLibJPEGImage;
 
     /// <summary>
     /// Returns a png image depicting this QR Code, with the specified
@@ -146,10 +130,8 @@ type
     /// <b>The caller is responsible for the lifetime of the returned image
     /// object.</b>
     /// </remarks>
-    function ToPngImage(AScale, ABorder: Int32):
-{$IFDEF FPC}TPortableNetworkGraphic{$ELSE}TPngImage{$ENDIF FPC};
+    function ToPngImage(AScale, ABorder: Int32): TQRCodeGenLibPNGImage;
 {$ENDIF FMX}
-
     /// <summary>
     /// Returns a string of SVG code for an image depicting this QR Code,
     /// with the specified number of border modules. The string always uses

+ 85 - 181
QRCodeGenLib/src/QRCodeGen/QlpQrCode.pas

@@ -1,4 +1,4 @@
-unit QlpQrCode;
+unit QlpQrCode;
 
 {$I ..\Include\QRCodeGenLib.inc}
 
@@ -8,18 +8,6 @@ uses
   Math,
   Classes,
   SysUtils,
-{$IF DEFINED(VCL)}
-  Vcl.Graphics,
-  Vcl.Imaging.jpeg, // for VCL JPEG Support
-  Vcl.Imaging.pngimage, // for VCL PNG Support
-{$ELSEIF DEFINED(FMX)}
-  FMX.Graphics,
-  UIConsts,
-  UITypes,
-{$ELSEIF DEFINED(LCL)}
-  Graphics,
-  Interfaces, // Added so that the LCL will Initialize the WidgetSet
-{$IFEND}
   QlpIQrCode,
   QlpIQrTemplate,
   QlpQrTemplate,
@@ -145,21 +133,16 @@ type
     FErrorCorrectionLevel: TEcc;
     // Private grid of modules/pixels:
     FModules: TQRCodeGenLibInt32Array;
-    FBackgroundColor, FForegroundColor:
-{$IFNDEF FMX}TColor{$ELSE}TAlphaColor{$ENDIF FMX};
+    FBackgroundColor, FForegroundColor: TQRCodeGenLibColor;
 
     function GetVersion: Int32; inline;
     function GetSize: Int32; inline;
     function GetMask: Int32; inline;
     function GetModules: TQRCodeGenLibInt32Array; inline;
-    function GetBackgroundColor:
-{$IFNDEF FMX}TColor{$ELSE}TAlphaColor{$ENDIF FMX}; inline;
-    function GetForegroundColor:
-{$IFNDEF FMX}TColor{$ELSE}TAlphaColor{$ENDIF FMX}; inline;
-    procedure SetBackgroundColor(const AColor:
-{$IFNDEF FMX}TColor{$ELSE}TAlphaColor{$ENDIF FMX}); inline;
-    procedure SetForegroundColor(const AColor:
-{$IFNDEF FMX}TColor{$ELSE}TAlphaColor{$ENDIF FMX}); inline;
+    function GetBackgroundColor: TQRCodeGenLibColor; inline;
+    function GetForegroundColor: TQRCodeGenLibColor; inline;
+    procedure SetBackgroundColor(const AColor: TQRCodeGenLibColor); inline;
+    procedure SetForegroundColor(const AColor: TQRCodeGenLibColor); inline;
 
     // Draws two copies of the format bits (with its own error correction code)
     // based on the given mask and this object's error correction level field.
@@ -201,13 +184,13 @@ type
     procedure ValidateImageDimensions(AScale, ABorder: Int32);
 
 {$IFDEF LCL}
-    function ToBmpImageInternalLCL(AScale, ABorder: Int32): TBitmap;
+    function ToBmpImageInternalLCL(AScale, ABorder: Int32): TQRCodeGenLibBitmap;
 {$ENDIF LCL}
 {$IFDEF VCL}
-    function ToBmpImageInternalVCL(AScale, ABorder: Int32): TBitmap;
+    function ToBmpImageInternalVCL(AScale, ABorder: Int32): TQRCodeGenLibBitmap;
 {$ENDIF VCL}
 {$IFDEF FMX}
-    function ToBmpImageInternalFMX(AScale, ABorder: Int32): TBitmap;
+    function ToBmpImageInternalFMX(AScale, ABorder: Int32): TQRCodeGenLibBitmap;
 {$ENDIF FMX}
     // Returns the number of 8-bit data (i.e. not error correction) codewords contained in any
     // QR Code of the given version number and error correction level, with remainder bits discarded.
@@ -226,42 +209,8 @@ type
 
     // Must be called at the end of a line (row or column) of modules. A helper function for GetPenaltyScore()
     function FinderPenaltyTerminateAndCount(ACurrentRunColor, ACurrentRunLength
-      : Int32; ARunHistory: TQRCodeGenLibInt32Array): Int32; inline;
-
-{$IFNDEF FMX}
-    class function GetRValue(Argb: UInt32): Byte; static; inline;
-    class function GetGValue(Argb: UInt32): Byte; static; inline;
-    class function GetBValue(Argb: UInt32): Byte; static; inline;
-
-    /// <summary>
-    /// Convert a Delphi/Lazarus <c>TColor</c> to <c>HTML</c> Color code in
-    /// Hex <c>.</c>
-    /// </summary>
-    /// <param name="AColor">
-    /// the <c>TColor</c> to convert
-    /// </param>
-    /// <returns>
-    /// returns a string containing the <c>HTML</c> Color code representation
-    /// of the <c>TColor</c> parameter in Hex
-    /// </returns>
-    class function TColorToHTMLColorHex(const AColor: TColor): String; inline;
+      : Int32; const ARunHistory: TQRCodeGenLibInt32Array): Int32; inline;
 
-{$ELSE}
-    /// <summary>
-    /// Convert a Delphi FireMonkey <c>TAlphaColor</c> to <c>HTML</c> Color code in
-    /// Hex <c>.</c>
-    /// </summary>
-    /// <param name="AColor">
-    /// the <c>TAlphaColor</c> to convert
-    /// </param>
-    /// <returns>
-    /// returns a string containing the <c>HTML</c> Color code representation
-    /// of the <c>TAlphaColor</c> parameter in Hex
-    /// </returns>
-    class function TAlphaColorToHTMLColorHex(const AColor: TAlphaColor)
-      : String; inline;
-
-{$ENDIF FMX}
   public
 
     const
@@ -341,16 +290,14 @@ type
     /// property for getting/setting the background color of the QRCode <br />
     /// Object <br />
     /// </summary>
-    property BackgroundColor:
-{$IFNDEF FMX}TColor{$ELSE}TAlphaColor{$ENDIF FMX} read GetBackgroundColor
+    property BackgroundColor: TQRCodeGenLibColor read GetBackgroundColor
       write SetBackgroundColor;
 
     /// <summary>
     /// property for getting/setting the foreground color of the QRCode
     /// Object
     /// </summary>
-    property ForegroundColor:
-{$IFNDEF FMX}TColor{$ELSE}TAlphaColor{$ENDIF FMX} read GetForegroundColor
+    property ForegroundColor: TQRCodeGenLibColor read GetForegroundColor
       write SetForegroundColor;
 
     /// <summary>
@@ -379,7 +326,7 @@ type
     /// <b>The caller is responsible for the lifetime of the returned image
     /// object.</b>
     /// </remarks>
-    function ToBmpImage(AScale, ABorder: Int32): TBitmap;
+    function ToBmpImage(AScale, ABorder: Int32): TQRCodeGenLibBitmap;
 
 {$IFNDEF FMX}
     /// <summary>
@@ -408,7 +355,7 @@ type
     /// <b>The caller is responsible for the lifetime of the returned image
     /// object.</b>
     /// </remarks>
-    function ToJpegImage(AScale, ABorder: Int32): TJPEGImage;
+    function ToJpegImage(AScale, ABorder: Int32): TQRCodeGenLibJPEGImage;
 
     /// <summary>
     /// Returns a png image depicting this QR Code, with the specified
@@ -436,8 +383,7 @@ type
     /// <b>The caller is responsible for the lifetime of the returned image
     /// object.</b>
     /// </remarks>
-    function ToPngImage(AScale, ABorder: Int32):
-{$IFDEF FPC}TPortableNetworkGraphic{$ELSE}TPngImage{$ENDIF FPC};
+    function ToPngImage(AScale, ABorder: Int32): TQRCodeGenLibPNGImage;
 {$ENDIF FMX}
     /// <summary>
     /// Returns a string of SVG code for an image depicting this QR Code,
@@ -607,41 +553,44 @@ type
 
 implementation
 
-{ TQrCode }
-
-{$IFNDEF FMX}
-
-class function TQrCode.GetRValue(Argb: UInt32): Byte;
-begin
-  Result := Byte(Argb);
-end;
-
-class function TQrCode.GetGValue(Argb: UInt32): Byte;
-begin
-  Result := Byte(Argb shr 8);
-end;
-
-class function TQrCode.GetBValue(Argb: UInt32): Byte;
-begin
-  Result := Byte(Argb shr 16);
-end;
+{ TQrCode.TEccHelper }
 
-class function TQrCode.TColorToHTMLColorHex(const AColor: TColor): String;
+function TQrCode.TEccHelper.GetFormatBits: Int32;
 begin
-  Result := Format('%.2x%.2x%.2x', [GetRValue(ColorToRGB(AColor)),
-    GetGValue(ColorToRGB(AColor)), GetBValue(ColorToRGB(AColor))]);
+  Result := Ord(Self);
 end;
 
-{$ELSE}
-
-class function TQrCode.TAlphaColorToHTMLColorHex(const AColor
-  : TAlphaColor): String;
+function TQrCode.TEccHelper.GetToInt32: Int32;
 begin
-  Result := Format('%.2x%.2x%.2x', [TAlphaColorRec(AColor).R,
-    TAlphaColorRec(AColor).G, TAlphaColorRec(AColor).B]);
+  case Self of
+    TQrCode.TEcc.eccLow:
+      begin
+        Result := 0;
+        Exit;
+      end;
+    TQrCode.TEcc.eccMedium:
+      begin
+        Result := 1;
+        Exit;
+      end;
+    TQrCode.TEcc.eccQuartile:
+      begin
+        Result := 2;
+        Exit;
+      end;
+    TQrCode.TEcc.eccHigh:
+      begin
+        Result := 3;
+        Exit;
+      end
+  else
+    begin
+      raise EInvalidOperationQRCodeGenLibException.CreateRes(@SInvalidState);
+    end;
+  end;
 end;
 
-{$ENDIF FMX}
+{ TQrCode }
 
 function TQrCode.GetVersion: Int32;
 begin
@@ -749,7 +698,7 @@ begin
 end;
 
 function TQrCode.FinderPenaltyTerminateAndCount(ACurrentRunColor,
-  ACurrentRunLength: Int32; ARunHistory: TQRCodeGenLibInt32Array): Int32;
+  ACurrentRunLength: Int32; const ARunHistory: TQRCodeGenLibInt32Array): Int32;
 begin
   // Terminate black run
   if (ACurrentRunColor = 1) then
@@ -1031,8 +980,8 @@ begin
   LAllCodewords := AddEccAndInterleave(ADataCodewords);
   DrawCodeWords(LTpl.DataOutputBitIndexes, LAllCodewords);
   FMask := HandleConstructorMasking(LTpl.Masks, AMask);
-  FBackgroundColor := {$IFNDEF FMX}clWhite{$ELSE}claWhite{$ENDIF FMX};
-  FForegroundColor := {$IFNDEF FMX}clBlack{$ELSE}claBlack{$ENDIF FMX};
+  FBackgroundColor := QRCodeGenLibWhiteColor;
+  FForegroundColor := QRCodeGenLibBlackColor;
 end;
 
 procedure TQrCode.SetModule(Ax, Ay, ABlack: Int32);
@@ -1055,16 +1004,16 @@ end;
 
 {$IFDEF LCL}
 
-function TQrCode.ToBmpImageInternalLCL(AScale, ABorder: Int32): TBitmap;
+function TQrCode.ToBmpImageInternalLCL(AScale, ABorder: Int32)
+  : TQRCodeGenLibBitmap;
 var
   LColumn, LRow: Int32;
   LDoColor: Boolean;
-  LBrushColor: TColor;
-  LForegroundColor, LBackgroundColor: TColor;
+  LBrushColor, LForegroundColor, LBackgroundColor: TQRCodeGenLibColor;
   LScanLine: PByte;
   LBytesPerPixel, LRedOffset, LGreenOffset, LBlueOffset: Byte;
 begin
-  Result := TBitmap.Create;
+  Result := TQRCodeGenLibBitmap.Create;
 
   Result.SetSize((FSize + (ABorder * 2)) * AScale, (FSize + (ABorder * 2))
     * AScale);
@@ -1100,9 +1049,9 @@ begin
         end;
         // Slow !!!
         // Result.Canvas.Pixels[LRow, LColumn] := LBrushColor;
-        (LScanLine + LBlueOffset)^ := GetBValue(LBrushColor);
-        (LScanLine + LGreenOffset)^ := GetGValue(LBrushColor);
-        (LScanLine + LRedOffset)^ := GetRValue(LBrushColor);
+        (LScanLine + LBlueOffset)^ := TConverters.GetBValue(LBrushColor);
+        (LScanLine + LGreenOffset)^ := TConverters.GetGValue(LBrushColor);
+        (LScanLine + LRedOffset)^ := TConverters.GetRValue(LBrushColor);
         System.Inc(LScanLine, LBytesPerPixel);
       end;
     end;
@@ -1113,7 +1062,8 @@ end;
 {$ENDIF LCL}
 {$IFDEF VCL}
 
-function TQrCode.ToBmpImageInternalVCL(AScale, ABorder: Int32): TBitmap;
+function TQrCode.ToBmpImageInternalVCL(AScale, ABorder: Int32)
+  : TQRCodeGenLibBitmap;
 type
   TRGBTriple = record
     B, G, R: Byte;
@@ -1126,12 +1076,11 @@ type
 var
   LColumn, LRow: Int32;
   LDoColor: Boolean;
-  LBrushColor: TColor;
-  LForegroundColor, LBackgroundColor: TColor;
+  LBrushColor, LForegroundColor, LBackgroundColor: TQRCodeGenLibColor;
   LScanLine: PRGBTripleArray;
 begin
-  Result := TBitmap.Create;
-  Result.PixelFormat := pf24bit;
+  Result := TQRCodeGenLibBitmap.Create;
+  Result.PixelFormat := TwentyFourBitPixelFormat;
 
   Result.SetSize((FSize + (ABorder * 2)) * AScale, (FSize + (ABorder * 2))
     * AScale);
@@ -1156,9 +1105,9 @@ begin
       end;
       // Slow !!!
       // Result.Canvas.Pixels[LRow, LColumn] := LBrushColor;
-      LScanLine^[LRow].B := GetBValue(LBrushColor);
-      LScanLine^[LRow].G := GetGValue(LBrushColor);
-      LScanLine^[LRow].R := GetRValue(LBrushColor);
+      LScanLine^[LRow].B := TConverters.GetBValue(LBrushColor);
+      LScanLine^[LRow].G := TConverters.GetGValue(LBrushColor);
+      LScanLine^[LRow].R := TConverters.GetRValue(LBrushColor);
     end;
 
   end;
@@ -1166,21 +1115,21 @@ end;
 {$ENDIF VCL}
 {$IFDEF FMX}
 
-function TQrCode.ToBmpImageInternalFMX(AScale, ABorder: Int32): TBitmap;
+function TQrCode.ToBmpImageInternalFMX(AScale, ABorder: Int32)
+  : TQRCodeGenLibBitmap;
 var
   LColumn, LRow: Int32;
   LDoColor: Boolean;
-  LBrushColor: TColor;
-  LForegroundColor, LBackgroundColor: TColor;
-  LBitData: TBitmapData;
+  LBrushColor, LForegroundColor, LBackgroundColor: TQRCodeGenLibColor;
+  LBitData: TQRCodeGenLibBitmapData;
 begin
-  Result := TBitmap.Create;
+  Result := TQRCodeGenLibBitmap.Create;
   Result.SetSize((FSize + (ABorder * 2)) * AScale, (FSize + (ABorder * 2))
     * AScale);
   LForegroundColor := FForegroundColor;
   LBackgroundColor := FBackgroundColor;
 
-  if Result.Map(TMapAccess.Write, LBitData) then
+  if Result.Map(TQRCodeGenLibMapAccess.Write, LBitData) then
   begin
     try
       for LColumn := 0 to System.Pred(Result.Height) do
@@ -1210,7 +1159,7 @@ begin
 end;
 {$ENDIF FMX}
 
-function TQrCode.ToBmpImage(AScale, ABorder: Int32): TBitmap;
+function TQrCode.ToBmpImage(AScale, ABorder: Int32): TQRCodeGenLibBitmap;
 begin
   ValidateImageDimensions(AScale, ABorder);
 {$IF DEFINED(LCL)}
@@ -1225,12 +1174,12 @@ begin
 end;
 {$IFNDEF FMX}
 
-function TQrCode.ToJpegImage(AScale, ABorder: Int32): TJPEGImage;
+function TQrCode.ToJpegImage(AScale, ABorder: Int32): TQRCodeGenLibJPEGImage;
 var
-  LBitmap: TBitmap;
+  LBitmap: TQRCodeGenLibBitmap;
 begin
   LBitmap := ToBmpImage(AScale, ABorder);
-  Result := TJPEGImage.Create;
+  Result := TQRCodeGenLibJPEGImage.Create;
   Result.CompressionQuality := 99;
   try
     Result.Assign(LBitmap);
@@ -1239,13 +1188,12 @@ begin
   end;
 end;
 
-function TQrCode.ToPngImage(AScale, ABorder: Int32):
-{$IFDEF FPC}TPortableNetworkGraphic{$ELSE}TPngImage{$ENDIF FPC};
+function TQrCode.ToPngImage(AScale, ABorder: Int32): TQRCodeGenLibPNGImage;
 var
-  LBitmap: TBitmap;
+  LBitmap: TQRCodeGenLibBitmap;
 begin
   LBitmap := ToBmpImage(AScale, ABorder);
-  Result := {$IFDEF FPC}TPortableNetworkGraphic{$ELSE}TPngImage{$ENDIF FPC}.Create;
+  Result := TQRCodeGenLibPNGImage.Create;
   try
     Result.Assign(LBitmap);
   finally
@@ -1266,13 +1214,10 @@ begin
     raise EArgumentInvalidQRCodeGenLibException.CreateRes(@SBorderNegative);
   end;
   LBorder := ABorder;
-{$IFNDEF FMX}
-  LForegroundColor := TColorToHTMLColorHex(FForegroundColor);
-  LBackgroundColor := TColorToHTMLColorHex(FBackgroundColor);
-{$ELSE}
-  LForegroundColor := TAlphaColorToHTMLColorHex(FForegroundColor);
-  LBackgroundColor := TAlphaColorToHTMLColorHex(FBackgroundColor);
-{$ENDIF FMX}
+
+  LForegroundColor := TConverters.ColorToHTMLColorHex(FForegroundColor);
+  LBackgroundColor := TConverters.ColorToHTMLColorHex(FBackgroundColor);
+
   LStringList := TStringList.Create;
   LStringList.LineBreak := '';
   try
@@ -1528,65 +1473,24 @@ begin
   Result := EncodeSegments(TQrSegment.MakeSegments(AText, AEncoding), AEcl);
 end;
 
-function TQrCode.GetBackgroundColor:
-{$IFNDEF FMX}TColor{$ELSE}TAlphaColor{$ENDIF FMX};
+function TQrCode.GetBackgroundColor: TQRCodeGenLibColor;
 begin
   Result := FBackgroundColor;
 end;
 
-function TQrCode.GetForegroundColor:
-{$IFNDEF FMX}TColor{$ELSE}TAlphaColor{$ENDIF FMX};
+function TQrCode.GetForegroundColor: TQRCodeGenLibColor;
 begin
   Result := FForegroundColor;
 end;
 
-procedure TQrCode.SetBackgroundColor(const AColor:
-{$IFNDEF FMX}TColor{$ELSE}TAlphaColor{$ENDIF FMX});
+procedure TQrCode.SetBackgroundColor(const AColor: TQRCodeGenLibColor);
 begin
   FBackgroundColor := AColor;
 end;
 
-procedure TQrCode.SetForegroundColor(const AColor:
-{$IFNDEF FMX}TColor{$ELSE}TAlphaColor{$ENDIF FMX});
+procedure TQrCode.SetForegroundColor(const AColor: TQRCodeGenLibColor);
 begin
   FForegroundColor := AColor;
 end;
 
-{ TQrCode.TEccHelper }
-
-function TQrCode.TEccHelper.GetFormatBits: Int32;
-begin
-  Result := Ord(Self);
-end;
-
-function TQrCode.TEccHelper.GetToInt32: Int32;
-begin
-  case Self of
-    TQrCode.TEcc.eccLow:
-      begin
-        Result := 0;
-        Exit;
-      end;
-    TQrCode.TEcc.eccMedium:
-      begin
-        Result := 1;
-        Exit;
-      end;
-    TQrCode.TEcc.eccQuartile:
-      begin
-        Result := 2;
-        Exit;
-      end;
-    TQrCode.TEcc.eccHigh:
-      begin
-        Result := 3;
-        Exit;
-      end
-  else
-    begin
-      raise EInvalidOperationQRCodeGenLibException.CreateRes(@SInvalidState);
-    end;
-  end;
-end;
-
 end.

+ 95 - 0
QRCodeGenLib/src/Utils/QlpConverters.pas

@@ -5,6 +5,16 @@ unit QlpConverters;
 interface
 
 uses
+{$IF DEFINED(VCL)}
+  Vcl.Graphics,
+{$ELSEIF DEFINED(FMX)}
+  FMX.Graphics,
+  UIConsts,
+  UITypes,
+{$ELSEIF DEFINED(LCL)}
+  Graphics,
+  Interfaces, // Added so that the LCL will Initialize the WidgetSet
+{$IFEND}
   SysUtils,
   QlpGuard,
   QlpQRCodeGenLibTypes;
@@ -15,13 +25,53 @@ resourcestring
 type
   TConverters = class sealed(TObject)
 
+  strict private
+{$IFNDEF FMX}
+    /// <summary>
+    /// Convert a Delphi/Lazarus <c>TColor</c> to <c>HTML</c> Color code in
+    /// Hex <c>.</c>
+    /// </summary>
+    /// <param name="AColor">
+    /// the <c>TColor</c> to convert
+    /// </param>
+    /// <returns>
+    /// returns a string containing the <c>HTML</c> Color code representation
+    /// of the <c>TColor</c> parameter in Hex
+    /// </returns>
+    class function TColorToHTMLColorHex(const AColor: TQRCodeGenLibColor)
+      : String; inline;
+
+{$ELSE}
+    /// <summary>
+    /// Convert a Delphi FireMonkey <c>TAlphaColor</c> to <c>HTML</c> Color code in
+    /// Hex <c>.</c>
+    /// </summary>
+    /// <param name="AColor">
+    /// the <c>TAlphaColor</c> to convert
+    /// </param>
+    /// <returns>
+    /// returns a string containing the <c>HTML</c> Color code representation
+    /// of the <c>TAlphaColor</c> parameter in Hex
+    /// </returns>
+    class function TAlphaColorToHTMLColorHex(const AColor: TQRCodeGenLibColor)
+      : String; inline;
+
+{$ENDIF FMX}
   public
 
+{$IFNDEF FMX}
+    class function GetRValue(Argb: UInt32): Byte; static; inline;
+    class function GetGValue(Argb: UInt32): Byte; static; inline;
+    class function GetBValue(Argb: UInt32): Byte; static; inline;
+{$ENDIF FMX}
     class function ConvertStringToBytes(const AInput: String;
       const AEncoding: TEncoding): TQRCodeGenLibByteArray; static;
 
     class function ConvertBytesToString(const AInput: TQRCodeGenLibByteArray;
       const AEncoding: TEncoding): String; static;
+
+    class function ColorToHTMLColorHex(const AColor: TQRCodeGenLibColor)
+      : String; inline;
   end;
 
 implementation
@@ -50,4 +100,49 @@ begin
 {$ENDIF FPC}
 end;
 
+{$IFNDEF FMX}
+
+class function TConverters.GetRValue(Argb: UInt32): Byte;
+begin
+  result := Byte(Argb);
+end;
+
+class function TConverters.GetGValue(Argb: UInt32): Byte;
+begin
+  result := Byte(Argb shr 8);
+end;
+
+class function TConverters.GetBValue(Argb: UInt32): Byte;
+begin
+  result := Byte(Argb shr 16);
+end;
+
+class function TConverters.TColorToHTMLColorHex(const AColor
+  : TQRCodeGenLibColor): String;
+begin
+  result := Format('%.2x%.2x%.2x', [GetRValue(ColorToRGB(AColor)),
+    GetGValue(ColorToRGB(AColor)), GetBValue(ColorToRGB(AColor))]);
+end;
+
+{$ELSE}
+
+class function TConverters.TAlphaColorToHTMLColorHex(const AColor
+  : TQRCodeGenLibColor): String;
+begin
+  result := Format('%.2x%.2x%.2x', [TAlphaColorRec(AColor).R,
+    TAlphaColorRec(AColor).G, TAlphaColorRec(AColor).B]);
+end;
+
+{$ENDIF FMX}
+
+class function TConverters.ColorToHTMLColorHex(const AColor
+  : TQRCodeGenLibColor): String;
+begin
+{$IFNDEF FMX}
+  result := TConverters.TColorToHTMLColorHex(AColor);
+{$ELSE}
+  result := TConverters.TAlphaColorToHTMLColorHex(AColor);
+{$ENDIF FMX}
+end;
+
 end.

+ 30 - 0
QRCodeGenLib/src/Utils/QlpQRCodeGenLibTypes.pas

@@ -5,6 +5,18 @@ unit QlpQRCodeGenLibTypes;
 interface
 
 uses
+{$IF DEFINED(VCL)}
+  Vcl.Graphics,
+  Vcl.Imaging.jpeg, // for VCL JPEG Support
+  Vcl.Imaging.pngimage, // for VCL PNG Support
+{$ELSEIF DEFINED(FMX)}
+  FMX.Graphics,
+  UIConsts,
+  UITypes,
+{$ELSEIF DEFINED(LCL)}
+  Graphics,
+  Interfaces, // Added so that the LCL will Initialize the WidgetSet
+{$IFEND}
   SysUtils;
 
 type
@@ -82,6 +94,24 @@ type
   TQRCodeGenLibMatrixInt32Array = array of TQRCodeGenLibInt32Array;
 
 {$ENDIF DELPHIXE_UP}
+  TQRCodeGenLibColor = {$IFNDEF FMX}TColor{$ELSE}TAlphaColor{$ENDIF FMX};
+  TQRCodeGenLibBitmap = TBitmap;
+{$IFNDEF FMX}
+  TQRCodeGenLibPNGImage =
+{$IFDEF FPC}TPortableNetworkGraphic{$ELSE}TPngImage{$ENDIF FPC};
+  TQRCodeGenLibJPEGImage = TJPEGImage;
+{$ELSE}
+  TQRCodeGenLibBitmapData = TBitmapData;
+  TQRCodeGenLibMapAccess = TMapAccess;
+{$ENDIF FMX}
+
+const
+  QRCodeGenLibWhiteColor = {$IFNDEF FMX}clWhite{$ELSE}claWhite{$ENDIF FMX};
+  QRCodeGenLibBlackColor = {$IFNDEF FMX}clBlack{$ELSE}claBlack{$ENDIF FMX};
+
+{$IFDEF VCL}
+  TwentyFourBitPixelFormat = pf24bit;
+{$ENDIF VCL}
 
 implementation