Solved

StringToColor conversion error

Posted on 2000-05-01
7
268 Views
Last Modified: 2010-04-04
StringToColor is returning wrong values, is there any solution to this?

ColorToRGB also did the same and I used the following function to correct it.

function TfrmPrincipal.BugColores(MyColor: integer): string;
begin
  MyColor := (MyColor and $FF) shl 16 + // Rojo
    (MyColor and $FF00) + // Verde
    (MyColor and $FF0000) shr 16; // Azul
  Result := IntToHex(MyColor, 6);
end;

thanks
0
Comment
Question by:pin_plunder
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 3

Expert Comment

by:bryan7
ID: 2767763
use StrToInt ..
a:= '15348745';
label1.colo:= strtoint(a);
0
 
LVL 3

Expert Comment

by:sburck
ID: 2768470
I tried to reproduce your problem, no luck.  For example:

ColorToString($FF0000) -> 'clBlue'
ColorToString($FF00) -> 'clLime'
ColorToString($FF) -> 'clRed'

Which shows R,G, and B in the places you stated.  Also

StringToColor('$ff00ff') -> $00ff00ff
StringToColor('$01234567') -> $01234567

are returning the values sent, which is what StrToInt does.

What version of Delphi are you using?
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 2768572
What makes you think you are getting the wrong values?

Cheers,

Raymond.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:pin_plunder
ID: 2779390
Ok, I'll explain everything from start to end. As I'm making a web page editor, I need to obtain the hex values from the color the user selects in the Color dialog.
But here's where all my problems start. To obtain the hex value of the selected colour I first need to convert the color value to an RGB value, and then convert that value to a hex one. For that I've been using this procedure. Which at a first look seems to work fine, but I'm afraid to say it doesn't. There are 2 errors.

procedure TForm1.Button1Click(Sender: TObject);
begin
if colordialog.Execute then begin
  label1.Color := colordialog.Color;
  label2.Caption := IntToStr(ColorToRGB(colordialog.color)) +
  '; ' + IntToHex(ColorToRGB(colordialog.color), 6);
end;
end;

              when color is blue:
correct hex value: 0000FF            hex value returned: FF0000

              when color is red:
correct hex value: FF0000            hex value returned: 0000FF

somebody here told me that was because the RGB conversion really returned BGR values, or something like that. (Look question here. http://www.experts-exchange.com/jsp/qShow.jsp?ta=delphi&qid=10298162 )
As a patch I decided to add the following procedure.

function TfrmPrincipal.BugColores(MyColor: integer): string;
begin
  MyColor := (MyColor and $FF) shl 16 + // Rojo
    (MyColor and $FF00) + // Verde
    (MyColor and $FF0000) shr 16; // Azul
  Result := IntToHex(MyColor, 6);
end;

Maybe there IS an easiest way to convert TColor values to hex values, so if you know it please tell me.

Well, everything worked fine since I added this procedure, but now I've got the hex values and need to return the TColor values. (just the other way round)
As simple as that.


just one final note. The following code, as simple as it may seem, uncovers a terrible bug, error or whatever you may want to call it.

procedure TForm1.Button1Click(Sender: TObject);
begin
if colordialog.execute then begin
label1.caption := IntToHex(ColorToRGB(colordialog.Color), 6);
label2.color := StringToColor('$' + label1.caption);
end;
end;

if you select the red color, the wrong value (0000FF) is returned and then when you convert that wrong value, instead of converting it to blue (cause that hex value belongs to the blue color), it colors label2 in red.
I realise that it's not really a bug, but didn't anybody at Inprise think that a stupid person like me would need to do a color conversion to hex values, or the other way round?

0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 2779418
Actually, this is not a bug, but rather some ambiguous documentation. ColorToRGB actually returns the three primary colours in the original BGR order, but zeros out the high order byte which is used to carry other state information (such as it being a system colour etc) about the color.

So in order to get the RGB order you want, you need to use your BugColores function all the time when dealing with a TColor.

Cheers,

Raymond.
0
 
LVL 3

Expert Comment

by:bryan7
ID: 2779516
to go way back just do like this..
Var col: integer; // color

col:= r + (g*256) + (b*65536)

r,g and b must be integer or byte values..

function HexToInt(HexStr : string) : cardinal;
begin
if HexStr[1]<>'$' then
 HexStr := '$' + HexStr;
Result := StrToInt(HexStr);
end;
0
 
LVL 12

Accepted Solution

by:
rwilson032697 earned 15 total points
ID: 2887177
Hmmm... <Lock>
0

Featured Post

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
HTML text in the body of an email (delphi code) 12 210
Delphi 2 77
Multiple image collision 13 88
Base1 Encode/Decode 3 86
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

756 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