Go Back   Wiki NewForum | Latest Entertainment News > Career Forum & Tips > Tech Forum & Tutorial > Oracle Database, SQL, Application, Programming


Convert your TColors to hex strings


Reply
Views: 1604  
Thread Tools Rate Thread
  #1  
Old 08-20-2010, 10:52 AM
bholas bholas is offline
Award Winner
 
Join Date: Apr 2010
Posts: 4,977
Default Convert your TColors to hex strings

In Delphi, color is often represented using the TColor object. In HTML documents, color is usually represented using a 6 character hex string. Following function will convert TColor type color values to "Internet-style" color codes:
{
Return TColor value in XXXXXX format
(X being a hex digit)
}
function
TColorToHex( Color : TColor )
: string;
begin
Result :=
{ red value }
IntToHex( GetRValue( Color ), 2 ) +
{ green value }
IntToHex( GetGValue( Color ), 2 ) +
{ blue value }
IntToHex( GetBValue( Color ), 2 );
end;
Listing #1 : Delphi code. Download clr2hex (0.3 KB).


Need to convert a hex color string to a TColor variable? Try this:
{
sColor should be in XXXXXX format
(X being a hex digit)
}
function
HexToTColor( sColor : string )
: TColor;
begin
Result :=
RGB(
{ get red value }
StrToInt( '$'+Copy( sColor, 1, 2 ) ),
{ get green value }
StrToInt( '$'+Copy( sColor, 3, 2 ) ),
{ get blue value }
StrToInt( '$'+Copy( sColor, 5, 2 ) )
);
end;

Reply With Quote
Reply

Tags
programming tips

New topics in Oracle Database, SQL, Application, Programming





Powered by vBulletin® Version 3.8.10
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
WikiNewForum)