* * *

Author Topic: How to define color constants?  (Read 639 times)

stem

  • New member
  • *
  • Posts: 34
How to define color constants?
« on: June 19, 2018, 07:51:10 pm »
Hi,

I want to have color constants in a separate unit. The line

Code: Pascal  [Select]
  1. MYCOLOR = TColor($FF0000);

gets compiled, but using the color in

Code: Pascal  [Select]
  1. xyz.Canvas.Pen.Color := MYCOLOR;

results in having the color blue, although I wanted to have red. What am I doing wrong?

Thank you!


stem

wp

  • Hero Member
  • *****
  • Posts: 4726
Re: How to define color constants?
« Reply #1 on: June 19, 2018, 07:55:25 pm »
TColor has the oppositve byte order of HTML: TColor = $ssBBGGRR (ss used for system colors, is zero for "normal" colors)), HTML = #RRGGBB.
« Last Edit: June 19, 2018, 08:02:32 pm by wp »
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

Thaddy

  • Hero Member
  • *****
  • Posts: 6330
Re: How to define color constants?
« Reply #2 on: June 19, 2018, 07:55:42 pm »
Code: Text  [Select]
  1.  const MYCOLOR:TColor = $FF0000;
I am enjoining wine, not whine....

Thaddy

  • Hero Member
  • *****
  • Posts: 6330
Re: How to define color constants?
« Reply #3 on: June 19, 2018, 08:00:10 pm »
TColor has the oppositve byte order of HTML: TColor = $ssBBGGRR (ss used for system colors), HTML = #RRGGBB.
Unless you read something more than I did (html): his declaration is wrong in the first place. This gets converted back to native resolution, so the cast is useless.
I am enjoining wine, not whine....

Handoko

  • Hero Member
  • *****
  • Posts: 2378
  • My goal: build my own game engine using Lazarus
Re: How to define color constants?
« Reply #4 on: June 19, 2018, 08:00:51 pm »
@stem

You can use the constants available in unit Graphics. clRed is TColor($0000FF) on my Linux system, on certain OS/hardware it may be different.

Here I copied/pasted some part of the code in unit Graphics:
Code: Pascal  [Select]
  1. const
  2.   // The following colors match the predefined Delphi Colors
  3.  
  4.   // standard colors
  5.   clBlack   = TColor($000000);
  6.   clMaroon  = TColor($000080);
  7.   clGreen   = TColor($008000);
  8.   clOlive   = TColor($008080);
  9.   clNavy    = TColor($800000);
  10.   clPurple  = TColor($800080);
  11.   clTeal    = TColor($808000);
  12.   clGray    = TColor($808080);
  13.   clSilver  = TColor($C0C0C0);
  14.   clRed     = TColor($0000FF);
  15.   clLime    = TColor($00FF00);
  16.   clYellow  = TColor($00FFFF);
  17.   clBlue    = TColor($FF0000);
  18.   clFuchsia = TColor($FF00FF);
  19.   clAqua    = TColor($FFFF00);
  20.   clLtGray  = TColor($C0C0C0); // clSilver alias
  21.   clDkGray  = TColor($808080); // clGray alias
  22.   clWhite   = TColor($FFFFFF);
  23.   StandardColorsCount = 16;
  24.  
  25.   // extended colors
  26.   clMoneyGreen = TColor($C0DCC0);
  27.   clSkyBlue    = TColor($F0CAA6);
  28.   clCream      = TColor($F0FBFF);
  29.   clMedGray    = TColor($A4A0A0);
  30.   ExtendedColorCount = 4;
  31.  
  32.   // special colors
  33.   clNone    = TColor($1FFFFFFF);
  34.   clDefault = TColor($20000000);
  35.  
  36.   // system colors
  37.   clScrollBar               = TColor(SYS_COLOR_BASE or COLOR_SCROLLBAR);
  38.   clBackground              = TColor(SYS_COLOR_BASE or COLOR_BACKGROUND);
  39.   clActiveCaption           = TColor(SYS_COLOR_BASE or COLOR_ACTIVECAPTION);
  40.   clInactiveCaption         = TColor(SYS_COLOR_BASE or COLOR_INACTIVECAPTION);
  41.   clMenu                    = TColor(SYS_COLOR_BASE or COLOR_MENU);
  42.   clWindow                  = TColor(SYS_COLOR_BASE or COLOR_WINDOW);
  43.   clWindowFrame             = TColor(SYS_COLOR_BASE or COLOR_WINDOWFRAME);
  44.   clMenuText                = TColor(SYS_COLOR_BASE or COLOR_MENUTEXT);
  45.   clWindowText              = TColor(SYS_COLOR_BASE or COLOR_WINDOWTEXT);
  46.   clCaptionText             = TColor(SYS_COLOR_BASE or COLOR_CAPTIONTEXT);
  47.   clActiveBorder            = TColor(SYS_COLOR_BASE or COLOR_ACTIVEBORDER);
  48.   clInactiveBorder          = TColor(SYS_COLOR_BASE or COLOR_INACTIVEBORDER);
  49.   clAppWorkspace            = TColor(SYS_COLOR_BASE or COLOR_APPWORKSPACE);
  50.   clHighlight               = TColor(SYS_COLOR_BASE or COLOR_HIGHLIGHT);
  51.   clHighlightText           = TColor(SYS_COLOR_BASE or COLOR_HIGHLIGHTTEXT);
  52.   clBtnFace                 = TColor(SYS_COLOR_BASE or COLOR_BTNFACE);
  53.   clBtnShadow               = TColor(SYS_COLOR_BASE or COLOR_BTNSHADOW);
  54.   clGrayText                = TColor(SYS_COLOR_BASE or COLOR_GRAYTEXT);
  55.   clBtnText                 = TColor(SYS_COLOR_BASE or COLOR_BTNTEXT);
  56.   clInactiveCaptionText     = TColor(SYS_COLOR_BASE or COLOR_INACTIVECAPTIONTEXT);
  57.   clBtnHighlight            = TColor(SYS_COLOR_BASE or COLOR_BTNHIGHLIGHT);
  58.   cl3DDkShadow              = TColor(SYS_COLOR_BASE or COLOR_3DDKSHADOW);
  59.   cl3DLight                 = TColor(SYS_COLOR_BASE or COLOR_3DLIGHT);
  60.   clInfoText                = TColor(SYS_COLOR_BASE or COLOR_INFOTEXT);
  61.   clInfoBk                  = TColor(SYS_COLOR_BASE or COLOR_INFOBK);
  62.  
  63.   clHotLight                = TColor(SYS_COLOR_BASE or COLOR_HOTLIGHT);
  64.   clGradientActiveCaption   = TColor(SYS_COLOR_BASE or COLOR_GRADIENTACTIVECAPTION);
  65.   clGradientInactiveCaption = TColor(SYS_COLOR_BASE or COLOR_GRADIENTINACTIVECAPTION);
  66.   clMenuHighlight           = TColor(SYS_COLOR_BASE or COLOR_MENUHILIGHT);
  67.   clMenuBar                 = TColor(SYS_COLOR_BASE or COLOR_MENUBAR);
  68.   clForm                    = TColor(SYS_COLOR_BASE or COLOR_FORM);
  69.  
  70.   // synonyms: do not show them in color lists
  71.   clColorDesktop            = TColor(SYS_COLOR_BASE or COLOR_DESKTOP);
  72.   cl3DFace                  = TColor(SYS_COLOR_BASE or COLOR_3DFACE);
  73.   cl3DShadow                = TColor(SYS_COLOR_BASE or COLOR_3DSHADOW);
  74.   cl3DHiLight               = TColor(SYS_COLOR_BASE or COLOR_3DHIGHLIGHT);
  75.   clBtnHiLight              = TColor(SYS_COLOR_BASE or COLOR_BTNHILIGHT);
