cvbn
asked on
HTML TO COLOR
HTML TO COLOR
How to convert HTML color to Delphi color ?
Thanks.
How to convert HTML color to Delphi color ?
Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
the HTML color is in the standard Hexadecimal notation, like FFFFFF or C0D5FF you can use StrToInt('FFA6D9') to get a TColor value
Canvas.Brush.Color := StrToInt('FFA6D9');
Canvas.Brush.Color := StrToInt('FFA6D9');
HTML colors are stored as Red/Green/Blue with one byte for every base color. Thus, FF0000 is red. FFFF00 is yellow, ect. In Delphi, colors are 4-byte values. Basically, Alpha-channel/Blue/Green/R ed. The Alpha-channel is used to blend colors with items on the background in some applications but generally Delphi doesn't use this one. Thus $00FF0000 is blue, $0000FF00 is green and $000000FF is red. All other color numbers are combinations of red, green and blue.
Now come closer... Closer to the screen... Much closer. Press your nose against the glass of the screen and look very closely. You will see that the screen of a normal monitor is build on red, green and blue fluorescent particles. Now you know what these values actually set. ;-)
So, how to convert HTML colors? Well, simple... That has been explained already.
Now come closer... Closer to the screen... Much closer. Press your nose against the glass of the screen and look very closely. You will see that the screen of a normal monitor is build on red, green and blue fluorescent particles. Now you know what these values actually set. ;-)
So, how to convert HTML colors? Well, simple... That has been explained already.
damn, my monitor is crashed, i was too close :-))
Nose problems ? :)
Hi,
if you want to use StrToInt,
you must set a "$" at the beginning
of the string.
like StrToInt('$FF');
GL
Bug
if you want to use StrToInt,
you must set a "$" at the beginning
of the string.
like StrToInt('$FF');
GL
Bug
Hello
There's a function in delphi to convert the string to color, StringToColor
and here's a function I made, it's test the string to decide it html or delphi and will return the Tcolor value
function GetTColor (S: String) : TColor;
var
R,G,B : Integer;
begin
if Copy(S,1,1) = '#' then
begin
B := StrToInt('$' + Copy(S,2,2));
G := StrToInt('$' + Copy(S,4,2)) * 256;
R := StrToInt('$' + (Copy(S,6,2))) * 65536;
Result := (R+G+B);
end
else if Copy(S,1,1) = '$' then
Result := StringToColor(S);
end;
procedure TForm1.Button1Click(Sender : TObject);
begin
Label1.color := GetTColor(Edit1.Text);
end;
Best regards
Mohammed Nasman
There's a function in delphi to convert the string to color, StringToColor
and here's a function I made, it's test the string to decide it html or delphi and will return the Tcolor value
function GetTColor (S: String) : TColor;
var
R,G,B : Integer;
begin
if Copy(S,1,1) = '#' then
begin
B := StrToInt('$' + Copy(S,2,2));
G := StrToInt('$' + Copy(S,4,2)) * 256;
R := StrToInt('$' + (Copy(S,6,2))) * 65536;
Result := (R+G+B);
end
else if Copy(S,1,1) = '$' then
Result := StringToColor(S);
end;
procedure TForm1.Button1Click(Sender
begin
Label1.color := GetTColor(Edit1.Text);
end;
Best regards
Mohammed Nasman
function HexToColor(Hex:string):TCo lor;
var R,G,B:string; ModInt:integer;
begin
ModInt:=0;
if Copy(Hex,1,1)='#' then ModInt:=1;
R:=Copy(Hex,1+ModInt,2);
G:=Copy(Hex,3+ModInt,2);
B:=Copy(Hex,5+ModInt,2);
Result:=StringToColor('$00 '+B+G+R);
end;
procedure TForm1.Button1Click(Sender : TObject);
begin
Color:=HexToColor('#FF9900 ');
end;
var R,G,B:string; ModInt:integer;
begin
ModInt:=0;
if Copy(Hex,1,1)='#' then ModInt:=1;
R:=Copy(Hex,1+ModInt,2);
G:=Copy(Hex,3+ModInt,2);
B:=Copy(Hex,5+ModInt,2);
Result:=StringToColor('$00
end;
procedure TForm1.Button1Click(Sender
begin
Color:=HexToColor('#FF9900
end;
Hey, cdbsi... Never heard that it's impolite to answer a question at EE? Especially if your answer is similar to those of others? What are you trying to archieve?
All programming has it's similarities, since it's based off of mathematics and math doesn't have large discoveries very often does it. The other "answers" had bitwise shifting, addition, and multiplication. I'm only giving the shortest, simplest, and most convenient answers. I am not trying to win an expert point contest or anything, I like to just help people with their development of programming wisdom.
Thank you and best wishes . . . cdbsi.
Thank you and best wishes . . . cdbsi.
Hi all,
Another version:
function HTMLToColor(c: string): TColor;
begin
if Length(c) = 7 then
result := RGB(StrToInt('$'+Copy(c,2, 2)),StrToI nt('$'+Cop y(c,4,2)), StrToInt(' $'+Copy(c, 6,2)))
else
result := RGB(StrToInt('$'+Copy(c,1, 2)),StrToI nt('$'+Cop y(c,3,2)), StrToInt(' $'+Copy(c, 5,2)));
end;
// usage:
procedure TForm1.Button1Click(Sender : TObject);
begin
Panel1.Color := HTMLToColor('$FF0000');
end;
Regards, Geo
Another version:
function HTMLToColor(c: string): TColor;
begin
if Length(c) = 7 then
result := RGB(StrToInt('$'+Copy(c,2,
else
result := RGB(StrToInt('$'+Copy(c,1,
end;
// usage:
procedure TForm1.Button1Click(Sender
begin
Panel1.Color := HTMLToColor('$FF0000');
end;
Regards, Geo
Or even easier:
function HTMLToColor(cc: string): TColor;
var
c: DWORD;
begin
if Length(cc) = 7 then cc := Copy(cc,2,6);
c:= StrToInt('$'+cc);
result := RGB(GetBValue(c),GetGValue (c),GetRVa lue(c));
end;
// usage:
procedure TForm1.Button1Click(Sender : TObject);
var
col: string;
begin
col := '#FF0000';
Panel1.Color := HTMLToColor(col);
end;
Regards, Geo
function HTMLToColor(cc: string): TColor;
var
c: DWORD;
begin
if Length(cc) = 7 then cc := Copy(cc,2,6);
c:= StrToInt('$'+cc);
result := RGB(GetBValue(c),GetGValue
end;
// usage:
procedure TForm1.Button1Click(Sender
var
col: string;
begin
col := '#FF0000';
Panel1.Color := HTMLToColor(col);
end;
Regards, Geo
cdbsi,
i do just agree with Workshop_Alex,
let the questioner decide, which helps him/her most
meikl ;-)
i do just agree with Workshop_Alex,
let the questioner decide, which helps him/her most
meikl ;-)
Anyone 'shortest and simplest' code has its shorter and simplier equivalent. Convenience of someone's code is a subjective category, i.e. depends on the point of view.
Regards, Geo
Regards, Geo
For reasons stated below, proposed answer rejected
Comments
Comments are intended to be used as a collaboration tool. Many Experts choose to post their solutions as comments only.
Answers
An answer is a specific solution to a question and should be submitted if it will solve the questioner's problem and doesn't duplicate a previous comment.
Comment Vs. Answer
Unless you are absolutely certain that your response is the ONLY completely accurate solution to a problem, post it as a comment. Members can accept comments as solutions and award you Expert Points for them.
For more tips on comments and answers, click here.
https://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp#14
kb
Experts Exchange Moderator
Comments
Comments are intended to be used as a collaboration tool. Many Experts choose to post their solutions as comments only.
Answers
An answer is a specific solution to a question and should be submitted if it will solve the questioner's problem and doesn't duplicate a previous comment.
Comment Vs. Answer
Unless you are absolutely certain that your response is the ONLY completely accurate solution to a problem, post it as a comment. Members can accept comments as solutions and award you Expert Points for them.
For more tips on comments and answers, click here.
https://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp#14
kb
Experts Exchange Moderator
cdbsi,
Your account has been suspended pending your reply to Admin.
Netminder
CS Moderator
Your account has been suspended pending your reply to Admin.
Netminder
CS Moderator
cvbn,
Any comments about our proposed solutions ? I put the first working solution on this topic and after me a lots of people try to help you . A comment from you I think that is a good way to solve your problem .
Best regards,
Nick
Any comments about our proposed solutions ? I put the first working solution on this topic and after me a lots of people try to help you . A comment from you I think that is a good way to solve your problem .
Best regards,
Nick
cvbn:
This old question needs to be finalized -- accept an answer, split points, or get a refund. For information on your options, please click here-> http:/help/closing.jsp#1
EXPERTS:
Post your closing recommendations! No comment means you don't care.
This old question needs to be finalized -- accept an answer, split points, or get a refund. For information on your options, please click here-> http:/help/closing.jsp#1
EXPERTS:
Post your closing recommendations! No comment means you don't care.
ginsonic first gave a working solution.
Thanks! ;)
yep, agree, ginsonic shoud get the grade ;-)
Yep, ginsonic was the first to answer correctly. All credits to ginsonic! However, PAQ the question, please. It's useful for future references. :-)
cvbn,
No comment has been added lately (15 days), so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area for this question:
RECOMMENDATION: Award points to ginsonic http:#7165454
Please leave any comments here within 7 days.
-- Please DO NOT accept this comment as an answer ! --
Thanks,
anAKiN
EE Cleanup Volunteer
No comment has been added lately (15 days), so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area for this question:
RECOMMENDATION: Award points to ginsonic http:#7165454
Please leave any comments here within 7 days.
-- Please DO NOT accept this comment as an answer ! --
Thanks,
anAKiN
EE Cleanup Volunteer
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
Panel1: TPanel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function HTML2Color(const HTML: String): Integer;
var
Offset: Integer;
begin
try
if Copy(HTML, 1, 1) = '#' then
Offset := 1
else
Offset := 0;
Result :=
Integer(StrToInt('$' + Copy(HTML, Offset + 1, 2))) +
Integer(StrToInt('$' + Copy(HTML, Offset + 3, 2))) shl 8 +
Integer(StrToInt('$' + Copy(HTML, Offset + 5, 2))) shl 16;
except
end;
end;
procedure TForm1.Button1Click(Sender
begin
Panel1.Color:=HTML2Color(E
end;
end.
If you type your hexadecimal values in Edit box ( dont use $ just for example FFFF00 ) and click the button then the panel will be colored with your HTML color.