Recent

Author Topic: Unit to convert HTML color-tokens to TColor  (Read 2236 times)

AlexTP

  • Hero Member
  • *****
  • Posts: 2402
    • UVviewsoft
Unit to convert HTML color-tokens to TColor
« on: August 14, 2021, 06:09:53 pm »
This is unit from ATSynEdit, it has no dependency from other ATSynEdit so I put it here as a separate mini project. It uses only few LCL units.
It gives the class, which helps to convert PChar or PWideChar buffer text to TColor. E.g. you have string/UnicodeString with some HTML color-tokens. You find the beginning of that tokens, and pass PChar/PWideChar to my class, it finds the TColor value (if parser finds incorrect token, it returns clNone, so you can pass position of any '#' in string).

It can handle tokens:
- #rgb
- #rrggbb
- #rgba
- #rrggbbaa
- rgb(..) and rgba(..)
- hsl(..) and hsla(..)

Code: Pascal  [Select][+][-]
  1. type
  2.   generic TATHtmlColorParser<TStr, TPChar> = class
  3.   private
  4.     ....
  5.   public
  6.     //convert TColor -> HTML color string #rrggbb
  7.     class function ColorToHtmlString(Color: TColor): string;
  8.     //convert string which starts with HTML color token #rgb, #rrggbb -> TColor, get len of color-string
  9.     class function ParseTokenRGB(S: TPChar; out Len: integer; Default: TColor): TColor;
  10.     //parses 'rgb(10,20,30)' and rgba(10,20,30,0.5)
  11.     class function ParseFunctionRGB(const S: TStr; FromPos: integer; out LenOfColor: integer): TColor;
  12.     //parses 'hsl(0,50%,100%)' and 'hsla(0,50%,100%,0.5)
  13.     class function ParseFunctionHSL(const S: TStr; FromPos: integer; out LenOfColor: integer): TColor;
  14.   end;
  15.  
  16. type
  17.   TATHtmlColorParserA = specialize TATHtmlColorParser<string, PChar>;
  18.   TATHtmlColorParserW = specialize TATHtmlColorParser<UnicodeString, PWideChar>;
  19.  


Full unit is attached in a file.
« Last Edit: August 14, 2021, 08:11:22 pm by Alextp »

paweld

  • Hero Member
  • *****
  • Posts: 1003
Re: Unit to convert HTML color-tokens to TColor
« Reply #1 on: August 14, 2021, 07:58:53 pm »
thanks
Best regards / Pozdrawiam
paweld

 

TinyPortal © 2005-2018