Solved

StringToColor conversion error

Posted on 2000-05-01
7
266 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

919 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

19 Experts available now in Live!

Get 1:1 Help Now