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