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