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, const char *title)
39 this->do_alpha = do_alpha;
40 this->do_okcancel = 0;
43 ColorThread::~ColorThread()
48 void ColorThread::start_window(int output, int alpha, int do_okcancel)
51 ColorWindow *gui = (ColorWindow *)get_gui();
53 gui->lock_window("ColorThread::start_window");
59 this->output = output;
61 this->do_okcancel = do_okcancel;
65 BC_Window* ColorThread::new_gui()
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;
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;
81 ColorWindow *gui = new ColorWindow(this, x, y, w, h, window_title);
82 gui->create_objects();
86 void ColorThread::update_gui(int output, int alpha)
88 ColorWindow *gui = (ColorWindow *)get_gui();
91 this->output = output;
94 gui->update_display();
98 int ColorThread::handle_new_color(int output, int alpha)
100 printf("ColorThread::handle_new_color undefined.\n");
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)
108 this->thread = thread;
111 void ColorWindow::create_objects()
113 int x0 = 10, y0 = 10;
114 lock_window("ColorWindow::create_objects");
118 add_tool(wheel = new PaletteWheel(this, x, y));
119 wheel->create_objects();
121 x += 180; add_tool(wheel_value = new PaletteWheelValue(this, x, y));
122 wheel_value->create_objects();
124 y += 180; add_tool(output = new PaletteOutput(this, x, y));
125 output->create_objects();
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));
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));
145 if( thread->do_okcancel ) {
146 add_tool(new BC_OKButton(this));
147 add_tool(new BC_CancelButton(this));
155 void ColorWindow::change_values()
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;
165 int ColorWindow::close_event()
167 set_done(thread->do_okcancel ? 1 : 0);
172 void ColorWindow::update_rgb()
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);
181 void ColorWindow::update_display()
193 wheel->draw(wheel->oldhue,
194 wheel->oldsaturation);
196 wheel->oldsaturation = s;
199 wheel_value->draw(h, s, v);
200 wheel_value->flash();
204 saturation->update(s);
207 HSV::hsv_to_rgb(r, g, b, h, s, v);
217 int ColorWindow::handle_event()
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));
227 PaletteWheel::PaletteWheel(ColorWindow *window, int x, int y)
228 : BC_SubWindow(x, y, 170, 170)
230 this->window = window;
236 PaletteWheel::~PaletteWheel()
240 int PaletteWheel::button_press_event()
242 if(get_cursor_x() >= 0 && get_cursor_x() < get_w() &&
243 get_cursor_y() >= 0 && get_cursor_y() < get_h() &&
247 cursor_motion_event();
253 int PaletteWheel::cursor_motion_event()
255 int x1, y1, distance;
256 if(button_down && is_event_win())
258 window->h = get_angle(get_w() / 2,
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();
274 int PaletteWheel::button_release_event()
284 void PaletteWheel::create_objects()
287 //printf("PaletteWheel::create_objects 1\n");
292 float x1, y1, x2, y2;
294 int default_r, default_g, default_b;
295 VFrame frame(0, -1, get_w(), get_h(), BC_RGBA8888, -1);
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");
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);
307 for(y2 = 0; y2 < get_h(); y2++)
309 unsigned char *row = (unsigned char*)frame.get_rows()[(int)y2];
310 for(x2 = 0; x2 < get_w(); x2++)
312 distance = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
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;
321 if(distance > x1 - 1)
324 if(get_h() - y2 < x2)
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;
344 h = get_angle(x1, y1, x2, y2);
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;
354 //printf("PaletteWheel::create_objects 1\n");
366 //printf("PaletteWheel::create_objects 1\n");
369 oldsaturation = window->s;
370 //printf("PaletteWheel::create_objects 1\n");
371 draw(oldhue, oldsaturation);
372 //printf("PaletteWheel::create_objects 1\n");
374 //printf("PaletteWheel::create_objects 2\n");
377 float PaletteWheel::torads(float angle)
379 return (float)angle / 360 * 2 * M_PI;
383 int PaletteWheel::draw(float hue, float saturation)
389 if(hue > 0 && hue < 90)
391 x = (int)(w - w * cos(torads(90 - hue)) * saturation);
392 y = (int)(h - h * sin(torads(90 - hue)) * saturation);
395 if(hue > 90 && hue < 180)
397 x = (int)(w - w * cos(torads(hue - 90)) * saturation);
398 y = (int)(h + h * sin(torads(hue - 90)) * saturation);
401 if(hue > 180 && hue < 270)
403 x = (int)(w + w * cos(torads(270 - hue)) * saturation);
404 y = (int)(h + h * sin(torads(270 - hue)) * saturation);
407 if(hue > 270 && hue < 360)
409 x = (int)(w + w * cos(torads(hue - 270)) * saturation);
410 y = (int)(h - w * sin(torads(hue - 270)) * saturation);
416 y = (int)(h - h * saturation);
421 x = (int)(w - w * saturation);
428 y = (int)(h + h * saturation);
433 x = (int)(w + w * saturation);
439 draw_circle(x - 5, y - 5, 10, 10);
444 int PaletteWheel::get_angle(float x1, float y1, float x2, float y2)
446 float result = -atan2(x2 - x1, y1 - y2) * (360 / M_PI / 2);
452 PaletteWheelValue::PaletteWheelValue(ColorWindow *window, int x, int y)
453 : BC_SubWindow(x, y, 40, 170, BLACK)
455 this->window = window;
458 PaletteWheelValue::~PaletteWheelValue()
463 void PaletteWheelValue::create_objects()
465 frame = new VFrame(0, -1, get_w(), get_h(), BC_RGB888, -1);
466 draw(window->h, window->s, window->v);
470 int PaletteWheelValue::button_press_event()
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() &&
477 //printf("PaletteWheelValue::button_press 2\n");
479 cursor_motion_event();
485 int PaletteWheelValue::cursor_motion_event()
487 if(button_down && is_event_win())
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();
498 int PaletteWheelValue::button_release_event()
502 //printf("PaletteWheelValue::button_release 1\n");
509 int PaletteWheelValue::draw(float hue, float saturation, float value)
514 for(i = get_h() - 3; i >= 2; i--)
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++)
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);
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);
541 PaletteOutput::PaletteOutput(ColorWindow *window, int x, int y)
542 : BC_SubWindow(x, y, 180, 30, BLACK)
544 this->window = window;
546 PaletteOutput::~PaletteOutput()
551 void PaletteOutput::create_objects()
557 int PaletteOutput::handle_event()
562 int PaletteOutput::draw()
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);
573 PaletteHue::PaletteHue(ColorWindow *window, int x, int y)
574 : BC_ISlider(x, y, 0, 150, 200, 0, 359, (int)(window->h), 0)
576 this->window = window;
578 PaletteHue::~PaletteHue()
582 int PaletteHue::handle_event()
584 window->h = get_value();
585 window->update_display();
586 window->handle_event();
590 PaletteSaturation::PaletteSaturation(ColorWindow *window, int x, int y)
591 : BC_FSlider(x, y, 0, 150, 200, 0, 1.0, window->s, 0)
593 this->window = window;
596 PaletteSaturation::~PaletteSaturation()
600 int PaletteSaturation::handle_event()
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();
611 PaletteValue::PaletteValue(ColorWindow *window, int x, int y)
612 : BC_FSlider(x, y, 0, 150, 200, 0, 1.0, window->v, 0)
614 this->window = window;
617 PaletteValue::~PaletteValue()
621 int PaletteValue::handle_event()
623 window->v = get_value();
624 window->update_display();
625 window->handle_event();
630 PaletteRed::PaletteRed(ColorWindow *window, int x, int y)
631 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->r, 0)
633 this->window = window;
636 PaletteRed::~PaletteRed()
640 int PaletteRed::handle_event()
642 window->update_rgb();
643 window->handle_event();
647 PaletteGreen::PaletteGreen(ColorWindow *window, int x, int y)
648 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->g, 0)
650 this->window = window;
653 PaletteGreen::~PaletteGreen()
657 int PaletteGreen::handle_event()
659 window->update_rgb();
660 window->handle_event();
664 PaletteBlue::PaletteBlue(ColorWindow *window, int x, int y)
665 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->b, 0)
667 this->window = window;
670 PaletteBlue::~PaletteBlue()
674 int PaletteBlue::handle_event()
676 window->update_rgb();
677 window->handle_event();
681 PaletteAlpha::PaletteAlpha(ColorWindow *window, int x, int y)
682 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->a, 0)
684 this->window = window;
687 PaletteAlpha::~PaletteAlpha()
691 int PaletteAlpha::handle_event()
693 window->a = get_value();
694 window->handle_event();