• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 438
  • Last Modified:

HTML TO COLOR

HTML TO COLOR

How to convert HTML color to Delphi color ?

Thanks.
0
cvbn
Asked:
cvbn
  • 5
  • 4
  • 3
  • +9
1 Solution
 
ginsonicCommented:
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
 
ginsonicCommented:
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
 
Slick812Commented:
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Wim ten BrinkSelf-employed developerCommented:
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
 
kretzschmarCommented:
damn, my monitor is crashed, i was too close :-))
0
 
ginsonicCommented:
Nose problems ? :)
0
 
bugrogerCommented:
Hi,

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

GL
Bug
0
 
Mohammed NasmanSoftware DeveloperCommented:
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
 
cdbsiCommented:
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
 
Wim ten BrinkSelf-employed developerCommented:
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
 
cdbsiCommented:
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
 
geobulCommented:
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
 
geobulCommented:
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
 
kretzschmarCommented:
cdbsi,
i do just agree with Workshop_Alex,
let the questioner decide, which helps him/her most

meikl ;-)
0
 
geobulCommented:
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
 
kodiakbearCommented:
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
 
NetminderCommented:
cdbsi,

Your account has been suspended pending your reply to Admin.

Netminder
CS Moderator
0
 
ginsonicCommented:
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
 
CleanupPingCommented:
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
 
geobulCommented:
ginsonic first gave a working solution.
0
 
ginsonicCommented:
Thanks! ;)
0
 
kretzschmarCommented:
yep, agree, ginsonic shoud get the grade ;-)
0
 
Wim ten BrinkSelf-employed developerCommented:
Yep, ginsonic was the first to answer correctly. All credits to ginsonic! However, PAQ the question, please. It's useful for future references. :-)
0
 
Lukasz LachCommented:
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

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

  • 5
  • 4
  • 3
  • +9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now