Colors/fr

From Lazarus wiki
Jump to navigationJump to search

Deutsch (de) English (en) español (es) suomi (fi) français (fr) 日本語 (ja) русский (ru) 中文(中国大陆)‎ (zh_CN)


Vue d'ensemble

Dans la LCL, TColor est le type couleur standard. Il est compatible avec le TColor de Delphi. TColor peut représenter soit une valeur RGB (3x8bit), soit une couleur système comme crDefault. La LCL peut aussi travailler avec le système fpImage qui emploit le type TFPColor qui est du RGBA (4x16bit), et non du RGB (3x8bit comme TColor).

Conversions entre TColor et valeurs RGB

L'unité Graphics fournit les fonctions suivantes :

function Blue(rgb: TColor): BYTE; // ne marche pas avec les couleurs système
function Green(rgb: TColor): BYTE; // ne marche pas avec les couleurs système
function Red(rgb: TColor): BYTE; // ne marche pas avec les couleurs système
function RGBToColor(R, G, B: Byte): TColor;
procedure RedGreenBlue(rgb: TColor; out Red, Green, Blue: Byte); // ne marche pas avec les couleurs système
function FPColorToTColor(const FPColor: TFPColor): TColor;
function TColorToFPColor(const c: TColor): TFPColor; // ne marche pas avec les couleurs système
function IsSysColor(AColor: TColorRef): Boolean;

Convertir TColor vers/depuis une string

Fonctions pour convertir des chaînes comme "25500" ou "$AA0088" ou "clNavy" vers le type TColor:

  • StringToColor
  • StringToColorDef

Pour convertir TColor bvers une jolie chaîne:

  • ColorToString

Table des couleurs standards

Environ 20 constantes de couleur prédéfinies sont fournies, elles sont compatible avec Delphi:


Constante Colour Signification Valeur héxadecimale pour emploi avec TColor Exemple
clBlack Noir TColor($000000);
clMaroon Marron TColor($000080);
clGreen Vert TColor($008000);
clOlive Vert Olive TColor($008080);
clNavy Bleu marine TColor($800000);
clPurple Violet TColor($800080);
clTeal Bleu canard TColor($808000);
clGray Gris TColor($808080);
clSilver Argent TColor($C0C0C0);
clRed Rouge TColor($0000FF);
clLime Vert Citron TColor($00FF00);
clYellow Jaune TColor($00FFFF);
clBlue Bleu TColor($FF0000);
clFuchsia Fuchsia TColor($FF00FF);
clAqua Bleu vert TColor($FFFF00);
clLtGray Gris clair TColor($C0C0C0); clSilver alias
clDkGray Gris foncé TColor($808080); clGray alias
clWhite Blanc TColor($FFFFFF);
clCream Crème TColor($F0FBFF); Lazarus 1.2 et plus récent
clMedGray Gris Moyen TColor($A4A0A0); Lazarus 1.2 et plus récent
clMoneyGreen Vert menthe TColor($C0DCC0); Lazarus 1.2 et plus récent
clSkyBlue Bleu ciel TColor($F0CAA6); Lazarus 1.2 et plus récent

Couleurs System

Exemple: clInfoBk, clInfoText

Les couleurs système sont des constantes avec une signification particulière. Leur valeur réelle dépend du contexte et du thème. Ce ne sont pas de simples couleurs. Par exemple clInfoBk:

Form1.Canvas.Brush.Color:=clInfoBk;  // use the default background brush of a hint window
Form1.Canvas.FillRect(10,10,50,50);

Une bulle d'aide dans MS Windows a un fond blanc donc cela va dessiner en blanc. Dans Linux/gtk2, cela peut être une texture métallique, donc le code du dessus va dessiner la texture. Si vous voulez placer du texte dessus vous avez besoin d'une couleur correspondante comme clInfoText, sinon votre texte sera illisible pour l'utilisateur. Par exemple:

Form1.Canvas.Brush.Color:=clInfoBk;  // utilise le fond par défaut d'une bulle d'aide
Form1.Canvas.FillRect(10,10,50,50);
Form1.Canvas.Font.Color:=clInfoText;  // utilise la couleur du ttexte par défaut d'une bulle d'aide
Form1.Canvas.TextOut(10,10,'Hint');

La couleur système clInfoBk ne peut pas être utilisée avec Pen.Color ni pour Font.Color. Si vous faites ainsi, le résultat est indéfini et dépend du widgetset et du thème de l'utilisateur.

