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