Solved

StringToColor conversion error

Posted on 2000-05-01
7
265 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

760 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

22 Experts available now in Live!

Get 1:1 Help Now