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(..)
type
generic TATHtmlColorParser<TStr, TPChar> = class
private
....
public
//convert TColor -> HTML color string #rrggbb
class function ColorToHtmlString(Color: TColor): string;
//convert string which starts with HTML color token #rgb, #rrggbb -> TColor, get len of color-string
class function ParseTokenRGB(S: TPChar; out Len: integer; Default: TColor): TColor;
//parses 'rgb(10,20,30)' and rgba(10,20,30,0.5)
class function ParseFunctionRGB(const S: TStr; FromPos: integer; out LenOfColor: integer): TColor;
//parses 'hsl(0,50%,100%)' and 'hsla(0,50%,100%,0.5)
class function ParseFunctionHSL(const S: TStr; FromPos: integer; out LenOfColor: integer): TColor;
end;
type
TATHtmlColorParserA = specialize TATHtmlColorParser<string, PChar>;
TATHtmlColorParserW = specialize TATHtmlColorParser<UnicodeString, PWideChar>;
Full unit is attached in a file.