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