Solved

HTML TO COLOR

Posted on 2002-07-19
24
416 Views
Last Modified: 2010-04-04
HTML TO COLOR

How to convert HTML color to Delphi color ?

Thanks.
0
Comment
Question by:cvbn
  • 5
  • 4
  • 3
  • +9
24 Comments
 
LVL 9

Expert Comment

by:ginsonic
ID: 7165454
unit Unit1;

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: TObject);
begin
   Panel1.Color:=HTML2Color(Edit1.Text);
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.
0
 
LVL 9

Accepted Solution

by:
ginsonic earned 300 total points
ID: 7165467
Sorry, I write the sample project very fast.

Use this function:

function HTML2Color(const HTML: String): Integer;
var
  Offset: Integer;
begin
    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;
end;

Now work for any cases , if use or not $ symbol .
0
 
LVL 33

Expert Comment

by:Slick812
ID: 7165472
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');
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 7165899
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/Red. 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.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7166259
damn, my monitor is crashed, i was too close :-))
0
 
LVL 9

Expert Comment

by:ginsonic
ID: 7166266
Nose problems ? :)
0
 
LVL 2

Expert Comment

by:bugroger
ID: 7166382
Hi,

if you want to use StrToInt,
you must set a "$" at the beginning
of the string.
like StrToInt('$FF');

GL
Bug
0
 
LVL 22

Expert Comment

by:mnasman
ID: 7166918
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
0
 

Expert Comment

by:cdbsi
ID: 7182680
function HexToColor(Hex:string):TColor;
 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;
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 7182697
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?
0
 

Expert Comment

by:cdbsi
ID: 7184306
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.
0
 
LVL 17

Expert Comment

by:geobul
ID: 7184747
Hi all,

Another version:

function HTMLToColor(c: string): TColor;
begin
  if Length(c) = 7 then
    result := RGB(StrToInt('$'+Copy(c,2,2)),StrToInt('$'+Copy(c,4,2)),StrToInt('$'+Copy(c,6,2)))
  else
    result := RGB(StrToInt('$'+Copy(c,1,2)),StrToInt('$'+Copy(c,3,2)),StrToInt('$'+Copy(c,5,2)));
end;

// usage:
procedure TForm1.Button1Click(Sender: TObject);
begin
  Panel1.Color := HTMLToColor('$FF0000');
end;

Regards, Geo
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 17

Expert Comment

by:geobul
ID: 7184776
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),GetRValue(c));
end;

// usage:
procedure TForm1.Button1Click(Sender: TObject);
var
  col: string;
begin
  col := '#FF0000';
  Panel1.Color := HTMLToColor(col);
end;

Regards, Geo
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7184777
cdbsi,
i do just agree with Workshop_Alex,
let the questioner decide, which helps him/her most

meikl ;-)
0
 
LVL 17

Expert Comment

by:geobul
ID: 7184888
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
0
 
LVL 1

Expert Comment

by:kodiakbear
ID: 7188879
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.
http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp#14

kb
Experts Exchange Moderator
0
 
LVL 5

Expert Comment

by:Netminder
ID: 7191317
cdbsi,

Your account has been suspended pending your reply to Admin.

Netminder
CS Moderator
0
 
LVL 9

Expert Comment

by:ginsonic
ID: 7191537
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
0
 

Expert Comment

by:CleanupPing
ID: 9343031
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.
0
 
LVL 17

Expert Comment

by:geobul
ID: 9361487
ginsonic first gave a working solution.
0
 
LVL 9

Expert Comment

by:ginsonic
ID: 9363810
Thanks! ;)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9363889
yep, agree, ginsonic shoud get the grade ;-)
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 9365319
Yep, ginsonic was the first to answer correctly. All credits to ginsonic! However, PAQ the question, please. It's useful for future references. :-)
0
 
LVL 5

Expert Comment

by:Lukasz Lach
ID: 9461305
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
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now