1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
//
// "$Id$"
//
// Color functions for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2016 by Bill Spitzak and others.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file. If this
// file is missing or damaged, see the license at:
//
// http://www.fltk.org/COPYING.php
//
// Please report all bugs and problems on the following page:
//
// http://www.fltk.org/str.php
//
/**
\file fl_color.cxx
\brief Color handling
*/
// Implementation of fl_color(i), fl_color(r,g,b).
#include <FL/Fl.H>
#include <FL/Fl_Device.H>
#include <FL/Fl_Graphics_Driver.H>
#include <FL/Fl.H>
// fl_cmap needs to be defined globally (here) and is used in the device
// specific graphics drivers
unsigned fl_cmap[256] = {
#include "fl_cmap.h" // this is a file produced by "cmap.cxx":
};
// -----------------------------------------------------------------------------
// all driver code is now in drivers/XXX/Fl_XXX_Graphics_Driver_xyz.cxx
// -----------------------------------------------------------------------------
/** \addtogroup fl_attributes
\{ */
/**
Returns the RGB value(s) for the given FLTK color index.
This form returns the RGB values packed in a 32-bit unsigned
integer with the red value in the upper 8 bits, the green value
in the next 8 bits, and the blue value in bits 8-15. The lower
8 bits will always be 0.
*/
unsigned Fl::get_color(Fl_Color i) {
if (i & 0xffffff00) return (i);
else return fl_cmap[i];
}
/**
Sets an entry in the fl_color index table.
You can set it to any 8-bit RGB color. The color is not allocated
until fl_color(i) is used.
*/
void Fl::set_color(Fl_Color i, uchar red, uchar green, uchar blue) {
Fl::set_color((Fl_Color)(i & 255),
((unsigned)red<<24)+((unsigned)green<<16)+((unsigned)blue<<8));
}
void Fl::set_color(Fl_Color i, unsigned c)
{
Fl_Graphics_Driver::default_driver().set_color(i, c);
}
void Fl::free_color(Fl_Color i, int overlay)
{
Fl_Graphics_Driver::default_driver().free_color(i, overlay);
}
/**
Returns the RGB value(s) for the given FLTK color index.
This form returns the red, green, and blue values
separately in referenced variables.
\see unsigned get_color(Fl_Color c)
*/
void Fl::get_color(Fl_Color i, uchar &red, uchar &green, uchar &blue) {
unsigned c;
if (i & 0xffffff00) c = (unsigned)i;
else c = fl_cmap[i];
red = uchar(c>>24);
green = uchar(c>>16);
blue = uchar(c>>8);
}
/**
Returns the weighted average color between the two given colors.
The red, green and blue values are averages using the following formula:
\code
color = color1 * weight + color2 * (1 - weight)
\endcode
Thus, a \p weight value of 1.0 will return the first color, while a
value of 0.0 will return the second color.
\param[in] color1, color2 boundary colors
\param[in] weight weighting factor
*/
Fl_Color fl_color_average(Fl_Color color1, Fl_Color color2, float weight) {
unsigned rgb1;
unsigned rgb2;
uchar r, g, b;
if (color1 & 0xffffff00) rgb1 = color1;
else rgb1 = fl_cmap[color1 & 255];
if (color2 & 0xffffff00) rgb2 = color2;
else rgb2 = fl_cmap[color2 & 255];
r = (uchar)(((uchar)(rgb1>>24))*weight + ((uchar)(rgb2>>24))*(1-weight));
g = (uchar)(((uchar)(rgb1>>16))*weight + ((uchar)(rgb2>>16))*(1-weight));
b = (uchar)(((uchar)(rgb1>>8))*weight + ((uchar)(rgb2>>8))*(1-weight));
return fl_rgb_color(r, g, b);
}
/**
Returns the inactive, dimmed version of the given color.
*/
Fl_Color fl_inactive(Fl_Color c) {
return fl_color_average(c, FL_GRAY, .33f);
}
/**
Returns a color that contrasts with the background color.
This will be the foreground color if it contrasts sufficiently with the
background color. Otherwise, returns \p FL_WHITE or \p FL_BLACK depending
on which color provides the best contrast.
\param[in] fg,bg foreground and background colors
\return contrasting color
*/
Fl_Color fl_contrast(Fl_Color fg, Fl_Color bg) {
unsigned c1, c2; // RGB colors
int l1, l2; // Luminosities
// Get the RGB values for each color...
if (fg & 0xffffff00) c1 = (unsigned)fg;
else c1 = fl_cmap[fg];
if (bg & 0xffffff00) c2 = (unsigned)bg;
else c2 = fl_cmap[bg];
// Compute the luminosity...
l1 = ((c1 >> 24) * 30 + ((c1 >> 16) & 255) * 59 + ((c1 >> 8) & 255) * 11) / 100;
l2 = ((c2 >> 24) * 30 + ((c2 >> 16) & 255) * 59 + ((c2 >> 8) & 255) * 11) / 100;
// Compare and return the contrasting color...
if ((l1 - l2) > 99) return fg;
else if ((l2 - l1) > 99) return fg;
else if (l2 > 127) return FL_BLACK;
else return FL_WHITE;
}
/**
\}
*/
//
// End of "$Id$".
//
|