Add back 2 patches for histogram and overlayframe that are working correctly and...
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / colorpicker.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 1997-2011 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "bcbutton.h"
23 #include "bccapture.h"
24 #include "bccolors.h"
25 #include "bcdisplayinfo.h"
26 #include "cstrdup.h"
27 #include "colorpicker.h"
28 #include "condition.h"
29 #include "keys.h"
30 #include "language.h"
31 #include "mutex.h"
32 #include "mwindow.h"
33 #include "bccolors.h"
34 #include "vframe.h"
35
36 #include <string.h>
37 #include <unistd.h>
38
39 #define PALETTE_DATA "palette.dat"
40
41 ColorPicker::ColorPicker(int do_alpha, const char *title)
42  : BC_DialogThread()
43 {
44         this->do_alpha = do_alpha;
45         this->title = cstrdup(title);
46 }
47
48 ColorPicker::~ColorPicker()
49 {
50         close_window();
51         delete [] title;
52 }
53
54 void ColorPicker::start_window(int color, int alpha, int ok_cancel)
55 {
56         if( running() ) {
57                 ColorWindow *window = (ColorWindow *)get_gui();
58                 if( window ) {
59                         window->lock_window("ColorPicker::start_window");
60                         window->raise_window(1);
61                         window->unlock_window();
62                 }
63                 return;
64         }
65         this->color = color;
66         this->alpha = alpha;
67         this->ok_cancel = ok_cancel;
68         start();
69 }
70
71 BC_Window* ColorPicker::new_gui()
72 {
73         char window_title[BCTEXTLEN];
74         strcpy(window_title, _(PROGRAM_NAME ": "));
75         strcat(window_title, title ? title : _("Color Picker"));
76         BC_DisplayInfo display_info;
77         int x = display_info.get_abs_cursor_x() + xS(25);
78         int y = display_info.get_abs_cursor_y() - yS(100);
79         int w = ColorWindow::calculate_w();
80         int h = ColorWindow::calculate_h();
81         if( ok_cancel )
82                 h += bmax(ColorOK::calculate_h(),ColorCancel::calculate_h());
83         int root_w = display_info.get_root_w(), root_h = display_info.get_root_h();
84         if( x+w > root_w ) x = root_w - w;
85         if( y+h > root_h ) y = root_h - h;
86         if( x < 0 ) x = 0;
87         if( y < 0 ) y = 0;
88         ColorWindow *window = new ColorWindow(this, x, y, w, h, window_title);
89         window->create_objects();
90         window->start_selection(color, !do_alpha ? -1 : alpha, ok_cancel);
91         return window;
92 }
93
94 void ColorPicker::update_gui(int color, int alpha)
95 {
96         ColorWindow *window = (ColorWindow *)get_gui();
97         if( !window ) return;
98         window->update_gui(color, alpha);
99 }
100
101 int ColorPicker::handle_new_color(int color, int alpha)
102 {
103         printf("ColorPicker::handle_new_color undefined.\n");
104         return 1;
105 }
106
107 ColorWindow::ColorWindow(ColorPicker *thread, int x, int y, int w, int h, const char *title)
108  : BC_Window(title, x, y, w, h, w, h, 0, 0, 1),
109    ColorGUI(this)
110 {
111         this->thread = thread;
112 // *** CONTEXT_HELP ***
113         context_help_set_keyword("Title");
114 }
115
116 ColorWindow::~ColorWindow()
117 {
118 }
119
120 void ColorWindow::create_objects()
121 {
122         BC_WindowBase::create_objects();
123         ColorGUI::create_objects();
124         thread->create_objects(this);
125 }
126
127 void ColorWindow::update_gui(int color, int alpha)
128 {
129         lock_window("ColorWindow::update_gui");
130         this->color = color;
131         this->alpha = alpha;
132         change_values();
133         update_display();
134         unlock_window();
135 }
136 int ColorWindow::handle_new_color(int color, int alpha)
137 {
138         lock_window("ColorWindow::handle_new_color");
139         thread->handle_new_color(color, alpha);
140         unlock_window();
141         return 1;
142 }
143
144
145 ColorGUI::ColorGUI(BC_WindowBase *window)
146 {
147         this->window = window;
148         this->title = 0;
149         this->do_alpha = 0;
150         this->ok_cancel = 0;
151         this->color = this->orig_color = BLACK;
152         this->alpha = this->orig_alpha = 255;
153
154         wheel = 0;
155         wheel_value = 0;
156         color = 0;
157
158         hue = 0; sat = 0; val = 0;
159         red = 0; grn = 0; blu = 0;
160         lum = 0; c_r = 0; c_b = 0;
161
162         hsv_h = 0;  hsv_s = 0;  hsv_v = 0;
163         rgb_r = 0;  rgb_g = 0;  rgb_b = 0;
164         yuv_y = 0;  yuv_u = 0;  yuv_v = 0;
165         aph_a = 0;
166
167         button_grabbed = 0;
168 }
169 ColorGUI::~ColorGUI()
170 {
171         delete hsv_h;  delete hsv_s;  delete hsv_v;
172         delete rgb_r;  delete rgb_g;  delete rgb_b;
173         delete yuv_y;  delete yuv_u;  delete yuv_v;
174         delete aph_a;
175
176         if( button_grabbed ) {
177                 window->ungrab_buttons();
178                 window->ungrab_cursor();
179         }
180         update_history(rgb888());
181         save_history();
182 }
183
184 void ColorGUI::add_tool(BC_WindowBase *sub_wdw)
185 {
186         window->add_tool(sub_wdw);
187 }
188
189 void ColorGUI::start_selection(int color, int alpha, int ok_cancel)
190 {
191         this->orig_color = color;
192         this->orig_alpha = alpha;
193         this->color = color;
194         this->alpha = alpha;
195         this->do_alpha = alpha < 0 ? 0 : 1;
196         this->ok_cancel = ok_cancel;
197         create_objects();
198 }
199
200 void ColorGUI::create_objects()
201 {
202         int xs5 = xS(5), xs10 = xS(10), xs15 = xS(15);
203         int ys10 = yS(10), ys15 = yS(15), ys25 = yS(25), ys40 = yS(40);
204         int x0 = xs10, y0 = ys10;
205         window->lock_window("ColorGUI::create_objects");
206         change_values();
207
208         int x = x0, y = y0;
209         add_tool(wheel = new PaletteWheel(this, x, y));
210         wheel->create_objects();
211
212         x += xS(180);  add_tool(wheel_value = new PaletteWheelValue(this, x, y));
213         wheel_value->create_objects();
214         x = x0;
215         y += yS(180);  add_tool(poutput = new PaletteOutput(this, x, y));
216         poutput->create_objects();
217         y += poutput->get_h() + yS(20);
218
219         load_history();  int x1 = x;
220         add_tool(hex_btn = new PaletteHexButton(this, x1, y));
221         char hex[BCSTRLEN];  sprintf(hex,"%06x",color);
222         x1 += hex_btn->get_w() + xs5;
223         add_tool(hex_box = new PaletteHex(this, x1, y, hex));
224         x1 += hex_box->get_w() + xs15;
225         add_tool(grab_btn = new PaletteGrabButton(this, x1, y));
226         y += hex_box->get_h() + ys15;
227         add_tool(history = new PaletteHistory(this, xs10, y));
228
229         x += xS(240);
230         add_tool(new BC_Title(x, y =y0, C_("H:"), SMALLFONT));
231         add_tool(new BC_Title(x, y+=ys25, C_("S:"), SMALLFONT));
232         add_tool(new BC_Title(x, y+=ys25, D_("colorpicker_value#V:"), SMALLFONT));
233         add_tool(new BC_Title(x, y+=ys40, C_("R:"), SMALLFONT));
234         add_tool(new BC_Title(x, y+=ys25, C_("G:"), SMALLFONT));
235         add_tool(new BC_Title(x, y+=ys25, C_("B:"), SMALLFONT));
236         add_tool(new BC_Title(x, y+=ys40, C_("Y:"), SMALLFONT));
237         add_tool(new BC_Title(x, y+=ys25, C_("U:"), SMALLFONT));
238         add_tool(new BC_Title(x, y+=ys25, D_("colorpicker_Cr#V:"), SMALLFONT));
239         if( do_alpha )
240                 add_tool(new BC_Title(x, y+=ys40, C_("A:"), SMALLFONT));
241         x += xS(24);
242         add_tool(hue = new PaletteHue(this, x, y= y0));
243         add_tool(sat = new PaletteSat(this, x, y+=ys25));
244         add_tool(val = new PaletteVal(this, x, y+=ys25));
245         add_tool(red = new PaletteRed(this, x, y+=ys40));
246         add_tool(grn = new PaletteGrn(this, x, y+=ys25));
247         add_tool(blu = new PaletteBlu(this, x, y+=ys25));
248         add_tool(lum = new PaletteLum(this, x, y+=ys40));
249         add_tool(c_r = new PaletteCr (this, x, y+=ys25));
250         add_tool(c_b = new PaletteCb (this, x, y+=ys25));
251         if( do_alpha )
252                 add_tool(palpha = new PaletteAlpha(this, x, y+=ys40));
253
254         x += hue->get_w() + xs10;
255         hsv_h = new PaletteHSV(this, x,y= y0, hsv.h, 0, 360);
256         hsv_h->set_increment(1);
257         hsv_h->set_precision(1);
258         hsv_h->create_objects();  hsv_h->set_tooltip(_("Hue"));
259         hsv_s = new PaletteHSV(this, x,y+=ys25, hsv.s, 0, 1);
260         hsv_s->create_objects();  hsv_s->set_tooltip(_("Saturation"));
261         hsv_v = new PaletteHSV(this, x,y+=ys25, hsv.v, 0, 1);
262         hsv_v->create_objects();  hsv_v->set_tooltip(_("Value"));
263         rgb_r = new PaletteRGB(this, x,y+=ys40, rgb.r, 0, 1);
264         rgb_r->create_objects();  rgb_r->set_tooltip(_("Red"));
265         rgb_g = new PaletteRGB(this, x,y+=ys25, rgb.g, 0, 1);
266         rgb_g->create_objects();  rgb_g->set_tooltip(_("Green"));
267         rgb_b = new PaletteRGB(this, x,y+=ys25, rgb.b, 0, 1);
268         rgb_b->create_objects();  rgb_b->set_tooltip(_("Blue"));
269         yuv_y = new PaletteYUV(this, x,y+=ys40, yuv.y, 0, 1);
270         yuv_y->create_objects();  yuv_y->set_tooltip(_("Luminance"));
271         yuv_u = new PaletteYUV(this, x,y+=ys25, yuv.u, 0, 1);
272         yuv_u->create_objects();  yuv_u->set_tooltip(_("Blue Luminance Difference"));
273         yuv_v = new PaletteYUV(this, x,y+=ys25, yuv.v, 0, 1);
274         yuv_v->create_objects();  yuv_v->set_tooltip(_("Red Luminance Difference"));
275         if( do_alpha ) {
276                 aph_a = new PaletteAPH(this, x,y+=ys40, aph, 0, 1);
277                 aph_a->create_objects();  aph_a->set_tooltip(_("Alpha"));
278         }
279         if( ok_cancel ) {
280                 add_tool(new ColorOK(this, window));
281                 add_tool(new ColorCancel(this, window));
282         }
283         create_objects(this);
284
285         update_display();
286         update_history();
287         window->show_window(1);
288         window->unlock_window();
289 }
290
291
292 void ColorGUI::change_values()
293 {
294         float r = ((color>>16) & 0xff) / 255.;
295         float g = ((color>>8)  & 0xff) / 255.;
296         float b = ((color>>0)  & 0xff) / 255.;
297         rgb.r = r;  rgb.g = g;  rgb.b = b;
298         aph = (float)alpha / 255;
299         update_rgb(rgb.r, rgb.g, rgb.b);
300 }
301
302
303 ColorOK::ColorOK(ColorGUI *gui, BC_WindowBase *window)
304  : BC_OKButton(window)
305 {
306         this->gui = gui;
307         this->window = window;
308 }
309 int ColorOK::handle_event()
310 {
311         gui->ok_cancel = 0;
312         gui->close_gui();
313         window->sync_display();
314         return 1;
315 }
316
317 ColorCancel::ColorCancel(ColorGUI *gui, BC_WindowBase *window)
318  : BC_CancelButton(window)
319 {
320         this->gui = gui;
321         this->window = window;
322 }
323 int ColorCancel::handle_event()
324 {
325         gui->ok_cancel = 1;
326         gui->close_gui();
327         window->sync_display();
328         return 1;
329 }
330
331 int ColorGUI::close_gui()
332 {
333         if( button_grabbed ) {
334                 button_grabbed = 0;
335                 window->ungrab_buttons();
336                 window->ungrab_cursor();
337         }
338         window->set_done(ok_cancel ? 1 : 0);
339         return 1;
340 }
341
342
343 void ColorGUI::update_rgb()
344 {
345         update_rgb(rgb.r, rgb.g, rgb.b);
346         update_display();
347 }
348 void ColorGUI::update_hsv()
349 {
350         update_hsv(hsv.h, hsv.s, hsv.v);
351         update_display();
352 }
353 void ColorGUI::update_yuv()
354 {
355         update_yuv(yuv.y, yuv.u, yuv.v);
356         update_display();
357 }
358
359 void ColorGUI::update_display()
360 {
361         wheel->draw(wheel->oldhue, wheel->oldsaturation);
362         wheel->oldhue = hsv.h;
363         wheel->oldsaturation = hsv.s;
364         wheel->draw(hsv.h, hsv.s);
365         wheel->flash();
366         wheel_value->draw(hsv.h, hsv.s, hsv.v);
367         wheel_value->flash();
368         poutput->draw();
369         poutput->flash();
370
371         hue->update((int)hsv.h);
372         sat->update(hsv.s);
373         val->update(hsv.v);
374
375         red->update(rgb.r);
376         grn->update(rgb.g);
377         blu->update(rgb.b);
378
379         lum->update(yuv.y);
380         c_r->update(yuv.u);
381         c_b->update(yuv.v);
382
383         hsv_h->update(hsv.h);
384         hsv_s->update(hsv.s);
385         hsv_v->update(hsv.v);
386         rgb_r->update(rgb.r);
387         rgb_g->update(rgb.g);
388         rgb_b->update(rgb.b);
389         yuv_y->update(yuv.y);
390         yuv_u->update(yuv.u);
391         yuv_v->update(yuv.v);
392         hex_box->update();
393
394         if( do_alpha ) {
395                 palpha->update(aph);
396                 aph_a->update(aph);
397         }
398 }
399
400 int ColorGUI::handle_gui()
401 {
402         window->unlock_window();
403         handle_new_color(rgb888(), alpha8());
404         window->lock_window("ColorGUI::handle_event");
405         return 1;
406 }
407
408 void ColorGUI::get_screen_sample()
409 {
410         int cx, cy;
411         window->get_abs_cursor(cx, cy);
412         BC_Capture capture_bitmap(1, 1, 0);
413         VFrame vframe(1,1,BC_RGB888);
414         capture_bitmap.capture_frame(&vframe, cx,cy);
415         unsigned char *data = vframe.get_data();
416         rgb.r = data[0]/255.;  rgb.g = data[1]/255.;  rgb.b = data[2]/255.;
417         update_rgb();
418 }
419
420 int ColorGUI::cursor_motion_gui()
421 {
422         if( button_grabbed && window->get_button_down() ) {
423                 get_screen_sample();
424                 return 1;
425         }
426         return 0;
427 }
428
429 int ColorGUI::button_press_gui()
430 {
431         if( button_grabbed ) {
432                 get_screen_sample();
433                 return 1;
434         }
435         return 0;
436 }
437
438 int ColorGUI::button_release_gui()
439 {
440         if( button_grabbed ) {
441                 window->ungrab_buttons();
442                 window->ungrab_cursor();
443                 grab_btn->enable();
444                 button_grabbed = 0;
445                 update_history();
446                 return handle_gui();
447         }
448         return 1;
449 }
450
451 void ColorGUI::update_rgb_hex(const char *hex)
452 {
453         unsigned color;
454         if( sscanf(hex,"%x",&color) == 1 ) {
455                 if( do_alpha ) {
456                         aph = ((color>>24) & 0xff) / 255.;
457                         aph_a->update(aph);
458                 }
459                 float r = ((color>>16) & 0xff) / 255.;
460                 float g = ((color>>8)  & 0xff) / 255.;
461                 float b = ((color>>0)  & 0xff) / 255.;
462                 rgb.r = r;  rgb.g = g;  rgb.b = b;
463                 update_rgb();
464                 update_history();
465                 handle_gui();
466         }
467 }
468
469 void ColorGUI::update_gui(int color, int alpha)
470 {
471         printf("ColorGUI::update_gui undefined.\n");
472 }
473
474 int ColorGUI::handle_new_color(int color, int alpha)
475 {
476         printf("ColorGUI::handle_new_color undefined.\n");
477         return 1;
478 }
479
480
481
482 PaletteWheel::PaletteWheel(ColorGUI *gui, int x, int y)
483  : BC_SubWindow(x, y, xS(170), yS(170))
484 {
485         this->gui = gui;
486         oldhue = 0;
487         oldsaturation = 0;
488         button_down = 0;
489 }
490
491 PaletteWheel::~PaletteWheel()
492 {
493 }
494
495 int PaletteWheel::button_press_event()
496 {
497         if( get_cursor_x() >= 0 && get_cursor_x() < get_w() &&
498                 get_cursor_y() >= 0 && get_cursor_y() < get_h() &&
499                 is_event_win() ) {
500                 button_down = 1;
501                 cursor_motion_event();
502                 return 1;
503         }
504         return 0;
505 }
506
507 int PaletteWheel::cursor_motion_event()
508 {
509         int x1, y1, distance;
510         if( button_down && is_event_win() ) {
511                 float h = get_angle(get_w()/2, get_h()/2, get_cursor_x(), get_cursor_y());
512                 bclamp(h, 0, 359.999);  gui->hsv.h = h;
513                 x1 = get_w() / 2 - get_cursor_x();
514                 y1 = get_h() / 2 - get_cursor_y();
515                 distance = (int)sqrt(x1 * x1 + y1 * y1);
516                 float s = (float)distance / (get_w() / 2);
517                 bclamp(s, 0, 1);  gui->hsv.s = s;
518                 gui->hsv.v = 1;
519                 gui->update_hsv();
520                 gui->handle_gui();
521                 return 1;
522         }
523         return 0;
524 }
525
526 int PaletteWheel::button_release_event()
527 {
528         if( button_down ) {
529                 button_down = 0;
530                 return 1;
531         }
532         return 0;
533 }
534
535 void PaletteWheel::create_objects()
536 {
537 // Upper right
538 //printf("PaletteWheel::create_objects 1\n");
539         float h, s, v = 1;
540         float r, g, b;
541         float x1, y1, x2, y2;
542         float distance;
543         int default_r, default_g, default_b;
544         VFrame frame(0, -1, get_w(), get_h(), BC_RGBA8888, -1);
545         x1 = get_w() / 2;
546         y1 = get_h() / 2;
547         default_r = (get_resources()->get_bg_color() & 0xff0000) >> 16;
548         default_g = (get_resources()->get_bg_color() & 0xff00) >> 8;
549         default_b = (get_resources()->get_bg_color() & 0xff);
550 //printf("PaletteWheel::create_objects 1\n");
551
552         int highlight_r = (get_resources()->button_light & 0xff0000) >> 16;
553         int highlight_g = (get_resources()->button_light & 0xff00) >> 8;
554         int highlight_b = (get_resources()->button_light & 0xff);
555
556         for( y2 = 0; y2 < get_h(); y2++ ) {
557                 unsigned char *row = (unsigned char*)frame.get_rows()[(int)y2];
558                 for( x2 = 0; x2 < get_w(); x2++ ) {
559                         distance = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
560                         if( distance > x1 ) {
561                                 row[(int)x2 * 4] = default_r;
562                                 row[(int)x2 * 4 + 1] = default_g;
563                                 row[(int)x2 * 4 + 2] = default_b;
564                                 row[(int)x2 * 4 + 3] = 0;
565                         }
566                         else
567                         if( distance > x1 - 1 ) {
568                                 int r_i, g_i, b_i;
569                                 if( get_h() - y2 < x2 ) {
570                                         r_i = highlight_r;
571                                         g_i = highlight_g;
572                                         b_i = highlight_b;
573                                 }
574                                 else {
575                                         r_i = 0;
576                                         g_i = 0;
577                                         b_i = 0;
578                                 }
579
580                                 row[(int)x2 * 4] = r_i;
581                                 row[(int)x2 * 4 + 1] = g_i;
582                                 row[(int)x2 * 4 + 2] = b_i;
583                                 row[(int)x2 * 4 + 3] = 255;
584                         }
585                         else {
586                                 h = get_angle(x1, y1, x2, y2);
587                                 s = distance / x1;
588                                 HSV::hsv_to_rgb(r, g, b, h, s, v);
589                                 row[(int)x2 * 4] = (int)(r * 255);
590                                 row[(int)x2 * 4 + 1] = (int)(g * 255);
591                                 row[(int)x2 * 4 + 2] = (int)(b * 255);
592                                 row[(int)x2 * 4 + 3] = 255;
593                         }
594                 }
595         }
596
597         draw_vframe(&frame,
598                 0, 0, get_w(), get_h(),
599                 0, 0, get_w(), get_h(), 0);
600
601         oldhue = gui->hsv.h;
602         oldsaturation = gui->hsv.s;
603         draw(oldhue, oldsaturation);
604         flash();
605 }
606
607 float PaletteWheel::torads(float angle)
608 {
609         return (float)angle / 360 * 2 * M_PI;
610 }
611
612
613 int PaletteWheel::draw(float hue, float saturation)
614 {
615         int x, y, w, h;
616         x = w = get_w() / 2;
617         y = h = get_h() / 2;
618
619         if( hue > 0 && hue < 90 ) {
620                 x = (int)(w - w * cos(torads(90 - hue)) * saturation);
621                 y = (int)(h - h * sin(torads(90 - hue)) * saturation);
622         }
623         else if( hue > 90 && hue < 180 ) {
624                 x = (int)(w - w * cos(torads(hue - 90)) * saturation);
625                 y = (int)(h + h * sin(torads(hue - 90)) * saturation);
626         }
627         else if( hue > 180 && hue < 270 ) {
628                 x = (int)(w + w * cos(torads(270 - hue)) * saturation);
629                 y = (int)(h + h * sin(torads(270 - hue)) * saturation);
630         }
631         else if( hue > 270 && hue < 360 ) {
632                 x = (int)(w + w * cos(torads(hue - 270)) * saturation);
633                 y = (int)(h - w * sin(torads(hue - 270)) * saturation);
634         }
635         else if( hue == 0 ) {
636                 x = w;
637                 y = (int)(h - h * saturation);
638         }
639         else if( hue == 90 ) {
640                 x = (int)(w - w * saturation);
641                 y = h;
642         }
643         else if( hue == 180 ) {
644                 x = w;
645                 y = (int)(h + h * saturation);
646         }
647         else if( hue == 270 ) {
648                 x = (int)(w + w * saturation);
649                 y = h;
650         }
651
652         set_inverse();
653         set_color(WHITE);
654         draw_circle(x - xS(5), y - yS(5), xS(10), yS(10));
655         set_opaque();
656         return 0;
657 }
658
659 int PaletteWheel::get_angle(float x1, float y1, float x2, float y2)
660 {
661         float result = -atan2(x2 - x1, y1 - y2) * (360 / M_PI / 2);
662         if( result < 0 )
663                 result += 360;
664         return (int)result;
665 }
666
667 PaletteWheelValue::PaletteWheelValue(ColorGUI *gui, int x, int y)
668  : BC_SubWindow(x, y, xS(40), yS(170), BLACK)
669 {
670         this->gui = gui;
671         button_down = 0;
672 }
673 PaletteWheelValue::~PaletteWheelValue()
674 {
675         delete frame;
676 }
677
678 void PaletteWheelValue::create_objects()
679 {
680         frame = new VFrame(get_w(), get_h(), BC_RGB888);
681         draw(gui->hsv.h, gui->hsv.s, gui->hsv.v);
682         flash();
683 }
684
685 int PaletteWheelValue::button_press_event()
686 {
687 //printf("PaletteWheelValue::button_press 1 %d\n", is_event_win());
688         if( get_cursor_x() >= 0 && get_cursor_x() < get_w() &&
689                 get_cursor_y() >= 0 && get_cursor_y() < get_h() &&
690                 is_event_win() ) {
691 //printf("PaletteWheelValue::button_press 2\n");
692                 button_down = 1;
693                 cursor_motion_event();
694                 return 1;
695         }
696         return 0;
697 }
698
699 int PaletteWheelValue::cursor_motion_event()
700 {
701         if( button_down && is_event_win() ) {
702 //printf("PaletteWheelValue::cursor_motion 1\n");
703                 float v = 1.0 - (float)(get_cursor_y() - 2) / (get_h() - 4);
704                 bclamp(v, 0, 1);  gui->hsv.v = v;
705                 gui->update_hsv();
706                 gui->handle_gui();
707                 return 1;
708         }
709         return 0;
710 }
711
712 int PaletteWheelValue::button_release_event()
713 {
714         if( button_down ) {
715 //printf("PaletteWheelValue::button_release 1\n");
716                 button_down = 0;
717                 return 1;
718         }
719         return 0;
720 }
721
722 int PaletteWheelValue::draw(float hue, float saturation, float value)
723 {
724         float r_f, g_f, b_f;
725         int i, j, r, g, b;
726
727         for( i = get_h() - 3; i >= 2; i-- ) {
728                 unsigned char *row = (unsigned char*)frame->get_rows()[i];
729                 HSV::hsv_to_rgb(r_f, g_f, b_f, hue, saturation,
730                         1.0 - (float)(i - 2) / (get_h() - 4));
731                 r = (int)(r_f * 255);
732                 g = (int)(g_f * 255);
733                 b = (int)(b_f * 255);
734                 for( j = 0; j < get_w(); j++ ) {
735                         row[j * 3] = r;
736                         row[j * 3 + 1] = g;
737                         row[j * 3 + 2] = b;
738                 }
739         }
740         draw_3d_border(0, 0, get_w(), get_h(), 1);
741         draw_vframe(frame, 2, 2, get_w() - 4, get_h() - 4,
742                 2, 2, get_w() - 4, get_h() - 4, 0);
743         set_color(BLACK);
744         draw_line(2, get_h() - 3 - (int)(value * (get_h() - 5)),
745                   get_w() - 3, get_h() - 3 - (int)(value * (get_h() - 5)));
746 //printf("PaletteWheelValue::draw %d %f\n", __LINE__, value);
747
748         return 0;
749 }
750
751 PaletteOutput::PaletteOutput(ColorGUI *gui, int x, int y)
752  : BC_SubWindow(x, y, xS(180), yS(30), BLACK)
753 {
754         this->gui = gui;
755 }
756 PaletteOutput::~PaletteOutput()
757 {
758 }
759
760
761 void PaletteOutput::create_objects()
762 {
763         draw();
764         flash();
765 }
766
767 int PaletteOutput::handle_event()
768 {
769         return 1;
770 }
771
772 int PaletteOutput::draw()
773 {
774         set_color(gui->rgb888());
775         draw_box(2, 2, get_w() - 4, get_h() - 4);
776         draw_3d_border(0, 0, get_w(), get_h(), 1);
777         return 0;
778 }
779
780 PaletteHue::PaletteHue(ColorGUI *gui, int x, int y)
781  : BC_ISlider(x, y, 0, xS(150), xS(200), 0, 359, (int)(gui->hsv.h), 0)
782 {
783         this->gui = gui;
784 }
785 PaletteHue::~PaletteHue()
786 {
787 }
788
789 int PaletteHue::handle_event()
790 {
791         gui->hsv.h = get_value();
792         gui->update_hsv();
793         gui->handle_gui();
794         return 1;
795 }
796
797 PaletteSat::PaletteSat(ColorGUI *gui, int x, int y)
798  : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1.0, gui->hsv.s, 0)
799 {
800         this->gui = gui;
801         set_precision(0.01);
802 }
803 PaletteSat::~PaletteSat()
804 {
805 }
806
807 int PaletteSat::handle_event()
808 {
809         gui->hsv.s = get_value();
810         gui->update_hsv();
811         gui->handle_gui();
812         return 1;
813 }
814
815
816 PaletteVal::PaletteVal(ColorGUI *gui, int x, int y)
817  : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1.0, gui->hsv.v, 0)
818 {
819         this->gui = gui;
820         set_precision(0.01);
821 }
822 PaletteVal::~PaletteVal()
823 {
824 }
825
826 int PaletteVal::handle_event()
827 {
828         gui->hsv.v = get_value();
829         gui->update_hsv();
830         gui->handle_gui();
831         return 1;
832 }
833
834
835 PaletteRed::PaletteRed(ColorGUI *gui, int x, int y)
836  : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, gui->rgb.r, 0)
837 {
838         this->gui = gui;
839         set_precision(0.01);
840 }
841 PaletteRed::~PaletteRed()
842 {
843 }
844
845 int PaletteRed::handle_event()
846 {
847         gui->rgb.r = get_value();
848         gui->update_rgb();
849         gui->handle_gui();
850         return 1;
851 }
852
853 PaletteGrn::PaletteGrn(ColorGUI *gui, int x, int y)
854  : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, gui->rgb.g, 0)
855 {
856         this->gui = gui;
857         set_precision(0.01);
858 }
859 PaletteGrn::~PaletteGrn()
860 {
861 }
862
863 int PaletteGrn::handle_event()
864 {
865         gui->rgb.g = get_value();
866         gui->update_rgb();
867         gui->handle_gui();
868         return 1;
869 }
870
871 PaletteBlu::PaletteBlu(ColorGUI *gui, int x, int y)
872  : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, gui->rgb.b, 0)
873 {
874         this->gui = gui;
875         set_precision(0.01);
876 }
877 PaletteBlu::~PaletteBlu()
878 {
879 }
880
881 int PaletteBlu::handle_event()
882 {
883         gui->rgb.b = get_value();
884         gui->update_rgb();
885         gui->handle_gui();
886         return 1;
887 }
888
889 PaletteAlpha::PaletteAlpha(ColorGUI *gui, int x, int y)
890  : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, gui->aph, 0)
891 {
892         this->gui = gui;
893         set_precision(0.01);
894 }
895 PaletteAlpha::~PaletteAlpha()
896 {
897 }
898
899 int PaletteAlpha::handle_event()
900 {
901         gui->aph = get_value();
902         gui->aph_a->update(gui->aph);
903         gui->hex_box->update();
904         gui->handle_gui();
905         return 1;
906 }
907
908 PaletteLum::PaletteLum(ColorGUI *gui, int x, int y)
909  : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, gui->yuv.y, 0)
910 {
911         this->gui = gui;
912         set_precision(0.01);
913 }
914 PaletteLum::~PaletteLum()
915 {
916 }
917
918 int PaletteLum::handle_event()
919 {
920         gui->yuv.y = get_value();
921         gui->update_yuv();
922         gui->handle_gui();
923         return 1;
924 }
925
926 PaletteCr::PaletteCr(ColorGUI *gui, int x, int y)
927  : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, gui->yuv.u, 0)
928 {
929         this->gui = gui;
930         set_precision(0.01);
931 }
932 PaletteCr::~PaletteCr()
933 {
934 }
935
936 int PaletteCr::handle_event()
937 {
938         gui->yuv.u = get_value();
939         gui->update_yuv();
940         gui->handle_gui();
941         return 1;
942 }
943
944 PaletteCb::PaletteCb(ColorGUI *gui, int x, int y)
945  : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, gui->yuv.v, 0)
946 {
947         this->gui = gui;
948         set_precision(0.01);
949 }
950 PaletteCb::~PaletteCb()
951 {
952 }
953
954 int PaletteCb::handle_event()
955 {
956         gui->yuv.v = get_value();
957         gui->update_yuv();
958         gui->handle_gui();
959         return 1;
960 }
961
962 void ColorGUI::update_rgb(float r, float g, float b)
963 {
964         { float y, u, v;
965         YUV::yuv.rgb_to_yuv_f(r, g, b, y, u, v);
966         u += 0.5;  v += 0.5;
967         bclamp(y, 0, 1);    yuv.y = y;
968         bclamp(u, 0, 1);    yuv.u = u;
969         bclamp(v, 0, 1);    yuv.v = v; }
970         { float h, s, v;
971         HSV::rgb_to_hsv(r,g,b, h,s,v);
972         bclamp(h, 0, 360);  hsv.h = h;
973         bclamp(s, 0, 1);    hsv.s = s;
974         bclamp(v, 0, 1);    hsv.v = v; }
975 }
976
977 void ColorGUI::update_yuv(float y, float u, float v)
978 {
979         u -= 0.5;  v -= 0.5;
980         { float r, g, b;
981         YUV::yuv.yuv_to_rgb_f(r, g, b, y, u, v);
982         bclamp(r, 0, 1);   rgb.r = r;
983         bclamp(g, 0, 1);   rgb.g = g;
984         bclamp(b, 0, 1);   rgb.b = b;
985         float h, s, v;
986         HSV::rgb_to_hsv(r,g,b, h, s, v);
987         bclamp(h, 0, 360); hsv.h = h;
988         bclamp(s, 0, 1);   hsv.s = s;
989         bclamp(v, 0, 1);   hsv.v = v; }
990 }
991
992 void ColorGUI::update_hsv(float h, float s, float v)
993 {
994         { float r, g, b;
995         HSV::hsv_to_rgb(r,g,b, h,s,v);
996         bclamp(r, 0, 1);   rgb.r = r;
997         bclamp(g, 0, 1);   rgb.g = g;
998         bclamp(b, 0, 1);   rgb.b = b;
999         float y, u, v;
1000         YUV::yuv.rgb_to_yuv_f(r, g, b, y, u, v);
1001         u += 0.5;  v += 0.5;
1002         bclamp(y, 0, 1);   yuv.y = y;
1003         bclamp(u, 0, 1);   yuv.u = u;
1004         bclamp(v, 0, 1);   yuv.v = v; }
1005 }
1006
1007 void ColorGUI::load_history()
1008 {
1009         char history_path[BCTEXTLEN];
1010         MWindow::create_defaults_path(history_path,PALETTE_DATA);
1011         FILE *fp = fopen(history_path,"r");
1012         int i=0;
1013         if( fp ) {
1014                 while( i < PALLETTE_HISTORY_SIZE ) {
1015                         char line[BCSTRLEN];
1016                         if( !fgets(line,sizeof(line)-1,fp) ) break;
1017                         line[sizeof(line)-1] = 0;
1018                         if( sscanf(line, "%x",&palette_history[i]) != 1 ) break;
1019                         ++i;
1020                 }
1021                 fclose(fp);
1022         }
1023         int r = 0, g = 0, b = 0;
1024         float v0 = 0, v1 = 1;
1025         while( i < PALLETTE_HISTORY_SIZE ) {
1026                 int grey_code = i ^ (i>>1);
1027                 r = 255 * ((grey_code&4) ? v0 : v1);
1028                 g = 255 * ((grey_code&2) ? v0 : v1);
1029                 b = 255 * ((grey_code&1) ? v0 : v1);
1030                 int color = (r<<16) | (g<<8) | (b<<0);
1031                 palette_history[i++] = color;
1032                 if( i & 7 ) continue;
1033                 v0 = 0.5f * (v0+.5f);
1034                 v1 = 0.5f * (v1+.5f);
1035         }
1036 }
1037 void ColorGUI::save_history()
1038 {
1039         char history_path[BCTEXTLEN];
1040         MWindow::create_defaults_path(history_path,PALETTE_DATA);
1041         FILE *fp = fopen(history_path,"w");
1042         if( fp ) {
1043                 for( int i=0; i<PALLETTE_HISTORY_SIZE; ++i ) {
1044                         fprintf(fp, "%06x\n", palette_history[i]);
1045                 }
1046                 fclose(fp);
1047         }
1048 }
1049 void ColorGUI::update_history(int color)
1050 {
1051         int out = palette_history[0];
1052         palette_history[0] = color;
1053         for( int i=1; out != color && i<PALLETTE_HISTORY_SIZE; ++i ) {
1054                 int in = out;
1055                 out = palette_history[i];
1056                 palette_history[i] = in;
1057         }
1058 }
1059 void ColorGUI::update_history()
1060 {
1061         update_history(rgb888());
1062         history->update(0);
1063 }
1064 int ColorGUI::rgb888()
1065 {
1066         int r = 255*rgb.r + 0.5, g = 255*rgb.g + 0.5, b = 255*rgb.b + 0.5;
1067         bclamp(r, 0, 255);  bclamp(g, 0, 255);  bclamp(b, 0, 255);
1068         return (r<<16) | (g<<8) | (b<<0);
1069 }
1070 int ColorGUI::alpha8()
1071 {
1072         int a = 255*aph + 0.5;
1073         bclamp(a, 0, 255);
1074         return a;
1075 }
1076
1077 PaletteNum::PaletteNum(ColorGUI *gui, int x, int y,
1078         float &output, float min, float max)
1079  : BC_TumbleTextBox(gui->window, output, min, max, x, y, xS(64))
1080 {
1081         this->gui = gui;
1082         this->output = &output;
1083         set_increment(0.01);
1084         set_precision(2);
1085 }
1086
1087 PaletteNum::~PaletteNum()
1088 {
1089 }
1090
1091
1092 int PaletteHSV::handle_event()
1093 {
1094         update_output();
1095         gui->update_hsv();
1096         gui->handle_gui();
1097         return 1;
1098 }
1099
1100 int PaletteRGB::handle_event()
1101 {
1102         update_output();
1103         gui->update_rgb();
1104         gui->handle_gui();
1105         return 1;
1106 }
1107
1108 int PaletteYUV::handle_event()
1109 {
1110         update_output();
1111         gui->update_yuv();
1112         gui->handle_gui();
1113         return 1;
1114 }
1115
1116 int PaletteAPH::handle_event()
1117 {
1118         update_output();
1119         gui->update_display();
1120         gui->handle_gui();
1121         return 1;
1122 }
1123
1124 PaletteHexButton::PaletteHexButton(ColorGUI *gui, int x, int y)
1125  : BC_GenericButton(x, y, xS(50), "#")
1126 {
1127         this->gui = gui;
1128         set_tooltip(_("hex rgb color"));
1129 }
1130 PaletteHexButton::~PaletteHexButton()
1131 {
1132 }
1133 int PaletteHexButton::handle_event()
1134 {
1135         const char *hex = gui->hex_box->get_text();
1136         gui->update_rgb_hex(hex);
1137         return 1;
1138 }
1139
1140 PaletteHex::PaletteHex(ColorGUI *gui, int x, int y, const char *hex)
1141  : BC_TextBox(x, y, xS(100), 1, hex)
1142 {
1143         this->gui = gui;
1144 }
1145 PaletteHex::~PaletteHex()
1146 {
1147 }
1148 void PaletteHex::update()
1149 {
1150         char hex[BCSTRLEN], *cp = hex;
1151         if( gui->do_alpha )
1152                 cp += sprintf(cp,"%02x", gui->alpha8());
1153         sprintf(cp,"%06x",gui->rgb888());
1154         BC_TextBox::update(hex);
1155 }
1156
1157 int PaletteHex::keypress_event()
1158 {
1159         if( get_keypress() != RETURN )
1160                 return BC_TextBox::keypress_event();
1161         gui->update_rgb_hex(get_text());
1162         return 1;
1163 }
1164
1165 #include "grabpick_up_png.h"
1166 #include "grabpick_hi_png.h"
1167 #include "grabpick_dn_png.h"
1168
1169 PaletteGrabButton::PaletteGrabButton(ColorGUI *gui, int x, int y)
1170  : BC_Button(x, y, vframes)
1171 {
1172         this->gui = gui;
1173         vframes[0] = new VFramePng(grabpick_up_png);
1174         vframes[1] = new VFramePng(grabpick_hi_png);
1175         vframes[2] = new VFramePng(grabpick_dn_png);
1176         set_tooltip(_("grab from anywhere picker"));
1177 }
1178 PaletteGrabButton::~PaletteGrabButton()
1179 {
1180         for( int i=0; i<3; ++i )
1181                 delete vframes[i];
1182 }
1183 int PaletteGrabButton::handle_event()
1184 {
1185         if( gui->window->grab_buttons() ) {
1186                 gui->window->grab_cursor();
1187                 gui->button_grabbed = 1;
1188                 return BC_Button::button_press_event(); // redraw face HI
1189         }
1190         return 1;
1191 }
1192
1193 PaletteHistory::PaletteHistory(ColorGUI *gui, int x, int y)
1194  : BC_SubWindow(x,y, xS(200), yS(24))
1195 {
1196         this->gui = gui;
1197         button_down = 0;
1198         set_tooltip(_("color history"));
1199 }
1200 PaletteHistory::~PaletteHistory()
1201 {
1202 }
1203 void PaletteHistory::update(int flush)
1204 {
1205         int x1 = 0, x2 = 0;
1206         for( int i=0; i<PALLETTE_HISTORY_SIZE; x1=x2 ) {
1207                 int rgb = gui->palette_history[i];
1208                 x2 = (++i * get_w())/PALLETTE_HISTORY_SIZE;
1209                 draw_3d_box(x1,0,x2-x1,get_h(),WHITE,BLACK,rgb,LTBLUE,DKBLUE);
1210         }
1211         flash(flush);
1212 }
1213
1214 int PaletteHistory::button_press_event()
1215 {
1216         if( button_down || !is_event_win() ) return 0;
1217         button_down =  1;
1218         cursor_motion_event();
1219         return 1;
1220 }
1221 int PaletteHistory::button_release_event()
1222 {
1223         if( !button_down || !is_event_win() ) return 0;
1224         cursor_motion_event();
1225         if( button_down > 0 ) {
1226                 gui->handle_gui();
1227                 gui->update_display();
1228                 gui->update_history();
1229         }
1230         button_down =  0;
1231         return 1;
1232 }
1233 int PaletteHistory::cursor_motion_event()
1234 {
1235         if( !button_down || !is_event_win() ) return 0;
1236         hide_tooltip();
1237         int pick = (PALLETTE_HISTORY_SIZE * get_cursor_x()) / get_w();
1238         bclamp(pick, 0, PALLETTE_HISTORY_SIZE-1);
1239         int color = gui->palette_history[pick];
1240         float r = ((color>>16) & 0xff) / 255.;
1241         float g = ((color>>8)  & 0xff) / 255.;
1242         float b = ((color>>0)  & 0xff) / 255.;
1243         if( gui->rgb.r != r || gui->rgb.g != g || gui->rgb.b != b ) {
1244                 gui->rgb.r = r;  gui->rgb.g = g;  gui->rgb.b = b;
1245                 gui->update_rgb();
1246         }
1247         return 1;
1248 }
1249
1250 int PaletteHistory::cursor_leave_event()
1251 {
1252         hide_tooltip();
1253         return 0;
1254 }
1255 int PaletteHistory::repeat_event(int64_t duration)
1256 {
1257         int result = 0;
1258
1259         if( duration == get_resources()->tooltip_delay &&
1260             get_tooltip() && *get_tooltip() && cursor_above() ) {
1261                 show_tooltip();
1262                 result = 1;
1263         }
1264         return result;
1265 }
1266
1267
1268 ColorButton::ColorButton(const char *title,
1269         int x, int y, int w, int h,
1270         int color, int alpha, int ok_cancel)
1271  : BC_Button(x, y, w, vframes)
1272 {
1273         this->title = title;
1274         this->color = this->orig_color = color;
1275         this->alpha = this->orig_alpha = alpha;
1276         this->ok_cancel = ok_cancel;
1277
1278         for( int i=0; i<3; ++i ) {
1279                 vframes[i] = new VFrame(w, h, BC_RGBA8888);
1280                 vframes[i]->clear_frame();
1281         }
1282         color_picker = 0;
1283         color_thread = 0;
1284 }
1285
1286 ColorButton::~ColorButton()
1287 {
1288         delete color_thread;
1289         delete color_picker;
1290         for( int i=0; i<3; ++i )
1291                 delete vframes[i];
1292 }
1293
1294 void ColorButton::set_color(int color)
1295 {
1296         printf("ColorButton::set_color %06x\n", color);
1297 }
1298 void ColorButton::handle_done_event(int result)
1299 {
1300         color_thread->stop();
1301 }
1302 int ColorButton::handle_new_color(int color, int alpha)
1303 {
1304         printf("ColorButton::handle_new_color %02x%06x\n", alpha, color);
1305         return 1;
1306 }
1307
1308 int ColorButton::handle_event()
1309 {
1310         unlock_window();
1311         delete color_picker;
1312         color_picker = new ColorButtonPicker(this);
1313         orig_color = color;  orig_alpha = alpha;
1314         color_picker->start_window(color, alpha, ok_cancel);
1315         if( !color_thread )
1316                 color_thread = new ColorButtonThread(this);
1317         color_thread->start();
1318         lock_window("ColorButtonButton::start_color_thread");
1319         return 1;
1320 }
1321
1322 void ColorButton::close_picker()
1323 {
1324         if( color_thread ) color_thread->stop();
1325         delete color_thread;  color_thread = 0;
1326         delete color_picker;  color_picker = 0;
1327 }
1328
1329 void ColorButton::update_gui(int color, int alpha)
1330 {
1331         if( color_picker )
1332                 color_picker->update_gui(color, alpha);
1333         update_gui(color | (~alpha<<24));
1334 }
1335
1336 void ColorButton::update_gui(int color)
1337 {
1338         set_color(color);
1339         draw_face();
1340 }
1341
1342 ColorButtonPicker::ColorButtonPicker(ColorButton *color_button)
1343  : ColorPicker(color_button->alpha >= 0 ? 1 : 0, color_button->title)
1344 {
1345         this->color_button = color_button;
1346 }
1347
1348 ColorButtonPicker::~ColorButtonPicker()
1349 {
1350 }
1351
1352 void ColorButtonPicker::handle_done_event(int result)
1353 {
1354         color_button->color_thread->stop();
1355         color_button->handle_done_event(result);
1356 }
1357
1358 void ColorButtonPicker::update(int color, int alpha)
1359 {
1360         color_button->color = color;
1361         color_button->alpha = alpha;
1362         color_button->color_thread->update_lock->unlock();
1363 }
1364
1365 int ColorButtonPicker::handle_new_color(int color, int alpha)
1366 {
1367         color_button->lock_window("ColorButtonPicker::handle_new_color");
1368         color_button->update_gui(color);
1369         color_button->unlock_window();
1370         return color_button->handle_new_color(color, alpha);
1371 }
1372
1373 void ColorButtonPicker::update_gui()
1374 {
1375         color_button->lock_window("ColorButtonPicker::update_gui");
1376         color_button->update_gui(color_button->color, color_button->alpha);
1377         color_button->unlock_window();
1378 }
1379
1380 void ColorButtonPicker::update_gui(int color, int alpha)
1381 {
1382         ColorPicker::update_gui(color, alpha);
1383         color_button->handle_new_color(color, alpha);
1384 }
1385
1386 ColorButtonThread::ColorButtonThread(ColorButton *color_button)
1387  : Thread(1, 0, 0)
1388 {
1389         this->color_button = color_button;
1390         this->update_lock = new Condition(0,"ColorButtonThread::update_lock");
1391         done = 1;
1392 }
1393
1394 ColorButtonThread::~ColorButtonThread()
1395 {
1396         stop();
1397         delete update_lock;
1398 }
1399
1400 void ColorButtonThread::start()
1401 {
1402         if( done ) {
1403                 done = 0;
1404                 Thread::start();
1405         }
1406 }
1407
1408 void ColorButtonThread::stop()
1409 {
1410         if( !done ) {
1411                 done = 1;
1412                 update_lock->unlock();
1413                 join();
1414         }
1415 }
1416
1417 void ColorButtonThread::run()
1418 {
1419         ColorButtonPicker *color_picker = color_button->color_picker;
1420         color_picker->update_gui();
1421         while( !done ) {
1422                 update_lock->lock("ColorButtonThread::run");
1423                 if( done ) break;
1424                 color_picker->update_gui();
1425         }
1426 }
1427
1428
1429 ColorBoxButton::ColorBoxButton(const char *title,
1430                 int x, int y, int w, int h,
1431                 int color, int alpha, int ok_cancel)
1432  : ColorButton(title, x, y, w, h, color, alpha, ok_cancel)
1433 {
1434 }
1435 ColorBoxButton::~ColorBoxButton()
1436 {
1437 }
1438
1439 int ColorBoxButton::handle_new_color(int color, int alpha)
1440 {
1441         return ColorButton::handle_new_color(color, alpha);
1442 }
1443 void ColorBoxButton::handle_done_event(int result)
1444 {
1445         ColorButton::handle_done_event(result);
1446 }
1447 void ColorBoxButton::create_objects()
1448 {
1449         update_gui(color, alpha);
1450 }
1451
1452 void ColorBoxButton::set_color(int color)
1453 {
1454         this->color = (color & 0xffffff);
1455         if( this->alpha >= 0 )
1456                 this->alpha = (~color>>24) & 0xff;
1457         int r = (color>>16) & 0xff;
1458         int g = (color>> 8) & 0xff;
1459         int b = (color>> 0) & 0xff;
1460         int color_model = vframes[0]->get_color_model();
1461         int bpp = BC_CModels::calculate_pixelsize(color_model);
1462         for( int i=0; i<3; ++i ) {
1463                 VFrame *vframe = vframes[i];
1464                 int ww = vframe->get_w(), hh = vframe->get_h();
1465                 uint8_t **rows = vframe->get_rows();
1466                 int rr = r, gg = g, bb = b;
1467                 switch( i ) {
1468                 case BUTTON_UP:
1469                         break;
1470                 case BUTTON_UPHI:
1471                         if( (rr+=48) > 0xff ) rr = 0xff;
1472                         if( (gg+=48) > 0xff ) gg = 0xff;
1473                         if( (bb+=48) > 0xff ) bb = 0xff;
1474                         break;
1475                 case BUTTON_DOWNHI:
1476                         if( (rr-=48) < 0x00 ) rr = 0x00;
1477                         if( (gg-=48) < 0x00 ) gg = 0x00;
1478                         if( (bb-=48) < 0x00 ) bb = 0x00;
1479                         break;
1480                 }
1481                 for( int y=0; y<hh; ++y ) {
1482                         uint8_t *rp = rows[y];
1483                         for( int x=0; x<ww; ++x ) {
1484                                 rp[0] = rr;  rp[1] = gg;  rp[2] = bb;
1485                                 if( bpp > 3 ) rp[3] = 0xff;
1486                                 rp += bpp;
1487                         }
1488                 }
1489         }
1490         set_images(vframes);
1491 }
1492
1493 ColorCircleButton::ColorCircleButton(const char *title,
1494                 int x, int y, int w, int h,
1495                 int color, int alpha, int ok_cancel)
1496  : ColorButton(title, x, y, w, h, color, alpha, ok_cancel)
1497 {
1498 }
1499 ColorCircleButton::~ColorCircleButton()
1500 {
1501 }
1502 int ColorCircleButton::handle_new_color(int color, int alpha)
1503 {
1504         return ColorButton::handle_new_color(color, alpha);
1505 }
1506 void ColorCircleButton::handle_done_event(int result)
1507 {
1508         ColorButton::handle_done_event(result);
1509 }
1510 void ColorCircleButton::create_objects()
1511 {
1512         update_gui(color, alpha);
1513 }
1514
1515 void ColorCircleButton::set_color(int color)
1516 {
1517         this->color = (color & 0xffffff);
1518         if( this->alpha >= 0 )
1519                 this->alpha = (~color>>24) & 0xff;
1520         int r = (color>>16) & 0xff;
1521         int g = (color>>8) & 0xff;
1522         int b = (color>>0) & 0xff;
1523         for( int i=0; i<3; ++i ) {
1524                 VFrame *vframe = vframes[i];
1525                 int ww = vframe->get_w(), hh = vframe->get_h();
1526                 int cx = (ww+1)/2, cy = hh/2;
1527                 double cc = (cx*cx + cy*cy) / 4.;
1528                 uint8_t *bp = vframe->get_data(), *dp = bp;
1529                 uint8_t *ep = dp + vframe->get_data_size();
1530                 int rr = r, gg = g, bb = b;
1531                 int bpl = vframe->get_bytes_per_line();
1532                 switch( i ) {
1533                 case BUTTON_UP:
1534                         break;
1535                 case BUTTON_UPHI:
1536                         if( (rr+=48) > 0xff ) rr = 0xff;
1537                         if( (gg+=48) > 0xff ) gg = 0xff;
1538                         if( (bb+=48) > 0xff ) bb = 0xff;
1539                         break;
1540                 case BUTTON_DOWNHI:
1541                         if( (rr-=48) < 0x00 ) rr = 0x00;
1542                         if( (gg-=48) < 0x00 ) gg = 0x00;
1543                         if( (bb-=48) < 0x00 ) bb = 0x00;
1544                         break;
1545                 }
1546                 while( dp < ep ) {
1547                         int yy = (dp-bp) / bpl, xx = ((dp-bp) % bpl) >> 2;
1548                         int dy = cy - yy, dx = cx - xx;
1549                         double s = dx*dx + dy*dy - cc;
1550                         double ss = s < 0 ? 1 : s >= cc ? 0 : 1 - s/cc;
1551                         int aa = ss * 0xff;
1552                         *dp++ = rr; *dp++ = gg; *dp++ = bb; *dp++ = aa;
1553                 }
1554         }
1555         set_images(vframes);
1556 }
1557