whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

20 Visitors Online


 
...convert TColor to HTML color?
Autor: Simon Grossenbacher
Homepage: http://www.swissdelphicenter.ch
[ Print tip ]  

Tip Rating (14):  
     


function ColorToHtml1(Color: TColor): string;
var
  
COL: LongInt;
begin
  
COL := ColorToRGB(Color);
  { first convert TColor to Integer to remove the higher bits }
  { erst TColor zu Integer, da die Unnötigen höheren Bit entfernt werden }
  
Result := '#' + IntToHex(COL and $FF, 2) +
    IntToHex(COL shr and $FF, 2) +
    IntToHex(COL shr 16 and $FF, 2);
end;


function ColorToHtml2(Clr: TColor): string;
begin
  
Result := IntToHex(clr, 6);
  Result := '#' + Copy(Result, 5, 2) + Copy(Result, 3, 2) + Copy(Result, 1, 2);
end;

function HtmlToColor(Color: string): TColor;
begin
  
Result := StringToColor('$' + Copy(Color, 6, 2) + Copy(Color, 4, 2) + Copy(Color, 2, 2));
end;

// Example:

procedure TForm1.Button1Click(Sender: TObject);
var
  
ColorTemp: TColor;
  ident: string;
begin
  
Edit1.Text := ColorToHtml($808080);    // $808080 (clGray) ----> #808080
  
ColorTemp  := HtmlToColor(Edit1.Text); // #808080 -----> $808080 (clGray)
  
ColorToIdent(StringToColor(IntToStr(ColorTemp)), ident); // ---> ident = clGray
  
Edit2.Text := ident; // clGray
end;

 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners