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