Sign in Join
Wiki copytemporary

Colors/zh CN: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: Colors/zh CN

OldNewLine
1+\
2+\
3+=概述=
4+
5+在[[LCL]],TColor是标准颜色类型。它和[[Delphi]]的TColor兼容。TColor 可以表示*要么*一个RGB (3x8比特)值,*要么*一个**system**颜色,像**clDefault**。LCL也可以和fpImage系统一起工作,使用TFPColor类型(它是RGBA(4x16比特),非 RGB(3x8比特),像TColor)。
6+
7+## 在TColor 和RGB值之间转换
8+
9+**Graphics**单元提供提供下面的函数:
10+
11+``` pascal
12+function Blue(rgb: TColor): BYTE; // does not work on system color
13+function Green(rgb: TColor): BYTE; // does not work on system color
14+function Red(rgb: TColor): BYTE; // does not work on system color
15+function RGBToColor(R, G, B: Byte): TColor;
16+procedure RedGreenBlue(rgb: TColor; out Red, Green, Blue: Byte); // does not work on system color
17+function FPColorToTColor(const FPColor: TFPColor): TColor;
18+function TColorToFPColor(const c: TColor): TFPColor; // does not work on system color
19+function IsSysColor(AColor: TColorRef): Boolean;
20+```
21+
22+## 转换TColor到/来自字符串
23+
24+函数来转换字符串像"25500"或"\$AA0088"或"clNavy"到TColor:
25+
26+- StringToColor
27+- StringToColorDef
28+
29+转换TColor到一个友好的字符串像"clNavy"或"\$AA0002":
30+
31+- ColorToString
32+
33+## 转换TColor到/来自HTML字符串 \#rrggbb
34+
35+查看代码在[[转换颜色到/来自HTML]]。
36+
37+# 标准颜色表
38+
39+大约20种预定义颜色常量被提供,它们是与Delphi-兼容的:
40+
41+| 颜色常量 | 意思 | TColor使用[[十六进制]]值 | 示例 |
42+|----|----|----|----|
43+| clBlack | 黑色 | TColor(\$000000); | |
44+| clMaroon | 褐红色 | TColor(\$000080); | |
45+| clGreen | 绿色 | TColor(\$008000); | |
46+| clOlive | 橄榄绿 | TColor(\$008080); | |
47+| clNavy | 深蓝色 | TColor(\$800000); | |
48+| clPurple | 紫色 | TColor(\$800080); | |
49+| clTeal | 蓝绿色 | TColor(\$808000); | |
50+| clGray | 灰色 | TColor(\$808080); | |
51+| clSilver | 银色 | TColor(\$C0C0C0); | |
52+| clRed | 红色 | TColor(\$0000FF); | |
53+| clLime | 酸橙绿 | TColor(\$00FF00); | |
54+| clYellow | 黄色 | TColor(\$00FFFF); | |
55+| clBlue | 蓝色 | TColor(\$FF0000); | |
56+| clFuchsia | 紫红色 | TColor(\$FF00FF); | |
57+| clAqua | 湖绿色 | TColor(\$FFFF00); | |
58+| clLtGray | 浅灰色 | TColor(\$C0C0C0); | clSilver别名 |
59+| clDkGray | 深灰色 | TColor(\$808080); | clGray别名 |
60+| clWhite | 白色 | TColor(\$FFFFFF); | |
61+| clCream | 奶油色 | TColor(\$F0FBFF); | Lazarus 1.2和较新的 |
62+| clMedGray | 中灰色 | TColor(\$A4A0A0); | Lazarus 1.2和较新的 |
63+| clMoneyGreen | 薄荷绿 | TColor(\$C0DCC0); | Lazarus 1.2和较新的 |
64+| clSkyBlue | 天蓝色 | TColor(\$F0CAA6); | LLazarus 1.2和较新的 |
65+
66+# System colors
67+
68+## Example: clInfoBk, clInfoText
69+
70+System colors are color constants with a special meaning. Their real value depends on the context and theme. They are not simple colors. For example **clInfoBk**:
71+
72+``` pascal
73+Form1.Canvas.Brush.Color:=clInfoBk; // use the default background brush of a hint window
74+Form1.Canvas.FillRect(10,10,50,50);
75+```
76+
77+A hint window on MS Windows might have a white background so the above will draw white. On Linux/gtk2 it might be a metallic texture, so the above will draw the texture. If you want to put some text onto this you need a corresponding color like **clInfoText**, otherwise your text might be unreadable for the user. For example:
78+
79+``` pascal
80+Form1.Canvas.Brush.Color:=clInfoBk; // use the default background brush of a hint window
81+Form1.Canvas.FillRect(10,10,50,50);
82+Form1.Canvas.Font.Color:=clInfoText; // use the default text color of a hint window
83+Form1.Canvas.TextOut(10,10,'Hint');
84+```
85+
86+The system color **clInfoBk** can not be used for Pen.Color and not for Font.Color. If you do so the result is undefined and depends on the widgetset and user theme. The same for **clInfoText**: It can only be used as a Font.Color. Using it as Brush.Color may not work. At the moment all widgetsets allow to use it as Pen.Color too.
87+
88+## Theme changes
89+
90+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:
91+
92+``` pascal
93+Form1.Canvas.Brush.Color:=clInfoBk; // this will not create a new brush handle
94+Form1.Canvas.FillRect(10,10,50,50); // this will create the brush handle with the currently active theme brush for hint windows
95+...
96+// if the theme changes in this moment the Brush.Handle is still allocated with the old values
97+...
98+Form1.Canvas.FillRect(10,10,50,50); // this will paint with the old theme brush
99+Form1.Canvas.Brush.Color:=clInfoBk; // assigning the old value will not create a new brush handle
100+Form1.Canvas.FillRect(10,10,50,50); // this will paint with the old theme brush
101+Form1.Canvas.Brush.Color:=clRed; // assigning a new color, old Handle invalid
102+Form1.Canvas.Brush.Color:=clInfoBk; // assigning a new color, old Handle invalid
103+Form1.Canvas.FillRect(10,10,50,50); // this will create a new handle and paint with the new theme
104+```
105+
106+## Table of system colors
107+
108+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**.
109+
110+<table>
111+<thead>
112+<tr>
113+<th><p>Constant</p></th>
114+<th><p>LCL definition</p></th>
115+<th><p>Delphi notes</p></th>
116+<th><p>Supported Widgetsets</p></th>
117+</tr>
118+</thead>
119+<tbody>
120+<tr>
121+<td><p>clNone</p></td>
122+<td><p>Indicates "do not paint". Using it as Control's color is undefined. The control will not get transparent.</p></td>
123+<td><p>-</p></td>
124+<td><p>all</p></td>
125+</tr>
126+<tr>
127+<td><p>clDefault</p></td>
128+<td><p>Using it for Brush will use the normal background brush of the target DC (device context).</p>
129+<ul>
130+<li>On a Form's canvas a <em>FillRect</em> 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.</li>
131+<li>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.</li>
132+<li>Using it as Pen color will use the default line color for the device context.</li>
133+<li>Using it as Font color will use the normal text color of the device context.</li>
134+</ul></td>
135+<td><p>| -</p></td>
136+<td><p>all</p></td>
137+</tr>
138+<tr>
139+<td><p>clScrollBar</p></td>
140+<td><p>Scrollbar body</p></td>
141+<td><p>-</p></td>
142+<td><p>all</p></td>
143+</tr>
144+<tr>
145+<td><p>clBackground</p></td>
146+<td><p>Desktop background color</p></td>
147+<td><p>-</p></td>
148+<td><p>all</p></td>
149+</tr>
150+<tr>
151+<td><p>clActiveCaption</p></td>
152+<td><p>Active window titlebar</p></td>
153+<td><p>-</p></td>
154+<td><p>none</p></td>
155+</tr>
156+<tr>
157+<td><p>clInactiveCaption</p></td>
158+<td><p>Inactive window titlebar</p></td>
159+<td><p>-</p></td>
160+<td><p>none</p></td>
161+</tr>
162+<tr>
163+<td><p>clMenu</p></td>
164+<td><p>Regular menu item background color</p></td>
165+<td><p>-</p></td>
166+<td><p>none</p></td>
167+</tr>
168+<tr>
169+<td><p>clWindow</p></td>
170+<td><p>The normal background brush of unselected text. Defined for controls like TEdit, TComboBox, TMemo, TListBox, TTreeView.</p></td>
171+<td><p>-</p></td>
172+<td><p>none</p></td>
173+</tr>
174+<tr>
175+<td><p>clWindowFrame</p></td>
176+<td><p>Color of frame around the window</p></td>
177+<td><p>-</p></td>
178+<td><p>none</p></td>
179+</tr>
180+<tr>
181+<td><p>clMenuText</p></td>
182+<td><p>The font color to use together with clMenu</p></td>
183+<td><p>-</p></td>
184+<td><p>none</p></td>
185+</tr>
186+<tr>
187+<td><p>clWindowText</p></td>
188+<td><p>Font color to use together with clWindow</p></td>
189+<td><p>-</p></td>
190+<td><p>none</p></td>
191+</tr>
192+<tr>
193+<td><p>clCaptionText</p></td>
194+<td><p>Active window titlebar text color</p></td>
195+<td><p>-</p></td>
196+<td><p>none</p></td>
197+</tr>
198+<tr>
199+<td><p>clActiveBorder</p></td>
200+<td><p>?</p></td>
201+<td><p>-</p></td>
202+<td><p>none</p></td>
203+</tr>
204+<tr>
205+<td><p>clInactiveBorder</p></td>
206+<td><p>?</p></td>
207+<td><p>-</p></td>
208+<td><p>none</p></td>
209+</tr>
210+<tr>
211+<td><p>clAppWorkspace</p></td>
212+<td><p>MDIMain form background</p></td>
213+<td><p>-</p></td>
214+<td><p>none</p></td>
215+</tr>
216+<tr>
217+<td><p>clHighlight</p></td>
218+<td><p>The brush color of selected element</p></td>
219+<td><p>-</p></td>
220+<td><p>none</p></td>
221+</tr>
222+<tr>
223+<td><p>clHighlightText</p></td>
224+<td><p>Font color of selected text (to use together with clHighligh).</p></td>
225+<td><p>-</p></td>
226+<td><p>none</p></td>
227+</tr>
228+<tr>
229+<td><p>clBtnFace</p></td>
230+<td><p>Button background</p></td>
231+<td><p>-</p></td>
232+<td><p>none</p></td>
233+</tr>
234+<tr>
235+<td><p>clBtnShadow</p></td>
236+<td><p>Button shadow color (bottom right) used to achieve 3D effect</p></td>
237+<td><p>-</p></td>
238+<td><p>none</p></td>
239+</tr>
240+<tr>
241+<td><p>clGrayText</p></td>
242+<td><p>The font color of disabled element</p></td>
243+<td><p>-</p></td>
244+<td><p>none</p></td>
245+</tr>
246+<tr>
247+<td><p>clBtnText</p></td>
248+<td><p>Button font color to use together with clBtnFace</p></td>
249+<td><p>-</p></td>
250+<td><p>none</p></td>
251+</tr>
252+<tr>
253+<td><p>clInactiveCaptionText</p></td>
254+<td><p>Inactive window titlebar text color</p></td>
255+<td><p>-</p></td>
256+<td><p>none</p></td>
257+</tr>
258+<tr>
259+<td><p>clBtnHighlight</p></td>
260+<td><p>Button highlight color (top left) used to achieve 3D effect</p></td>
261+<td><p>-</p></td>
262+<td><p>none</p></td>
263+</tr>
264+<tr>
265+<td><p>cl3DDkShadow</p></td>
266+<td><p>?</p></td>
267+<td><p>-</p></td>
268+<td><p>none</p></td>
269+</tr>
270+<tr>
271+<td><p>cl3DLight</p></td>
272+<td><p>?</p></td>
273+<td><p>-</p></td>
274+<td><p>none</p></td>
275+</tr>
276+<tr>
277+<td><p>clInfoText</p></td>
278+<td><p>Font color for hints. Use together with clInfoBk</p></td>
279+<td><p>-</p></td>
280+<td><p>all</p></td>
281+</tr>
282+<tr>
283+<td><p>clInfoBk</p></td>
284+<td><p>Brush color for hints. Use together with clInfoText</p></td>
285+<td><p>-</p></td>
286+<td><p>all</p></td>
287+</tr>
288+<tr>
289+<td><p>clHotLight</p></td>
290+<td><p>?</p></td>
291+<td><p>-</p></td>
292+<td><p>none</p></td>
293+</tr>
294+<tr>
295+<td><p>clGradientActiveCaption</p></td>
296+<td><p>The second color used to make gradient of active window titlebar</p></td>
297+<td><p>-</p></td>
298+<td><p>none</p></td>
299+</tr>
300+<tr>
301+<td><p>clGradientInactiveCaption</p></td>
302+<td><p>The second color used to make gradient for inactive window titlebar</p></td>
303+<td><p>-</p></td>
304+<td><p>none</p></td>
305+</tr>
306+<tr>
307+<td><p>clMenuHighlight</p></td>
308+<td><p>The background color of selected menu item</p></td>
309+<td><p>-</p></td>
310+<td><p>none</p></td>
311+</tr>
312+<tr>
313+<td><p>clMenuBar</p></td>
314+<td><p>The Backround color of menu bar</p></td>
315+<td><p>-</p></td>
316+<td><p>none</p></td>
317+</tr>
318+<tr>
319+<td><p>clForm</p></td>
320+<td><p>?</p></td>
321+<td><p>-</p></td>
322+<td><p>none</p></td>
323+</tr>
324+<tr>
325+<td><p>clColorDesktop</p></td>
326+<td><p>?</p></td>
327+<td><p>-</p></td>
328+<td><p>none</p></td>
329+</tr>
330+<tr>
331+<td><p>cl3DFace</p></td>
332+<td><p>?</p></td>
333+<td><p>-</p></td>
334+<td><p>none</p></td>
335+</tr>
336+<tr>
337+<td><p>cl3DShadow</p></td>
338+<td><p>?</p></td>
339+<td><p>-</p></td>
340+<td><p>none</p></td>
341+</tr>
342+<tr>
343+<td><p>cl3DHiLight</p></td>
344+<td><p>?</p></td>
345+<td><p>-</p></td>
346+<td><p>none</p></td>
347+</tr>
348+<tr>
349+<td><p>clBtnHiLight</p></td>
350+<td><p>Same as clBtnHighlight</p></td>
351+<td><p>-</p></td>
352+<td><p>none</p></td>
353+</tr>
354+</tbody>
355+</table>
356+
357+## Finding the rgb values of a system color
358+
359+Use the function `ColorToRGB` (in unit `Graphics` to determine the rgb components of a system color. This functions detects whether a color is a system color and, if this is true, looks up the system color in the themes color. The returned [[Longint]] can be understood as a normal color:
360+
361+``` pascal
362+// This example has a TColorBox on a form and uses this OnChange handler for the TColorbox:
363+procedure TForm1.ColorBox1Change(Sender: TObject);
364+var
365+ c: TColor;
366+begin
367+ c := ColorToRGB(ColorBox1.Selected);
368+ Caption := Format('R%d G%d B%d', [Red(c), Green(c), Blue(c)]);
369+end;
370+```
371+
372+# Drawing theme elements on your custom controls
373+
374+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:
375+
376+``` pascal
377+uses Themes;
378+
379+...
380+
381+procedure TYourCustomControl.Paint;
382+const
383+ PlusMinusDetail: array[Boolean {Hot}, Boolean {Expanded}] of TThemedTreeview =
384+ (
385+ (ttGlyphClosed, ttGlyphOpened),
386+ (ttHotGlyphClosed, ttHotGlyphOpened)
387+ );
388+var
389+ Details: TThemedElementDetails;
390+ R: TRect;
391+ Collapse: boolean;
392+begin
393+ ...
394+ //draw a themed expand sign.
395+ Details := ThemeServices.GetElementDetails(PlusMinusDetail[False, Collapse]);
396+ R := Rect(ALeft, ATop, ARight + 1, ABottom + 1);
397+ ThemeServices.DrawElement(Canvas.Handle, Details, R, nil);
398+ ...
399+end;
400+```
401+
402+## 贡献者和更改
403+
404+- 简体中文版本由 robsean 于 2019-07-17 创建。
405+
406+---
407+*From the Free Pascal wiki, page "Colors/zh CN", as it stood on 2020-02-11 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
408+Category: Graphics

Back to history