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