remove whitespace at eol
[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 "bcdisplayinfo.h"
23 #include "colorpicker.h"
24 #include "condition.h"
25 #include "language.h"
26 #include "mutex.h"
27 #include "mwindow.inc"
28 #include "cicolors.h"
29 #include "vframe.h"
30
31 #include <string.h>
32 #include <unistd.h>
33
34
35 ColorThread::ColorThread(int do_alpha, const char *title)
36  : BC_DialogThread()
37 {
38         this->title = title;
39         this->do_alpha = do_alpha;
40         this->do_okcancel = 0;
41 }
42
43 ColorThread::~ColorThread()
44 {
45         close_window();
46 }
47
48 void ColorThread::start_window(int output, int alpha, int do_okcancel)
49 {
50         if( running() ) {
51                 ColorWindow *gui = (ColorWindow *)get_gui();
52                 if( gui ) {
53                         gui->lock_window("ColorThread::start_window");
54                         gui->raise_window(1);
55                         gui->unlock_window();
56                 }
57                 return;
58         }
59         this->output = output;
60         this->alpha = alpha;
61         this->do_okcancel = do_okcancel;
62         start();
63 }
64
65 BC_Window* ColorThread::new_gui()
66 {
67         char window_title[BCTEXTLEN];
68         strcpy(window_title, _(PROGRAM_NAME ": "));
69         strcat(window_title, title ? title : _("Color Picker"));
70         BC_DisplayInfo display_info;
71         int x = display_info.get_abs_cursor_x() + 25;
72         int y = display_info.get_abs_cursor_y() - 100;
73         int w = 410, h = 320;
74         if( do_okcancel )
75                 h += 10 + MAX(BC_OKButton::calculate_h(),BC_CancelButton::calculate_h());
76         int root_w = display_info.get_root_w(), root_h = display_info.get_root_h();
77         if( x+w > root_w ) x = root_w - w;
78         if( y+h > root_h ) y = root_h - h;
79         if( x < 0 ) x = 0;
80         if( y < 0 ) y = 0;
81         ColorWindow *gui = new ColorWindow(this, x, y, w, h, window_title);
82         gui->create_objects();
83         return gui;
84 }
85
86 void ColorThread::update_gui(int output, int alpha)
87 {
88         ColorWindow *gui = (ColorWindow *)get_gui();
89         if( !gui ) return;
90         gui->lock_window();
91         this->output = output;
92         this->alpha = alpha;
93         gui->change_values();
94         gui->update_display();
95         gui->unlock_window();
96 }
97
98 int ColorThread::handle_new_color(int output, int alpha)
99 {
100         printf("ColorThread::handle_new_color undefined.\n");
101         return 0;
102 }
103
104
105 ColorWindow::ColorWindow(ColorThread *thread, int x, int y, int w, int h, const char *title)
106  : BC_Window(title, x, y, w, h, w, h, 0, 0, 1)
107 {
108         this->thread = thread;
109 }
110
111 void ColorWindow::create_objects()
112 {
113         int x0 = 10, y0 = 10;
114         lock_window("ColorWindow::create_objects");
115         change_values();
116
117         int x = x0, y = y0;
118         add_tool(wheel = new PaletteWheel(this, x, y));
119         wheel->create_objects();
120
121         x += 180;  add_tool(wheel_value = new PaletteWheelValue(this, x, y));
122         wheel_value->create_objects();
123         x = x0;
124         y += 180;  add_tool(output = new PaletteOutput(this, x, y));
125         output->create_objects();
126
127         x += 240;
128         y = y0;    add_tool(new BC_Title(x, y, _("Hue"), SMALLFONT));
129         y += 15;   add_tool(hue = new PaletteHue(this, x, y));
130         y += 30;   add_tool(new BC_Title(x, y, _("Saturation"), SMALLFONT));
131         y += 15;   add_tool(saturation = new PaletteSaturation(this, x, y));
132         y += 30;   add_tool(new BC_Title(x, y, _("Value"), SMALLFONT));
133         y += 15;   add_tool(value = new PaletteValue(this, x, y));
134         y += 30;   add_tool(new BC_Title(x, y, _("Red"), SMALLFONT));
135         y += 15;   add_tool(red = new PaletteRed(this, x, y));
136         y += 30;   add_tool(new BC_Title(x, y, _("Green"), SMALLFONT));
137         y += 15;   add_tool(green = new PaletteGreen(this, x, y));
138         y += 30;   add_tool(new BC_Title(x, y, _("Blue"), SMALLFONT));
139         y += 15;   add_tool(blue = new PaletteBlue(this, x, y));
140
141         if(thread->do_alpha) {
142                 y += 30;   add_tool(new BC_Title(x, y, _("Alpha"), SMALLFONT));
143                 y += 15;   add_tool(alpha = new PaletteAlpha(this, x, y));
144         }
145         if( thread->do_okcancel ) {
146                 add_tool(new BC_OKButton(this));
147                 add_tool(new BC_CancelButton(this));
148         }
149         update_display();
150         show_window(1);
151         unlock_window();
152 }
153
154
155 void ColorWindow::change_values()
156 {
157         r = (float)((thread->output & 0xff0000) >> 16) / 255;
158         g = (float)((thread->output & 0xff00) >> 8) / 255;
159         b = (float)((thread->output & 0xff)) / 255;
160         HSV::rgb_to_hsv(r, g, b, h, s, v);
161         a = (float)thread->alpha / 255;
162 }
163
164
165 int ColorWindow::close_event()
166 {
167         set_done(thread->do_okcancel ? 1 : 0);
168         return 1;
169 }
170
171
172 void ColorWindow::update_rgb()
173 {
174         float r = red->get_value();
175         float g = green->get_value();
176         float b = blue->get_value();
177         HSV::rgb_to_hsv(r, g, b, h, s, v);
178         update_display();
179 }
180
181 void ColorWindow::update_display()
182 {
183         float r, g, b;
184         if(h < 0) h = 0;
185         if(h > 360) h = 360;
186         if(s < 0) s = 0;
187         if(s > 1) s = 1;
188         if(v < 0) v = 0;
189         if(v > 1) v = 1;
190         if(a < 0) a = 0;
191         if(a > 1) a = 1;
192
193         wheel->draw(wheel->oldhue,
194                                 wheel->oldsaturation);
195         wheel->oldhue = h;
196         wheel->oldsaturation = s;
197         wheel->draw(h, s);
198         wheel->flash();
199         wheel_value->draw(h, s, v);
200         wheel_value->flash();
201         output->draw();
202         output->flash();
203         hue->update((int)h);
204         saturation->update(s);
205         value->update(v);
206
207         HSV::hsv_to_rgb(r, g, b, h, s, v);
208         red->update(r);
209         green->update(g);
210         blue->update(b);
211         if(thread->do_alpha)
212         {
213                 alpha->update(a);
214         }
215 }
216
217 int ColorWindow::handle_event()
218 {
219         float r, g, b;
220         HSV::hsv_to_rgb(r, g, b, h, s, v);
221         int result = (((int)(r * 255)) << 16) | (((int)(g * 255)) << 8) | ((int)(b * 255));
222         thread->handle_new_color(result, (int)(a * 255));
223         return 1;
224 }
225
226
227 PaletteWheel::PaletteWheel(ColorWindow *window, int x, int y)
228  : BC_SubWindow(x, y, 170, 170)
229 {
230         this->window = window;
231         oldhue = 0;
232         oldsaturation = 0;
233         button_down = 0;
234 }
235
236 PaletteWheel::~PaletteWheel()
237 {
238 }
239
240 int PaletteWheel::button_press_event()
241 {
242         if(get_cursor_x() >= 0 && get_cursor_x() < get_w() &&
243                 get_cursor_y() >= 0 && get_cursor_y() < get_h() &&
244                 is_event_win())
245         {
246                 button_down = 1;
247                 cursor_motion_event();
248                 return 1;
249         }
250         return 0;
251 }
252
253 int PaletteWheel::cursor_motion_event()
254 {
255         int x1, y1, distance;
256         if(button_down && is_event_win())
257         {
258                 window->h = get_angle(get_w() / 2,
259                         get_h() / 2,
260                         get_cursor_x(),
261                         get_cursor_y());
262                 x1 = get_w() / 2 - get_cursor_x();
263                 y1 = get_h() / 2 - get_cursor_y();
264                 distance = (int)sqrt(x1 * x1 + y1 * y1);
265                 if(distance > get_w() / 2) distance = get_w() / 2;
266                 window->s = (float)distance / (get_w() / 2);
267                 window->update_display();
268                 window->handle_event();
269                 return 1;
270         }
271         return 0;
272 }
273
274 int PaletteWheel::button_release_event()
275 {
276         if(button_down)
277         {
278                 button_down = 0;
279                 return 1;
280         }
281         return 0;
282 }
283
284 void PaletteWheel::create_objects()
285 {
286 // Upper right
287 //printf("PaletteWheel::create_objects 1\n");
288         float h;
289         float s;
290         float v = 1;
291         float r, g, b;
292         float x1, y1, x2, y2;
293         float distance;
294         int default_r, default_g, default_b;
295         VFrame frame(0, -1, get_w(), get_h(), BC_RGBA8888, -1);
296         x1 = get_w() / 2;
297         y1 = get_h() / 2;
298         default_r = (get_resources()->get_bg_color() & 0xff0000) >> 16;
299         default_g = (get_resources()->get_bg_color() & 0xff00) >> 8;
300         default_b = (get_resources()->get_bg_color() & 0xff);
301 //printf("PaletteWheel::create_objects 1\n");
302
303         int highlight_r = (get_resources()->button_light & 0xff0000) >> 16;
304         int highlight_g = (get_resources()->button_light & 0xff00) >> 8;
305         int highlight_b = (get_resources()->button_light & 0xff);
306
307         for(y2 = 0; y2 < get_h(); y2++)
308         {
309                 unsigned char *row = (unsigned char*)frame.get_rows()[(int)y2];
310                 for(x2 = 0; x2 < get_w(); x2++)
311                 {
312                         distance = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
313                         if(distance > x1)
314                         {
315                                 row[(int)x2 * 4] = default_r;
316                                 row[(int)x2 * 4 + 1] = default_g;
317                                 row[(int)x2 * 4 + 2] = default_b;
318                                 row[(int)x2 * 4 + 3] = 0;
319                         }
320                         else
321                         if(distance > x1 - 1)
322                         {
323                                 int r_i, g_i, b_i;
324                                 if(get_h() - y2 < x2)
325                                 {
326                                         r_i = highlight_r;
327                                         g_i = highlight_g;
328                                         b_i = highlight_b;
329                                 }
330                                 else
331                                 {
332                                         r_i = 0;
333                                         g_i = 0;
334                                         b_i = 0;
335                                 }
336
337                                 row[(int)x2 * 4] = r_i;
338                                 row[(int)x2 * 4 + 1] = g_i;
339                                 row[(int)x2 * 4 + 2] = b_i;
340                                 row[(int)x2 * 4 + 3] = 255;
341                         }
342                         else
343                         {
344                                 h = get_angle(x1, y1, x2, y2);
345                                 s = distance / x1;
346                                 HSV::hsv_to_rgb(r, g, b, h, s, v);
347                                 row[(int)x2 * 4] = (int)(r * 255);
348                                 row[(int)x2 * 4 + 1] = (int)(g * 255);
349                                 row[(int)x2 * 4 + 2] = (int)(b * 255);
350                                 row[(int)x2 * 4 + 3] = 255;
351                         }
352                 }
353         }
354 //printf("PaletteWheel::create_objects 1\n");
355
356         draw_vframe(&frame,
357                 0,
358                 0,
359                 get_w(),
360                 get_h(),
361                 0,
362                 0,
363                 get_w(),
364                 get_h(),
365                 0);
366 //printf("PaletteWheel::create_objects 1\n");
367
368         oldhue = window->h;
369         oldsaturation = window->s;
370 //printf("PaletteWheel::create_objects 1\n");
371         draw(oldhue, oldsaturation);
372 //printf("PaletteWheel::create_objects 1\n");
373         flash();
374 //printf("PaletteWheel::create_objects 2\n");
375 }
376
377 float PaletteWheel::torads(float angle)
378 {
379         return (float)angle / 360 * 2 * M_PI;
380 }
381
382
383 int PaletteWheel::draw(float hue, float saturation)
384 {
385         int x, y, w, h;
386         x = w = get_w() / 2;
387         y = h = get_h() / 2;
388
389         if(hue > 0 && hue < 90)
390         {
391                 x = (int)(w - w * cos(torads(90 - hue)) * saturation);
392                 y = (int)(h - h * sin(torads(90 - hue)) * saturation);
393         }
394         else
395         if(hue > 90 && hue < 180)
396         {
397                 x = (int)(w - w * cos(torads(hue - 90)) * saturation);
398                 y = (int)(h + h * sin(torads(hue - 90)) * saturation);
399         }
400         else
401         if(hue > 180 && hue < 270)
402         {
403                 x = (int)(w + w * cos(torads(270 - hue)) * saturation);
404                 y = (int)(h + h * sin(torads(270 - hue)) * saturation);
405         }
406         else
407         if(hue > 270 && hue < 360)
408         {
409                 x = (int)(w + w * cos(torads(hue - 270)) * saturation);
410                 y = (int)(h - w * sin(torads(hue - 270)) * saturation);
411         }
412         else
413         if(hue == 0)
414         {
415                 x = w;
416                 y = (int)(h - h * saturation);
417         }
418         else
419         if(hue == 90)
420         {
421                 x = (int)(w - w * saturation);
422                 y = h;
423         }
424         else
425         if(hue == 180)
426         {
427                 x = w;
428                 y = (int)(h + h * saturation);
429         }
430         else
431         if(hue == 270)
432         {
433                 x = (int)(w + w * saturation);
434                 y = h;
435         }
436
437         set_inverse();
438         set_color(WHITE);
439         draw_circle(x - 5, y - 5, 10, 10);
440         set_opaque();
441         return 0;
442 }
443
444 int PaletteWheel::get_angle(float x1, float y1, float x2, float y2)
445 {
446         float result = -atan2(x2 - x1, y1 - y2) * (360 / M_PI / 2);
447         if (result < 0)
448                 result += 360;
449         return (int)result;
450 }
451
452 PaletteWheelValue::PaletteWheelValue(ColorWindow *window, int x, int y)
453  : BC_SubWindow(x, y, 40, 170, BLACK)
454 {
455         this->window = window;
456         button_down = 0;
457 }
458 PaletteWheelValue::~PaletteWheelValue()
459 {
460         delete frame;
461 }
462
463 void PaletteWheelValue::create_objects()
464 {
465         frame = new VFrame(0, -1, get_w(), get_h(), BC_RGB888, -1);
466         draw(window->h, window->s, window->v);
467         flash();
468 }
469
470 int PaletteWheelValue::button_press_event()
471 {
472 //printf("PaletteWheelValue::button_press 1 %d\n", is_event_win());
473         if(get_cursor_x() >= 0 && get_cursor_x() < get_w() &&
474                 get_cursor_y() >= 0 && get_cursor_y() < get_h() &&
475                 is_event_win())
476         {
477 //printf("PaletteWheelValue::button_press 2\n");
478                 button_down = 1;
479                 cursor_motion_event();
480                 return 1;
481         }
482         return 0;
483 }
484
485 int PaletteWheelValue::cursor_motion_event()
486 {
487         if(button_down && is_event_win())
488         {
489 //printf("PaletteWheelValue::cursor_motion 1\n");
490                 window->v = 1.0 - (float)(get_cursor_y() - 2) / (get_h() - 4);
491                 window->update_display();
492                 window->handle_event();
493                 return 1;
494         }
495         return 0;
496 }
497
498 int PaletteWheelValue::button_release_event()
499 {
500         if(button_down)
501         {
502 //printf("PaletteWheelValue::button_release 1\n");
503                 button_down = 0;
504                 return 1;
505         }
506         return 0;
507 }
508
509 int PaletteWheelValue::draw(float hue, float saturation, float value)
510 {
511         float r_f, g_f, b_f;
512         int i, j, r, g, b;
513
514         for(i = get_h() - 3; i >= 2; i--)
515         {
516                 unsigned char *row = (unsigned char*)frame->get_rows()[i];
517                 HSV::hsv_to_rgb(r_f, g_f, b_f, hue, saturation,
518                         1.0 - (float)(i - 2) / (get_h() - 4));
519                 r = (int)(r_f * 255);
520                 g = (int)(g_f * 255);
521                 b = (int)(b_f * 255);
522                 for(j = 0; j < get_w(); j++)
523                 {
524                         row[j * 3] = r;
525                         row[j * 3 + 1] = g;
526                         row[j * 3 + 2] = b;
527                 }
528         }
529
530         draw_3d_border(0, 0, get_w(), get_h(), 1);
531         draw_vframe(frame, 2, 2, get_w() - 4, get_h() - 4,
532                 2, 2, get_w() - 4, get_h() - 4, 0);
533         set_color(BLACK);
534         draw_line(2, get_h() - 3 - (int)(value * (get_h() - 5)),
535                   get_w() - 3, get_h() - 3 - (int)(value * (get_h() - 5)));
536 //printf("PaletteWheelValue::draw %d %f\n", __LINE__, value);
537
538         return 0;
539 }
540
541 PaletteOutput::PaletteOutput(ColorWindow *window, int x, int y)
542  : BC_SubWindow(x, y, 180, 30, BLACK)
543 {
544         this->window = window;
545 }
546 PaletteOutput::~PaletteOutput()
547 {
548 }
549
550
551 void PaletteOutput::create_objects()
552 {
553         draw();
554         flash();
555 }
556
557 int PaletteOutput::handle_event()
558 {
559         return 1;
560 }
561
562 int PaletteOutput::draw()
563 {
564         float r_f, g_f, b_f;
565
566         HSV::hsv_to_rgb(r_f, g_f, b_f, window->h, window->s, window->v);
567         set_color(((int)(r_f * 255) << 16) | ((int)(g_f * 255) << 8) | ((int)(b_f * 255)));
568         draw_box(2, 2, get_w() - 4, get_h() - 4);
569         draw_3d_border(0, 0, get_w(), get_h(), 1);
570         return 0;
571 }
572
573 PaletteHue::PaletteHue(ColorWindow *window, int x, int y)
574  : BC_ISlider(x, y, 0, 150, 200, 0, 359, (int)(window->h), 0)
575 {
576         this->window = window;
577 }
578 PaletteHue::~PaletteHue()
579 {
580 }
581
582 int PaletteHue::handle_event()
583 {
584         window->h = get_value();
585         window->update_display();
586         window->handle_event();
587         return 1;
588 }
589
590 PaletteSaturation::PaletteSaturation(ColorWindow *window, int x, int y)
591  : BC_FSlider(x, y, 0, 150, 200, 0, 1.0, window->s, 0)
592 {
593         this->window = window;
594         set_precision(0.01);
595 }
596 PaletteSaturation::~PaletteSaturation()
597 {
598 }
599
600 int PaletteSaturation::handle_event()
601 {
602 //printf("PaletteSaturation::handle_event 1 %f\n", get_value());
603         window->s = get_value();
604         window->update_display();
605 //printf("PaletteSaturation::handle_event 2 %f\n", get_value());
606         window->handle_event();
607         return 1;
608 }
609
610
611 PaletteValue::PaletteValue(ColorWindow *window, int x, int y)
612  : BC_FSlider(x, y, 0, 150, 200, 0, 1.0, window->v, 0)
613 {
614         this->window = window;
615         set_precision(0.01);
616 }
617 PaletteValue::~PaletteValue()
618 {
619 }
620
621 int PaletteValue::handle_event()
622 {
623         window->v = get_value();
624         window->update_display();
625         window->handle_event();
626         return 1;
627 }
628
629
630 PaletteRed::PaletteRed(ColorWindow *window, int x, int y)
631  : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->r, 0)
632 {
633         this->window = window;
634         set_precision(0.01);
635 }
636 PaletteRed::~PaletteRed()
637 {
638 }
639
640 int PaletteRed::handle_event()
641 {
642         window->update_rgb();
643         window->handle_event();
644         return 1;
645 }
646
647 PaletteGreen::PaletteGreen(ColorWindow *window, int x, int y)
648  : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->g, 0)
649 {
650         this->window = window;
651         set_precision(0.01);
652 }
653 PaletteGreen::~PaletteGreen()
654 {
655 }
656
657 int PaletteGreen::handle_event()
658 {
659         window->update_rgb();
660         window->handle_event();
661         return 1;
662 }
663
664 PaletteBlue::PaletteBlue(ColorWindow *window, int x, int y)
665  : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->b, 0)
666 {
667         this->window = window;
668         set_precision(0.01);
669 }
670 PaletteBlue::~PaletteBlue()
671 {
672 }
673
674 int PaletteBlue::handle_event()
675 {
676         window->update_rgb();
677         window->handle_event();
678         return 1;
679 }
680
681 PaletteAlpha::PaletteAlpha(ColorWindow *window, int x, int y)
682  : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->a, 0)
683 {
684         this->window = window;
685         set_precision(0.01);
686 }
687 PaletteAlpha::~PaletteAlpha()
688 {
689 }
690
691 int PaletteAlpha::handle_event()
692 {
693         window->a = get_value();
694         window->handle_event();
695         return 1;
696 }
697
698