Sign in Join
Wiki copytemporary

Colors/zh CN: what changed

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

OldNewLine
1-\
2-\
31=概述=
42
53在[[LCL]],TColor是标准颜色类型。它和[[Delphi]]的TColor兼容。TColor 可以表示*要么*一个RGB (3x8比特)值,*要么*一个**system**颜色,像**clDefault**。LCL也可以和fpImage系统一起工作,使用TFPColor类型(它是RGBA(4x16比特),非 RGB(3x8比特),像TColor)。
64
75## 在TColor 和RGB值之间转换
86
97**Graphics**单元提供提供下面的函数:
108
119``` pascal
1210function Blue(rgb: TColor): BYTE; // does not work on system color
1311function Green(rgb: TColor): BYTE; // does not work on system color
1412function Red(rgb: TColor): BYTE; // does not work on system color
1513function RGBToColor(R, G, B: Byte): TColor;
1614procedure RedGreenBlue(rgb: TColor; out Red, Green, Blue: Byte); // does not work on system color
1715function FPColorToTColor(const FPColor: TFPColor): TColor;
1816function TColorToFPColor(const c: TColor): TFPColor; // does not work on system color
1917function IsSysColor(AColor: TColorRef): Boolean;
2018```
2119
2220## 转换TColor到/来自字符串
2321
2422函数来转换字符串像"25500"或"\$AA0088"或"clNavy"到TColor:
2523
2624- StringToColor
2725- StringToColorDef
2826
2927转换TColor到一个友好的字符串像"clNavy"或"\$AA0002":
3028
3129- ColorToString
3230
3331## 转换TColor到/来自HTML字符串 \#rrggbb
3432
3533查看代码在[[转换颜色到/来自HTML]]。
3634
3735# 标准颜色表
3836
3937大约20种预定义颜色常量被提供,它们是与Delphi-兼容的:
4038
4139| 颜色常量 | 意思 | TColor使用[[十六进制]]值 | 示例 |
4240|----|----|----|----|
4341| clBlack | 黑色 | TColor(\$000000); | |
4442| clMaroon | 褐红色 | TColor(\$000080); | |
4543| clGreen | 绿色 | TColor(\$008000); | |
4644| clOlive | 橄榄绿 | TColor(\$008080); | |
4745| clNavy | 深蓝色 | TColor(\$800000); | |
4846| clPurple | 紫色 | TColor(\$800080); | |
4947| clTeal | 蓝绿色 | TColor(\$808000); | |
5048| clGray | 灰色 | TColor(\$808080); | |
5149| clSilver | 银色 | TColor(\$C0C0C0); | |
5250| clRed | 红色 | TColor(\$0000FF); | |
5351| clLime | 酸橙绿 | TColor(\$00FF00); | |
5452| clYellow | 黄色 | TColor(\$00FFFF); | |
5553| clBlue | 蓝色 | TColor(\$FF0000); | |
5654| clFuchsia | 紫红色 | TColor(\$FF00FF); | |
5755| clAqua | 湖绿色 | TColor(\$FFFF00); | |
5856| clLtGray | 浅灰色 | TColor(\$C0C0C0); | clSilver别名 |
5957| clDkGray | 深灰色 | TColor(\$808080); | clGray别名 |
6058| clWhite | 白色 | TColor(\$FFFFFF); | |
6159| clCream | 奶油色 | TColor(\$F0FBFF); | Lazarus 1.2和较新的 |
6260| clMedGray | 中灰色 | TColor(\$A4A0A0); | Lazarus 1.2和较新的 |
6361| clMoneyGreen | 薄荷绿 | TColor(\$C0DCC0); | Lazarus 1.2和较新的 |
6462| clSkyBlue | 天蓝色 | TColor(\$F0CAA6); | LLazarus 1.2和较新的 |
6563
6664# System colors
6765
6866## Example: clInfoBk, clInfoText
6967
7068System 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**:
7169
7270``` pascal
7371Form1.Canvas.Brush.Color:=clInfoBk; // use the default background brush of a hint window
7472Form1.Canvas.FillRect(10,10,50,50);
7573```
7674
7775A 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:
7876
7977``` pascal
8078Form1.Canvas.Brush.Color:=clInfoBk; // use the default background brush of a hint window
8179Form1.Canvas.FillRect(10,10,50,50);
8280Form1.Canvas.Font.Color:=clInfoText; // use the default text color of a hint window
8381Form1.Canvas.TextOut(10,10,'Hint');
8482```
8583
8684The 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.
8785
8886## Theme changes
8987
9088When 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:
9189
9290``` pascal
9391Form1.Canvas.Brush.Color:=clInfoBk; // this will not create a new brush handle
9492Form1.Canvas.FillRect(10,10,50,50); // this will create the brush handle with the currently active theme brush for hint windows
9593...
9694// if the theme changes in this moment the Brush.Handle is still allocated with the old values
9795...
9896Form1.Canvas.FillRect(10,10,50,50); // this will paint with the old theme brush
9997Form1.Canvas.Brush.Color:=clInfoBk; // assigning the old value will not create a new brush handle
10098Form1.Canvas.FillRect(10,10,50,50); // this will paint with the old theme brush
10199Form1.Canvas.Brush.Color:=clRed; // assigning a new color, old Handle invalid
102100Form1.Canvas.Brush.Color:=clInfoBk; // assigning a new color, old Handle invalid
103101Form1.Canvas.FillRect(10,10,50,50); // this will create a new handle and paint with the new theme
104102```
105103
106104## Table of system colors
107105
108106The 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**.
109107
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>
108+**Constant**
109+
110+**LCL definition**
111+
112+**Delphi notes**
113+
114+**Supported Widgetsets**
115+
116+clNone
117+
118+Indicates "do not paint". Using it as Control's color is undefined. The control will not get transparent.
119+
120+\-
121+
122+all
123+
124+clDefault
125+
126+Using it for Brush will use the normal background brush of the target DC (device context).
127+
128+- 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.
129+- 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.
130+- Using it as Pen color will use the default line color for the device context.
131+- Using it as Font color will use the normal text color of the device context.
132+
133+\| -
134+
135+all
136+
137+clScrollBar
138+
139+Scrollbar body
140+
141+\-
142+
143+all
144+
145+clBackground
146+
147+Desktop background color
148+
149+\-
150+
151+all
152+
153+clActiveCaption
154+
155+Active window titlebar
156+
157+\-
158+
159+none
160+
161+clInactiveCaption
162+
163+Inactive window titlebar
164+
165+\-
166+
167+none
168+
169+clMenu
170+
171+Regular menu item background color
172+
173+\-
174+
175+none
176+
177+clWindow
178+
179+The normal background brush of unselected text. Defined for controls like TEdit, TComboBox, TMemo, TListBox, TTreeView.
180+
181+\-
182+
183+none
184+
185+clWindowFrame
186+
187+Color of frame around the window
188+
189+\-
190+
191+none
192+
193+clMenuText
194+
195+The font color to use together with clMenu
196+
197+\-
198+
199+none
200+
201+clWindowText
202+
203+Font color to use together with clWindow
204+
205+\-
206+
207+none
208+
209+clCaptionText
210+
211+Active window titlebar text color
212+
213+\-
214+
215+none
216+
217+clActiveBorder
218+
219+?
220+
221+\-
222+
223+none
224+
225+clInactiveBorder
226+
227+?
228+
229+\-
230+
231+none
232+
233+clAppWorkspace
234+
235+MDIMain form background
236+
237+\-
238+
239+none
240+
241+clHighlight
242+
243+The brush color of selected element
244+
245+\-
246+
247+none
248+
249+clHighlightText
250+
251+Font color of selected text (to use together with clHighligh).
252+
253+\-
254+
255+none
256+
257+clBtnFace
258+
259+Button background
260+
261+\-
262+
263+none
264+
265+clBtnShadow
266+
267+Button shadow color (bottom right) used to achieve 3D effect
268+
269+\-
270+
271+none
272+
273+clGrayText
274+
275+The font color of disabled element
276+
277+\-
278+
279+none
280+
281+clBtnText
282+
283+Button font color to use together with clBtnFace
284+
285+\-
286+
287+none
288+
289+clInactiveCaptionText
290+
291+Inactive window titlebar text color
292+
293+\-
294+
295+none
296+
297+clBtnHighlight
298+
299+Button highlight color (top left) used to achieve 3D effect
300+
301+\-
302+
303+none
304+
305+cl3DDkShadow
306+
307+?
308+
309+\-
310+
311+none
312+
313+cl3DLight
314+
315+?
316+
317+\-
318+
319+none
320+
321+clInfoText
322+
323+Font color for hints. Use together with clInfoBk
324+
325+\-
326+
327+all
328+
329+clInfoBk
330+
331+Brush color for hints. Use together with clInfoText
332+
333+\-
334+
335+all
336+
337+clHotLight
338+
339+?
340+
341+\-
342+
343+none
344+
345+clGradientActiveCaption
346+
347+The second color used to make gradient of active window titlebar
348+
349+\-
350+
351+none
352+
353+clGradientInactiveCaption
354+
355+The second color used to make gradient for inactive window titlebar
356+
357+\-
358+
359+none
360+
361+clMenuHighlight
362+
363+The background color of selected menu item
364+
365+\-
366+
367+none
368+
369+clMenuBar
370+
371+The Backround color of menu bar
372+
373+\-
374+
375+none
376+
377+clForm
378+
379+?
380+
381+\-
382+
383+none
384+
385+clColorDesktop
386+
387+?
388+
389+\-
390+
391+none
392+
393+cl3DFace
394+
395+?
396+
397+\-
398+
399+none
400+
401+cl3DShadow
402+
403+?
404+
405+\-
406+
407+none
408+
409+cl3DHiLight
410+
411+?
412+
413+\-
414+
415+none
416+
417+clBtnHiLight
418+
419+Same as clBtnHighlight
420+
421+\-
422+
423+none
356424
357425## Finding the rgb values of a system color
358426
359427Use 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:
360428
361429``` pascal
362430// This example has a TColorBox on a form and uses this OnChange handler for the TColorbox:
363431procedure TForm1.ColorBox1Change(Sender: TObject);
364432var
365433 c: TColor;
366434begin
367435 c := ColorToRGB(ColorBox1.Selected);
368436 Caption := Format('R%d G%d B%d', [Red(c), Green(c), Blue(c)]);
369437end;
370438```
371439
372440# Drawing theme elements on your custom controls
373441
374442The 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:
375443
376444``` pascal
377445uses Themes;
378446
379447...
380448
381449procedure TYourCustomControl.Paint;
382450const
383451 PlusMinusDetail: array[Boolean {Hot}, Boolean {Expanded}] of TThemedTreeview =
384452 (
385453 (ttGlyphClosed, ttGlyphOpened),
386454 (ttHotGlyphClosed, ttHotGlyphOpened)
387455 );
388456var
389457 Details: TThemedElementDetails;
390458 R: TRect;
391459 Collapse: boolean;
392460begin
393461 ...
394462 //draw a themed expand sign.
395463 Details := ThemeServices.GetElementDetails(PlusMinusDetail[False, Collapse]);
396464 R := Rect(ALeft, ATop, ARight + 1, ABottom + 1);
397465 ThemeServices.DrawElement(Canvas.Handle, Details, R, nil);
398466 ...
399467end;
400468```
401469
402470## 贡献者和更改
403471
404472- 简体中文版本由 robsean 于 2019-07-17 创建。
405473
406474---
407475*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).*
408476Category: Graphics

Back to history