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