Recent

Author Topic: Setting the color property  (Read 9713 times)

jack

  • Full Member
  • ***
  • Posts: 107
Setting the color property
« on: March 20, 2012, 07:29:17 pm »
I have an object, for example a label. 
In my database I have a char field holding the word red.

I want to use the value in the database to change the color property of the label object.

label1.Color:='red';  throws an error.
Error: Incompatible type for arg no. 1: Got "Constant String", expected "TGraphicsColor"

How do I make the transition from the word in the database to a TGraphicsColor?

label1.Color:=strToGraphicsColor('red'); is not the answer.


typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Setting the color property
« Reply #1 on: March 20, 2012, 07:31:10 pm »
Label1.Color := clRed;

Color is of type TColor.

jack

  • Full Member
  • ***
  • Posts: 107
Re: Setting the color property
« Reply #2 on: March 20, 2012, 07:58:14 pm »
And what datatype is clRed in my database?
This is where I get confused.
« Last Edit: March 20, 2012, 08:04:28 pm by jack »

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Setting the color property
« Reply #3 on: March 20, 2012, 08:18:57 pm »
According to the compiler error message, it is of type String.

jack

  • Full Member
  • ***
  • Posts: 107
Re: Setting the color property
« Reply #4 on: March 20, 2012, 08:23:08 pm »
And how do I get it converted from that string to the TColor?

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Setting the color property
« Reply #5 on: March 20, 2012, 08:30:23 pm »
Probably you need to write your own function and StrToGraphicsColor could be a good name for it.
« Last Edit: March 20, 2012, 08:34:11 pm by typo »

jack

  • Full Member
  • ***
  • Posts: 107
Re: Setting the color property
« Reply #6 on: March 20, 2012, 08:35:36 pm »
Thank you so very much for your help.


User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Setting the color property
« Reply #7 on: March 20, 2012, 09:05:35 pm »
TColor can be represented as DWORD type that is unsigned 32-bit integer. It supports all RGB colors 0..255 combinations. So depending what you need the colors for, simple string can't represent every color in the world.

jack

  • Full Member
  • ***
  • Posts: 107
Re: Setting the color property
« Reply #8 on: March 20, 2012, 09:16:55 pm »
Label1.Color := StrToInt('$' + '0000ff');                // Background
Label1.Font.Color := StrToInt('$' + 'FFFFFF');       // Text

This answer will do for now.

Thank you User137 for your explanation.
« Last Edit: March 20, 2012, 09:40:58 pm by jack »

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
Re: Setting the color property
« Reply #9 on: March 20, 2012, 10:18:26 pm »
Your code is equivalent to this faster one:

Label1.Color := $0000ff;                // Background
Label1.Font.Color := $FFFFFF;       // Text

 

TinyPortal © 2005-2018