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