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