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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
//
// "$Id: fl_color_win32.cxx,v 1.13 1999/01/07 19:17:37 mike Exp $"
//
// WIN32 color functions for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-1999 by Bill Spitzak and others.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
// USA.
//
// Please report all bugs and problems to "fltk-bugs@easysw.com".
//
// The fltk "colormap". This allows ui colors to be stored in 8-bit
// locations, and provides a level of indirection so that global color
// changes can be made. Not to be confused with the X colormap, which
// I try to hide completely.
// SGI compiler seems to have problems with unsigned char arguments
// being used to index arrays. So I always copy them to an integer
// before use.
#include <config.h>
#include <FL/Fl.H>
#include <FL/win32.H>
#include <FL/fl_draw.H>
static unsigned fl_cmap[256] = {
#include "fl_cmap.h" // this is a file produced by "cmap.cxx":
};
// Translations to win32 data structures:
Fl_XMap fl_xmap[256];
Fl_XMap* fl_current_xmap;
HPALETTE fl_palette;
HPEN tmppen=0;
HBRUSH tmpbrush=0;
static void clear_xmap(Fl_XMap& xmap) {
if (xmap.pen) {
if(!tmppen) tmppen = CreatePen(PS_SOLID, 1, 0);
if(!tmpbrush) tmpbrush = CreateSolidBrush(0);
HPEN oldpen = (HPEN)SelectObject(fl_gc, tmppen); // Push out the current pen of the gc
if(oldpen != xmap.pen) SelectObject(fl_gc, oldpen); // Put it back if it is not the one we are about to delete
SelectObject(fl_gc, tmpbrush); // Push out the old pen of the gc
//fl_current_xmap = 0;
DeleteObject((HGDIOBJ)(xmap.pen));
xmap.pen = 0;
xmap.brush = -1;
}
}
static void set_xmap(Fl_XMap& xmap, COLORREF c) {
xmap.rgb = c;
xmap.pen = CreatePen(PS_SOLID, 1, xmap.rgb);
xmap.brush = -1;
}
Fl_Color fl_color_;
void fl_color(Fl_Color i) {
fl_color_ = i;
Fl_XMap &xmap = fl_xmap[i];
if (!xmap.pen) {
#if USE_COLORMAP
if (fl_palette) {
set_xmap(xmap, PALETTEINDEX(i));
} else {
#endif
unsigned c = fl_cmap[i];
set_xmap(xmap, RGB(uchar(c>>24), uchar(c>>16), uchar(c>>8)));
#if USE_COLORMAP
}
#endif
}
fl_current_xmap = ⟼
SelectObject(fl_gc, (HGDIOBJ)(xmap.pen));
}
void fl_color(uchar r, uchar g, uchar b) {
static Fl_XMap xmap;
COLORREF c = RGB(r,g,b);
if (!xmap.pen || c != xmap.rgb) {
clear_xmap(xmap);
set_xmap(xmap, c);
}
fl_current_xmap = ⟼
SelectObject(fl_gc, (HGDIOBJ)(xmap.pen));
}
HBRUSH fl_brush() {
Fl_XMap *xmap = fl_current_xmap;
// Wonko: we use some statistics to cache only a limited number
// of brushes:
#define FL_N_BRUSH 16
static struct Fl_Brush {
HBRUSH brush;
unsigned short usage;
Fl_XMap* backref;
} brushes[FL_N_BRUSH];
int i = xmap->brush; // find the associated brush
if (i != -1) { // if the brush was allready allocated
if (brushes[i].brush == NULL) goto CREATE_BRUSH;
if ( (++brushes[i].usage) > 32000 ) { // keep a usage statistic
for (int j=0; j<FL_N_BRUSH; j++) {
if (brushes[j].usage>16000)
brushes[j].usage -= 16000;
else
brushes[j].usage = 0;
}
}
return brushes[i].brush;
} else {
int umin = 32000, imin = 0;
for (i=0; i<FL_N_BRUSH; i++) {
if (brushes[i].brush == NULL) goto CREATE_BRUSH;
if (brushes[i].usage<umin) {
umin = brushes[i].usage;
imin = i;
}
}
i = imin;
DeleteObject(brushes[i].brush);
brushes[i].brush = NULL;
brushes[i].backref->brush = -1;
}
CREATE_BRUSH:
brushes[i].brush = CreateSolidBrush(xmap->rgb);
brushes[i].usage = 0;
brushes[i].backref = xmap;
xmap->brush = i;
return brushes[i].brush;
}
Fl_Color fl_color_average(Fl_Color color1, Fl_Color color2, float weight) {
Fl_Color avg;
unsigned rgb1 = fl_cmap[color1];
unsigned rgb2 = fl_cmap[color2];
uchar r, g, b;
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));
if (r == g && r == b) { // get it out of gray ramp
avg = fl_gray_ramp(r*FL_NUM_GRAY/256);
} else { // get it out of color cube:
avg = fl_color_cube(r*FL_NUM_RED/256,g*FL_NUM_GREEN/256,b*FL_NUM_BLUE/256);
}
return avg;
}
Fl_Color contrast(Fl_Color fg, Fl_Color bg) {
// bright/dark is decided based on high bit of green:
if (fl_cmap[bg] & 0x800000) {
if (fl_cmap[fg] & 0x800000) return FL_GRAY_RAMP; // black from gray ramp
} else {
if (!(fl_cmap[fg] & 0x800000))
return (Fl_Color)(FL_COLOR_CUBE-1); // white from gray ramp
}
return fg; // this color is ok
}
void Fl::free_color(Fl_Color i, int overlay) {
if (overlay) return; // do something about GL overlay?
clear_xmap(fl_xmap[i]);
}
void Fl::set_color(Fl_Color i, unsigned c) {
if (fl_cmap[i] != c) {
clear_xmap(fl_xmap[i]);
fl_cmap[i] = c;
}
}
unsigned Fl::get_color(Fl_Color i) {
return fl_cmap[i];
}
void Fl::set_color(Fl_Color i, uchar red, uchar green, uchar blue) {
Fl::set_color(i,
((unsigned)red<<24)+((unsigned)green<<16)+((unsigned)blue<<8));
}
void Fl::get_color(Fl_Color i, uchar &red, uchar &green, uchar &blue) {
unsigned c = fl_cmap[i];
red = uchar(c>>24);
green = uchar(c>>16);
blue = uchar(c>>8);
}
#if USE_COLORMAP
// 'fl_select_palette()' - Make a color palette for 8-bit displays if necessary
// Thanks to Michael Sweet @ Easy Software Products for this
HPALETTE
fl_select_palette(void)
{
static char beenhere;
if (!beenhere) {
beenhere = 1;
//if (GetDeviceCaps(fl_gc, BITSPIXEL) > 8) return NULL;
int nColors = GetDeviceCaps(fl_gc, SIZEPALETTE);
if (nColors <= 0 || nColors > 256) return NULL;
// this will try to work on < 256 color screens, but will probably
// come out quite badly.
// I lamely try to get this variable-sized object allocated on stack:
ulong foo[(sizeof(LOGPALETTE)+256*sizeof(PALETTEENTRY))/sizeof(ulong)+1];
LOGPALETTE *pPal = (LOGPALETTE*)foo;
pPal->palVersion = 0x300;
pPal->palNumEntries = nColors;
// Build 256 colors from the standard FLTK colormap...
for (int i = 0; i < nColors; i ++) {
pPal->palPalEntry[i].peRed = (fl_cmap[i] >> 24) & 255;
pPal->palPalEntry[i].peGreen = (fl_cmap[i] >> 16) & 255;
pPal->palPalEntry[i].peBlue = (fl_cmap[i] >> 8) & 255;
pPal->palPalEntry[i].peFlags = 0;
};
// Create the palette:
fl_palette = CreatePalette(pPal);
}
if (fl_palette) {
SelectPalette(fl_gc, fl_palette, FALSE);
RealizePalette(fl_gc);
}
return fl_palette;
}
#endif
//
// End of "$Id: fl_color_win32.cxx,v 1.13 1999/01/07 19:17:37 mike Exp $".
//
|