Topic: Convert Color Constant

Hello Dmitry,
I'm trying to change a label color based on a color constant (clred, clblue etc).
If I hard code the color constant in a script, it works fine (for example form1.edit1.font.color := clred;).  however, if I pass the color constant through from a combobox,  it produces a runtime error.
If I select the hex equivalent of a color constant from a combobox it works fine (but users won't know which hex value relates to which color constant).
So the question is how to change a label color to a color constant that is passed from a combobox?  Any ideas?
Thanks,
Derek.

Post's attachments

Attachment icon labelcolor.zip 336.82 kb, 371 downloads since 2018-10-15 

Re: Convert Color Constant

Derek,
See attached. I know it is not exactly what you are looking for, but it is a workaround using both the hex value and the color dropdown. It also will work with the corresponding ascii value in place of the hex value.

Post's attachments

Attachment icon labelcolor_revised.zip 581.7 kb, 419 downloads since 2018-10-15 

Re: Convert Color Constant

Hello Derek and ehwagner

When I used Delphi 7 (it was long ago before Windows10), I wrote myself a procedure that displayed the colors of windows in a combobox and I clicked on the one that interested me to apply it.
The code was this one:

In the evenment Create of the form, I wrote :

With Combobox1.Items do
    begin
        Add(IntToStr(clBlack));
        Add(IntToStr(cl Blue)); and so on for each other colours.

and in the evenment OnDrawItem of the ComboBox1, I wrote :

Var  r : TRect;
Begin
   R := Rect;
  With Control as TComboBox,Canvas do
  begin
   FillRect(R);
  InflateRect(R,-2,-2);
  BrushColor := StrToInt(Items[Index));
  FillRect(R);
end;

Of course, I didn't use hexa value of the colors, but only 16 basic colors of Windows, but
here 'Je vous parle d'un  temps que les moins de 20 ans ne peuvent pas connaître'(1) as said the late Charles Aznavour, a french singer recently gone.

But as MVD has not implemented evenment OnDrawItem, It will probably be difficult to implement.

Regards to you

JB

(1) I'm talking about a time that people under 20 can not know

Re: Convert Color Constant

Hi EHW, JB,
Many thanks for both your responses.
As a work-around, your approach of using an sqlexecute to get the hex value by passing the color constant through from the combobox works well enough for what I need to do, so I'll run with that (although I'll put it into a variable and then just use the variable value as I've got about 20 labels that I need to change color on, so it will be quicker that way). 
I had just wondered if there wasn't a way of doing it DIRECTLY from the color constant (a 'tcolortoint'  command - or something of that sort  LOL!), but I guess not.
Jean - interesting bit of code which I'm trying to understand, but it's always helpful to see new things as they are always useful at some stage.
Regards,
Derek.

Re: Convert Color Constant

Derek,
I'm not aware of any such animal. It would be nice to have. The other option would be to use TColorDialog instead of a combobox. Just a thought.

Re: Convert Color Constant

Hello EHW,
Great - tcolordialog is a much better solution all round for me.  The user can now simply pick their own colours and my script can then work out the integer value for the colour - the user is never exposed to that now.  It's much slicker all round.
Many thanks,
Derek.

Post's attachments

Attachment icon labelcolor_revised2.zip 338.38 kb, 439 downloads since 2018-10-17