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