OWLNext    7.0
Borland's Object Windows Library for the modern age
Loading...
Searching...
No Matches
color.cpp
Go to the documentation of this file.
1//----------------------------------------------------------------------------
2// Borland WinSys Library
3// Copyright (c) 1992, 1996 by Borland International, All Rights Reserved
4//
5/// \file
6/// Implementation of color classes
7//----------------------------------------------------------------------------
8
9#include <owl/pch.h>
10#include <owl/defs.h>
11#include <owl/color.h>
12#include <owl/system.h>
13
14
15
16namespace owl {
17
18/// \class TColor
19///
20/// TColor is a support class used in conjunction with the classes TPalette,
21/// TPaletteEntry, TRgbQuad, and TRgbTriple to simplify all color operations.
22/// TColor has ten static data members representing the standard RGB COLORREF values,
23/// from Black to White. Constructors are provided to create TColor objects from
24/// COLORREF and RGB values, palette indexes, palette entries, and RGBQUAD and RGBTRIPLE values.
25///
26/// See the entries for NBits and NColors for a description of TColor-related functions.
27//
28
29
30
31// Few constants only defined when WINVER >= 0x0400
32//
33#if !defined(COLOR_3DDKSHADOW)
34# define COLOR_3DDKSHADOW 21
35#endif
36#if !defined(COLOR_3DLIGHT)
37# define COLOR_3DLIGHT 22
38#endif
39#if !defined(COLOR_INFOTEXT)
40# define COLOR_INFOTEXT 23
41#endif
42#if !defined(COLOR_INFOBK)
43# define COLOR_INFOBK 24
44#endif
45
46const TColor TColor::Black(0, 0, 0); ///< Static TColor object with fixed Value set by RGB(0, 0, 0).
47const TColor TColor::LtGray(192, 192, 192); ///< Static TColor object with fixed Value set by RGB(192, 192, 192).
48const TColor TColor::Gray(128, 128, 128); ///< Static TColor object with fixed Value set by RGB(128, 128, 128).
49const TColor TColor::LtRed(255, 0, 0); ///< Static TColor object with fixed Value set by RGB(255, 0, 0).
50const TColor TColor::LtGreen(0, 255, 0); ///< Static TColor object with fixed Value set by RGB(0, 255, 0).
51const TColor TColor::LtYellow(255, 255, 0); ///< Static TColor object with fixed Value set by RGB(255, 255, 0).
52const TColor TColor::LtBlue(0, 0, 255); ///< Static TColor object with fixed Value set by RGB(0, 0, 255).
53const TColor TColor::LtMagenta(255, 0, 255); ///< Static TColor object with fixed Value set by RGB(255, 0, 255).
54const TColor TColor::LtCyan(0, 255, 255); ///< Static TColor object with fixed Value set by RGB(0, 255, 255).
55const TColor TColor::White(255, 255, 255); ///< Static TColor object with fixed Value set by RGB(255, 255, 255).
56
57//
58// Special marker colors using flag bit pattern. Value never really used.
59// Value must not change for streaming compatibility w/ OWL's TWindow
60//
61const TColor TColor::None((COLORREF) 0xFF000000l);
62const TColor TColor::Transparent((COLORREF) 0xFE000000l);
63
64//
65// Symbolic system colors looked up on evaluation or conversion
66//
67#define Symbolic 0x80000000UL
68/// The symbolic system color value for what is usually the gray area of scrollbars.
69/// This is the region that the scrollbar slider slides upon.
70///
71/// Performs GetSysColor() on conversion to COLORREF.
73
74/// The symbolic system color value for the desktop.
75/// Performs GetSysColor() on conversion to COLORREF.
77
78/// The symbolic system color value for the caption of the active window.
79///
80/// Performs GetSysColor() on conversion to COLORREF.
82
83/// The symbolic system color value for the caption background of every inactive window.
84///
85/// Performs GetSysColor() on conversion to COLORREF.
87
88/// The symbolic system color value for the background of menus.
89///
90/// Performs GetSysColor() on conversion to COLORREF.
91const TColor TColor::SysMenu ((COLORREF) (Symbolic | COLOR_MENU));
92
93/// The symbolic system color value for the background of each window.
94///
95/// Performs GetSysColor() on conversion to COLORREF.
97
98/// The symbolic system color value for the frame around each window. The frame is not the same as the border.
99///
100/// Performs GetSysColor() on conversion to COLORREF.
102
103/// The symbolic system color value for the text shown on menus.
104///
105/// Performs GetSysColor() on conversion to COLORREF.
107
108/// The symbolic system color value for text in every window.
109///
110/// Performs GetSysColor() on conversion to COLORREF.
112
113/// The symbolic system color value for text in captions and size boxes, and for the arrow boxes on scroll bars.
114///
115/// Performs GetSysColor() on conversion to COLORREF.
117
118/// The symbolic system color value for the borders of the active window.
119///
120/// Performs GetSysColor() on conversion to COLORREF.
122
123/// The symbolic system color value for the borders of every inactive window.
124///
125/// Performs GetSysColor() on conversion to COLORREF.
127
128/// The symbolic system color value for the background of multiple document interface (MDI) applications.
129///
130/// Performs GetSysColor() on conversion to COLORREF.
132
133/// The symbolic system color value for items selected in a control.
134///
135/// Performs GetSysColor() on conversion to COLORREF.
137
138/// The symbolic system color value for text selected in a control.
139///
140/// Performs GetSysColor() on conversion to COLORREF.
142
143/// The symbolic system color value for the face color of 3-dimensional display elements.
144///
145/// Performs GetSysColor() on conversion to COLORREF.
147
148/// The symbolic system color value for the shadow regions of 3-dimensional display elements (for edges facing away from the light source).
149///
150/// Performs GetSysColor() on conversion to COLORREF.
152
153/// The symbolic system color value for grayed (disabled) text.
154///
155/// This color is set to 0 if the current display driver does not support a solid gray color.
156/// Performs GetSysColor() on conversion to COLORREF.
158
159/// The symbolic system color value for the text on buttons.
160/// Performs GetSysColor() on conversion to COLORREF.
162
163// NOTE: WINVER >= 0x030a
164//
165/// The symbolic system color value for the caption text of every inactive window.
167/// The symbolic system color value for highlighted 3-dimensional display elements (for edges facing the light source).
169
170// NOTE: WINVER >= 0x0400
171//
172/// The symbolic system color value for dark shadow regions of 3-dimensional display elements.
174/// The symbolic system color value for the light color for 3-dimensional display elements (for edges facing the light source).
176/// The symbolic system color value for text shown on tooltip controls.
178/// The symbolic system color value for the background of tooltip controls.
180
181//
182/// Convert a bit count into a color count for color table use, verifying that
183/// the bit count is one that is supported by Windows, ie 1, 4, 8, 16, 24, 32.
184//
185/// If the bit count is not supported, -1 is returned.
186//
187_OWLFUNC(long)
189{
190 if (bitCount == 1 || bitCount == 4 || bitCount == 8)
191 return 1 << bitCount;
192 if (bitCount == 16 || bitCount == 24 || bitCount == 32)
193 return 0;
194 return -1;
195}
196
197//
198/// Return the number of bits required to represent a given number of colors
199//
202{
203 if (colors <= 2)
204 return 1;
205 if (colors <= 16)
206 return 4;
207 if (colors <= 256)
208 return 8;
209 if (colors <= 65536L)
210 return 16;
211 if (colors <= 16777216L)
212 return 24;
213 return 32;
214}
215
216//
217/// Get a 32bit COLORREF type from this color object.
218//
219/// Performs a GetSysColor() lookup if the object represents a symbolic sys-color index.
220//
223{
224 return IsSysColor() ? ::GetSysColor(Index()) : Value;
225}
226
227// copied from old unix owl (JAM 04-16-01)
228//DLN UNIX added MwGetTrueRGBValue conversion calls
229#if defined(UNIX) && !defined(WINELIB)
230#include <mainwin.h>
231#endif
232
233
234//
235/// Return the color's red component
236//
237uint8
239{
240 return (uint8)(uint16)GetValue();
241}
242
243//
244/// Return the color's green component
245//
246uint8
248{
249 return (uint8)(uint16)(((uint16)GetValue()) >> 8);
250}
251
252//
253/// Return the color's blue component
254//
255uint8
257{
258 return (uint8)(uint16)(GetValue()>>16);
259}
260
261////////////////////////////////////////////////////////////////////////////////
262
263void
265{
266 uint r = Red();
267 uint g = Green();
268 uint b = Blue();
269
270 r = (r > (uint)(255 - val)) ? 255 : r + val;
271 g = (g > (uint)(255 - val)) ? 255 : g + val;
272 b = (b > (uint)(255 - val)) ? 255 : b + val;
273
274 Value = RGB(r, g, b);
275}
276
277void
279{
280 uint r = Red();
281 uint g = Green();
282 uint b = Blue();
283
284 r = (r < (uint)val) ? 0 : r - val;
285 g = (g < (uint)val) ? 0 : g - val;
286 b = (b < (uint)val) ? 0 : b - val;
287
288 Value = RGB(r, g, b);
289}
290
291void
293{
294 Value = MkRGB(
295 (static_cast<int>(Red()) + static_cast<int>(other.Red())) / 2,
296 (static_cast<int>(Green()) + static_cast<int>(other.Green())) / 2,
297 (static_cast<int>(Blue()) + static_cast<int>(other.Blue())) / 2);
298}
299
300////////////////////////////////////////////////////////////////////////////////
301
304{
305 uint8 minval = Red();
306 uint8 maxval = Red();
307
308 if (minval > Green())
309 minval = Green();
310 if (maxval < Green())
311 maxval = Green();
312
313 if (minval > Blue())
314 minval = Blue();
315 if (maxval < Blue())
316 maxval = Blue();
317
318 float mdiff = float(maxval) - float(minval);
319 float msum = float(maxval) + float(minval);
320
321 float luminance = msum / 510.0f;
322 float saturation = 0.0f;
323 float hue = 0.0f;
324
325 if (maxval != minval)
326 {
327 float rnorm = (maxval - Red()) / mdiff;
328 float gnorm = (maxval - Green()) / mdiff;
329 float bnorm = (maxval - Blue()) / mdiff;
330
331 saturation = (luminance <= 0.5f) ? (mdiff / msum) : (mdiff / (510.0f - msum));
332
333 if (Red() == maxval)
334 hue = 60.0f * (6.0f + bnorm - gnorm);
335
336 if (Green() == maxval)
337 hue = 60.0f * (2.0f + rnorm - bnorm);
338
339 if (Blue() == maxval)
340 hue = 60.0f * (4.0f + gnorm - rnorm);
341
342 if (hue > 360.0f)
343 hue = hue - 360.0f;
344 }
345
346 return HLS ((hue * 255) / 360, luminance * 255, saturation * 255);
347}
348
349static uint8
350_ToRgb(float rm1, float rm2, float rh)
351{
352 if (rh > 360.0f)
353 rh -= 360.0f;
354 else if (rh < 0.0f)
355 rh += 360.0f;
356
357 if (rh < 60.0f)
358 rm1 = rm1 + (rm2 - rm1) * rh / 60.0f;
359 else if (rh < 180.0f)
360 rm1 = rm2;
361 else if (rh < 240.0f)
362 rm1 = rm1 + (rm2 - rm1) * (240.0f - rh) / 60.0f;
363
364 return (BYTE)(rm1 * 255);
365}
366
367void
369{
370 float hue = ((int)HLS_H(hls)*360)/255.0f;
371 float luminance = HLS_L(hls)/255.0f;
372 float saturation = HLS_S(hls)/255.0f;
373
374 if (saturation == 0.0f)
375 {
377 return;
378 }
379
380 float rm1, rm2;
381
382 if (luminance <= 0.5f)
384 else
386
387 rm1 = 2.0f * luminance - rm2;
388 BYTE red = _ToRgb(rm1, rm2, hue + 120.0f);
389 BYTE green = _ToRgb(rm1, rm2, hue);
390 BYTE blue = _ToRgb(rm1, rm2, hue - 120.0f);
391
393}
394
395void
397{
398 HLSCOLOR hls = Rgb2Hls();
399 BYTE h = HLS_H(hls);
400 BYTE l = HLS_L(hls);
401 BYTE s = HLS_S(hls);
402
403 if (percent_L > 0)
404 {
405 l = BYTE(l + ((255 - l) * percent_L) / 100);
406 }
407 else if (percent_L < 0)
408 {
409 l = BYTE((l * (100 + percent_L)) / 100);
410 }
411
412 if (percent_S > 0)
413 {
414 s = BYTE(s + ((255 - s) * percent_S) / 100);
415 }
416 else if (percent_S < 0)
417 {
418 s = BYTE((s * (100 + percent_S)) / 100);
419 }
420
421 Hls2Rgb(HLS(h, l, s));
422}
423
424
425
426} // OWL namespace
427/* ========================================================================== */
428
Class wrapper for management of color values.
Definition color.h:245
static const TColor SysGrayText
The symbolic system color value for grayed (disabled) text.
Definition color.h:341
COLORREF GetValue() const
Get a 32bit COLORREF type from this color object.
Definition color.cpp:222
static const TColor LtBlue
Static TColor object with fixed Value set by RGB(0, 0, 255).
Definition color.h:311
void Hls2Rgb(HLSCOLOR hls)
Definition color.cpp:368
static const TColor SysInfoBk
The symbolic system color value for the background of tooltip controls.
Definition color.h:348
static const TColor SysMenuText
The symbolic system color value for the text shown on menus.
Definition color.h:331
static const TColor SysHighlightText
The symbolic system color value for text selected in a control.
Definition color.h:338
static const TColor LtMagenta
Static TColor object with fixed Value set by RGB(255, 0, 255).
Definition color.h:312
static const TColor SysBtnText
The symbolic system color value for the text on buttons.
Definition color.h:342
static const TColor Transparent
a non-painting color
Definition color.h:319
static const TColor SysHighlight
The symbolic system color value for items selected in a control.
Definition color.h:337
static const TColor SysActiveCaption
The symbolic system color value for the caption of the active window.
Definition color.h:326
static const TColor Sys3dHilight
The symbolic system color value for highlighted 3-dimensional display elements (for edges facing the ...
Definition color.h:344
static const TColor SysCaptionText
The symbolic system color value for text in captions and size boxes, and for the arrow boxes on scrol...
Definition color.h:333
static const TColor None
not-a-color
Definition color.h:318
static const TColor SysActiveBorder
The symbolic system color value for the borders of the active window.
Definition color.h:334
HLSCOLOR Rgb2Hls() const
Definition color.cpp:303
static const TColor LtCyan
Static TColor object with fixed Value set by RGB(0, 255, 255).
Definition color.h:313
static const TColor SysDesktop
The symbolic system color value for the desktop.
Definition color.h:325
static const TColor Sys3dDkShadow
The symbolic system color value for dark shadow regions of 3-dimensional display elements.
Definition color.h:345
static const TColor SysInactiveCaption
The symbolic system color value for the caption background of every inactive window.
Definition color.h:327
static const TColor SysInactiveCaptionText
The symbolic system color value for the caption text of every inactive window.
Definition color.h:343
int Index() const
Return the index of the palette entry.
Definition color.h:690
static const TColor SysWindowFrame
The symbolic system color value for the frame around each window.
Definition color.h:330
void SetValue(const COLORREF &value)
Change the color after it has been constructed.
Definition color.h:671
uint8 Blue() const
Return the color's blue component.
Definition color.cpp:256
static const TColor SysScrollbar
The symbolic system color value for what is usually the gray area of scrollbars.
Definition color.h:324
void Merge(const TColor &other)
Definition color.cpp:292
static const TColor LtYellow
Static TColor object with fixed Value set by RGB(255, 255, 0).
Definition color.h:310
static const TColor SysInfoText
The symbolic system color value for text shown on tooltip controls.
Definition color.h:347
bool IsSysColor() const
Return true if the color is a system color.
Definition color.h:738
static const TColor LtGray
Static TColor object with fixed Value set by RGB(192, 192, 192).
Definition color.h:306
static const TColor SysWindowText
The symbolic system color value for text in every window.
Definition color.h:332
static const TColor Sys3dShadow
The symbolic system color value for the shadow regions of 3-dimensional display elements (for edges f...
Definition color.h:340
static const TColor SysAppWorkspace
The symbolic system color value for the background of multiple document interface (MDI) applications.
Definition color.h:336
void HlsTransform(int percent_L, int percent_S)
Definition color.cpp:396
static const TColor Sys3dFace
The symbolic system color value for the face color of 3-dimensional display elements.
Definition color.h:339
static const TColor LtGreen
Static TColor object with fixed Value set by RGB(0, 255, 0).
Definition color.h:309
static const TColor SysMenu
The symbolic system color value for the background of menus.
Definition color.h:328
static const TColor SysInactiveBorder
The symbolic system color value for the borders of every inactive window.
Definition color.h:335
static const TColor Gray
Static TColor object with fixed Value set by RGB(128, 128, 128).
Definition color.h:307
static const TColor Sys3dLight
The symbolic system color value for the light color for 3-dimensional display elements (for edges fac...
Definition color.h:346
uint8 Red() const
Return the color's red component.
Definition color.cpp:238
void Lighten(uint8 val)
Definition color.cpp:264
static const TColor White
Static TColor object with fixed Value set by RGB(255, 255, 255).
Definition color.h:314
static const TColor SysWindow
The symbolic system color value for the background of each window.
Definition color.h:329
void Darken(uint8 val)
Definition color.cpp:278
static const TColor LtRed
Static TColor object with fixed Value set by RGB(255, 0, 0).
Definition color.h:308
uint8 Green() const
Return the color's green component.
Definition color.cpp:247
static const TColor Black
Static TColor object with fixed Value set by RGB(0, 0, 0).
Definition color.h:305
#define COLOR_INFOBK
Definition color.cpp:43
#define Symbolic
Definition color.cpp:67
#define COLOR_3DDKSHADOW
Definition color.cpp:34
#define COLOR_INFOTEXT
Definition color.cpp:40
#define COLOR_3DLIGHT
Definition color.cpp:37
Definition of windowing system color classes.
#define HLS_S(hls)
Definition color.h:26
#define HLS_L(hls)
Definition color.h:25
#define HLS_H(hls)
Definition color.h:24
#define HLS(h, l, s)
Definition color.h:23
DWORD HLSCOLOR
Definition color.h:22
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
COLORREF MkRGB(uint r, uint g, uint b)
Definition color.h:42
long NColors(uint16 bitCount)
Functions to convert number of bits to number of palette colors and back, and build an RGB COLORREF.
Definition color.cpp:188
unsigned char uint8
Definition number.h:32
uint16 NBits(long colors)
Return the number of bits required to represent a given number of colors.
Definition color.cpp:201
unsigned short uint16
Definition number.h:33
unsigned int uint
Definition number.h:25
General definitions used by all ObjectWindows programs.
#define _OWLFUNC(p)
Definition defs.h:341
Definition of TSystem, a system information provider class.