Pareil pour clInfoText: il ne peut être utilisé que comme une Font.Color. L'utiliser comme Brush.Color peut ne pas marcher. Actuellement, tous les Widgetsets permettent de l'utiliser comme Pen.Color aussi.

Theme changes

When the user switches the theme the system colors changes. A clInfoBk might change from white to blue or from a color to a texture. This change will happen when you allocate a new Brush handle. Keep in mind that a simple assignment Brush.Color:=clInfoBk does not allocate a Brush Handle. The Brush Handle is allocated on use. For example:

Form1.Canvas.Brush.Color:=clInfoBk; // this will not create a new brush handle
Form1.Canvas.FillRect(10,10,50,50); // this will create the brush handle with the currently active theme brush for hint windows
...
// if the theme changes in this moment the Brush.Handle is still allocated with the old values
...
Form1.Canvas.FillRect(10,10,50,50); // this will paint with the old theme brush
Form1.Canvas.Brush.Color:=clInfoBk; // assigning the old value will not create a new brush handle
Form1.Canvas.FillRect(10,10,50,50); // this will paint with the old theme brush
Form1.Canvas.Brush.Color:=clRed;    // assigning a new color, old Handle invalid
Form1.Canvas.Brush.Color:=clInfoBk; // assigning a new color, old Handle invalid
Form1.Canvas.FillRect(10,10,50,50); // this will create a new handle and paint with the new theme

Table of system colors

The following table lists the system colors and their meaning. Using them outside the scope of the definition is undefined and the result depends on the widgetset and theme. For example clDefault is the normal background brush of the used device context. If you want to paint button elements on your own custom controls use the drawing functions of the unit Themes.

Constant LCL definition Delphi notes Supported Widgetsets
clNone Indicates "do not paint". Using it as Control's color is undefined. The control will not get transparent. - all
clDefault 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.
- all
clScrollBar Scrollbar body - all
clBackground ? - all
clActiveCaption Active window titlebar - none
clInactiveCaption Inactive window titlebar - none
clMenu Regular menu item background color - none
clWindow The normal background brush of unselected text. Defined for controls like TEdit, TComboBox, TMemo, TListBox, TTreeView. - none
clWindowFrame Color of frame around the window - none
clMenuText The font color to use together with clMenu - none
clWindowText Font color to use together with clWindow - none
clCaptionText Active window titlebar text color - none
clActiveBorder ? - none
clInactiveBorder ? - none
clAppWorkspace MDIMain form background - none
clHighlight The brush color of selected element - none
clHighlightText Font color of selected text (to use together with clHighligh). - none
clBtnFace Button background - none
clBtnShadow Button shadow color (bottom right) used to achieve 3D effect - none
clGrayText The font color of disabled element - none
clBtnText Button font color to use together with clBtnFace - none
clInactiveCaptionText Inactive window titlebar text color - none
clBtnHighlight Button highlight color (top left) used to achieve 3D effect - none
cl3DDkShadow ? - none
cl3DLight ? - none
clInfoText Font color for hints. Use together with clInfoBk - all
clInfoBk Brush color for hints. Use together with clInfoText - all
clHotLight ? - none
clGradientActiveCaption The second color used to make gradient of active window titlebar - none
clGradientInactiveCaption The second color used to make gradient for inactive window titlebar - none
clMenuHighlight The background color of selected menu item - none
clMenuBar The Backround color of menu bar - none
clForm ? - none
clColorDesktop ? - none
cl3DFace ? - none
cl3DShadow ? - none
cl3DHiLight ? - none
clBtnHiLight Same as clBtnHighlight - none

Drawing theme elements on your custom controls

The unit Themes provides functions to draw single elements of standard controls. For example to draw an expand sign like a TTreeView use the following code:

uses Themes;

...

procedure TYourCustomControl.Paint;
const
  PlusMinusDetail: array[Boolean {Hot}, Boolean {Expanded}] of TThemedTreeview =
  (
    (ttGlyphClosed, ttGlyphOpened),
    (ttHotGlyphClosed, ttHotGlyphOpened)
  );
var
  Details: TThemedElementDetails;
  R: TRect;
  Collapse: boolean;
begin
  ...
  //draw a themed expand sign.
  Details := ThemeServices.GetElementDetails(PlusMinusDetail[False, Collapse]);
  R := Rect(ALeft, ATop, ARight + 1, ABottom + 1);
  ThemeServices.DrawElement(Canvas.Handle, Details, R, nil);
  ...
end;