Thursday, May 31, 2018

Delphi Color values to HTML Color


function ColorToHtml(const Color: Integer): AnsiString;
const
  Hex = '0123456789ABCDEF';
var
  b, s: Byte;
begin
  Result := '#';
  s := 0;
  repeat
    b := (Color shr s) and $FF;
    Result := Result + Hex[b div 16 + 1] + Hex[b mod 16 + 1];
    Inc(s, 8);
  until s = 24;
end;

function HtmlToColor(const Html: AnsiString): Integer;
const
  Hex = '0123456789ABCDEF';
var
  p, s: Byte;
begin
  Result := 0;
  s := 0;
  p := 1;
  if Html[1] = '#' then
    Inc(p);
  repeat
    Result := Result or (((Pos(Html[p], Hex) - 1) * 16 +
      Pos(Html[p + 1], Hex) - 1) shl s);
    Inc(p, 2);
    Inc(s, 8);
  until s = 24;
end;

Sample code:

  c := StringToColor(Trim(<COLOR>));
       //ColorToString(<COLOR>)
  Panel1.Caption := ColorToHtml(c);
  Panel1.Color := HtmlToColor(ColorToHtml(c));

No comments:

Delphi Thread Example

Delphi Thread Example Threads mean a lot with the latest computer technology. They allow you to perform multiple tasks at the same time ...