update version, update Features5 docs
[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->do_okcancel = 0;
46         this->output = BLACK;
47         this->alpha = 255;
48 }
49
50 ColorPicker::~ColorPicker()
51 {
52         close_window();
53 }
54
55 void ColorPicker::start_window(int output, int alpha, int do_okcancel)
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->output = output;
67         this->alpha = alpha;
68         this->do_okcancel = do_okcancel;
69         start();
70 }
71
72 BC_Window* ColorPicker::new_gui()
73 {
74         char window_title[BCTEXTLEN];
75         strcpy(window_title, _(PROGRAM_NAME ": "));
76         strcat(window_title, title ? title : _("Color Picker"));
77         BC_DisplayInfo display_info;
78         int x = display_info.get_abs_cursor_x() + 25;
79         int y = display_info.get_abs_cursor_y() - 100;
80         int w = 540, h = 330;
81         if( do_okcancel )
82                 h += bmax(BC_OKButton::calculate_h(),BC_CancelButton::calculate_h());
83         int root_w = display_info.get_root_w(), root_h = display_info.get_root_h();
84         if( x+w > root_w ) x = root_w - w;
85         if( y+h > root_h ) y = root_h - h;
86         if( x < 0 ) x = 0;
87         if( y < 0 ) y = 0;
88         ColorWindow *gui = new ColorWindow(this, x, y, w, h, window_title);
89         gui->create_objects();
90         return gui;
91 }
92
93 void ColorPicker::update_gui(int output, int alpha)
94 {
95         ColorWindow *gui = (ColorWindow *)get_gui();
96         if( !gui ) return;
97         gui->lock_window();
98         this->output = output;
99         this->alpha = alpha;
100         gui->change_values();
101         gui->update_display();
102         gui->unlock_window();
103 }
104
105 int ColorPicker::handle_new_color(int output, int alpha)
106 {
107         printf("ColorPicker::handle_new_color undefined.\n");
108         return 0;
109 }
110
111
112 ColorWindow::ColorWindow(ColorPicker *thread, int x, int y, int w, int h, const char *title)
113  : BC_Window(title, x, y, w, h, w, h, 0, 0, 1)
114 {
115         this->thread = thread;
116         wheel = 0;
117         wheel_value = 0;
118         output = 0;
119
120         hue = 0; sat = 0; val = 0;
121         red = 0; grn = 0; blu = 0;
122         lum = 0; c_r = 0; c_b = 0;
123         alpha = 0;
124
125         hsv_h = 0;  hsv_s = 0;  hsv_v = 0;
126         rgb_r = 0;  rgb_g = 0;  rgb_b = 0;
127         yuv_y = 0;  yuv_u = 0;  yuv_v = 0;
128         aph_a = 0;
129
130         button_grabbed = 0;
131 }
132 ColorWindow::~ColorWindow()
133 {
134         delete hsv_h;  delete hsv_s;  delete hsv_v;
135         delete rgb_r;  delete rgb_g;  delete rgb_b;
136         delete yuv_y;  delete yuv_u;  delete yuv_v;
137         delete aph_a;
138
139         if( button_grabbed ) {
140                 ungrab_buttons();
141                 ungrab_cursor();
142         }
143         update_history(rgb888());
144         save_history();
145 }
146
147 void ColorWindow::create_objects()
148 {
149         int x0 = 10, y0 = 10;
150         lock_window("ColorWindow::create_objects");
151         change_values();
152
153         int x = x0, y = y0;
154         add_tool(wheel = new PaletteWheel(this, x, y));
155         wheel->create_objects();
156
157         x += 180;  add_tool(wheel_value = new PaletteWheelValue(this, x, y));
158         wheel_value->create_objects();
159         x = x0;
160         y += 180;  add_tool(output = new PaletteOutput(this, x, y));
161         output->create_objects();
162         y += output->get_h() + 20;
163
164         load_history();  int x1 = x;
165         add_tool(hex_btn = new PaletteHexButton(this, x1, y));
166         char hex[BCSTRLEN];  sprintf(hex,"%06x",thread->output);
167         x1 += hex_btn->get_w() + 5;
168         add_tool(hex_box = new PaletteHex(this, x1, y, hex));
169         x1 += hex_box->get_w() + 15;
170         add_tool(grab_btn = new PaletteGrabButton(this, x1, y));
171         y += hex_box->get_h() + 15;
172         add_tool(history = new PaletteHistory(this, 10, y));
173
174         x += 240;
175         add_tool(new BC_Title(x, y =y0, C_("H:"), SMALLFONT));
176         add_tool(new BC_Title(x, y+=25, C_("S:"), SMALLFONT));
177         add_tool(new BC_Title(x, y+=25, D_("colorpicker_value#V:"), SMALLFONT));
178         add_tool(new BC_Title(x, y+=40, C_("R:"), SMALLFONT));
179         add_tool(new BC_Title(x, y+=25, C_("G:"), SMALLFONT));
180         add_tool(new BC_Title(x, y+=25, C_("B:"), SMALLFONT));
181         add_tool(new BC_Title(x, y+=40, C_("Y:"), SMALLFONT));
182         add_tool(new BC_Title(x, y+=25, C_("U:"), SMALLFONT));
183         add_tool(new BC_Title(x, y+=25, D_("colorpicker_Cr#V:"), SMALLFONT));
184         if( thread->do_alpha )
185                 add_tool(new BC_Title(x, y+=40, C_("A:"), SMALLFONT));
186         x += 24;
187         add_tool(hue = new PaletteHue(this, x, y= y0));
188         add_tool(sat = new PaletteSat(this, x, y+=25));
189         add_tool(val = new PaletteVal(this, x, y+=25));
190         add_tool(red = new PaletteRed(this, x, y+=40));
191         add_tool(grn = new PaletteGrn(this, x, y+=25));
192         add_tool(blu = new PaletteBlu(this, x, y+=25));
193         add_tool(lum = new PaletteLum(this, x, y+=40));
194         add_tool(c_r = new PaletteCr (this, x, y+=25));
195         add_tool(c_b = new PaletteCb (this, x, y+=25));
196         if( thread->do_alpha )
197                 add_tool(alpha = new PaletteAlpha(this, x, y+=40));
198
199         x += hue->get_w() + 10;
200         hsv_h = new PaletteHSV(this, x,y= y0, hsv.h, 0, 360);
201         hsv_h->create_objects();  hsv_h->set_tooltip(_("Hue"));
202         hsv_s = new PaletteHSV(this, x,y+=25, hsv.s, 0, 1);
203         hsv_s->create_objects();  hsv_s->set_tooltip(_("Saturation"));
204         hsv_v = new PaletteHSV(this, x,y+=25, hsv.v, 0, 1);
205         hsv_v->create_objects();  hsv_v->set_tooltip(_("Value"));
206         rgb_r = new PaletteRGB(this, x,y+=40, rgb.r, 0, 1);
207         rgb_r->create_objects();  rgb_r->set_tooltip(_("Red"));
208         rgb_g = new PaletteRGB(this, x,y+=25, rgb.g, 0, 1);
209         rgb_g->create_objects();  rgb_g->set_tooltip(_("Green"));
210         rgb_b = new PaletteRGB(this, x,y+=25, rgb.b, 0, 1);
211         rgb_b->create_objects();  rgb_b->set_tooltip(_("Blue"));
212         yuv_y = new PaletteYUV(this, x,y+=40, yuv.y, 0, 1);
213         yuv_y->create_objects();  yuv_y->set_tooltip(_("Luminance"));
214         yuv_u = new PaletteYUV(this, x,y+=25, yuv.u, 0, 1);
215         yuv_u->create_objects();  yuv_u->set_tooltip(_("Blue Luminance Difference"));
216         yuv_v = new PaletteYUV(this, x,y+=25, yuv.v, 0, 1);
217         yuv_v->create_objects();  yuv_v->set_tooltip(_("Red Luminance Difference"));
218         if( thread->do_alpha ) {
219                 aph_a = new PaletteAPH(this, x,y+=40, aph, 0, 1);
220                 aph_a->create_objects();  aph_a->set_tooltip(_("Alpha"));
221         }
222         if( thread->do_okcancel ) {
223                 add_tool(new BC_OKButton(this));
224                 add_tool(new BC_CancelButton(this));
225         }
226
227         update_display();
228         update_history();
229         show_window(1);
230         unlock_window();
231 }
232
233
234 void ColorWindow::change_values()
235 {
236         float r = ((thread->output>>16) & 0xff) / 255.;
237         float g = ((thread->output>>8)  & 0xff) / 255.;
238         float b = ((thread->output>>0)  & 0xff) / 255.;
239         rgb.r = r;  rgb.g = g;  rgb.b = b;
240         aph = (float)thread->alpha / 255;
241         update_rgb(rgb.r, rgb.g, rgb.b);
242 }
243
244
245 int ColorWindow::close_event()
246 {
247         set_done(thread->do_okcancel ? 1 : 0);
248         return 1;
249 }
250
251
252 void ColorWindow::update_rgb()
253 {
254         update_rgb(rgb.r, rgb.g, rgb.b);
255         update_display();
256 }
257 void ColorWindow::update_hsv()
258 {
259         update_hsv(hsv.h, hsv.s, hsv.v);
260         update_display();
261 }
262 void ColorWindow::update_yuv()
263 {
264         update_yuv(yuv.y, yuv.u, yuv.v);
265         update_display();
266 }
267
268 void ColorWindow::update_display()
269 {
270         wheel->draw(wheel->oldhue, wheel->oldsaturation);
271         wheel->oldhue = hsv.h;
272         wheel->oldsaturation = hsv.s;
273         wheel->draw(hsv.h, hsv.s);
274         wheel->flash();
275         wheel_value->draw(hsv.h, hsv.s, hsv.v);
276         wheel_value->flash();
277         output->draw();
278         output->flash();
279
280         hue->update((int)hsv.h);
281         sat->update(hsv.s);
282         val->update(hsv.v);
283
284         red->update(rgb.r);
285         grn->update(rgb.g);
286         blu->update(rgb.b);
287
288         lum->update(yuv.y);
289         c_r->update(yuv.u);
290         c_b->update(yuv.v);
291
292         hsv_h->update(hsv.h);
293         hsv_s->update(hsv.s);
294         hsv_v->update(hsv.v);
295         rgb_r->update(rgb.r);
296         rgb_g->update(rgb.g);
297         rgb_b->update(rgb.b);
298         yuv_y->update(yuv.y);
299         yuv_u->update(yuv.u);
300         yuv_v->update(yuv.v);
301         hex_box->update();
302
303         if( thread->do_alpha )
304                 aph_a->update(aph);
305 }
306
307 int ColorWindow::handle_event()
308 {
309         thread->handle_new_color(rgb888(), (int)(255*aph + 0.5));
310         return 1;
311 }
312
313 void ColorWindow::get_screen_sample()
314 {
315         int cx, cy;
316         get_abs_cursor(cx, cy);
317         BC_Capture capture_bitmap(1, 1, 0);
318         VFrame vframe(1,1,BC_RGB888);
319         capture_bitmap.capture_frame(&vframe, cx,cy);
320         unsigned char *data = vframe.get_data();
321         rgb.r = data[0]/255.;  rgb.g = data[1]/255.;  rgb.b = data[2]/255.;
322         update_rgb();
323 }
324
325 int ColorWindow::cursor_motion_event()
326 {
327         if( button_grabbed && get_button_down() ) {
328                 get_screen_sample();
329                 return 1;
330         }
331         return 0;
332 }
333
334 int ColorWindow::button_press_event()
335 {
336         if( button_grabbed ) {
337                 get_screen_sample();
338                 return 1;
339         }
340         return 0;
341 }
342
343 int ColorWindow::button_release_event()
344 {
345         if( button_grabbed ) {
346                 ungrab_buttons();
347                 ungrab_cursor();
348                 grab_btn->enable();
349                 button_grabbed = 0;
350                 update_history();
351                 return handle_event();
352         }
353         return 1;
354 }
355
356 void ColorWindow::update_rgb_hex(const char *hex)
357 {
358         int color;
359         if( sscanf(hex,"%x",&color) == 1 ) {
360                 float r = ((color>>16) & 0xff) / 255.;
361                 float g = ((color>>8)  & 0xff) / 255.;
362                 float b = ((color>>0)  & 0xff) / 255.;
363                 rgb.r = r;  rgb.g = g;  rgb.b = b;
364                 update_rgb();
365                 update_history();
366                 handle_event();
367         }
368 }
369
370
371 PaletteWheel::PaletteWheel(ColorWindow *window, int x, int y)
372  : BC_SubWindow(x, y, 170, 170)
373 {
374         this->window = window;
375         oldhue = 0;
376         oldsaturation = 0;
377         button_down = 0;
378 }
379
380 PaletteWheel::~PaletteWheel()
381 {
382 }
383
384 int PaletteWheel::button_press_event()
385 {
386         if( get_cursor_x() >= 0 && get_cursor_x() < get_w() &&
387                 get_cursor_y() >= 0 && get_cursor_y() < get_h() &&
388                 is_event_win() ) {
389                 button_down = 1;
390                 cursor_motion_event();
391                 return 1;
392         }
393         return 0;
394 }
395
396 int PaletteWheel::cursor_motion_event()
397 {
398         int x1, y1, distance;
399         if( button_down && is_event_win() ) {
400                 float h = get_angle(get_w()/2, get_h()/2, get_cursor_x(), get_cursor_y());
401                 bclamp(h, 0, 359.999);  window->hsv.h = h;
402                 x1 = get_w() / 2 - get_cursor_x();
403                 y1 = get_h() / 2 - get_cursor_y();
404                 distance = (int)sqrt(x1 * x1 + y1 * y1);
405                 float s = (float)distance / (get_w() / 2);
406                 bclamp(s, 0, 1);  window->hsv.s = s;
407                 window->hsv.v = 1;
408                 window->update_hsv();
409                 window->handle_event();
410                 return 1;
411         }
412         return 0;
413 }
414
415 int PaletteWheel::button_release_event()
416 {
417         if( button_down ) {
418                 button_down = 0;
419                 return 1;
420         }
421         return 0;
422 }
423
424 void PaletteWheel::create_objects()
425 {
426 // Upper right
427 //printf("PaletteWheel::create_objects 1\n");
428         float h, s, v = 1;
429         float r, g, b;
430         float x1, y1, x2, y2;
431         float distance;
432         int default_r, default_g, default_b;
433         VFrame frame(0, -1, get_w(), get_h(), BC_RGBA8888, -1);
434         x1 = get_w() / 2;
435         y1 = get_h() / 2;
436         default_r = (get_resources()->get_bg_color() & 0xff0000) >> 16;
437         default_g = (get_resources()->get_bg_color() & 0xff00) >> 8;
438         default_b = (get_resources()->get_bg_color() & 0xff);
439 //printf("PaletteWheel::create_objects 1\n");
440
441         int highlight_r = (get_resources()->button_light & 0xff0000) >> 16;
442         int highlight_g = (get_resources()->button_light & 0xff00) >> 8;
443         int highlight_b = (get_resources()->button_light & 0xff);
444
445         for( y2 = 0; y2 < get_h(); y2++ ) {
446                 unsigned char *row = (unsigned char*)frame.get_rows()[(int)y2];
447                 for( x2 = 0; x2 < get_w(); x2++ ) {
448                         distance = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
449                         if( distance > x1 ) {
450                                 row[(int)x2 * 4] = default_r;
451                                 row[(int)x2 * 4 + 1] = default_g;
452                                 row[(int)x2 * 4 + 2] = default_b;
453                                 row[(int)x2 * 4 + 3] = 0;
454                         }
455                         else
456                         if( distance > x1 - 1 ) {
457                                 int r_i, g_i, b_i;
458                                 if( get_h() - y2 < x2 ) {
459                                         r_i = highlight_r;
460                                         g_i = highlight_g;
461                                         b_i = highlight_b;
462                                 }
463                                 else {
464                                         r_i = 0;
465                                         g_i = 0;
466                                         b_i = 0;
467                                 }
468
469                                 row[(int)x2 * 4] = r_i;
470                                 row[(int)x2 * 4 + 1] = g_i;
471                                 row[(int)x2 * 4 + 2] = b_i;
472                                 row[(int)x2 * 4 + 3] = 255;
473                         }
474                         else {
475                                 h = get_angle(x1, y1, x2, y2);
476                                 s = distance / x1;
477                                 HSV::hsv_to_rgb(r, g, b, h, s, v);
478                                 row[(int)x2 * 4] = (int)(r * 255);
479                                 row[(int)x2 * 4 + 1] = (int)(g * 255);
480                                 row[(int)x2 * 4 + 2] = (int)(b * 255);
481                                 row[(int)x2 * 4 + 3] = 255;
482                         }
483                 }
484         }
485 //printf("PaletteWheel::create_objects 1\n");
486
487         draw_vframe(&frame,
488                 0,
489                 0,
490                 get_w(),
491                 get_h(),
492                 0,
493                 0,
494                 get_w(),
495                 get_h(),
496                 0);
497 //printf("PaletteWheel::create_objects 1\n");
498
499         oldhue = window->hsv.h;
500         oldsaturation = window->hsv.s;
501 //printf("PaletteWheel::create_objects 1\n");
502         draw(oldhue, oldsaturation);
503 //printf("PaletteWheel::create_objects 1\n");
504         flash();
505 //printf("PaletteWheel::create_objects 2\n");
506 }
507
508 float PaletteWheel::torads(float angle)
509 {
510         return (float)angle / 360 * 2 * M_PI;
511 }
512
513
514 int PaletteWheel::draw(float hue, float saturation)
515 {
516         int x, y, w, h;
517         x = w = get_w() / 2;
518         y = h = get_h() / 2;
519
520         if( hue > 0 && hue < 90 ) {
521                 x = (int)(w - w * cos(torads(90 - hue)) * saturation);
522                 y = (int)(h - h * sin(torads(90 - hue)) * saturation);
523         }
524         else if( hue > 90 && hue < 180 ) {
525                 x = (int)(w - w * cos(torads(hue - 90)) * saturation);
526                 y = (int)(h + h * sin(torads(hue - 90)) * saturation);
527         }
528         else if( hue > 180 && hue < 270 ) {
529                 x = (int)(w + w * cos(torads(270 - hue)) * saturation);
530                 y = (int)(h + h * sin(torads(270 - hue)) * saturation);
531         }
532         else if( hue > 270 && hue < 360 ) {
533                 x = (int)(w + w * cos(torads(hue - 270)) * saturation);
534                 y = (int)(h - w * sin(torads(hue - 270)) * saturation);
535         }
536         else if( hue == 0 ) {
537                 x = w;
538                 y = (int)(h - h * saturation);
539         }
540         else if( hue == 90 ) {
541                 x = (int)(w - w * saturation);
542                 y = h;
543         }
544         else if( hue == 180 ) {
545                 x = w;
546                 y = (int)(h + h * saturation);
547         }
548         else if( hue == 270 ) {
549                 x = (int)(w + w * saturation);
550                 y = h;
551         }
552
553         set_inverse();
554         set_color(WHITE);
555         draw_circle(x - 5, y - 5, 10, 10);
556         set_opaque();
557         return 0;
558 }
559
560 int PaletteWheel::get_angle(float x1, float y1, float x2, float y2)
561 {
562         float result = -atan2(x2 - x1, y1 - y2) * (360 / M_PI / 2);
563         if( result < 0 )
564                 result += 360;
565         return (int)result;
566 }
567
568 PaletteWheelValue::PaletteWheelValue(ColorWindow *window, int x, int y)
569  : BC_SubWindow(x, y, 40, 170, BLACK)
570 {
571         this->window = window;
572         button_down = 0;
573 }
574 PaletteWheelValue::~PaletteWheelValue()
575 {
576         delete frame;
577 }
578
579 void PaletteWheelValue::create_objects()
580 {
581         frame = new VFrame(get_w(), get_h(), BC_RGB888);
582         draw(window->hsv.h, window->hsv.s, window->hsv.v);
583         flash();
584 }
585
586 int PaletteWheelValue::button_press_event()
587 {
588 //printf("PaletteWheelValue::button_press 1 %d\n", is_event_win());
589         if( get_cursor_x() >= 0 && get_cursor_x() < get_w() &&
590                 get_cursor_y() >= 0 && get_cursor_y() < get_h() &&
591                 is_event_win() ) {
592 //printf("PaletteWheelValue::button_press 2\n");
593                 button_down = 1;
594                 cursor_motion_event();
595                 return 1;
596         }
597         return 0;
598 }
599
600 int PaletteWheelValue::cursor_motion_event()
601 {
602         if( button_down && is_event_win() ) {
603 //printf("PaletteWheelValue::cursor_motion 1\n");
604                 float v = 1.0 - (float)(get_cursor_y() - 2) / (get_h() - 4);
605                 bclamp(v, 0, 1);  window->hsv.v = v;
606                 window->update_hsv();
607                 window->handle_event();
608                 return 1;
609         }
610         return 0;
611 }
612
613 int PaletteWheelValue::button_release_event()
614 {
615         if( button_down ) {
616 //printf("PaletteWheelValue::button_release 1\n");
617                 button_down = 0;
618                 return 1;
619         }
620         return 0;
621 }
622
623 int PaletteWheelValue::draw(float hue, float saturation, float value)
624 {
625         float r_f, g_f, b_f;
626         int i, j, r, g, b;
627
628         for( i = get_h() - 3; i >= 2; i-- ) {
629                 unsigned char *row = (unsigned char*)frame->get_rows()[i];
630                 HSV::hsv_to_rgb(r_f, g_f, b_f, hue, saturation,
631                         1.0 - (float)(i - 2) / (get_h() - 4));
632                 r = (int)(r_f * 255);
633                 g = (int)(g_f * 255);
634                 b = (int)(b_f * 255);
635                 for( j = 0; j < get_w(); j++ ) {
636                         row[j * 3] = r;
637                         row[j * 3 + 1] = g;
638                         row[j * 3 + 2] = b;
639                 }
640         }
641
642         draw_3d_border(0, 0, get_w(), get_h(), 1);
643         draw_vframe(frame, 2, 2, get_w() - 4, get_h() - 4,
644                 2, 2, get_w() - 4, get_h() - 4, 0);
645         set_color(BLACK);
646         draw_line(2, get_h() - 3 - (int)(value * (get_h() - 5)),
647                   get_w() - 3, get_h() - 3 - (int)(value * (get_h() - 5)));
648 //printf("PaletteWheelValue::draw %d %f\n", __LINE__, value);
649
650         return 0;
651 }
652
653 PaletteOutput::PaletteOutput(ColorWindow *window, int x, int y)
654  : BC_SubWindow(x, y, 180, 30, BLACK)
655 {
656         this->window = window;
657 }
658 PaletteOutput::~PaletteOutput()
659 {
660 }
661
662
663 void PaletteOutput::create_objects()
664 {
665         draw();
666         flash();
667 }
668
669 int PaletteOutput::handle_event()
670 {
671         return 1;
672 }
673
674 int PaletteOutput::draw()
675 {
676         set_color(window->rgb888());
677         draw_box(2, 2, get_w() - 4, get_h() - 4);
678         draw_3d_border(0, 0, get_w(), get_h(), 1);
679         return 0;
680 }
681
682 PaletteHue::PaletteHue(ColorWindow *window, int x, int y)
683  : BC_ISlider(x, y, 0, 150, 200, 0, 359, (int)(window->hsv.h), 0)
684 {
685         this->window = window;
686 }
687 PaletteHue::~PaletteHue()
688 {
689 }
690
691 int PaletteHue::handle_event()
692 {
693         window->hsv.h = get_value();
694         window->update_hsv();
695         window->handle_event();
696         return 1;
697 }
698
699 PaletteSat::PaletteSat(ColorWindow *window, int x, int y)
700  : BC_FSlider(x, y, 0, 150, 200, 0, 1.0, window->hsv.s, 0)
701 {
702         this->window = window;
703         set_precision(0.01);
704 }
705 PaletteSat::~PaletteSat()
706 {
707 }
708
709 int PaletteSat::handle_event()
710 {
711         window->hsv.s = get_value();
712         window->update_hsv();
713         window->handle_event();
714         return 1;
715 }
716
717
718 PaletteVal::PaletteVal(ColorWindow *window, int x, int y)
719  : BC_FSlider(x, y, 0, 150, 200, 0, 1.0, window->hsv.v, 0)
720 {
721         this->window = window;
722         set_precision(0.01);
723 }
724 PaletteVal::~PaletteVal()
725 {
726 }
727
728 int PaletteVal::handle_event()
729 {
730         window->hsv.v = get_value();
731         window->update_hsv();
732         window->handle_event();
733         return 1;
734 }
735
736
737 PaletteRed::PaletteRed(ColorWindow *window, int x, int y)
738  : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->rgb.r, 0)
739 {
740         this->window = window;
741         set_precision(0.01);
742 }
743 PaletteRed::~PaletteRed()
744 {
745 }
746
747 int PaletteRed::handle_event()
748 {
749         window->rgb.r = get_value();
750         window->update_rgb();
751         window->handle_event();
752         return 1;
753 }
754
755 PaletteGrn::PaletteGrn(ColorWindow *window, int x, int y)
756  : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->rgb.g, 0)
757 {
758         this->window = window;
759         set_precision(0.01);
760 }
761 PaletteGrn::~PaletteGrn()
762 {
763 }
764
765 int PaletteGrn::handle_event()
766 {
767         window->rgb.g = get_value();
768         window->update_rgb();
769         window->handle_event();
770         return 1;
771 }
772
773 PaletteBlu::PaletteBlu(ColorWindow *window, int x, int y)
774  : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->rgb.b, 0)
775 {
776         this->window = window;
777         set_precision(0.01);
778 }
779 PaletteBlu::~PaletteBlu()
780 {
781 }
782
783 int PaletteBlu::handle_event()
784 {
785         window->rgb.b = get_value();
786         window->update_rgb();
787         window->handle_event();
788         return 1;
789 }
790
791 PaletteAlpha::PaletteAlpha(ColorWindow *window, int x, int y)
792  : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->aph, 0)
793 {
794         this->window = window;
795         set_precision(0.01);
796 }
797 PaletteAlpha::~PaletteAlpha()
798 {
799 }
800
801 int PaletteAlpha::handle_event()
802 {
803         window->aph = get_value();
804         window->handle_event();
805         return 1;
806 }
807
808 PaletteLum::PaletteLum(ColorWindow *window, int x, int y)
809  : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->yuv.y, 0)
810 {
811         this->window = window;
812         set_precision(0.01);
813 }
814 PaletteLum::~PaletteLum()
815 {
816 }
817
818 int PaletteLum::handle_event()
819 {
820         window->yuv.y = get_value();
821         window->update_yuv();
822         window->handle_event();
823         return 1;
824 }
825
826 PaletteCr::PaletteCr(ColorWindow *window, int x, int y)
827  : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->yuv.u, 0)
828 {
829         this->window = window;
830         set_precision(0.01);
831 }
832 PaletteCr::~PaletteCr()
833 {
834 }
835
836 int PaletteCr::handle_event()
837 {
838         window->yuv.u = get_value();
839         window->update_yuv();
840         window->handle_event();
841         return 1;
842 }
843
844 PaletteCb::PaletteCb(ColorWindow *window, int x, int y)
845  : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->yuv.v, 0)
846 {
847         this->window = window;
848         set_precision(0.01);
849 }
850 PaletteCb::~PaletteCb()
851 {
852 }
853
854 int PaletteCb::handle_event()
855 {
856         window->yuv.v = get_value();
857         window->update_yuv();
858         window->handle_event();
859         return 1;
860 }
861
862 void ColorWindow::update_rgb(float r, float g, float b)
863 {
864         { float y, u, v;
865         YUV::yuv.rgb_to_yuv_f(r, g, b, y, u, v);
866         u += 0.5;  v += 0.5;
867         bclamp(y, 0, 1);    yuv.y = y;
868         bclamp(u, 0, 1);    yuv.u = u;
869         bclamp(v, 0, 1);    yuv.v = v; }
870         { float h, s, v;
871         HSV::rgb_to_hsv(r,g,b, h,s,v);
872         bclamp(h, 0, 360);  hsv.h = h;
873         bclamp(s, 0, 1);    hsv.s = s;
874         bclamp(v, 0, 1);    hsv.v = v; }
875 }
876
877 void ColorWindow::update_yuv(float y, float u, float v)
878 {
879         u -= 0.5;  v -= 0.5;
880         { float r, g, b;
881         YUV::yuv.yuv_to_rgb_f(r, g, b, y, u, v);
882         bclamp(r, 0, 1);   rgb.r = r;
883         bclamp(g, 0, 1);   rgb.g = g;
884         bclamp(b, 0, 1);   rgb.b = b;
885         float h, s, v;
886         HSV::rgb_to_hsv(r,g,b, h, s, v);
887         bclamp(h, 0, 360); hsv.h = h;
888         bclamp(s, 0, 1);   hsv.s = s;
889         bclamp(v, 0, 1);   hsv.v = v; }
890 }
891
892 void ColorWindow::update_hsv(float h, float s, float v)
893 {
894         { float r, g, b;
895         HSV::hsv_to_rgb(r,g,b, h,s,v);
896         bclamp(r, 0, 1);   rgb.r = r;
897         bclamp(g, 0, 1);   rgb.g = g;
898         bclamp(b, 0, 1);   rgb.b = b;
899         float y, u, v;
900         YUV::yuv.rgb_to_yuv_f(r, g, b, y, u, v);
901         u += 0.5;  v += 0.5;
902         bclamp(y, 0, 1);   yuv.y = y;
903         bclamp(u, 0, 1);   yuv.u = u;
904         bclamp(v, 0, 1);   yuv.v = v; }
905 }
906
907 void ColorWindow::load_history()
908 {
909         char history_path[BCTEXTLEN];
910         MWindow::create_defaults_path(history_path,PALETTE_DATA);
911         FILE *fp = fopen(history_path,"r");
912         int i=0;
913         if( fp ) {
914                 while( i < PALLETTE_HISTORY_SIZE ) {
915                         char line[BCSTRLEN];
916                         if( !fgets(line,sizeof(line)-1,fp) ) break;
917                         line[sizeof(line)-1] = 0;
918                         if( sscanf(line, "%x",&palette_history[i]) != 1 ) break;
919                         ++i;
920                 }
921                 fclose(fp);
922         }
923         int r = 0, g = 0, b = 0;
924         float v0 = 0, v1 = 1;
925         while( i < PALLETTE_HISTORY_SIZE ) {
926                 int grey_code = i ^ (i>>1);
927                 r = 255 * ((grey_code&4) ? v0 : v1);
928                 g = 255 * ((grey_code&2) ? v0 : v1);
929                 b = 255 * ((grey_code&1) ? v0 : v1);
930                 int color = (r<<16) | (g<<8) | (b<<0);
931                 palette_history[i++] = color;
932                 if( i & 7 ) continue;
933                 v0 = 0.5f * (v0+.5f);
934                 v1 = 0.5f * (v1+.5f);
935         }
936 }
937 void ColorWindow::save_history()
938 {
939         char history_path[BCTEXTLEN];
940         MWindow::create_defaults_path(history_path,PALETTE_DATA);
941         FILE *fp = fopen(history_path,"w");
942         if( fp ) {
943                 for( int i=0; i<PALLETTE_HISTORY_SIZE; ++i ) {
944                         fprintf(fp, "%06x\n", palette_history[i]);
945                 }
946                 fclose(fp);
947         }
948 }
949 void ColorWindow::update_history(int color)
950 {
951         int out = palette_history[0];
952         palette_history[0] = color;
953         for( int i=1; out != color && i<PALLETTE_HISTORY_SIZE; ++i ) {
954                 int in = out;
955                 out = palette_history[i];
956                 palette_history[i] = in;
957         }
958 }
959 void ColorWindow::update_history()
960 {
961         update_history(rgb888());
962         history->update(0);
963 }
964 int ColorWindow::rgb888()
965 {
966         int r = 255*rgb.r + 0.5, g = 255*rgb.g + 0.5, b = 255*rgb.b + 0.5;
967         bclamp(r, 0, 255);  bclamp(g, 0, 255);  bclamp(b, 0, 255);
968         return (r<<16) | (g<<8) | (b<<0);
969 }
970
971 PaletteNum::PaletteNum(ColorWindow *window, int x, int y,
972         float &output, float min, float max)
973  : BC_TumbleTextBox(window, output, min, max, x, y, 64)
974 {
975         this->window = window;
976         this->output = &output;
977         set_increment(0.01);
978         set_precision(2);
979 }
980
981 PaletteNum::~PaletteNum()
982 {
983 }
984
985
986 int PaletteHSV::handle_event()
987 {
988         update_output();
989         window->update_hsv();
990         window->handle_event();
991         return 1;
992 }
993
994 int PaletteRGB::handle_event()
995 {
996         update_output();
997         window->update_rgb();
998         window->handle_event();
999         return 1;
1000 }
1001
1002 int PaletteYUV::handle_event()
1003 {
1004         update_output();
1005         window->update_yuv();
1006         window->handle_event();
1007         return 1;
1008 }
1009
1010 int PaletteAPH::handle_event()
1011 {
1012         update_output();
1013         window->update_display();
1014         window->handle_event();
1015         return 1;
1016 }
1017
1018 PaletteHexButton::PaletteHexButton(ColorWindow *window, int x, int y)
1019  : BC_GenericButton(x, y, 50, "#")
1020 {
1021         this->window = window;
1022         set_tooltip(_("hex rgb color"));
1023 }
1024 PaletteHexButton::~PaletteHexButton()
1025 {
1026 }
1027 int PaletteHexButton::handle_event()
1028 {
1029         const char *hex = window->hex_box->get_text();
1030         window->update_rgb_hex(hex);
1031         return 1;
1032 }
1033
1034 PaletteHex::PaletteHex(ColorWindow *window, int x, int y, const char *hex)
1035  : BC_TextBox(x, y, 100, 1, hex)
1036 {
1037         this->window = window;
1038 }
1039 PaletteHex::~PaletteHex()
1040 {
1041 }
1042 void PaletteHex::update()
1043 {
1044         char hex[BCSTRLEN];  sprintf(hex,"%06x",window->rgb888());
1045         BC_TextBox::update(hex);
1046 }
1047
1048 int PaletteHex::keypress_event()
1049 {
1050         if( get_keypress() != RETURN )
1051                 return BC_TextBox::keypress_event();
1052         window->update_rgb_hex(get_text());
1053         return 1;
1054 }
1055
1056 #include "grabpick_up_png.h"
1057 #include "grabpick_hi_png.h"
1058 #include "grabpick_dn_png.h"
1059
1060 PaletteGrabButton::PaletteGrabButton(ColorWindow *window, int x, int y)
1061  : BC_Button(x, y, vframes)
1062 {
1063         this->window = window;
1064         vframes[0] = new VFramePng(grabpick_up_png);
1065         vframes[1] = new VFramePng(grabpick_hi_png);
1066         vframes[2] = new VFramePng(grabpick_dn_png);
1067         set_tooltip(_("grab from anywhere picker"));
1068 }
1069 PaletteGrabButton::~PaletteGrabButton()
1070 {
1071         for( int i=0; i<3; ++i )
1072                 delete vframes[i];
1073 }
1074 int PaletteGrabButton::handle_event()
1075 {
1076         if( window->grab_buttons() ) {
1077                 grab_cursor();
1078                 window->button_grabbed = 1;
1079                 button_press_event(); // redraw face HI
1080         }
1081         return 1;
1082 }
1083
1084 PaletteHistory::PaletteHistory(ColorWindow *window, int x, int y)
1085  : BC_SubWindow(x,y, 200, 24)
1086 {
1087         this->window = window;
1088         button_down = 0;
1089         set_tooltip(_("color history"));
1090 }
1091 PaletteHistory::~PaletteHistory()
1092 {
1093 }
1094 void PaletteHistory::update(int flush)
1095 {
1096         int x1 = 0, x2 = 0;
1097         for( int i=0; i<PALLETTE_HISTORY_SIZE; x1=x2 ) {
1098                 int rgb = window->palette_history[i];
1099                 x2 = (++i * get_w())/PALLETTE_HISTORY_SIZE;
1100                 draw_3d_box(x1,0,x2-x1,get_h(),WHITE,BLACK,rgb,LTBLUE,DKBLUE);
1101         }
1102         flash(flush);
1103 }
1104
1105 int PaletteHistory::button_press_event()
1106 {
1107         if( button_down || !is_event_win() ) return 0;
1108         button_down =  1;
1109         cursor_motion_event();
1110         return 1;
1111 }
1112 int PaletteHistory::button_release_event()
1113 {
1114         if( !button_down || !is_event_win() ) return 0;
1115         cursor_motion_event();
1116         if( button_down > 0 ) {
1117                 window->handle_event();
1118                 window->update_display();
1119                 window->update_history();
1120         }
1121         button_down =  0;
1122         return 1;
1123 }
1124 int PaletteHistory::cursor_motion_event()
1125 {
1126         if( !button_down || !is_event_win() ) return 0;
1127         hide_tooltip();
1128         int pick = (PALLETTE_HISTORY_SIZE * get_cursor_x()) / get_w();
1129         bclamp(pick, 0, PALLETTE_HISTORY_SIZE-1);
1130         int color = window->palette_history[pick];
1131         float r = ((color>>16) & 0xff) / 255.;
1132         float g = ((color>>8)  & 0xff) / 255.;
1133         float b = ((color>>0)  & 0xff) / 255.;
1134         if( window->rgb.r != r || window->rgb.g != g || window->rgb.b != b ) {
1135                 window->rgb.r = r;  window->rgb.g = g;  window->rgb.b = b;
1136                 window->update_rgb();
1137         }
1138         return 1;
1139 }
1140
1141 int PaletteHistory::cursor_leave_event()
1142 {
1143         hide_tooltip();
1144         return 0;
1145 }
1146 int PaletteHistory::repeat_event(int64_t duration)
1147 {
1148         int result = 0;
1149
1150         if( duration == get_resources()->tooltip_delay &&
1151             get_tooltip() && *get_tooltip() && cursor_above() ) {
1152                 show_tooltip();
1153                 result = 1;
1154         }
1155         return result;
1156 }
1157