Using Colours of Type TColor
Variables of the TColour type are stored in 32 bits. If the first bit is 1 then the colour is a system colour such as clBtnFace and the representation and routines are different from the non-system colours that we discuss below.
The preceding page gives examples of the use of several of the inbuilt colour values such as clRed, clFuchsia and clMagenta, but since eight bits are used to represent each of the red, green and blue components you need not restrict yourself to the pre-set named colours. The RGBToColor function takes as parameters the red, green and blue components of the required colour in the expected order. The order of storage of the bytes of a TColor is not intuitive, however, with the last byte being red. The following little console program illustrates the use of the functions given in the Free Pascal documentation and also shows you several ways to create a colour. See the documentation for tables of standard and system colours.
Note that a TColor does not have an alpha component so blending is not possible.
Code and Output of Console Program
program TColorCanvas; // LCL package added to project uses Graphics; var Colour: TColor; r, g, b: byte; procedure WriteColourComponents(C: TColor); begin writeln('Red: ', Red(C), ' Green: ', Green(C), ' Blue: ', Blue(C)); end; begin writeln('Instructions and output'); writeln('-----------------------'); Colour := clSkyBlue; writeln(#13#10'Colour := clSkyBlue;'); WriteColourComponents(Colour); Colour := RGBToColor(166, 202, 240); writeln(#13#10'Colour := RGBToColor(166, 202, 240);'); WriteColourComponents(Colour); Colour := $F0CAA6; writeln(#13#10'Colour := $F0CAA6;'); WriteColourComponents(Colour); Colour := TColor($F0CAA6); writeln(#13#10'Colour := TColor($F0CAA6);'); WriteColourComponents(Colour); Colour := $A6 + $CA * 256 + $F0 * 256 * 256; writeln(#13#10'Colour := $A6 + $CA * 256 + $F0 * 256 * 256;'); WriteColourComponents(Colour); Colour := 166 + 202 * 256 + 240 * 256 * 256; writeln(#13#10'Colour := 166 + 202 * 256 + 240 * 256 * 256;'); WriteColourComponents(Colour); RedGreenBlue(Colour, r, g, b); writeln(#13#10'RedGreenBlue(Colour, r, g, b);'); writeln('Red (value of variable r): ', r, ' Green (value of g): ', g, ' Blue (value of b): ', b); writeln(#13#10'Value of Colour shr 31: ', Colour shr 31); Colour := clBtnFace; writeln(#13#10'Colour := clBtnFace;'); writeln('Value of Colour shr 31: ', Colour shr 31); readln; end.
Instructions and output ----------------------- Colour := clSkyBlue; Red: 166 Green: 202 Blue: 240 Colour := RGBToColor(166, 202, 240); Red: 166 Green: 202 Blue: 240 Colour := $F0CAA6; Red: 166 Green: 202 Blue: 240 Colour := TColor($F0CAA6); Red: 166 Green: 202 Blue: 240 Colour := $A6 + $CA * 256 + $F0 * 256 * 256; Red: 166 Green: 202 Blue: 240 Colour := 166 + 202 * 256 + 240 * 256 * 256; Red: 166 Green: 202 Blue: 240 RedGreenBlue(Colour, r, g, b); Red (value of variable r): 166 Green (value of g): 202 Blue (value of b): 240 Value of Colour shr 31: 0 Colour := clBtnFace; Value of Colour shr 31: 1