StringToColor conversion error

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
pin_plunderAsked:
Who is Participating?
 
rwilson032697Connect With a Mentor Commented:
Hmmm... <Lock>
0
 
bryan7Commented:
use StrToInt ..
a:= '15348745';
label1.colo:= strtoint(a);
0
 
sburckCommented:
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
rwilson032697Commented:
What makes you think you are getting the wrong values?

Cheers,

Raymond.
0
 
pin_plunderAuthor Commented:
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
 
rwilson032697Commented:
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
 
bryan7Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.