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