« Last Edit: June 19, 2018, 08:08:21 pm by Handoko »

Thaddy

  • Hero Member
  • *****
  • Posts: 6330
Re: How to define color constants?
« Reply #5 on: June 19, 2018, 08:05:50 pm »
const or vars? If they are not declared as I showed those are bugs.
I am enjoining wine, not whine....

Handoko

  • Hero Member
  • *****
  • Posts: 2378
  • My goal: build my own game engine using Lazarus
Re: How to define color constants?
« Reply #6 on: June 19, 2018, 08:09:10 pm »
clRed is declared as a const in unit Graphics.

wp

  • Hero Member
  • *****
  • Posts: 4726
Re: How to define color constants?
« Reply #7 on: June 19, 2018, 08:10:57 pm »
his declaration is wrong in the first place.
His declaration is correct (see the declaration of the clXXX constants in unit graphics). What's wrong is that he is writing the bytes in the wrong order. In TColor the red-green-blue bytes must be read from right to left - unlike HTML where they are read from left to right.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

wp

  • Hero Member
  • *****
  • Posts: 4726
Re: How to define color constants?
« Reply #8 on: June 19, 2018, 08:12:47 pm »
const or vars?
Hey come on Thaddy - an experienced guy like you knows that a var cannot be declared without type.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

stem

  • New member
  • *
  • Posts: 34
Re: How to define color constants?
« Reply #9 on: June 19, 2018, 08:44:35 pm »
Thank you!  :)

valdir.marcos

  • Sr. Member
  • ****
  • Posts: 394
Re: How to define color constants?
« Reply #10 on: June 22, 2018, 05:26:07 am »
const or vars? If they are not declared as I showed those are bugs.

his declaration is wrong in the first place.
His declaration is correct (see the declaration of the clXXX constants in unit graphics). What's wrong is that he is writing the bytes in the wrong order. In TColor the red-green-blue bytes must be read from right to left - unlike HTML where they are read from left to right.

const or vars?
Hey come on Thaddy - an experienced guy like you knows that a var cannot be declared without type.

Good to see that Thaddy can make mistakes as any of us!... :) O:-)

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus