Solved

StringToColor conversion error

Posted on 2000-05-01
7
267 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
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

825 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