-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vp_Color_Chooser.cpp
589 lines (547 loc) · 16.8 KB
/
Vp_Color_Chooser.cpp
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
// viewpoints - interactive linked scatterplots and more.
// copyright 2005 Creon Levit and Paul Gazis, all rights reserved.
//***************************************************************************
// File name: Vp_Color_Chooser.cpp
//
// Class definitions:
// Flcc_HueBox --
// Flcc_ValueBox --
// Flcc_Value_Input --
// Vp_Color_Chooser -- Color chooser window for Creon Levit's viewpoints
//
// Classes referenced:
// Various FLTK classes
//
// Required packages: none
//
// Compiler directives:
// Requires WIN32 to be defined
//
// Purpose: Source code for <Vp_Color_Chooser.h>
//
// Author: Bill Spitzak and others 1998-2005
// Modified: Creon Levitt 14-Aug-2007
//***************************************************************************
// modified version of Fl_Color_Chooser by creon
#include <FL/Fl.H>
#include <FL/fl_draw.H>
#include <FL/math.h>
#include <stdio.h>
#include "Vp_Color_Chooser.h"
// The "hue box" can be a circle or rectilinear.
// You get a circle by defining this:
#define CIRCLE 1
// And the "hue box" can auto-update when the value changes
// you get this by defining this:
#define UPDATE_HUE_BOX 1
//*****************************************************************************
// Vp_Color_Chooser::hsv2rgb( H, S, V, R, G, B) -- Convert HSV to RGB
void Vp_Color_Chooser::hsv2rgb(
double H, double S, double V, double& R, double& G, double& B)
{
if (S < 5.0e-6) {
R = G = B = V;
}
else {
int i = (int)H;
double f = H - (float)i;
double p1 = V*(1.0-S);
double p2 = V*(1.0-S*f);
double p3 = V*(1.0-S*(1.0-f));
switch (i) {
case 0: R = V; G = p3; B = p1; break;
case 1: R = p2; G = V; B = p1; break;
case 2: R = p1; G = V; B = p3; break;
case 3: R = p1; G = p2; B = V; break;
case 4: R = p3; G = p1; B = V; break;
case 5: R = V; G = p1; B = p2; break;
}
}
}
//*****************************************************************************
// Vp_Color_Chooser::hsv2rgb( H, S, V, R, G, B) -- Convert RGB to HSV
void Vp_Color_Chooser::rgb2hsv(
double R, double G, double B, double& H, double& S, double& V)
{
double maxv = R > G ? R : G; if (B > maxv) maxv = B;
V = maxv;
if (maxv>0) {
double minv = R < G ? R : G; if (B < minv) minv = B;
S = 1.0 - double(minv)/maxv;
if (maxv > minv) {
if (maxv == R) {H = (G-B)/double(maxv-minv); if (H<0) H += 6.0;}
else if (maxv == G) H = 2.0+(B-R)/double(maxv-minv);
else H = 4.0+(R-G)/double(maxv-minv);
}
}
}
// Enumeration and static variable to hold SV and RGB mosed
enum {M_RGB, M_HSV}; // modes
static Fl_Menu_Item mode_menu[] = {
{"RGB"},
{"HSV"},
{0}
};
//*****************************************************************************
// Flcc_Value_Input::format -- Pass format to Fl_Valuator
int Flcc_Value_Input::format(char* buf) {
return Fl_Valuator::format(buf);
}
//*****************************************************************************
// Vp_Color_Chooser::set_valuators() -- Set valuators for HSV or RGB modes
void Vp_Color_Chooser::set_valuators()
{
switch (mode()) {
case M_RGB:
rvalue.range(0,1); rvalue.step(1,1000); rvalue.value(r_);
gvalue.range(0,1); gvalue.step(1,1000); gvalue.value(g_);
bvalue.range(0,1); bvalue.step(1,1000); bvalue.value(b_);
break;
case M_HSV:
rvalue.range(0,6); rvalue.step(1,1000); rvalue.value(hue_);
gvalue.range(0,1); gvalue.step(1,1000); gvalue.value(saturation_);
bvalue.range(0,1); bvalue.step(1,1000); bvalue.value(value_);
break;
}
}
//*****************************************************************************
// Vp_Color_Chooser::rgb( R, G, B) -- Set RGB values
int Vp_Color_Chooser::rgb(double R, double G, double B)
{
if (R == r_ && G == g_ && B == b_) return 0;
r_ = R; g_ = G; b_ = B;
double ph = hue_;
double ps = saturation_;
double pv = value_;
rgb2hsv(R,G,B,hue_,saturation_,value_);
set_valuators();
set_changed();
if (value_ != pv) {
#ifdef UPDATE_HUE_BOX
huebox.damage(FL_DAMAGE_SCROLL);
#endif
valuebox.damage(FL_DAMAGE_EXPOSE);}
if (hue_ != ph || saturation_ != ps) {
huebox.damage(FL_DAMAGE_EXPOSE);
valuebox.damage(FL_DAMAGE_SCROLL);
}
return 1;
}
//*****************************************************************************
// Vp_Color_Chooser::hsv( R, G, B) -- Set HSV values
int Vp_Color_Chooser::hsv(double H, double S, double V)
{
H = fmod(H,6.0); if (H < 0.0) H += 6.0;
if (S < 0.0) S = 0.0; else if (S > 1.0) S = 1.0;
if (V < 0.0) V = 0.0; else if (V > 1.0) V = 1.0;
if (H == hue_ && S == saturation_ && V == value_) return 0;
double ph = hue_;
double ps = saturation_;
double pv = value_;
hue_ = H; saturation_ = S; value_ = V;
if (value_ != pv) {
#ifdef UPDATE_HUE_BOX
huebox.damage(FL_DAMAGE_SCROLL);
#endif
valuebox.damage(FL_DAMAGE_EXPOSE);}
if (hue_ != ph || saturation_ != ps) {
huebox.damage(FL_DAMAGE_EXPOSE);
valuebox.damage(FL_DAMAGE_SCROLL);
}
hsv2rgb(H,S,V,r_,g_,b_);
set_valuators();
set_changed();
return 1;
}
////////////////////////////////////////////////////////////////
//*****************************************************************************
// tohs( x, y, h, s) -- Global method
static void tohs(double x, double y, double& h, double& s) {
#ifdef CIRCLE
x = 2*x-1;
y = 1-2*y;
s = sqrt(x*x+y*y); if (s > 1.0) s = 1.0;
h = (3.0/M_PI)*atan2(y,x);
if (h<0) h += 6.0;
#else
h = fmod(6.0*x,6.0); if (h < 0.0) h += 6.0;
s = 1.0-y; if (s < 0.0) s = 0.0; else if (s > 1.0) s = 1.0;
#endif
}
//*****************************************************************************
// Flcc_HueBox::handle( e) --
int Flcc_HueBox::handle( int e)
{
static double ih, is;
Vp_Color_Chooser* c = (Vp_Color_Chooser*)parent();
switch (e) {
case FL_PUSH:
if (Fl::visible_focus()) {
Fl::focus(this);
redraw();
}
ih = c->hue();
is = c->saturation();
case FL_DRAG: {
double Xf, Yf, H, S;
Xf = (Fl::event_x()-x()-Fl::box_dx(box()))/double(w()-Fl::box_dw(box()));
Yf = (Fl::event_y()-y()-Fl::box_dy(box()))/double(h()-Fl::box_dh(box()));
tohs(Xf, Yf, H, S);
if (fabs(H-ih) < 3*6.0/w()) H = ih;
if (fabs(S-is) < 3*1.0/h()) S = is;
if (Fl::event_state(FL_CTRL)) H = ih;
if (c->hsv(H, S, c->value())) c->do_callback();
} return 1;
case FL_FOCUS :
case FL_UNFOCUS :
if (Fl::visible_focus()) {
redraw();
return 1;
}
else return 1;
case FL_KEYBOARD :
return handle_key(Fl::event_key());
default:
return 0;
}
}
//*****************************************************************************
// generate_image( void* vv, X, Y, W, buf) -- global static method
static void generate_image(void* vv, int X, int Y, int W, uchar* buf)
{
Flcc_HueBox* v = (Flcc_HueBox*)vv;
int iw = v->w()-Fl::box_dw(v->box());
double Yf = double(Y)/(v->h()-Fl::box_dh(v->box()));
#ifdef UPDATE_HUE_BOX
const double V = ((Vp_Color_Chooser*)(v->parent()))->value();
#else
const double V = 1.0;
#endif
for (int x = X; x < X+W; x++) {
double Xf = double(x)/iw;
double H,S; tohs(Xf,Yf,H,S);
double r=0,g=0,b=0;
Vp_Color_Chooser::hsv2rgb(H,S,V,r,g,b);
*buf++ = uchar(255*r+.5);
*buf++ = uchar(255*g+.5);
*buf++ = uchar(255*b+.5);
}
}
//*****************************************************************************
// Flcc_HueBox::handle_key( key) --
int Flcc_HueBox::handle_key(int key)
{
int w1 = w()-Fl::box_dw(box())-6;
int h1 = h()-Fl::box_dh(box())-6;
Vp_Color_Chooser* c = (Vp_Color_Chooser*)parent();
#ifdef CIRCLE
int X = int(.5*(cos(c->hue()*(M_PI/3.0))*c->saturation()+1) * w1);
int Y = int(.5*(1-sin(c->hue()*(M_PI/3.0))*c->saturation()) * h1);
#else
int X = int(c->hue()/6.0*w1);
int Y = int((1-c->saturation())*h1);
#endif
switch (key) {
case FL_Up :
Y -= 3;
break;
case FL_Down :
Y += 3;
break;
case FL_Left :
X -= 3;
break;
case FL_Right :
X += 3;
break;
default :
return 0;
}
double Xf, Yf, H, S;
Xf = (double)X/(double)w1;
Yf = (double)Y/(double)h1;
tohs(Xf, Yf, H, S);
if (c->hsv(H, S, c->value())) c->do_callback();
return 1;
}
//*****************************************************************************
// Flcc_HueBox::draw() --
void Flcc_HueBox::draw()
{
if (damage()&FL_DAMAGE_ALL) draw_box();
int x1 = x()+Fl::box_dx(box());
int yy1 = y()+Fl::box_dy(box());
int w1 = w()-Fl::box_dw(box());
int h1 = h()-Fl::box_dh(box());
if (damage() == FL_DAMAGE_EXPOSE) fl_clip(x1+px,yy1+py,6,6);
fl_draw_image(generate_image, this, x1, yy1, w1, h1);
if (damage() == FL_DAMAGE_EXPOSE) fl_pop_clip();
Vp_Color_Chooser* c = (Vp_Color_Chooser*)parent();
#ifdef CIRCLE
int X = int(.5*(cos(c->hue()*(M_PI/3.0))*c->saturation()+1) * (w1-6));
int Y = int(.5*(1-sin(c->hue()*(M_PI/3.0))*c->saturation()) * (h1-6));
#else
int X = int(c->hue()/6.0*(w1-6));
int Y = int((1-c->saturation())*(h1-6));
#endif
if (X < 0) X = 0; else if (X > w1-6) X = w1-6;
if (Y < 0) Y = 0; else if (Y > h1-6) Y = h1-6;
// fl_color(c->value()>.75 ? FL_BLACK : FL_WHITE);
draw_box(FL_UP_BOX,x1+X,yy1+Y,6,6,Fl::focus() == this ? FL_FOREGROUND_COLOR : FL_GRAY);
px = X; py = Y;
}
////////////////////////////////////////////////////////////////
//*****************************************************************************
// Flcc_ValueBox::handle( e) --
int Flcc_ValueBox::handle( int e)
{
static double iv;
Vp_Color_Chooser* c = (Vp_Color_Chooser*)parent();
switch (e) {
case FL_PUSH:
if (Fl::visible_focus()) {
Fl::focus(this);
redraw();
}
iv = c->value();
case FL_DRAG: {
double Yf;
Yf = 1-(Fl::event_y()-y()-Fl::box_dy(box()))/double(h()-Fl::box_dh(box()));
if (fabs(Yf-iv)<(3*1.0/h())) Yf = iv;
if (c->hsv(c->hue(),c->saturation(),Yf)) c->do_callback();
} return 1;
case FL_FOCUS :
case FL_UNFOCUS :
if (Fl::visible_focus()) {
redraw();
return 1;
}
else return 1;
case FL_KEYBOARD :
return handle_key(Fl::event_key());
default:
return 0;
}
}
//*****************************************************************************
// generate_vimage( void* vv, X, Y, W, buf) -- staic global method
static double tr, tg, tb;
static void generate_vimage(void* vv, int X, int Y, int W, uchar* buf) {
Flcc_ValueBox* v = (Flcc_ValueBox*)vv;
double Yf = 255*(1.0-double(Y)/(v->h()-Fl::box_dh(v->box())));
uchar r = uchar(tr*Yf+.5);
uchar g = uchar(tg*Yf+.5);
uchar b = uchar(tb*Yf+.5);
for (int x = X; x < X+W; x++) {
*buf++ = r; *buf++ = g; *buf++ = b;
}
}
//*****************************************************************************
// Flcc_ValueBox::draw() --
void Flcc_ValueBox::draw() {
if (damage()&FL_DAMAGE_ALL) draw_box();
Vp_Color_Chooser* c = (Vp_Color_Chooser*)parent();
c->hsv2rgb(c->hue(),c->saturation(),1.0,tr,tg,tb);
int x1 = x()+Fl::box_dx(box());
int yy1 = y()+Fl::box_dy(box());
int w1 = w()-Fl::box_dw(box());
int h1 = h()-Fl::box_dh(box());
if (damage() == FL_DAMAGE_EXPOSE) fl_clip(x1,yy1+py,w1,6);
fl_draw_image(generate_vimage, this, x1, yy1, w1, h1);
if (damage() == FL_DAMAGE_EXPOSE) fl_pop_clip();
int Y = int((1-c->value()) * (h1-6));
if (Y < 0) Y = 0; else if (Y > h1-6) Y = h1-6;
draw_box(FL_UP_BOX,x1,yy1+Y,w1,6,Fl::focus() == this ? FL_FOREGROUND_COLOR : FL_GRAY);
py = Y;
}
//*****************************************************************************
// Flcc_ValueBox::handle_key( key) --
int Flcc_ValueBox::handle_key(int key) {
int h1 = h()-Fl::box_dh(box())-6;
Vp_Color_Chooser* c = (Vp_Color_Chooser*)parent();
int Y = int((1-c->value()) * h1);
if (Y < 0) Y = 0; else if (Y > h1) Y = h1;
switch (key) {
case FL_Up :
Y -= 3;
break;
case FL_Down :
Y += 3;
break;
default :
return 0;
}
double Yf;
Yf = 1-((double)Y/(double)h1);
if (c->hsv(c->hue(),c->saturation(),Yf)) c->do_callback();
return 1;
}
////////////////////////////////////////////////////////////////
//*****************************************************************************
// Vp_Color_Chooser::rgb_cb( Fl_Widget* o, void*) --
void Vp_Color_Chooser::rgb_cb(Fl_Widget* o, void*)
{
Vp_Color_Chooser* c = (Vp_Color_Chooser*)(o->parent());
double R = c->rvalue.value();
double G = c->gvalue.value();
double B = c->bvalue.value();
if (c->mode() == M_HSV) {
if (c->hsv(R,G,B)) c->do_callback();
return;
}
if (c->mode() != M_RGB) {
R = R/255;
G = G/255;
B = B/255;
}
if (c->rgb(R,G,B)) c->do_callback();
}
//*****************************************************************************
// Vp_Color_Chooser::mode_cb( Fl_Widget* o, void*) --
void Vp_Color_Chooser::mode_cb(Fl_Widget* o, void*)
{
Vp_Color_Chooser* c = (Vp_Color_Chooser*)(o->parent());
// force them to redraw even if value is the same:
c->rvalue.value(-1);
c->gvalue.value(-1);
c->bvalue.value(-1);
c->set_valuators();
}
////////////////////////////////////////////////////////////////
//*****************************************************************************
// Vp_Color_Chooser::Vp_Color_Chooser( X, Y, W, H, L) -- Constructor
Vp_Color_Chooser::Vp_Color_Chooser(int X, int Y, int W, int H, const char* L)
: Fl_Group(0,0,195,115,L),
huebox(0,0,115,115),
valuebox(115,0,20,115),
choice(140,0,45,25),
rvalue(140,30,45,25),
gvalue(140,60,45,25),
bvalue(140,90,45,25),
resize_box(0,0,115,115)
{
end();
resizable(resize_box);
resize(X,Y,W,H);
r_ = g_ = b_ = 0;
hue_ = 0.0;
saturation_ = 0.0;
value_ = 0.0;
huebox.box(FL_DOWN_FRAME);
valuebox.box(FL_DOWN_FRAME);
choice.menu(mode_menu);
set_valuators();
rvalue.callback(rgb_cb);
gvalue.callback(rgb_cb);
bvalue.callback(rgb_cb);
rvalue.textsize(10);
gvalue.textsize(10);
bvalue.textsize(10);
choice.callback(mode_cb);
choice.box(FL_NO_BOX);
choice.textfont(FL_HELVETICA);
choice.textsize(11);
choice.clear_visible_focus();
}
////////////////////////////////////////////////////////////////
// fl_color_chooser():
#include <FL/Fl_Window.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Return_Button.H>
//*****************************************************************************
// Class: ColorChip
//
// Class definitions:
// ColorChip
//
// Classes referenced:
//
// Purpose: Derived class of Fl_Widget
//
// Functions:
// ColorChip( X, Y, W, H)
// draw()
//
// Author: Creon Levit 14-Aug-2007
// Modified: P. R. Gazis 15-Aug-2007
//*****************************************************************************
class ColorChip : public Fl_Widget {
void draw();
public:
uchar r,g,b;
ColorChip(int X, int Y, int W, int H) : Fl_Widget(X,Y,W,H) {
box(FL_ENGRAVED_FRAME);}
};
//*****************************************************************************
// ColorChip::draw() --
void ColorChip::draw() {
if (damage()&FL_DAMAGE_ALL) draw_box();
fl_rectf(x()+Fl::box_dx(box()),
y()+Fl::box_dy(box()),
w()-Fl::box_dw(box()),
h()-Fl::box_dh(box()),r,g,b);
}
//*****************************************************************************
// chooser_cb( Fl_Object* o, void* vv) -- Global static method
static void chooser_cb(Fl_Object* o, void* vv)
{
Vp_Color_Chooser* c = (Vp_Color_Chooser*)o;
ColorChip* v = (ColorChip*)vv;
v->r = uchar(255*c->r()+.5);
v->g = uchar(255*c->g()+.5);
v->b = uchar(255*c->b()+.5);
v->damage(FL_DAMAGE_EXPOSE);
}
extern const char* fl_ok;
extern const char* fl_cancel;
//*****************************************************************************
// vp_color_chooser( name, r, g, b) -- Global method
int vp_color_chooser( const char* name, double& r, double& g, double& b)
{
Fl_Window window(215,200,name);
Vp_Color_Chooser chooser(10, 10, 195, 115);
ColorChip ok_color(10, 130, 95, 25);
Fl_Return_Button ok_button(10, 165, 95, 25, fl_ok);
ColorChip cancel_color(110, 130, 95, 25);
cancel_color.r = uchar(255*r+.5); ok_color.r = cancel_color.r;
ok_color.g = cancel_color.g = uchar(255*g+.5);
ok_color.b = cancel_color.b = uchar(255*b+.5);
Fl_Button cancel_button(110, 165, 95, 25, fl_cancel);
window.resizable(chooser);
chooser.rgb(r,g,b);
chooser.callback(chooser_cb, &ok_color);
window.end();
window.set_modal();
window.hotspot(window);
window.show();
while (window.shown()) {
Fl::wait();
for (;;) {
Fl_Widget* o = Fl::readqueue();
if (!o) break;
if (o == &ok_button) {
r = chooser.r();
g = chooser.g();
b = chooser.b();
return 1;
}
if (o == &window || o == &cancel_button) return 0;
}
}
return 0;
}
//*****************************************************************************
// vp_color_chooser( name, r, g, b) -- Global method
int vp_color_chooser( const char* name, uchar& r, uchar& g, uchar& b)
{
double dr = r/255.0;
double dg = g/255.0;
double db = b/255.0;
if (vp_color_chooser(name,dr,dg,db)) {
r = uchar(255*dr+.5);
g = uchar(255*dg+.5);
b = uchar(255*db+.5);
return 1;
}
return 0;
}