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