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;
112 // *** CONTEXT_HELP ***
113 context_help_set_keyword("Title");
116 ColorWindow::~ColorWindow()
120 void ColorWindow::create_objects()
122 BC_WindowBase::create_objects();
123 ColorGUI::create_objects();
124 thread->create_objects(this);
127 void ColorWindow::update_gui(int color, int alpha)
129 lock_window("ColorWindow::update_gui");
136 int ColorWindow::handle_new_color(int color, int alpha)
138 lock_window("ColorWindow::handle_new_color");
139 thread->handle_new_color(color, alpha);
145 ColorGUI::ColorGUI(BC_WindowBase *window)
147 this->window = window;
151 this->color = this->orig_color = BLACK;
152 this->alpha = this->orig_alpha = 255;
158 hue = 0; sat = 0; val = 0;
159 red = 0; grn = 0; blu = 0;
160 lum = 0; c_r = 0; c_b = 0;
162 hsv_h = 0; hsv_s = 0; hsv_v = 0;
163 rgb_r = 0; rgb_g = 0; rgb_b = 0;
164 yuv_y = 0; yuv_u = 0; yuv_v = 0;
169 ColorGUI::~ColorGUI()
171 delete hsv_h; delete hsv_s; delete hsv_v;
172 delete rgb_r; delete rgb_g; delete rgb_b;
173 delete yuv_y; delete yuv_u; delete yuv_v;
176 if( button_grabbed ) {
177 window->ungrab_buttons();
178 window->ungrab_cursor();
180 update_history(rgb888());
184 void ColorGUI::add_tool(BC_WindowBase *sub_wdw)
186 window->add_tool(sub_wdw);
189 void ColorGUI::start_selection(int color, int alpha, int ok_cancel)
191 this->orig_color = color;
192 this->orig_alpha = alpha;
195 this->do_alpha = alpha < 0 ? 0 : 1;
196 this->ok_cancel = ok_cancel;
200 void ColorGUI::create_objects()
202 int xs5 = xS(5), xs10 = xS(10), xs15 = xS(15);
203 int ys10 = yS(10), ys15 = yS(15), ys25 = yS(25), ys40 = yS(40);
204 int x0 = xs10, y0 = ys10;
205 window->lock_window("ColorGUI::create_objects");
209 add_tool(wheel = new PaletteWheel(this, x, y));
210 wheel->create_objects();
212 x += xS(180); add_tool(wheel_value = new PaletteWheelValue(this, x, y));
213 wheel_value->create_objects();
215 y += yS(180); add_tool(poutput = new PaletteOutput(this, x, y));
216 poutput->create_objects();
217 y += poutput->get_h() + yS(20);
219 load_history(); int x1 = x;
220 add_tool(hex_btn = new PaletteHexButton(this, x1, y));
221 char hex[BCSTRLEN]; sprintf(hex,"%06x",color);
222 x1 += hex_btn->get_w() + xs5;
223 add_tool(hex_box = new PaletteHex(this, x1, y, hex));
224 x1 += hex_box->get_w() + xs15;
225 add_tool(grab_btn = new PaletteGrabButton(this, x1, y));
226 y += hex_box->get_h() + ys15;
227 add_tool(history = new PaletteHistory(this, xs10, y));
230 add_tool(new BC_Title(x, y =y0, C_("H:"), SMALLFONT));
231 add_tool(new BC_Title(x, y+=ys25, C_("S:"), SMALLFONT));
232 add_tool(new BC_Title(x, y+=ys25, D_("colorpicker_value#V:"), SMALLFONT));
233 add_tool(new BC_Title(x, y+=ys40, C_("R:"), SMALLFONT));
234 add_tool(new BC_Title(x, y+=ys25, C_("G:"), SMALLFONT));
235 add_tool(new BC_Title(x, y+=ys25, C_("B:"), SMALLFONT));
236 add_tool(new BC_Title(x, y+=ys40, C_("Y:"), SMALLFONT));
237 add_tool(new BC_Title(x, y+=ys25, C_("U:"), SMALLFONT));
238 add_tool(new BC_Title(x, y+=ys25, D_("colorpicker_Cr#V:"), SMALLFONT));
240 add_tool(new BC_Title(x, y+=ys40, C_("A:"), SMALLFONT));
242 add_tool(hue = new PaletteHue(this, x, y= y0));
243 add_tool(sat = new PaletteSat(this, x, y+=ys25));
244 add_tool(val = new PaletteVal(this, x, y+=ys25));
245 add_tool(red = new PaletteRed(this, x, y+=ys40));
246 add_tool(grn = new PaletteGrn(this, x, y+=ys25));
247 add_tool(blu = new PaletteBlu(this, x, y+=ys25));
248 add_tool(lum = new PaletteLum(this, x, y+=ys40));
249 add_tool(c_r = new PaletteCr (this, x, y+=ys25));
250 add_tool(c_b = new PaletteCb (this, x, y+=ys25));
252 add_tool(palpha = new PaletteAlpha(this, x, y+=ys40));
254 x += hue->get_w() + xs10;
255 hsv_h = new PaletteHSV(this, x,y= y0, hsv.h, 0, 360);
256 hsv_h->set_increment(1);
257 hsv_h->set_precision(1);
258 hsv_h->create_objects(); hsv_h->set_tooltip(_("Hue"));
259 hsv_s = new PaletteHSV(this, x,y+=ys25, hsv.s, 0, 1);
260 hsv_s->create_objects(); hsv_s->set_tooltip(_("Saturation"));
261 hsv_v = new PaletteHSV(this, x,y+=ys25, hsv.v, 0, 1);
262 hsv_v->create_objects(); hsv_v->set_tooltip(_("Value"));
263 rgb_r = new PaletteRGB(this, x,y+=ys40, rgb.r, 0, 1);
264 rgb_r->create_objects(); rgb_r->set_tooltip(_("Red"));
265 rgb_g = new PaletteRGB(this, x,y+=ys25, rgb.g, 0, 1);
266 rgb_g->create_objects(); rgb_g->set_tooltip(_("Green"));
267 rgb_b = new PaletteRGB(this, x,y+=ys25, rgb.b, 0, 1);
268 rgb_b->create_objects(); rgb_b->set_tooltip(_("Blue"));
269 yuv_y = new PaletteYUV(this, x,y+=ys40, yuv.y, 0, 1);
270 yuv_y->create_objects(); yuv_y->set_tooltip(_("Luminance"));
271 yuv_u = new PaletteYUV(this, x,y+=ys25, yuv.u, 0, 1);
272 yuv_u->create_objects(); yuv_u->set_tooltip(_("Blue Luminance Difference"));
273 yuv_v = new PaletteYUV(this, x,y+=ys25, yuv.v, 0, 1);
274 yuv_v->create_objects(); yuv_v->set_tooltip(_("Red Luminance Difference"));
276 aph_a = new PaletteAPH(this, x,y+=ys40, aph, 0, 1);
277 aph_a->create_objects(); aph_a->set_tooltip(_("Alpha"));
280 add_tool(new ColorOK(this, window));
281 add_tool(new ColorCancel(this, window));
283 create_objects(this);
287 window->show_window(1);
288 window->unlock_window();
292 void ColorGUI::change_values()
294 float r = ((color>>16) & 0xff) / 255.;
295 float g = ((color>>8) & 0xff) / 255.;
296 float b = ((color>>0) & 0xff) / 255.;
297 rgb.r = r; rgb.g = g; rgb.b = b;
298 aph = (float)alpha / 255;
299 update_rgb(rgb.r, rgb.g, rgb.b);
303 ColorOK::ColorOK(ColorGUI *gui, BC_WindowBase *window)
304 : BC_OKButton(window)
307 this->window = window;
309 int ColorOK::handle_event()
313 window->sync_display();
317 ColorCancel::ColorCancel(ColorGUI *gui, BC_WindowBase *window)
318 : BC_CancelButton(window)
321 this->window = window;
323 int ColorCancel::handle_event()
327 window->sync_display();
331 int ColorGUI::close_gui()
333 if( button_grabbed ) {
335 window->ungrab_buttons();
336 window->ungrab_cursor();
338 window->set_done(ok_cancel ? 1 : 0);
343 void ColorGUI::update_rgb()
345 update_rgb(rgb.r, rgb.g, rgb.b);
348 void ColorGUI::update_hsv()
350 update_hsv(hsv.h, hsv.s, hsv.v);
353 void ColorGUI::update_yuv()
355 update_yuv(yuv.y, yuv.u, yuv.v);
359 void ColorGUI::update_display()
361 wheel->draw(wheel->oldhue, wheel->oldsaturation);
362 wheel->oldhue = hsv.h;
363 wheel->oldsaturation = hsv.s;
364 wheel->draw(hsv.h, hsv.s);
366 wheel_value->draw(hsv.h, hsv.s, hsv.v);
367 wheel_value->flash();
371 hue->update((int)hsv.h);
383 hsv_h->update(hsv.h);
384 hsv_s->update(hsv.s);
385 hsv_v->update(hsv.v);
386 rgb_r->update(rgb.r);
387 rgb_g->update(rgb.g);
388 rgb_b->update(rgb.b);
389 yuv_y->update(yuv.y);
390 yuv_u->update(yuv.u);
391 yuv_v->update(yuv.v);
400 int ColorGUI::handle_gui()
402 window->unlock_window();
403 handle_new_color(rgb888(), alpha8());
404 window->lock_window("ColorGUI::handle_event");
408 void ColorGUI::get_screen_sample()
411 window->get_abs_cursor(cx, cy);
412 BC_Capture capture_bitmap(1, 1, 0);
413 VFrame vframe(1,1,BC_RGB888);
414 capture_bitmap.capture_frame(&vframe, cx,cy);
415 unsigned char *data = vframe.get_data();
416 rgb.r = data[0]/255.; rgb.g = data[1]/255.; rgb.b = data[2]/255.;
420 int ColorGUI::cursor_motion_gui()
422 if( button_grabbed && window->get_button_down() ) {
429 int ColorGUI::button_press_gui()
431 if( button_grabbed ) {
438 int ColorGUI::button_release_gui()
440 if( button_grabbed ) {
441 window->ungrab_buttons();
442 window->ungrab_cursor();
451 void ColorGUI::update_rgb_hex(const char *hex)
454 if( sscanf(hex,"%x",&color) == 1 ) {
456 aph = ((color>>24) & 0xff) / 255.;
459 float r = ((color>>16) & 0xff) / 255.;
460 float g = ((color>>8) & 0xff) / 255.;
461 float b = ((color>>0) & 0xff) / 255.;
462 rgb.r = r; rgb.g = g; rgb.b = b;
469 void ColorGUI::update_gui(int color, int alpha)
471 printf("ColorGUI::update_gui undefined.\n");
474 int ColorGUI::handle_new_color(int color, int alpha)
476 printf("ColorGUI::handle_new_color undefined.\n");
482 PaletteWheel::PaletteWheel(ColorGUI *gui, int x, int y)
483 : BC_SubWindow(x, y, xS(170), yS(170))
491 PaletteWheel::~PaletteWheel()
495 int PaletteWheel::button_press_event()
497 if( get_cursor_x() >= 0 && get_cursor_x() < get_w() &&
498 get_cursor_y() >= 0 && get_cursor_y() < get_h() &&
501 cursor_motion_event();
507 int PaletteWheel::cursor_motion_event()
509 int x1, y1, distance;
510 if( button_down && is_event_win() ) {
511 float h = get_angle(get_w()/2, get_h()/2, get_cursor_x(), get_cursor_y());
512 bclamp(h, 0, 359.999); gui->hsv.h = h;
513 x1 = get_w() / 2 - get_cursor_x();
514 y1 = get_h() / 2 - get_cursor_y();
515 distance = (int)sqrt(x1 * x1 + y1 * y1);
516 float s = (float)distance / (get_w() / 2);
517 bclamp(s, 0, 1); gui->hsv.s = s;
526 int PaletteWheel::button_release_event()
535 void PaletteWheel::create_objects()
538 //printf("PaletteWheel::create_objects 1\n");
541 float x1, y1, x2, y2;
543 int default_r, default_g, default_b;
544 VFrame frame(0, -1, get_w(), get_h(), BC_RGBA8888, -1);
547 default_r = (get_resources()->get_bg_color() & 0xff0000) >> 16;
548 default_g = (get_resources()->get_bg_color() & 0xff00) >> 8;
549 default_b = (get_resources()->get_bg_color() & 0xff);
550 //printf("PaletteWheel::create_objects 1\n");
552 int highlight_r = (get_resources()->button_light & 0xff0000) >> 16;
553 int highlight_g = (get_resources()->button_light & 0xff00) >> 8;
554 int highlight_b = (get_resources()->button_light & 0xff);
556 for( y2 = 0; y2 < get_h(); y2++ ) {
557 unsigned char *row = (unsigned char*)frame.get_rows()[(int)y2];
558 for( x2 = 0; x2 < get_w(); x2++ ) {
559 distance = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
560 if( distance > x1 ) {
561 row[(int)x2 * 4] = default_r;
562 row[(int)x2 * 4 + 1] = default_g;
563 row[(int)x2 * 4 + 2] = default_b;
564 row[(int)x2 * 4 + 3] = 0;
567 if( distance > x1 - 1 ) {
569 if( get_h() - y2 < x2 ) {
580 row[(int)x2 * 4] = r_i;
581 row[(int)x2 * 4 + 1] = g_i;
582 row[(int)x2 * 4 + 2] = b_i;
583 row[(int)x2 * 4 + 3] = 255;
586 h = get_angle(x1, y1, x2, y2);
588 HSV::hsv_to_rgb(r, g, b, h, s, v);
589 row[(int)x2 * 4] = (int)(r * 255);
590 row[(int)x2 * 4 + 1] = (int)(g * 255);
591 row[(int)x2 * 4 + 2] = (int)(b * 255);
592 row[(int)x2 * 4 + 3] = 255;
598 0, 0, get_w(), get_h(),
599 0, 0, get_w(), get_h(), 0);
602 oldsaturation = gui->hsv.s;
603 draw(oldhue, oldsaturation);
607 float PaletteWheel::torads(float angle)
609 return (float)angle / 360 * 2 * M_PI;
613 int PaletteWheel::draw(float hue, float saturation)
619 if( hue > 0 && hue < 90 ) {
620 x = (int)(w - w * cos(torads(90 - hue)) * saturation);
621 y = (int)(h - h * sin(torads(90 - hue)) * saturation);
623 else if( hue > 90 && hue < 180 ) {
624 x = (int)(w - w * cos(torads(hue - 90)) * saturation);
625 y = (int)(h + h * sin(torads(hue - 90)) * saturation);
627 else if( hue > 180 && hue < 270 ) {
628 x = (int)(w + w * cos(torads(270 - hue)) * saturation);
629 y = (int)(h + h * sin(torads(270 - hue)) * saturation);
631 else if( hue > 270 && hue < 360 ) {
632 x = (int)(w + w * cos(torads(hue - 270)) * saturation);
633 y = (int)(h - w * sin(torads(hue - 270)) * saturation);
635 else if( hue == 0 ) {
637 y = (int)(h - h * saturation);
639 else if( hue == 90 ) {
640 x = (int)(w - w * saturation);
643 else if( hue == 180 ) {
645 y = (int)(h + h * saturation);
647 else if( hue == 270 ) {
648 x = (int)(w + w * saturation);
654 draw_circle(x - xS(5), y - yS(5), xS(10), yS(10));
659 int PaletteWheel::get_angle(float x1, float y1, float x2, float y2)
661 float result = -atan2(x2 - x1, y1 - y2) * (360 / M_PI / 2);
667 PaletteWheelValue::PaletteWheelValue(ColorGUI *gui, int x, int y)
668 : BC_SubWindow(x, y, xS(40), yS(170), BLACK)
673 PaletteWheelValue::~PaletteWheelValue()
678 void PaletteWheelValue::create_objects()
680 frame = new VFrame(get_w(), get_h(), BC_RGB888);
681 draw(gui->hsv.h, gui->hsv.s, gui->hsv.v);
685 int PaletteWheelValue::button_press_event()
687 //printf("PaletteWheelValue::button_press 1 %d\n", is_event_win());
688 if( get_cursor_x() >= 0 && get_cursor_x() < get_w() &&
689 get_cursor_y() >= 0 && get_cursor_y() < get_h() &&
691 //printf("PaletteWheelValue::button_press 2\n");
693 cursor_motion_event();
699 int PaletteWheelValue::cursor_motion_event()
701 if( button_down && is_event_win() ) {
702 //printf("PaletteWheelValue::cursor_motion 1\n");
703 float v = 1.0 - (float)(get_cursor_y() - 2) / (get_h() - 4);
704 bclamp(v, 0, 1); gui->hsv.v = v;
712 int PaletteWheelValue::button_release_event()
715 //printf("PaletteWheelValue::button_release 1\n");
722 int PaletteWheelValue::draw(float hue, float saturation, float value)
727 for( i = get_h() - 3; i >= 2; i-- ) {
728 unsigned char *row = (unsigned char*)frame->get_rows()[i];
729 HSV::hsv_to_rgb(r_f, g_f, b_f, hue, saturation,
730 1.0 - (float)(i - 2) / (get_h() - 4));
731 r = (int)(r_f * 255);
732 g = (int)(g_f * 255);
733 b = (int)(b_f * 255);
734 for( j = 0; j < get_w(); j++ ) {
740 draw_3d_border(0, 0, get_w(), get_h(), 1);
741 draw_vframe(frame, 2, 2, get_w() - 4, get_h() - 4,
742 2, 2, get_w() - 4, get_h() - 4, 0);
744 draw_line(2, get_h() - 3 - (int)(value * (get_h() - 5)),
745 get_w() - 3, get_h() - 3 - (int)(value * (get_h() - 5)));
746 //printf("PaletteWheelValue::draw %d %f\n", __LINE__, value);
751 PaletteOutput::PaletteOutput(ColorGUI *gui, int x, int y)
752 : BC_SubWindow(x, y, xS(180), yS(30), BLACK)
756 PaletteOutput::~PaletteOutput()
761 void PaletteOutput::create_objects()
767 int PaletteOutput::handle_event()
772 int PaletteOutput::draw()
774 set_color(gui->rgb888());
775 draw_box(2, 2, get_w() - 4, get_h() - 4);
776 draw_3d_border(0, 0, get_w(), get_h(), 1);
780 PaletteHue::PaletteHue(ColorGUI *gui, int x, int y)
781 : BC_ISlider(x, y, 0, xS(150), xS(200), 0, 359, (int)(gui->hsv.h), 0)
785 PaletteHue::~PaletteHue()
789 int PaletteHue::handle_event()
791 gui->hsv.h = get_value();
797 PaletteSat::PaletteSat(ColorGUI *gui, int x, int y)
798 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1.0, gui->hsv.s, 0)
803 PaletteSat::~PaletteSat()
807 int PaletteSat::handle_event()
809 gui->hsv.s = get_value();
816 PaletteVal::PaletteVal(ColorGUI *gui, int x, int y)
817 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1.0, gui->hsv.v, 0)
822 PaletteVal::~PaletteVal()
826 int PaletteVal::handle_event()
828 gui->hsv.v = get_value();
835 PaletteRed::PaletteRed(ColorGUI *gui, int x, int y)
836 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, gui->rgb.r, 0)
841 PaletteRed::~PaletteRed()
845 int PaletteRed::handle_event()
847 gui->rgb.r = get_value();
853 PaletteGrn::PaletteGrn(ColorGUI *gui, int x, int y)
854 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, gui->rgb.g, 0)
859 PaletteGrn::~PaletteGrn()
863 int PaletteGrn::handle_event()
865 gui->rgb.g = get_value();
871 PaletteBlu::PaletteBlu(ColorGUI *gui, int x, int y)
872 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, gui->rgb.b, 0)
877 PaletteBlu::~PaletteBlu()
881 int PaletteBlu::handle_event()
883 gui->rgb.b = get_value();
889 PaletteAlpha::PaletteAlpha(ColorGUI *gui, int x, int y)
890 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, gui->aph, 0)
895 PaletteAlpha::~PaletteAlpha()
899 int PaletteAlpha::handle_event()
901 gui->aph = get_value();
902 gui->aph_a->update(gui->aph);
903 gui->hex_box->update();
908 PaletteLum::PaletteLum(ColorGUI *gui, int x, int y)
909 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, gui->yuv.y, 0)
914 PaletteLum::~PaletteLum()
918 int PaletteLum::handle_event()
920 gui->yuv.y = get_value();
926 PaletteCr::PaletteCr(ColorGUI *gui, int x, int y)
927 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, gui->yuv.u, 0)
932 PaletteCr::~PaletteCr()
936 int PaletteCr::handle_event()
938 gui->yuv.u = get_value();
944 PaletteCb::PaletteCb(ColorGUI *gui, int x, int y)
945 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, gui->yuv.v, 0)
950 PaletteCb::~PaletteCb()
954 int PaletteCb::handle_event()
956 gui->yuv.v = get_value();
962 void ColorGUI::update_rgb(float r, float g, float b)
965 YUV::yuv.rgb_to_yuv_f(r, g, b, y, u, v);
967 bclamp(y, 0, 1); yuv.y = y;
968 bclamp(u, 0, 1); yuv.u = u;
969 bclamp(v, 0, 1); yuv.v = v; }
971 HSV::rgb_to_hsv(r,g,b, h,s,v);
972 bclamp(h, 0, 360); hsv.h = h;
973 bclamp(s, 0, 1); hsv.s = s;
974 bclamp(v, 0, 1); hsv.v = v; }
977 void ColorGUI::update_yuv(float y, float u, float v)
981 YUV::yuv.yuv_to_rgb_f(r, g, b, y, u, v);
982 bclamp(r, 0, 1); rgb.r = r;
983 bclamp(g, 0, 1); rgb.g = g;
984 bclamp(b, 0, 1); rgb.b = b;
986 HSV::rgb_to_hsv(r,g,b, h, s, v);
987 bclamp(h, 0, 360); hsv.h = h;
988 bclamp(s, 0, 1); hsv.s = s;
989 bclamp(v, 0, 1); hsv.v = v; }
992 void ColorGUI::update_hsv(float h, float s, float v)
995 HSV::hsv_to_rgb(r,g,b, h,s,v);
996 bclamp(r, 0, 1); rgb.r = r;
997 bclamp(g, 0, 1); rgb.g = g;
998 bclamp(b, 0, 1); rgb.b = b;
1000 YUV::yuv.rgb_to_yuv_f(r, g, b, y, u, v);
1002 bclamp(y, 0, 1); yuv.y = y;
1003 bclamp(u, 0, 1); yuv.u = u;
1004 bclamp(v, 0, 1); yuv.v = v; }
1007 void ColorGUI::load_history()
1009 char history_path[BCTEXTLEN];
1010 MWindow::create_defaults_path(history_path,PALETTE_DATA);
1011 FILE *fp = fopen(history_path,"r");
1014 while( i < PALLETTE_HISTORY_SIZE ) {
1015 char line[BCSTRLEN];
1016 if( !fgets(line,sizeof(line)-1,fp) ) break;
1017 line[sizeof(line)-1] = 0;
1018 if( sscanf(line, "%x",&palette_history[i]) != 1 ) break;
1023 int r = 0, g = 0, b = 0;
1024 float v0 = 0, v1 = 1;
1025 while( i < PALLETTE_HISTORY_SIZE ) {
1026 int grey_code = i ^ (i>>1);
1027 r = 255 * ((grey_code&4) ? v0 : v1);
1028 g = 255 * ((grey_code&2) ? v0 : v1);
1029 b = 255 * ((grey_code&1) ? v0 : v1);
1030 int color = (r<<16) | (g<<8) | (b<<0);
1031 palette_history[i++] = color;
1032 if( i & 7 ) continue;
1033 v0 = 0.5f * (v0+.5f);
1034 v1 = 0.5f * (v1+.5f);
1037 void ColorGUI::save_history()
1039 char history_path[BCTEXTLEN];
1040 MWindow::create_defaults_path(history_path,PALETTE_DATA);
1041 FILE *fp = fopen(history_path,"w");
1043 for( int i=0; i<PALLETTE_HISTORY_SIZE; ++i ) {
1044 fprintf(fp, "%06x\n", palette_history[i]);
1049 void ColorGUI::update_history(int color)
1051 int out = palette_history[0];
1052 palette_history[0] = color;
1053 for( int i=1; out != color && i<PALLETTE_HISTORY_SIZE; ++i ) {
1055 out = palette_history[i];
1056 palette_history[i] = in;
1059 void ColorGUI::update_history()
1061 update_history(rgb888());
1064 int ColorGUI::rgb888()
1066 int r = 255*rgb.r + 0.5, g = 255*rgb.g + 0.5, b = 255*rgb.b + 0.5;
1067 bclamp(r, 0, 255); bclamp(g, 0, 255); bclamp(b, 0, 255);
1068 return (r<<16) | (g<<8) | (b<<0);
1070 int ColorGUI::alpha8()
1072 int a = 255*aph + 0.5;
1077 PaletteNum::PaletteNum(ColorGUI *gui, int x, int y,
1078 float &output, float min, float max)
1079 : BC_TumbleTextBox(gui->window, output, min, max, x, y, xS(64))
1082 this->output = &output;
1083 set_increment(0.01);
1087 PaletteNum::~PaletteNum()
1092 int PaletteHSV::handle_event()
1100 int PaletteRGB::handle_event()
1108 int PaletteYUV::handle_event()
1116 int PaletteAPH::handle_event()
1119 gui->update_display();
1124 PaletteHexButton::PaletteHexButton(ColorGUI *gui, int x, int y)
1125 : BC_GenericButton(x, y, xS(50), "#")
1128 set_tooltip(_("hex rgb color"));
1130 PaletteHexButton::~PaletteHexButton()
1133 int PaletteHexButton::handle_event()
1135 const char *hex = gui->hex_box->get_text();
1136 gui->update_rgb_hex(hex);
1140 PaletteHex::PaletteHex(ColorGUI *gui, int x, int y, const char *hex)
1141 : BC_TextBox(x, y, xS(100), 1, hex)
1145 PaletteHex::~PaletteHex()
1148 void PaletteHex::update()
1150 char hex[BCSTRLEN], *cp = hex;
1152 cp += sprintf(cp,"%02x", gui->alpha8());
1153 sprintf(cp,"%06x",gui->rgb888());
1154 BC_TextBox::update(hex);
1157 int PaletteHex::keypress_event()
1159 if( get_keypress() != RETURN )
1160 return BC_TextBox::keypress_event();
1161 gui->update_rgb_hex(get_text());
1165 #include "grabpick_up_png.h"
1166 #include "grabpick_hi_png.h"
1167 #include "grabpick_dn_png.h"
1169 PaletteGrabButton::PaletteGrabButton(ColorGUI *gui, int x, int y)
1170 : BC_Button(x, y, vframes)
1173 vframes[0] = new VFramePng(grabpick_up_png);
1174 vframes[1] = new VFramePng(grabpick_hi_png);
1175 vframes[2] = new VFramePng(grabpick_dn_png);
1176 set_tooltip(_("grab from anywhere picker"));
1178 PaletteGrabButton::~PaletteGrabButton()
1180 for( int i=0; i<3; ++i )
1183 int PaletteGrabButton::handle_event()
1185 if( gui->window->grab_buttons() ) {
1186 gui->window->grab_cursor();
1187 gui->button_grabbed = 1;
1188 return BC_Button::button_press_event(); // redraw face HI
1193 PaletteHistory::PaletteHistory(ColorGUI *gui, int x, int y)
1194 : BC_SubWindow(x,y, xS(200), yS(24))
1198 set_tooltip(_("color history"));
1200 PaletteHistory::~PaletteHistory()
1203 void PaletteHistory::update(int flush)
1206 for( int i=0; i<PALLETTE_HISTORY_SIZE; x1=x2 ) {
1207 int rgb = gui->palette_history[i];
1208 x2 = (++i * get_w())/PALLETTE_HISTORY_SIZE;
1209 draw_3d_box(x1,0,x2-x1,get_h(),WHITE,BLACK,rgb,LTBLUE,DKBLUE);
1214 int PaletteHistory::button_press_event()
1216 if( button_down || !is_event_win() ) return 0;
1218 cursor_motion_event();
1221 int PaletteHistory::button_release_event()
1223 if( !button_down || !is_event_win() ) return 0;
1224 cursor_motion_event();
1225 if( button_down > 0 ) {
1227 gui->update_display();
1228 gui->update_history();
1233 int PaletteHistory::cursor_motion_event()
1235 if( !button_down || !is_event_win() ) return 0;
1237 int pick = (PALLETTE_HISTORY_SIZE * get_cursor_x()) / get_w();
1238 bclamp(pick, 0, PALLETTE_HISTORY_SIZE-1);
1239 int color = gui->palette_history[pick];
1240 float r = ((color>>16) & 0xff) / 255.;
1241 float g = ((color>>8) & 0xff) / 255.;
1242 float b = ((color>>0) & 0xff) / 255.;
1243 if( gui->rgb.r != r || gui->rgb.g != g || gui->rgb.b != b ) {
1244 gui->rgb.r = r; gui->rgb.g = g; gui->rgb.b = b;
1250 int PaletteHistory::cursor_leave_event()
1255 int PaletteHistory::repeat_event(int64_t duration)
1259 if( duration == get_resources()->tooltip_delay &&
1260 get_tooltip() && *get_tooltip() && cursor_above() ) {
1268 ColorButton::ColorButton(const char *title,
1269 int x, int y, int w, int h,
1270 int color, int alpha, int ok_cancel)
1271 : BC_Button(x, y, w, vframes)
1273 this->title = title;
1274 this->color = this->orig_color = color;
1275 this->alpha = this->orig_alpha = alpha;
1276 this->ok_cancel = ok_cancel;
1278 for( int i=0; i<3; ++i ) {
1279 vframes[i] = new VFrame(w, h, BC_RGBA8888);
1280 vframes[i]->clear_frame();
1286 ColorButton::~ColorButton()
1288 delete color_thread;
1289 delete color_picker;
1290 for( int i=0; i<3; ++i )
1294 void ColorButton::set_color(int color)
1296 printf("ColorButton::set_color %06x\n", color);
1298 void ColorButton::handle_done_event(int result)
1300 color_thread->stop();
1302 int ColorButton::handle_new_color(int color, int alpha)
1304 printf("ColorButton::handle_new_color %02x%06x\n", alpha, color);
1308 int ColorButton::handle_event()
1311 delete color_picker;
1312 color_picker = new ColorButtonPicker(this);
1313 orig_color = color; orig_alpha = alpha;
1314 color_picker->start_window(color, alpha, ok_cancel);
1316 color_thread = new ColorButtonThread(this);
1317 color_thread->start();
1318 lock_window("ColorButtonButton::start_color_thread");
1322 void ColorButton::close_picker()
1324 if( color_thread ) color_thread->stop();
1325 delete color_thread; color_thread = 0;
1326 delete color_picker; color_picker = 0;
1329 void ColorButton::update_gui(int color, int alpha)
1332 color_picker->update_gui(color, alpha);
1333 update_gui(color | (~alpha<<24));
1336 void ColorButton::update_gui(int color)
1342 ColorButtonPicker::ColorButtonPicker(ColorButton *color_button)
1343 : ColorPicker(color_button->alpha >= 0 ? 1 : 0, color_button->title)
1345 this->color_button = color_button;
1348 ColorButtonPicker::~ColorButtonPicker()
1352 void ColorButtonPicker::handle_done_event(int result)
1354 color_button->color_thread->stop();
1355 color_button->handle_done_event(result);
1358 void ColorButtonPicker::update(int color, int alpha)
1360 color_button->color = color;
1361 color_button->alpha = alpha;
1362 color_button->color_thread->update_lock->unlock();
1365 int ColorButtonPicker::handle_new_color(int color, int alpha)
1367 color_button->lock_window("ColorButtonPicker::handle_new_color");
1368 color_button->update_gui(color);
1369 color_button->unlock_window();
1370 return color_button->handle_new_color(color, alpha);
1373 void ColorButtonPicker::update_gui()
1375 color_button->lock_window("ColorButtonPicker::update_gui");
1376 color_button->update_gui(color_button->color, color_button->alpha);
1377 color_button->unlock_window();
1380 void ColorButtonPicker::update_gui(int color, int alpha)
1382 ColorPicker::update_gui(color, alpha);
1383 color_button->handle_new_color(color, alpha);
1386 ColorButtonThread::ColorButtonThread(ColorButton *color_button)
1389 this->color_button = color_button;
1390 this->update_lock = new Condition(0,"ColorButtonThread::update_lock");
1394 ColorButtonThread::~ColorButtonThread()
1400 void ColorButtonThread::start()
1408 void ColorButtonThread::stop()
1412 update_lock->unlock();
1417 void ColorButtonThread::run()
1419 ColorButtonPicker *color_picker = color_button->color_picker;
1420 color_picker->update_gui();
1422 update_lock->lock("ColorButtonThread::run");
1424 color_picker->update_gui();
1429 ColorBoxButton::ColorBoxButton(const char *title,
1430 int x, int y, int w, int h,
1431 int color, int alpha, int ok_cancel)
1432 : ColorButton(title, x, y, w, h, color, alpha, ok_cancel)
1435 ColorBoxButton::~ColorBoxButton()
1439 int ColorBoxButton::handle_new_color(int color, int alpha)
1441 return ColorButton::handle_new_color(color, alpha);
1443 void ColorBoxButton::handle_done_event(int result)
1445 ColorButton::handle_done_event(result);
1447 void ColorBoxButton::create_objects()
1449 update_gui(color, alpha);
1452 void ColorBoxButton::set_color(int color)
1454 this->color = (color & 0xffffff);
1455 if( this->alpha >= 0 )
1456 this->alpha = (~color>>24) & 0xff;
1457 int r = (color>>16) & 0xff;
1458 int g = (color>> 8) & 0xff;
1459 int b = (color>> 0) & 0xff;
1460 int color_model = vframes[0]->get_color_model();
1461 int bpp = BC_CModels::calculate_pixelsize(color_model);
1462 for( int i=0; i<3; ++i ) {
1463 VFrame *vframe = vframes[i];
1464 int ww = vframe->get_w(), hh = vframe->get_h();
1465 uint8_t **rows = vframe->get_rows();
1466 int rr = r, gg = g, bb = b;
1471 if( (rr+=48) > 0xff ) rr = 0xff;
1472 if( (gg+=48) > 0xff ) gg = 0xff;
1473 if( (bb+=48) > 0xff ) bb = 0xff;
1476 if( (rr-=48) < 0x00 ) rr = 0x00;
1477 if( (gg-=48) < 0x00 ) gg = 0x00;
1478 if( (bb-=48) < 0x00 ) bb = 0x00;
1481 for( int y=0; y<hh; ++y ) {
1482 uint8_t *rp = rows[y];
1483 for( int x=0; x<ww; ++x ) {
1484 rp[0] = rr; rp[1] = gg; rp[2] = bb;
1485 if( bpp > 3 ) rp[3] = 0xff;
1490 set_images(vframes);
1493 ColorCircleButton::ColorCircleButton(const char *title,
1494 int x, int y, int w, int h,
1495 int color, int alpha, int ok_cancel)
1496 : ColorButton(title, x, y, w, h, color, alpha, ok_cancel)
1499 ColorCircleButton::~ColorCircleButton()
1502 int ColorCircleButton::handle_new_color(int color, int alpha)
1504 return ColorButton::handle_new_color(color, alpha);
1506 void ColorCircleButton::handle_done_event(int result)
1508 ColorButton::handle_done_event(result);
1510 void ColorCircleButton::create_objects()
1512 update_gui(color, alpha);
1515 void ColorCircleButton::set_color(int color)
1517 this->color = (color & 0xffffff);
1518 if( this->alpha >= 0 )
1519 this->alpha = (~color>>24) & 0xff;
1520 int r = (color>>16) & 0xff;
1521 int g = (color>>8) & 0xff;
1522 int b = (color>>0) & 0xff;
1523 for( int i=0; i<3; ++i ) {
1524 VFrame *vframe = vframes[i];
1525 int ww = vframe->get_w(), hh = vframe->get_h();
1526 int cx = (ww+1)/2, cy = hh/2;
1527 double cc = (cx*cx + cy*cy) / 4.;
1528 uint8_t *bp = vframe->get_data(), *dp = bp;
1529 uint8_t *ep = dp + vframe->get_data_size();
1530 int rr = r, gg = g, bb = b;
1531 int bpl = vframe->get_bytes_per_line();
1536 if( (rr+=48) > 0xff ) rr = 0xff;
1537 if( (gg+=48) > 0xff ) gg = 0xff;
1538 if( (bb+=48) > 0xff ) bb = 0xff;
1541 if( (rr-=48) < 0x00 ) rr = 0x00;
1542 if( (gg-=48) < 0x00 ) gg = 0x00;
1543 if( (bb-=48) < 0x00 ) bb = 0x00;
1547 int yy = (dp-bp) / bpl, xx = ((dp-bp) % bpl) >> 2;
1548 int dy = cy - yy, dx = cx - xx;
1549 double s = dx*dx + dy*dy - cc;
1550 double ss = s < 0 ? 1 : s >= cc ? 0 : 1 - s/cc;
1552 *dp++ = rr; *dp++ = gg; *dp++ = bb; *dp++ = aa;
1555 set_images(vframes);