-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcolbrowser.cxx
347 lines (287 loc) · 8.26 KB
/
colbrowser.cxx
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
//
// "$Id$"
//
// Forms test program for the Fast Light Tool Kit (FLTK).
//
// This is an XForms program from the 0.86 distribution of XForms.
// It has been modified as little as possible to work under fltk by
// using fltk's Forms emulation. Search for "fltk" to find all the
// changes
//
// Copyright 1998-2006 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 on the following page:
//
// http://www.fltk.org/str.php
//
#include <fltk/forms.h> // changed for fltk
#include <stdlib.h>
#include <stdio.h> // added for fltk
#include <string.h> // added for fltk
#define MAX_RGB 3000
static FL_FORM *cl;
static Fl_Widget *rescol, *dbobj, *colbr, *rs, *gs, *bs;
char dbname[FL_PATH_MAX];
static void create_form_cl(void);
static int load_browser(char *);
/* the RGB data file does not have a standard location on unix. */
#ifdef __VMS
static const char *rgbfile = "SYS$MANAGER:DECW$RGB.DAT";
#else
#ifdef __EMX__ /* OS2 */
#include <X11/XlibInt.h>
static const char *rgbfile = "/XFree86/lib/X11/rgb.txt";
#else
#ifdef __FreeBSD__
static const char *rgbfile = "/usr/X11R6/lib/X11/rgb.txt";
#else
static const char *rgbfile = "/usr/lib/X11/rgb.txt";
#endif
#endif
#endif
typedef struct { int r, g, b; } RGBdb;
static RGBdb rgbdb[MAX_RGB];
int
main(int argc, char *argv[])
{
fl_initialize(&argc, argv, "FormDemo", 0, 0);
create_form_cl();
strcpy(dbname, rgbfile);
if (load_browser(dbname))
fl_set_object_label(dbobj, dbname);
else
fl_set_object_label(dbobj, "None");
// fl_set_form_minsize(cl, cl->w , cl->h); // removed for fltk
// fl_set_form_maxsize(cl, 2*cl->w , 2*cl->h); // removed for fltk
cl->size_range(cl->w(),cl->h(),2*cl->w(),2*cl->h()); // added for fltk
// border changed from FL_TRANSIENT for fltk:
// This is so Esc & the close box will close the window.
// (on transient windows attempting to close it just calls the callback)
fl_show_form(cl, FL_PLACE_FREE, 1/*FL_TRANSIENT*/, "RGB Browser");
while (fl_do_forms())
;
return 0;
}
static void
set_entry(int i)
{
RGBdb *db = rgbdb + i;
fl_freeze_form(cl);
// unclear why demo is doing this. This messes up FL:
// fl_mapcolor(FL_FREE_COL4+i, db->r, db->g, db->b);
fl_mapcolor(FL_FREE_COL4, db->r, db->g, db->b);
fl_set_slider_value(rs, db->r);
fl_set_slider_value(gs, db->g);
fl_set_slider_value(bs, db->b);
fl_redraw_object(rescol);
fl_unfreeze_form(cl);
}
static void
br_cb(Fl_Widget * ob, long)
{
int r = fl_get_browser(ob);
if (r <= 0)
return;
set_entry(r - 1);
}
static int
read_entry(FILE * fp, int *r, int *g, int *b, char *name)
{
int n;
char buf[512], *p;
if (!fgets(buf, sizeof(buf) - 1, fp))
return 0;
if(buf[0] == '!')
fgets(buf,sizeof(buf)-1,fp);
if(sscanf(buf, " %d %d %d %n", r, g, b, &n) < 3)
return 0;
p = buf + n;
/* squeeze out all spaces */
while (*p)
{
if (*p != ' ' && *p != '\n')
*name++ = *p;
p++;
}
*name = 0;
return (feof(fp) || ferror(fp)) ? 0 : 1;
}
static int
load_browser(char *fname)
{
FILE *fp;
RGBdb *db = rgbdb, *dbs = db + MAX_RGB;
int r, g, b, lr = -1 , lg = -1, lb = -1;
char name[256], buf[256];
#ifdef __EMX__
if (!(fp = fopen(__XOS2RedirRoot(fname), "r")))
#else
if (!(fp = fopen(fname, "r")))
#endif
{
fl_show_alert("Load", fname, "Can't open", 0);
return 0;
}
/* read the items */
fl_freeze_form(cl);
for (; db < dbs && read_entry(fp, &r, &g, &b, name);)
{
db->r = r;
db->g = g;
db->b = b;
/* unique the entries on the fly */
if (lr != r || lg != g || lb != b)
{
db++;
lr = r;
lg = g;
lb = b;
sprintf(buf, "(%3d %3d %3d) %s", r, g, b, name);
fl_addto_browser(colbr, buf);
}
}
fclose(fp);
if (db < dbs)
db->r = 1000; /* sentinel */
else
{
db--;
db->r = 1000;
}
fl_set_browser_topline(colbr, 1);
fl_select_browser_line(colbr, 1);
set_entry(0);
fl_unfreeze_form(cl);
return 1;
}
static int
search_entry(int r, int g, int b)
{
register RGBdb *db = rgbdb;
int i, j, diffr, diffg, diffb;
unsigned int diff, mindiff;
mindiff = ~0;
for (i = j = 0; db->r < 256; db++, i++)
{
diffr = r - db->r;
diffg = g - db->g;
diffb = b - db->b;
#ifdef FL_LINEAR
diff = unsigned(3.0 * (FL_abs(r - db->r)) +
(5.9 * FL_abs(g - db->g)) +
(1.1 * (FL_abs(b - db->b)));
#else
diff = unsigned(3.0 * (diffr *diffr) +
5.9 * (diffg *diffg) +
1.1 * (diffb *diffb));
#endif
if (mindiff > diff)
{
mindiff = diff;
j = i;
}
}
return j;
}
static void
search_rgb(Fl_Widget *, long)
{
int r, g, b, i;
int top = fl_get_browser_topline(colbr);
r = int(fl_get_slider_value(rs));
g = int(fl_get_slider_value(gs));
b = int(fl_get_slider_value(bs));
fl_freeze_form(cl);
fl_mapcolor(FL_FREE_COL4, r, g, b);
fl_redraw_object(rescol);
i = search_entry(r, g, b);
/* change topline only if necessary */
if(i < top || i > (top+15))
fl_set_browser_topline(colbr, i-8);
fl_select_browser_line(colbr, i + 1);
fl_unfreeze_form(cl);
}
/* change database */
static void
db_cb(Fl_Widget * ob, long)
{
const char *p = fl_show_input("Enter New Database Name", dbname);
char buf[512];
if (!p || strcmp(p, dbname) == 0)
return;
strcpy(buf, p);
if (load_browser(buf))
strcpy(dbname, buf);
else
fl_set_object_label(ob, dbname);
}
static void
done_cb(Fl_Widget *, long)
{
exit(0);
}
static void
create_form_cl(void)
{
Fl_Widget *obj;
if (cl)
return;
cl = fl_bgn_form(FL_NO_BOX, 330, 385);
obj = fl_add_box(FL_UP_BOX, 0, 0, 330, 385, "");
fl_set_object_color(obj, FL_INDIANRED, FL_COL1);
obj = fl_add_box(FL_NO_BOX, 40, 10, 250, 30, "Color Browser");
fl_set_object_lcol(obj, FL_RED);
fl_set_object_lsize(obj, FL_HUGE_SIZE);
fl_set_object_lstyle(obj, FL_BOLD_STYLE + FL_SHADOW_STYLE);
dbobj = obj = fl_add_button(FL_NORMAL_BUTTON, 40, 50, 250, 25, "");
fl_set_object_boxtype(obj, FL_BORDER_BOX);
fl_set_object_color(obj, /*fl_get_visual_depth()==1 ? FL_WHITE:*/ FL_INDIANRED,
FL_INDIANRED);
fl_set_object_callback(obj, db_cb, 0);
rs = obj = fl_add_valslider(FL_VERT_FILL_SLIDER, 225, 130, 30, 200, "");
fl_set_object_color(obj, FL_INDIANRED, FL_RED);
fl_set_slider_bounds(obj, 0, 255);
fl_set_slider_precision(obj, 0);
fl_set_object_callback(obj, search_rgb, 0);
fl_set_slider_return(obj, 0);
gs = obj = fl_add_valslider(FL_VERT_FILL_SLIDER, 255, 130, 30, 200, "");
fl_set_object_color(obj, FL_INDIANRED, FL_GREEN);
fl_set_slider_bounds(obj, 0, 255);
fl_set_slider_precision(obj, 0);
fl_set_object_callback(obj, search_rgb, 1);
fl_set_slider_return(obj, 0);
bs = obj = fl_add_valslider(FL_VERT_FILL_SLIDER, 285, 130, 30, 200, "");
fl_set_object_color(obj, FL_INDIANRED, FL_BLUE);
fl_set_slider_bounds(obj, 0, 255);
fl_set_slider_precision(obj, 0);
fl_set_object_callback(obj, search_rgb, 2);
fl_set_slider_return(obj, 0);
colbr = obj = fl_add_browser(FL_HOLD_BROWSER, 10, 90, 205, 240, "");
fl_set_browser_fontstyle(obj, FL_FIXED_STYLE);
fl_set_object_callback(obj, br_cb, 0);
obj = fl_add_button(FL_NORMAL_BUTTON, 135, 345, 80, 30, "Done");
fl_set_object_callback(obj, done_cb, 0);
rescol = obj = fl_add_box(FL_FLAT_BOX, 225, 90, 90, 35, "");
fl_set_object_color(obj, FL_FREE_COL4, FL_FREE_COL4);
fl_set_object_boxtype(obj, FL_BORDER_BOX);
fl_end_form();
fl_scale_form(cl, 1.1, 1.0);
}
//
// End of "$Id$".
//