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
23 #include "bccapture.h"
25 #include "bcdisplayinfo.h"
27 #include "colorpicker.h"
28 #include "condition.h"
39 #define PALETTE_DATA "palette.dat"
41 ColorPicker::ColorPicker(int do_alpha, const char *title)
44 this->do_alpha = do_alpha;
45 this->title = cstrdup(title);
48 ColorPicker::~ColorPicker()
54 void ColorPicker::start_window(int color, int alpha, int ok_cancel)
57 ColorWindow *window = (ColorWindow *)get_gui();
59 window->lock_window("ColorPicker::start_window");
60 window->raise_window(1);
61 window->unlock_window();
67 this->ok_cancel = ok_cancel;
71 BC_Window* ColorPicker::new_gui()
73 char window_title[BCTEXTLEN];
74 strcpy(window_title, _(PROGRAM_NAME ": "));
75 strcat(window_title, title ? title : _("Color Picker"));
76 BC_DisplayInfo display_info;
77 int x = display_info.get_abs_cursor_x() + xS(25);
78 int y = display_info.get_abs_cursor_y() - yS(100);
79 int w = ColorWindow::calculate_w();
80 int h = ColorWindow::calculate_h();
82 h += bmax(ColorOK::calculate_h(),ColorCancel::calculate_h());
83 int root_w = display_info.get_root_w(), root_h = display_info.get_root_h();
84 if( x+w > root_w ) x = root_w - w;
85 if( y+h > root_h ) y = root_h - h;
88 ColorWindow *window = new ColorWindow(this, x, y, w, h, window_title);
89 window->create_objects();
90 window->start_selection(color, !do_alpha ? -1 : alpha, ok_cancel);
94 void ColorPicker::update_gui(int color, int alpha)
96 ColorWindow *window = (ColorWindow *)get_gui();
98 window->update_gui(color, alpha);
101 int ColorPicker::handle_new_color(int color, int alpha)
103 printf("ColorPicker::handle_new_color undefined.\n");
107 ColorWindow::ColorWindow(ColorPicker *thread, int x, int y, int w, int h, const char *title)
108 : BC_Window(title, x, y, w, h, w, h, 0, 0, 1),
111 this->thread = thread;
114 ColorWindow::~ColorWindow()
118 void ColorWindow::create_objects()
120 BC_WindowBase::create_objects();
121 ColorGUI::create_objects();
122 thread->create_objects(this);
125 void ColorWindow::update_gui(int color, int alpha)
127 lock_window("ColorWindow::update_gui");
134 int ColorWindow::handle_new_color(int color, int alpha)
136 lock_window("ColorWindow::handle_new_color");
137 thread->handle_new_color(color, alpha);
143 ColorGUI::ColorGUI(BC_WindowBase *window)
145 this->window = window;
149 this->color = this->orig_color = BLACK;
150 this->alpha = this->orig_alpha = 255;
156 hue = 0; sat = 0; val = 0;
157 red = 0; grn = 0; blu = 0;
158 lum = 0; c_r = 0; c_b = 0;
161 hsv_h = 0; hsv_s = 0; hsv_v = 0;
162 rgb_r = 0; rgb_g = 0; rgb_b = 0;
163 yuv_y = 0; yuv_u = 0; yuv_v = 0;
168 ColorGUI::~ColorGUI()
170 delete hsv_h; delete hsv_s; delete hsv_v;
171 delete rgb_r; delete rgb_g; delete rgb_b;
172 delete yuv_y; delete yuv_u; delete yuv_v;
175 if( button_grabbed ) {
176 window->ungrab_buttons();
177 window->ungrab_cursor();
179 update_history(rgb888());
183 void ColorGUI::add_tool(BC_WindowBase *sub_wdw)
185 window->add_tool(sub_wdw);
188 void ColorGUI::start_selection(int color, int alpha, int ok_cancel)
190 this->orig_color = color;
191 this->orig_alpha = alpha;
194 this->do_alpha = alpha < 0 ? 0 : 1;
195 this->ok_cancel = ok_cancel;
199 void ColorGUI::create_objects()
201 int xs5 = xS(5), xs10 = xS(10), xs15 = xS(15);
202 int ys10 = yS(10), ys15 = yS(15), ys25 = yS(25), ys40 = yS(40);
203 int x0 = xs10, y0 = ys10;
204 window->lock_window("ColorGUI::create_objects");
208 add_tool(wheel = new PaletteWheel(this, x, y));
209 wheel->create_objects();
211 x += xS(180); add_tool(wheel_value = new PaletteWheelValue(this, x, y));
212 wheel_value->create_objects();
214 y += yS(180); add_tool(poutput = new PaletteOutput(this, x, y));
215 poutput->create_objects();
216 y += poutput->get_h() + yS(20);
218 load_history(); int x1 = x;
219 add_tool(hex_btn = new PaletteHexButton(this, x1, y));
220 char hex[BCSTRLEN]; sprintf(hex,"%06x",color);
221 x1 += hex_btn->get_w() + xs5;
222 add_tool(hex_box = new PaletteHex(this, x1, y, hex));
223 x1 += hex_box->get_w() + xs15;
224 add_tool(grab_btn = new PaletteGrabButton(this, x1, y));
225 y += hex_box->get_h() + ys15;
226 add_tool(history = new PaletteHistory(this, xs10, y));
229 add_tool(new BC_Title(x, y =y0, C_("H:"), SMALLFONT));
230 add_tool(new BC_Title(x, y+=ys25, C_("S:"), SMALLFONT));
231 add_tool(new BC_Title(x, y+=ys25, D_("colorpicker_value#V:"), SMALLFONT));
232 add_tool(new BC_Title(x, y+=ys40, C_("R:"), SMALLFONT));
233 add_tool(new BC_Title(x, y+=ys25, C_("G:"), SMALLFONT));
234 add_tool(new BC_Title(x, y+=ys25, C_("B:"), SMALLFONT));
235 add_tool(new BC_Title(x, y+=ys40, C_("Y:"), SMALLFONT));
236 add_tool(new BC_Title(x, y+=ys25, C_("U:"), SMALLFONT));
237 add_tool(new BC_Title(x, y+=ys25, D_("colorpicker_Cr#V:"), SMALLFONT));
239 add_tool(new BC_Title(x, y+=ys40, C_("A:"), SMALLFONT));
241 add_tool(hue = new PaletteHue(this, x, y= y0));
242 add_tool(sat = new PaletteSat(this, x, y+=ys25));
243 add_tool(val = new PaletteVal(this, x, y+=ys25));
244 add_tool(red = new PaletteRed(this, x, y+=ys40));
245 add_tool(grn = new PaletteGrn(this, x, y+=ys25));
246 add_tool(blu = new PaletteBlu(this, x, y+=ys25));
247 add_tool(lum = new PaletteLum(this, x, y+=ys40));
248 add_tool(c_r = new PaletteCr (this, x, y+=ys25));
249 add_tool(c_b = new PaletteCb (this, x, y+=ys25));
251 add_tool(palpha = new PaletteAlpha(this, x, y+=ys40));
253 x += hue->get_w() + xs10;
254 hsv_h = new PaletteHSV(this, x,y= y0, hsv.h, 0, 360);
255 hsv_h->set_increment(1);
256 hsv_h->set_precision(1);
257 hsv_h->create_objects(); hsv_h->set_tooltip(_("Hue"));
258 hsv_s = new PaletteHSV(this, x,y+=ys25, hsv.s, 0, 1);
259 hsv_s->create_objects(); hsv_s->set_tooltip(_("Saturation"));
260 hsv_v = new PaletteHSV(this, x,y+=ys25, hsv.v, 0, 1);
261 hsv_v->create_objects(); hsv_v->set_tooltip(_("Value"));
262 rgb_r = new PaletteRGB(this, x,y+=ys40, rgb.r, 0, 1);
263 rgb_r->create_objects(); rgb_r->set_tooltip(_("Red"));
264 rgb_g = new PaletteRGB(this, x,y+=ys25, rgb.g, 0, 1);
265 rgb_g->create_objects(); rgb_g->set_tooltip(_("Green"));
266 rgb_b = new PaletteRGB(this, x,y+=ys25, rgb.b, 0, 1);
267 rgb_b->create_objects(); rgb_b->set_tooltip(_("Blue"));
268 yuv_y = new PaletteYUV(this, x,y+=ys40, yuv.y, 0, 1);
269 yuv_y->create_objects(); yuv_y->set_tooltip(_("Luminance"));
270 yuv_u = new PaletteYUV(this, x,y+=ys25, yuv.u, 0, 1);
271 yuv_u->create_objects(); yuv_u->set_tooltip(_("Blue Luminance Difference"));
272 yuv_v = new PaletteYUV(this, x,y+=ys25, yuv.v, 0, 1);
273 yuv_v->create_objects(); yuv_v->set_tooltip(_("Red Luminance Difference"));
275 aph_a = new PaletteAPH(this, x,y+=ys40, aph, 0, 1);
276 aph_a->create_objects(); aph_a->set_tooltip(_("Alpha"));
279 add_tool(new ColorOK(this, window));
280 add_tool(new ColorCancel(this, window));
282 create_objects(this);
286 window->show_window(1);
287 window->unlock_window();
291 void ColorGUI::change_values()
293 float r = ((color>>16) & 0xff) / 255.;
294 float g = ((color>>8) & 0xff) / 255.;
295 float b = ((color>>0) & 0xff) / 255.;
296 rgb.r = r; rgb.g = g; rgb.b = b;
297 aph = (float)alpha / 255;
298 update_rgb(rgb.r, rgb.g, rgb.b);
302 ColorOK::ColorOK(ColorGUI *gui, BC_WindowBase *window)
303 : BC_OKButton(window)
306 this->window = window;
308 int ColorOK::handle_event()
312 window->sync_display();
316 ColorCancel::ColorCancel(ColorGUI *gui, BC_WindowBase *window)
317 : BC_CancelButton(window)
320 this->window = window;
322 int ColorCancel::handle_event()
326 window->sync_display();
330 int ColorGUI::close_gui()
332 if( button_grabbed ) {
334 window->ungrab_buttons();
335 window->ungrab_cursor();
337 window->set_done(ok_cancel ? 1 : 0);
342 void ColorGUI::update_rgb()
344 update_rgb(rgb.r, rgb.g, rgb.b);
347 void ColorGUI::update_hsv()
349 update_hsv(hsv.h, hsv.s, hsv.v);
352 void ColorGUI::update_yuv()
354 update_yuv(yuv.y, yuv.u, yuv.v);
358 void ColorGUI::update_display()
360 wheel->draw(wheel->oldhue, wheel->oldsaturation);
361 wheel->oldhue = hsv.h;
362 wheel->oldsaturation = hsv.s;
363 wheel->draw(hsv.h, hsv.s);
365 wheel_value->draw(hsv.h, hsv.s, hsv.v);
366 wheel_value->flash();
370 hue->update((int)hsv.h);
382 hsv_h->update(hsv.h);
383 hsv_s->update(hsv.s);
384 hsv_v->update(hsv.v);
385 rgb_r->update(rgb.r);
386 rgb_g->update(rgb.g);
387 rgb_b->update(rgb.b);
388 yuv_y->update(yuv.y);
389 yuv_u->update(yuv.u);
390 yuv_v->update(yuv.v);
399 int ColorGUI::handle_gui()
401 window->unlock_window();
402 handle_new_color(rgb888(), alpha8());
403 window->lock_window("ColorGUI::handle_event");
407 void ColorGUI::get_screen_sample()
410 window->get_abs_cursor(cx, cy);
411 BC_Capture capture_bitmap(1, 1, 0);
412 VFrame vframe(1,1,BC_RGB888);
413 capture_bitmap.capture_frame(&vframe, cx,cy);
414 unsigned char *data = vframe.get_data();
415 rgb.r = data[0]/255.; rgb.g = data[1]/255.; rgb.b = data[2]/255.;
419 int ColorGUI::cursor_motion_gui()
421 if( button_grabbed && window->get_button_down() ) {
428 int ColorGUI::button_press_gui()
430 if( button_grabbed ) {
437 int ColorGUI::button_release_gui()
439 if( button_grabbed ) {
440 window->ungrab_buttons();
441 window->ungrab_cursor();
450 void ColorGUI::update_rgb_hex(const char *hex)
453 if( sscanf(hex,"%x",&color) == 1 ) {
455 aph = ((color>>24) & 0xff) / 255.;
458 float r = ((color>>16) & 0xff) / 255.;
459 float g = ((color>>8) & 0xff) / 255.;
460 float b = ((color>>0) & 0xff) / 255.;
461 rgb.r = r; rgb.g = g; rgb.b = b;
468 void ColorGUI::update_gui(int color, int alpha)
470 printf("ColorGUI::update_gui undefined.\n");
473 int ColorGUI::handle_new_color(int color, int alpha)
475 printf("ColorGUI::handle_new_color undefined.\n");
481 PaletteWheel::PaletteWheel(ColorGUI *gui, int x, int y)
482 : BC_SubWindow(x, y, xS(170), yS(170))
490 PaletteWheel::~PaletteWheel()
494 int PaletteWheel::button_press_event()
496 if( get_cursor_x() >= 0 && get_cursor_x() < get_w() &&
497 get_cursor_y() >= 0 && get_cursor_y() < get_h() &&
500 cursor_motion_event();
506 int PaletteWheel::cursor_motion_event()
508 int x1, y1, distance;
509 if( button_down && is_event_win() ) {
510 float h = get_angle(get_w()/2, get_h()/2, get_cursor_x(), get_cursor_y());
511 bclamp(h, 0, 359.999); gui->hsv.h = h;
512 x1 = get_w() / 2 - get_cursor_x();
513 y1 = get_h() / 2 - get_cursor_y();
514 distance = (int)sqrt(x1 * x1 + y1 * y1);
515 float s = (float)distance / (get_w() / 2);
516 bclamp(s, 0, 1); gui->hsv.s = s;
525 int PaletteWheel::button_release_event()
534 void PaletteWheel::create_objects()
537 //printf("PaletteWheel::create_objects 1\n");
540 float x1, y1, x2, y2;
542 int default_r, default_g, default_b;
543 VFrame frame(0, -1, get_w(), get_h(), BC_RGBA8888, -1);
546 default_r = (get_resources()->get_bg_color() & 0xff0000) >> 16;
547 default_g = (get_resources()->get_bg_color() & 0xff00) >> 8;
548 default_b = (get_resources()->get_bg_color() & 0xff);
549 //printf("PaletteWheel::create_objects 1\n");
551 int highlight_r = (get_resources()->button_light & 0xff0000) >> 16;
552 int highlight_g = (get_resources()->button_light & 0xff00) >> 8;
553 int highlight_b = (get_resources()->button_light & 0xff);
555 for( y2 = 0; y2 < get_h(); y2++ ) {
556 unsigned char *row = (unsigned char*)frame.get_rows()[(int)y2];
557 for( x2 = 0; x2 < get_w(); x2++ ) {
558 distance = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
559 if( distance > x1 ) {
560 row[(int)x2 * 4] = default_r;
561 row[(int)x2 * 4 + 1] = default_g;
562 row[(int)x2 * 4 + 2] = default_b;
563 row[(int)x2 * 4 + 3] = 0;
566 if( distance > x1 - 1 ) {
568 if( get_h() - y2 < x2 ) {
579 row[(int)x2 * 4] = r_i;
580 row[(int)x2 * 4 + 1] = g_i;
581 row[(int)x2 * 4 + 2] = b_i;
582 row[(int)x2 * 4 + 3] = 255;
585 h = get_angle(x1, y1, x2, y2);
587 HSV::hsv_to_rgb(r, g, b, h, s, v);
588 row[(int)x2 * 4] = (int)(r * 255);
589 row[(int)x2 * 4 + 1] = (int)(g * 255);
590 row[(int)x2 * 4 + 2] = (int)(b * 255);
591 row[(int)x2 * 4 + 3] = 255;
597 0, 0, get_w(), get_h(),
598 0, 0, get_w(), get_h(), 0);
601 oldsaturation = gui->hsv.s;
602 draw(oldhue, oldsaturation);
606 float PaletteWheel::torads(float angle)
608 return (float)angle / 360 * 2 * M_PI;
612 int PaletteWheel::draw(float hue, float saturation)
618 if( hue > 0 && hue < 90 ) {
619 x = (int)(w - w * cos(torads(90 - hue)) * saturation);
620 y = (int)(h - h * sin(torads(90 - hue)) * saturation);
622 else if( hue > 90 && hue < 180 ) {
623 x = (int)(w - w * cos(torads(hue - 90)) * saturation);
624 y = (int)(h + h * sin(torads(hue - 90)) * saturation);
626 else if( hue > 180 && hue < 270 ) {
627 x = (int)(w + w * cos(torads(270 - hue)) * saturation);
628 y = (int)(h + h * sin(torads(270 - hue)) * saturation);
630 else if( hue > 270 && hue < 360 ) {
631 x = (int)(w + w * cos(torads(hue - 270)) * saturation);
632 y = (int)(h - w * sin(torads(hue - 270)) * saturation);
634 else if( hue == 0 ) {
636 y = (int)(h - h * saturation);
638 else if( hue == 90 ) {
639 x = (int)(w - w * saturation);
642 else if( hue == 180 ) {
644 y = (int)(h + h * saturation);
646 else if( hue == 270 ) {
647 x = (int)(w + w * saturation);
653 draw_circle(x - xS(5), y - yS(5), xS(10), yS(10));
658 int PaletteWheel::get_angle(float x1, float y1, float x2, float y2)
660 float result = -atan2(x2 - x1, y1 - y2) * (360 / M_PI / 2);
666 PaletteWheelValue::PaletteWheelValue(ColorGUI *gui, int x, int y)
667 : BC_SubWindow(x, y, xS(40), yS(170), BLACK)
672 PaletteWheelValue::~PaletteWheelValue()
677 void PaletteWheelValue::create_objects()
679 frame = new VFrame(get_w(), get_h(), BC_RGB888);
680 draw(gui->hsv.h, gui->hsv.s, gui->hsv.v);
684 int PaletteWheelValue::button_press_event()
686 //printf("PaletteWheelValue::button_press 1 %d\n", is_event_win());
687 if( get_cursor_x() >= 0 && get_cursor_x() < get_w() &&
688 get_cursor_y() >= 0 && get_cursor_y() < get_h() &&
690 //printf("PaletteWheelValue::button_press 2\n");
692 cursor_motion_event();
698 int PaletteWheelValue::cursor_motion_event()
700 if( button_down && is_event_win() ) {
701 //printf("PaletteWheelValue::cursor_motion 1\n");
702 float v = 1.0 - (float)(get_cursor_y() - 2) / (get_h() - 4);
703 bclamp(v, 0, 1); gui->hsv.v = v;
711 int PaletteWheelValue::button_release_event()
714 //printf("PaletteWheelValue::button_release 1\n");
721 int PaletteWheelValue::draw(float hue, float saturation, float value)
726 for( i = get_h() - 3; i >= 2; i-- ) {
727 unsigned char *row = (unsigned char*)frame->get_rows()[i];
728 HSV::hsv_to_rgb(r_f, g_f, b_f, hue, saturation,
729 1.0 - (float)(i - 2) / (get_h() - 4));
730 r = (int)(r_f * 255);
731 g = (int)(g_f * 255);
732 b = (int)(b_f * 255);
733 for( j = 0; j < get_w(); j++ ) {
739 draw_3d_border(0, 0, get_w(), get_h(), 1);
740 draw_vframe(frame, 2, 2, get_w() - 4, get_h() - 4,
741 2, 2, get_w() - 4, get_h() - 4, 0);
743 draw_line(2, get_h() - 3 - (int)(value * (get_h() - 5)),
744 get_w() - 3, get_h() - 3 - (int)(value * (get_h() - 5)));
745 //printf("PaletteWheelValue::draw %d %f\n", __LINE__, value);
750 PaletteOutput::PaletteOutput(ColorGUI *gui, int x, int y)
751 : BC_SubWindow(x, y, xS(180), yS(30), BLACK)
755 PaletteOutput::~PaletteOutput()
760 void PaletteOutput::create_objects()
766 int PaletteOutput::handle_event()
771 int PaletteOutput::draw()
773 set_color(gui->rgb888());
774 draw_box(2, 2, get_w() - 4, get_h() - 4);
775 draw_3d_border(0, 0, get_w(), get_h(), 1);
779 PaletteHue::PaletteHue(ColorGUI *gui, int x, int y)
780 : BC_ISlider(x, y, 0, xS(150), xS(200), 0, 359, (int)(gui->hsv.h), 0)
784 PaletteHue::~PaletteHue()
788 int PaletteHue::handle_event()
790 gui->hsv.h = get_value();
796 PaletteSat::PaletteSat(ColorGUI *gui, int x, int y)
797 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1.0, gui->hsv.s, 0)
802 PaletteSat::~PaletteSat()
806 int PaletteSat::handle_event()
808 gui->hsv.s = get_value();
815 PaletteVal::PaletteVal(ColorGUI *gui, int x, int y)
816 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1.0, gui->hsv.v, 0)
821 PaletteVal::~PaletteVal()
825 int PaletteVal::handle_event()
827 gui->hsv.v = get_value();
834 PaletteRed::PaletteRed(ColorGUI *gui, int x, int y)
835 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, gui->rgb.r, 0)
840 PaletteRed::~PaletteRed()
844 int PaletteRed::handle_event()
846 gui->rgb.r = get_value();
852 PaletteGrn::PaletteGrn(ColorGUI *gui, int x, int y)
853 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, gui->rgb.g, 0)
858 PaletteGrn::~PaletteGrn()
862 int PaletteGrn::handle_event()
864 gui->rgb.g = get_value();
870 PaletteBlu::PaletteBlu(ColorGUI *gui, int x, int y)
871 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, gui->rgb.b, 0)
876 PaletteBlu::~PaletteBlu()
880 int PaletteBlu::handle_event()
882 gui->rgb.b = get_value();
888 PaletteAlpha::PaletteAlpha(ColorGUI *gui, int x, int y)
889 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, gui->aph, 0)
894 PaletteAlpha::~PaletteAlpha()
898 int PaletteAlpha::handle_event()
900 gui->aph = get_value();
901 gui->aph_a->update(gui->aph);
902 gui->hex_box->update();
907 PaletteLum::PaletteLum(ColorGUI *gui, int x, int y)
908 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, gui->yuv.y, 0)
913 PaletteLum::~PaletteLum()
917 int PaletteLum::handle_event()
919 gui->yuv.y = get_value();
925 PaletteCr::PaletteCr(ColorGUI *gui, int x, int y)
926 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, gui->yuv.u, 0)
931 PaletteCr::~PaletteCr()
935 int PaletteCr::handle_event()
937 gui->yuv.u = get_value();
943 PaletteCb::PaletteCb(ColorGUI *gui, int x, int y)
944 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, gui->yuv.v, 0)
949 PaletteCb::~PaletteCb()
953 int PaletteCb::handle_event()
955 gui->yuv.v = get_value();
961 void ColorGUI::update_rgb(float r, float g, float b)
964 YUV::yuv.rgb_to_yuv_f(r, g, b, y, u, v);
966 bclamp(y, 0, 1); yuv.y = y;
967 bclamp(u, 0, 1); yuv.u = u;
968 bclamp(v, 0, 1); yuv.v = v; }
970 HSV::rgb_to_hsv(r,g,b, h,s,v);
971 bclamp(h, 0, 360); hsv.h = h;
972 bclamp(s, 0, 1); hsv.s = s;
973 bclamp(v, 0, 1); hsv.v = v; }
976 void ColorGUI::update_yuv(float y, float u, float v)
980 YUV::yuv.yuv_to_rgb_f(r, g, b, y, u, v);
981 bclamp(r, 0, 1); rgb.r = r;
982 bclamp(g, 0, 1); rgb.g = g;
983 bclamp(b, 0, 1); rgb.b = b;
985 HSV::rgb_to_hsv(r,g,b, h, s, v);
986 bclamp(h, 0, 360); hsv.h = h;
987 bclamp(s, 0, 1); hsv.s = s;
988 bclamp(v, 0, 1); hsv.v = v; }
991 void ColorGUI::update_hsv(float h, float s, float v)
994 HSV::hsv_to_rgb(r,g,b, h,s,v);
995 bclamp(r, 0, 1); rgb.r = r;
996 bclamp(g, 0, 1); rgb.g = g;
997 bclamp(b, 0, 1); rgb.b = b;
999 YUV::yuv.rgb_to_yuv_f(r, g, b, y, u, v);
1001 bclamp(y, 0, 1); yuv.y = y;
1002 bclamp(u, 0, 1); yuv.u = u;
1003 bclamp(v, 0, 1); yuv.v = v; }
1006 void ColorGUI::load_history()
1008 char history_path[BCTEXTLEN];
1009 MWindow::create_defaults_path(history_path,PALETTE_DATA);
1010 FILE *fp = fopen(history_path,"r");
1013 while( i < PALLETTE_HISTORY_SIZE ) {
1014 char line[BCSTRLEN];
1015 if( !fgets(line,sizeof(line)-1,fp) ) break;
1016 line[sizeof(line)-1] = 0;
1017 if( sscanf(line, "%x",&palette_history[i]) != 1 ) break;
1022 int r = 0, g = 0, b = 0;
1023 float v0 = 0, v1 = 1;
1024 while( i < PALLETTE_HISTORY_SIZE ) {
1025 int grey_code = i ^ (i>>1);
1026 r = 255 * ((grey_code&4) ? v0 : v1);
1027 g = 255 * ((grey_code&2) ? v0 : v1);
1028 b = 255 * ((grey_code&1) ? v0 : v1);
1029 int color = (r<<16) | (g<<8) | (b<<0);
1030 palette_history[i++] = color;
1031 if( i & 7 ) continue;
1032 v0 = 0.5f * (v0+.5f);
1033 v1 = 0.5f * (v1+.5f);
1036 void ColorGUI::save_history()
1038 char history_path[BCTEXTLEN];
1039 MWindow::create_defaults_path(history_path,PALETTE_DATA);
1040 FILE *fp = fopen(history_path,"w");
1042 for( int i=0; i<PALLETTE_HISTORY_SIZE; ++i ) {
1043 fprintf(fp, "%06x\n", palette_history[i]);
1048 void ColorGUI::update_history(int color)
1050 int out = palette_history[0];
1051 palette_history[0] = color;
1052 for( int i=1; out != color && i<PALLETTE_HISTORY_SIZE; ++i ) {
1054 out = palette_history[i];
1055 palette_history[i] = in;
1058 void ColorGUI::update_history()
1060 update_history(rgb888());
1063 int ColorGUI::rgb888()
1065 int r = 255*rgb.r + 0.5, g = 255*rgb.g + 0.5, b = 255*rgb.b + 0.5;
1066 bclamp(r, 0, 255); bclamp(g, 0, 255); bclamp(b, 0, 255);
1067 return (r<<16) | (g<<8) | (b<<0);
1069 int ColorGUI::alpha8()
1071 int a = 255*aph + 0.5;
1076 PaletteNum::PaletteNum(ColorGUI *gui, int x, int y,
1077 float &output, float min, float max)
1078 : BC_TumbleTextBox(gui->window, output, min, max, x, y, xS(64))
1081 this->output = &output;
1082 set_increment(0.01);
1086 PaletteNum::~PaletteNum()
1091 int PaletteHSV::handle_event()
1099 int PaletteRGB::handle_event()
1107 int PaletteYUV::handle_event()
1115 int PaletteAPH::handle_event()
1118 gui->update_display();
1123 PaletteHexButton::PaletteHexButton(ColorGUI *gui, int x, int y)
1124 : BC_GenericButton(x, y, xS(50), "#")
1127 set_tooltip(_("hex rgb color"));
1129 PaletteHexButton::~PaletteHexButton()
1132 int PaletteHexButton::handle_event()
1134 const char *hex = gui->hex_box->get_text();
1135 gui->update_rgb_hex(hex);
1139 PaletteHex::PaletteHex(ColorGUI *gui, int x, int y, const char *hex)
1140 : BC_TextBox(x, y, xS(100), 1, hex)
1144 PaletteHex::~PaletteHex()
1147 void PaletteHex::update()
1149 char hex[BCSTRLEN], *cp = hex;
1151 cp += sprintf(cp,"%02x", gui->alpha8());
1152 sprintf(cp,"%06x",gui->rgb888());
1153 BC_TextBox::update(hex);
1156 int PaletteHex::keypress_event()
1158 if( get_keypress() != RETURN )
1159 return BC_TextBox::keypress_event();
1160 gui->update_rgb_hex(get_text());
1164 #include "grabpick_up_png.h"
1165 #include "grabpick_hi_png.h"
1166 #include "grabpick_dn_png.h"
1168 PaletteGrabButton::PaletteGrabButton(ColorGUI *gui, int x, int y)
1169 : BC_Button(x, y, vframes)
1172 vframes[0] = new VFramePng(grabpick_up_png);
1173 vframes[1] = new VFramePng(grabpick_hi_png);
1174 vframes[2] = new VFramePng(grabpick_dn_png);
1175 set_tooltip(_("grab from anywhere picker"));
1177 PaletteGrabButton::~PaletteGrabButton()
1179 for( int i=0; i<3; ++i )
1182 int PaletteGrabButton::handle_event()
1184 if( gui->window->grab_buttons() ) {
1185 gui->window->grab_cursor();
1186 gui->button_grabbed = 1;
1187 return BC_Button::button_press_event(); // redraw face HI
1192 PaletteHistory::PaletteHistory(ColorGUI *gui, int x, int y)
1193 : BC_SubWindow(x,y, xS(200), yS(24))
1197 set_tooltip(_("color history"));
1199 PaletteHistory::~PaletteHistory()
1202 void PaletteHistory::update(int flush)
1205 for( int i=0; i<PALLETTE_HISTORY_SIZE; x1=x2 ) {
1206 int rgb = gui->palette_history[i];
1207 x2 = (++i * get_w())/PALLETTE_HISTORY_SIZE;
1208 draw_3d_box(x1,0,x2-x1,get_h(),WHITE,BLACK,rgb,LTBLUE,DKBLUE);
1213 int PaletteHistory::button_press_event()
1215 if( button_down || !is_event_win() ) return 0;
1217 cursor_motion_event();
1220 int PaletteHistory::button_release_event()
1222 if( !button_down || !is_event_win() ) return 0;
1223 cursor_motion_event();
1224 if( button_down > 0 ) {
1226 gui->update_display();
1227 gui->update_history();
1232 int PaletteHistory::cursor_motion_event()
1234 if( !button_down || !is_event_win() ) return 0;
1236 int pick = (PALLETTE_HISTORY_SIZE * get_cursor_x()) / get_w();
1237 bclamp(pick, 0, PALLETTE_HISTORY_SIZE-1);
1238 int color = gui->palette_history[pick];
1239 float r = ((color>>16) & 0xff) / 255.;
1240 float g = ((color>>8) & 0xff) / 255.;
1241 float b = ((color>>0) & 0xff) / 255.;
1242 if( gui->rgb.r != r || gui->rgb.g != g || gui->rgb.b != b ) {
1243 gui->rgb.r = r; gui->rgb.g = g; gui->rgb.b = b;
1249 int PaletteHistory::cursor_leave_event()
1254 int PaletteHistory::repeat_event(int64_t duration)
1258 if( duration == get_resources()->tooltip_delay &&
1259 get_tooltip() && *get_tooltip() && cursor_above() ) {
1267 ColorButton::ColorButton(const char *title,
1268 int x, int y, int w, int h,
1269 int color, int alpha, int ok_cancel)
1270 : BC_Button(x, y, w, vframes)
1272 this->title = title;
1273 this->color = this->orig_color = color;
1274 this->alpha = this->orig_alpha = alpha;
1275 this->ok_cancel = ok_cancel;
1277 for( int i=0; i<3; ++i ) {
1278 vframes[i] = new VFrame(w, h, BC_RGBA8888);
1279 vframes[i]->clear_frame();
1285 ColorButton::~ColorButton()
1287 delete color_thread;
1288 delete color_picker;
1289 for( int i=0; i<3; ++i )
1293 void ColorButton::set_color(int color)
1295 printf("ColorButton::set_color %06x\n", color);
1297 void ColorButton::handle_done_event(int result)
1299 color_thread->stop();
1301 int ColorButton::handle_new_color(int color, int alpha)
1303 printf("ColorButton::handle_new_color %02x%06x\n", alpha, color);
1307 int ColorButton::handle_event()
1310 delete color_picker;
1311 color_picker = new ColorButtonPicker(this);
1312 orig_color = color; orig_alpha = alpha;
1313 color_picker->start_window(color, alpha, ok_cancel);
1315 color_thread = new ColorButtonThread(this);
1316 color_thread->start();
1317 lock_window("ColorButtonButton::start_color_thread");
1321 void ColorButton::close_picker()
1323 if( color_thread ) color_thread->stop();
1324 delete color_thread; color_thread = 0;
1325 delete color_picker; color_picker = 0;
1328 void ColorButton::update_gui(int color, int alpha)
1331 color_picker->update_gui(color, alpha);
1332 update_gui(color | (~alpha<<24));
1335 void ColorButton::update_gui(int color)
1341 ColorButtonPicker::ColorButtonPicker(ColorButton *color_button)
1342 : ColorPicker(color_button->alpha >= 0 ? 1 : 0, color_button->title)
1344 this->color_button = color_button;
1347 ColorButtonPicker::~ColorButtonPicker()
1351 void ColorButtonPicker::handle_done_event(int result)
1353 color_button->color_thread->stop();
1354 color_button->handle_done_event(result);
1357 void ColorButtonPicker::update(int color, int alpha)
1359 color_button->color = color;
1360 color_button->alpha = alpha;
1361 color_button->color_thread->update_lock->unlock();
1364 int ColorButtonPicker::handle_new_color(int color, int alpha)
1366 color_button->lock_window("ColorButtonPicker::handle_new_color");
1367 color_button->update_gui(color);
1368 color_button->unlock_window();
1369 return color_button->handle_new_color(color, alpha);
1372 void ColorButtonPicker::update_gui()
1374 color_button->lock_window("ColorButtonPicker::update_gui");
1375 color_button->update_gui(color_button->color, color_button->alpha);
1376 color_button->unlock_window();
1379 void ColorButtonPicker::update_gui(int color, int alpha)
1381 ColorPicker::update_gui(color, alpha);
1382 color_button->handle_new_color(color, alpha);
1385 ColorButtonThread::ColorButtonThread(ColorButton *color_button)
1388 this->color_button = color_button;
1389 this->update_lock = new Condition(0,"ColorButtonThread::update_lock");
1393 ColorButtonThread::~ColorButtonThread()
1399 void ColorButtonThread::start()
1407 void ColorButtonThread::stop()
1411 update_lock->unlock();
1416 void ColorButtonThread::run()
1418 ColorButtonPicker *color_picker = color_button->color_picker;
1419 color_picker->update_gui();
1421 update_lock->lock("ColorButtonThread::run");
1423 color_picker->update_gui();
1428 ColorBoxButton::ColorBoxButton(const char *title,
1429 int x, int y, int w, int h,
1430 int color, int alpha, int ok_cancel)
1431 : ColorButton(title, x, y, w, h, color, alpha, ok_cancel)
1434 ColorBoxButton::~ColorBoxButton()
1438 int ColorBoxButton::handle_new_color(int color, int alpha)
1440 return ColorButton::handle_new_color(color, alpha);
1442 void ColorBoxButton::handle_done_event(int result)
1444 ColorButton::handle_done_event(result);
1446 void ColorBoxButton::create_objects()
1448 update_gui(color, alpha);
1451 void ColorBoxButton::set_color(int color)
1453 this->color = (color & 0xffffff);
1454 this->alpha = (~color>>24) & 0xff;
1455 int r = (color>>16) & 0xff;
1456 int g = (color>> 8) & 0xff;
1457 int b = (color>> 0) & 0xff;
1458 int color_model = vframes[0]->get_color_model();
1459 int bpp = BC_CModels::calculate_pixelsize(color_model);
1460 for( int i=0; i<3; ++i ) {
1461 VFrame *vframe = vframes[i];
1462 int ww = vframe->get_w(), hh = vframe->get_h();
1463 uint8_t **rows = vframe->get_rows();
1464 int rr = r, gg = g, bb = b;
1469 if( (rr+=48) > 0xff ) rr = 0xff;
1470 if( (gg+=48) > 0xff ) gg = 0xff;
1471 if( (bb+=48) > 0xff ) bb = 0xff;
1474 if( (rr-=48) < 0x00 ) rr = 0x00;
1475 if( (gg-=48) < 0x00 ) gg = 0x00;
1476 if( (bb-=48) < 0x00 ) bb = 0x00;
1479 for( int y=0; y<hh; ++y ) {
1480 uint8_t *rp = rows[y];
1481 for( int x=0; x<ww; ++x ) {
1482 rp[0] = rr; rp[1] = gg; rp[2] = bb;
1483 if( bpp > 3 ) rp[3] = 0xff;
1488 set_images(vframes);
1491 ColorCircleButton::ColorCircleButton(const char *title,
1492 int x, int y, int w, int h,
1493 int color, int alpha, int ok_cancel)
1494 : ColorButton(title, x, y, w, h, color, alpha, ok_cancel)
1497 ColorCircleButton::~ColorCircleButton()
1500 int ColorCircleButton::handle_new_color(int color, int alpha)
1502 return ColorButton::handle_new_color(color, alpha);
1504 void ColorCircleButton::handle_done_event(int result)
1506 ColorButton::handle_done_event(result);
1508 void ColorCircleButton::create_objects()
1510 update_gui(color, alpha);
1513 void ColorCircleButton::set_color(int color)
1515 this->color = (color & 0xffffff);
1516 this->alpha = (~color>>24) & 0xff;
1517 int r = (color>>16) & 0xff;
1518 int g = (color>>8) & 0xff;
1519 int b = (color>>0) & 0xff;
1520 for( int i=0; i<3; ++i ) {
1521 VFrame *vframe = vframes[i];
1522 int ww = vframe->get_w(), hh = vframe->get_h();
1523 int cx = (ww+1)/2, cy = hh/2;
1524 double cc = (cx*cx + cy*cy) / 4.;
1525 uint8_t *bp = vframe->get_data(), *dp = bp;
1526 uint8_t *ep = dp + vframe->get_data_size();
1527 int rr = r, gg = g, bb = b;
1528 int bpl = vframe->get_bytes_per_line();
1533 if( (rr+=48) > 0xff ) rr = 0xff;
1534 if( (gg+=48) > 0xff ) gg = 0xff;
1535 if( (bb+=48) > 0xff ) bb = 0xff;
1538 if( (rr-=48) < 0x00 ) rr = 0x00;
1539 if( (gg-=48) < 0x00 ) gg = 0x00;
1540 if( (bb-=48) < 0x00 ) bb = 0x00;
1544 int yy = (dp-bp) / bpl, xx = ((dp-bp) % bpl) >> 2;
1545 int dy = cy - yy, dx = cx - xx;
1546 double s = dx*dx + dy*dy - cc;
1547 double ss = s < 0 ? 1 : s >= cc ? 0 : 1 - s/cc;
1549 *dp++ = rr; *dp++ = gg; *dp++ = bb; *dp++ = aa;
1552 set_images(vframes);