4 * Copyright (C) 1997-2011 Adam Williams <broadcast at earthling dot net>
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.
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.
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
22 #include "bcdisplayinfo.h"
23 #include "colorpicker.h"
24 #include "condition.h"
27 #include "mwindow.inc"
35 ColorThread::ColorThread(int do_alpha, char *title)
40 this->do_alpha = do_alpha;
42 mutex = new Mutex("ColorThread::mutex");
43 completion = new Condition(1, "ColorThread::completion");
46 ColorThread::~ColorThread()
51 completion->lock("ColorThread::~ColorThread");
58 void ColorThread::start_window(int output, int alpha)
60 mutex->lock("ColorThread::start_window 1");
61 this->output = output;
67 completion->lock("ColorThread::start_window");
72 window->raise_window();
77 void ColorThread::run()
80 //printf("ColorThread::run 1\n");
81 char window_title[BCTEXTLEN];
83 strcpy(window_title, _(PROGRAM_NAME ": "));
85 strcat(window_title, title);
87 strcat(window_title, _("Color Picker"));
90 mutex->lock("ColorThread::run 1");
91 window = new ColorWindow(this,
92 info.get_abs_cursor_x() - 200,
93 info.get_abs_cursor_y() - 200,
95 window->create_objects();
98 mutex->lock("lorThread::run 2");
102 completion->unlock();
105 void ColorThread::update_gui(int output, int alpha)
107 mutex->lock("ColorThread::update_gui");
110 this->output = output;
112 window->change_values();
113 window->lock_window();
114 window->update_display();
115 window->unlock_window();
120 int ColorThread::handle_new_color(int output, int alpha)
122 printf("ColorThread::handle_new_color undefined.\n");
126 ColorWindow* ColorThread::get_gui()
133 ColorWindow::ColorWindow(ColorThread *thread, int x, int y, char *title)
145 this->thread = thread;
148 void ColorWindow::create_objects()
150 int x = 10, init_x = 10, y = 10, init_y = 10;
152 lock_window("ColorWindow::create_objects");
157 //printf("ColorWindow::create_objects 1 %p\n", this);
158 add_tool(wheel = new PaletteWheel(this, x, y));
159 //printf("ColorWindow::create_objects 1\n");
160 wheel->create_objects();
161 //printf("ColorWindow::create_objects 1\n");
164 add_tool(wheel_value = new PaletteWheelValue(this, x, y));
165 //printf("ColorWindow::create_objects 1\n");
166 wheel_value->create_objects();
171 //printf("ColorWindow::create_objects 1\n");
172 add_tool(output = new PaletteOutput(this, x, y));
173 //printf("ColorWindow::create_objects 1\n");
174 output->create_objects();
175 //printf("ColorWindow::create_objects 1\n");
177 x += 240; y = init_y;
178 add_tool(new BC_Title(x, y, _("Hue"), SMALLFONT));
180 //printf("ColorWindow::create_objects 1 %p\n", this);
181 add_tool(hue = new PaletteHue(this, x, y));
183 //printf("ColorWindow::create_objects 1\n");
184 add_tool(new BC_Title(x, y, _("Saturation"), SMALLFONT));
186 //printf("ColorWindow::create_objects 1\n");
187 add_tool(saturation = new PaletteSaturation(this, x, y));
189 //printf("ColorWindow::create_objects 1\n");
190 add_tool(new BC_Title(x, y, _("Value"), SMALLFONT));
192 //printf("ColorWindow::create_objects 1\n");
193 add_tool(value = new PaletteValue(this, x, y));
195 //printf("ColorWindow::create_objects 1\n");
196 add_tool(new BC_Title(x, y, _("Red"), SMALLFONT));
198 //printf("ColorWindow::create_objects 1\n");
199 add_tool(red = new PaletteRed(this, x, y));
201 //printf("ColorWindow::create_objects 1\n");
202 add_tool(new BC_Title(x, y, _("Green"), SMALLFONT));
204 //printf("ColorWindow::create_objects 1\n");
205 add_tool(green = new PaletteGreen(this, x, y));
207 //printf("ColorWindow::create_objects 1\n");
208 add_tool(new BC_Title(x, y, _("Blue"), SMALLFONT));
210 //printf("ColorWindow::create_objects 1\n");
211 add_tool(blue = new PaletteBlue(this, x, y));
216 add_tool(new BC_Title(x, y, _("Alpha"), SMALLFONT));
218 add_tool(alpha = new PaletteAlpha(this, x, y));
229 void ColorWindow::change_values()
231 r = (float)((thread->output & 0xff0000) >> 16) / 255;
232 g = (float)((thread->output & 0xff00) >> 8) / 255;
233 b = (float)((thread->output & 0xff)) / 255;
234 HSV::rgb_to_hsv(r, g, b, h, s, v);
235 a = (float)thread->alpha / 255;
239 int ColorWindow::close_event()
246 void ColorWindow::update_rgb()
248 HSV::rgb_to_hsv(red->get_value(),
257 void ColorWindow::update_display()
269 wheel->draw(wheel->oldhue,
270 wheel->oldsaturation);
272 wheel->oldsaturation = s;
275 wheel_value->draw(h, s, v);
276 wheel_value->flash();
280 saturation->update(s);
283 HSV::hsv_to_rgb(r, g, b, h, s, v);
293 int ColorWindow::handle_event()
296 HSV::hsv_to_rgb(r, g, b, h, s, v);
297 int result = (((int)(r * 255)) << 16) | (((int)(g * 255)) << 8) | ((int)(b * 255));
298 thread->handle_new_color(result, (int)(a * 255));
308 PaletteWheel::PaletteWheel(ColorWindow *window, int x, int y)
309 : BC_SubWindow(x, y, 170, 170)
311 this->window = window;
316 PaletteWheel::~PaletteWheel()
320 int PaletteWheel::button_press_event()
322 if(get_cursor_x() >= 0 && get_cursor_x() < get_w() &&
323 get_cursor_y() >= 0 && get_cursor_y() < get_h() &&
327 cursor_motion_event();
333 int PaletteWheel::cursor_motion_event()
335 int x1, y1, distance;
336 if(button_down && is_event_win())
338 window->h = get_angle(get_w() / 2,
342 x1 = get_w() / 2 - get_cursor_x();
343 y1 = get_h() / 2 - get_cursor_y();
344 distance = (int)sqrt(x1 * x1 + y1 * y1);
345 if(distance > get_w() / 2) distance = get_w() / 2;
346 window->s = (float)distance / (get_w() / 2);
347 window->update_display();
348 window->handle_event();
354 int PaletteWheel::button_release_event()
364 void PaletteWheel::create_objects()
367 //printf("PaletteWheel::create_objects 1\n");
372 float x1, y1, x2, y2;
374 int default_r, default_g, default_b;
375 VFrame frame(0, -1, get_w(), get_h(), BC_RGBA8888, -1);
378 default_r = (get_resources()->get_bg_color() & 0xff0000) >> 16;
379 default_g = (get_resources()->get_bg_color() & 0xff00) >> 8;
380 default_b = (get_resources()->get_bg_color() & 0xff);
381 //printf("PaletteWheel::create_objects 1\n");
383 int highlight_r = (get_resources()->button_light & 0xff0000) >> 16;
384 int highlight_g = (get_resources()->button_light & 0xff00) >> 8;
385 int highlight_b = (get_resources()->button_light & 0xff);
387 for(y2 = 0; y2 < get_h(); y2++)
389 unsigned char *row = (unsigned char*)frame.get_rows()[(int)y2];
390 for(x2 = 0; x2 < get_w(); x2++)
392 distance = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
395 row[(int)x2 * 4] = default_r;
396 row[(int)x2 * 4 + 1] = default_g;
397 row[(int)x2 * 4 + 2] = default_b;
398 row[(int)x2 * 4 + 3] = 0;
401 if(distance > x1 - 1)
404 if(get_h() - y2 < x2)
417 row[(int)x2 * 4] = r_i;
418 row[(int)x2 * 4 + 1] = g_i;
419 row[(int)x2 * 4 + 2] = b_i;
420 row[(int)x2 * 4 + 3] = 255;
424 h = get_angle(x1, y1, x2, y2);
426 HSV::hsv_to_rgb(r, g, b, h, s, v);
427 row[(int)x2 * 4] = (int)(r * 255);
428 row[(int)x2 * 4 + 1] = (int)(g * 255);
429 row[(int)x2 * 4 + 2] = (int)(b * 255);
430 row[(int)x2 * 4 + 3] = 255;
434 //printf("PaletteWheel::create_objects 1\n");
446 //printf("PaletteWheel::create_objects 1\n");
449 oldsaturation = window->s;
450 //printf("PaletteWheel::create_objects 1\n");
451 draw(oldhue, oldsaturation);
452 //printf("PaletteWheel::create_objects 1\n");
454 //printf("PaletteWheel::create_objects 2\n");
457 float PaletteWheel::torads(float angle)
459 return (float)angle / 360 * 2 * M_PI;
463 int PaletteWheel::draw(float hue, float saturation)
469 if(hue > 0 && hue < 90)
471 x = (int)(w - w * cos(torads(90 - hue)) * saturation);
472 y = (int)(h - h * sin(torads(90 - hue)) * saturation);
475 if(hue > 90 && hue < 180)
477 x = (int)(w - w * cos(torads(hue - 90)) * saturation);
478 y = (int)(h + h * sin(torads(hue - 90)) * saturation);
481 if(hue > 180 && hue < 270)
483 x = (int)(w + w * cos(torads(270 - hue)) * saturation);
484 y = (int)(h + h * sin(torads(270 - hue)) * saturation);
487 if(hue > 270 && hue < 360)
489 x = (int)(w + w * cos(torads(hue - 270)) * saturation);
490 y = (int)(h - w * sin(torads(hue - 270)) * saturation);
496 y = (int)(h - h * saturation);
501 x = (int)(w - w * saturation);
508 y = (int)(h + h * saturation);
513 x = (int)(w + w * saturation);
519 draw_circle(x - 5, y - 5, 10, 10);
524 int PaletteWheel::get_angle(float x1, float y1, float x2, float y2)
526 float result = -atan2(x2 - x1, y1 - y2) * (360 / M_PI / 2);
532 PaletteWheelValue::PaletteWheelValue(ColorWindow *window, int x, int y)
533 : BC_SubWindow(x, y, 40, 170, BLACK)
535 this->window = window;
538 PaletteWheelValue::~PaletteWheelValue()
543 void PaletteWheelValue::create_objects()
545 frame = new VFrame(0, -1, get_w(), get_h(), BC_RGB888, -1);
546 draw(window->h, window->s, window->v);
550 int PaletteWheelValue::button_press_event()
552 //printf("PaletteWheelValue::button_press 1 %d\n", is_event_win());
553 if(get_cursor_x() >= 0 && get_cursor_x() < get_w() &&
554 get_cursor_y() >= 0 && get_cursor_y() < get_h() &&
557 //printf("PaletteWheelValue::button_press 2\n");
559 cursor_motion_event();
565 int PaletteWheelValue::cursor_motion_event()
567 if(button_down && is_event_win())
569 //printf("PaletteWheelValue::cursor_motion 1\n");
570 window->v = 1.0 - (float)(get_cursor_y() - 2) / (get_h() - 4);
571 window->update_display();
572 window->handle_event();
578 int PaletteWheelValue::button_release_event()
582 //printf("PaletteWheelValue::button_release 1\n");
589 int PaletteWheelValue::draw(float hue, float saturation, float value)
594 for(i = get_h() - 3; i >= 2; i--)
596 unsigned char *row = (unsigned char*)frame->get_rows()[i];
602 1.0 - (float)(i - 2) / (get_h() - 4));
603 r = (int)(r_f * 255);
604 g = (int)(g_f * 255);
605 b = (int)(b_f * 255);
606 for(j = 0; j < get_w(); j++)
631 get_h() - 3 - (int)(value * (get_h() - 5)),
633 get_h() - 3 - (int)(value * (get_h() - 5)));
634 //printf("PaletteWheelValue::draw %d %f\n", __LINE__, value);
639 PaletteOutput::PaletteOutput(ColorWindow *window, int x, int y)
640 : BC_SubWindow(x, y, 180, 30, BLACK)
642 this->window = window;
644 PaletteOutput::~PaletteOutput()
649 void PaletteOutput::create_objects()
655 int PaletteOutput::handle_event()
660 int PaletteOutput::draw()
664 HSV::hsv_to_rgb(r_f, g_f, b_f, window->h, window->s, window->v);
665 set_color(((int)(r_f * 255) << 16) | ((int)(g_f * 255) << 8) | ((int)(b_f * 255)));
666 draw_box(2, 2, get_w() - 4, get_h() - 4);
677 PaletteHue::PaletteHue(ColorWindow *window, int x, int y)
678 : BC_ISlider(x, y, 0, 150, 200, 0, 359, (int)(window->h), 0)
680 this->window = window;
682 PaletteHue::~PaletteHue()
686 int PaletteHue::handle_event()
688 window->h = get_value();
689 window->update_display();
690 window->handle_event();
694 PaletteSaturation::PaletteSaturation(ColorWindow *window, int x, int y)
695 : BC_FSlider(x, y, 0, 150, 200, 0, 1.0, window->s, 0)
697 this->window = window;
700 PaletteSaturation::~PaletteSaturation()
704 int PaletteSaturation::handle_event()
706 //printf("PaletteSaturation::handle_event 1 %f\n", get_value());
707 window->s = get_value();
708 window->update_display();
709 //printf("PaletteSaturation::handle_event 2 %f\n", get_value());
710 window->handle_event();
714 PaletteValue::PaletteValue(ColorWindow *window, int x, int y)
715 : BC_FSlider(x, y, 0, 150, 200, 0, 1.0, window->v, 0)
717 this->window = window;
720 PaletteValue::~PaletteValue()
724 int PaletteValue::handle_event()
726 window->v = get_value();
727 window->update_display();
728 window->handle_event();
733 PaletteRed::PaletteRed(ColorWindow *window, int x, int y)
734 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->r, 0)
736 this->window = window;
739 PaletteRed::~PaletteRed()
743 int PaletteRed::handle_event()
745 window->update_rgb();
746 window->handle_event();
750 PaletteGreen::PaletteGreen(ColorWindow *window, int x, int y)
751 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->g, 0)
753 this->window = window;
756 PaletteGreen::~PaletteGreen()
760 int PaletteGreen::handle_event()
762 window->update_rgb();
763 window->handle_event();
767 PaletteBlue::PaletteBlue(ColorWindow *window, int x, int y)
768 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->b, 0)
770 this->window = window;
773 PaletteBlue::~PaletteBlue()
777 int PaletteBlue::handle_event()
779 window->update_rgb();
780 window->handle_event();
784 PaletteAlpha::PaletteAlpha(ColorWindow *window, int x, int y)
785 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->a, 0)
787 this->window = window;
790 PaletteAlpha::~PaletteAlpha()
794 int PaletteAlpha::handle_event()
796 window->a = get_value();
797 window->handle_event();