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