Fabrizio
Fabrizio

Reputation: 8043

How does clDefault color work?

I noticed some unusual behaviors using clDefault color.

clDefault = TColor($20000000); //536870912

As an example (http://wiki.freepascal.org/Colors):

  • Using it for Brush will use the normal background brush of the target DC (device context).
  • On a Form's canvas a FillRect will paint a rectangular area filled with the normal background of a standard window. This is whatever the widgetset and theme defines. This might be the color gray or a gradient or a picture.

  • Using clDefault on the Canvas of a TListBox will paint with the normal background, which is normally white on Windows. So in a TListBox clDefault is the same as clWindow.

  • Using it as Pen color will use the default line color for the device context.

  • Using it as Font color will use the normal text color of the device context.

Could someone tell me how clDefault works?

I mean, is it interpreted by the OS or by the VCL code and how?

Upvotes: 1

Views: 775

Answers (1)

David Heffernan
David Heffernan

Reputation: 612824

You provide a link to FPC documentation, but then talk about the VCL and tag the question Delphi. I'm going to assume that you are asking about Delphi VCL and just ignore your linked documentation which does not apply.

In the VCL, for pens, fonts, and brushes, the default color is black. That's because $20000000 has 0 for R, G and B channels. The alpha channel is $20, but is ignored, certainly for methods like LineTo, FillRect, etc. This is quite different from the FPC documentation you linked to.

If you use clDefault for the Color of a TListBox, then it will again be black. Again, different from the FPC documentation you linked to.

Certainly the value $20000000 has no special meaning to GDI. For pens, fonts, brushes etc. the value is passed down to GDI which interprets it as black.

If you study the VCL source code you will find a variety of places where clDefault is used. In the main clDefault is used as a sentinel value. The VCL code checks whether or not a specified color property is equal to clDefault, and then takes special action if it is.

So far as I can tell, that's all there is to it. Some parts of the VCL treat clDefault as a sentinel, otherwise it is black.

Upvotes: 6

Related Questions