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;
160 hsv_h = 0; hsv_s = 0; hsv_v = 0;
161 rgb_r = 0; rgb_g = 0; rgb_b = 0;
162 yuv_y = 0; yuv_u = 0; yuv_v = 0;
167 ColorGUI::~ColorGUI()
169 delete hsv_h; delete hsv_s; delete hsv_v;
170 delete rgb_r; delete rgb_g; delete rgb_b;
171 delete yuv_y; delete yuv_u; delete yuv_v;
174 if( button_grabbed ) {
175 window->ungrab_buttons();
176 window->ungrab_cursor();
178 update_history(rgb888());
182 void ColorGUI::add_tool(BC_WindowBase *sub_wdw)
184 window->add_tool(sub_wdw);
187 void ColorGUI::start_selection(int color, int alpha, int ok_cancel)
189 this->orig_color = color;
190 this->orig_alpha = alpha;
193 this->do_alpha = alpha < 0 ? 0 : 1;
194 this->ok_cancel = ok_cancel;
198 void ColorGUI::create_objects()
200 int xs5 = xS(5), xs10 = xS(10), xs15 = xS(15);
201 int ys10 = yS(10), ys15 = yS(15), ys25 = yS(25), ys40 = yS(40);
202 int x0 = xs10, y0 = ys10;
203 window->lock_window("ColorGUI::create_objects");
207 add_tool(wheel = new PaletteWheel(this, x, y));
208 wheel->create_objects();
210 x += xS(180); add_tool(wheel_value = new PaletteWheelValue(this, x, y));
211 wheel_value->create_objects();
213 y += yS(180); add_tool(poutput = new PaletteOutput(this, x, y));
214 poutput->create_objects();
215 y += poutput->get_h() + yS(20);
217 load_history(); int x1 = x;
218 add_tool(hex_btn = new PaletteHexButton(this, x1, y));
219 char hex[BCSTRLEN]; sprintf(hex,"%06x",color);
220 x1 += hex_btn->get_w() + xs5;
221 add_tool(hex_box = new PaletteHex(this, x1, y, hex));
222 x1 += hex_box->get_w() + xs15;
223 add_tool(grab_btn = new PaletteGrabButton(this, x1, y));
224 y += hex_box->get_h() + ys15;
225 add_tool(history = new PaletteHistory(this, xs10, y));
228 add_tool(new BC_Title(x, y =y0, C_("H:"), SMALLFONT));
229 add_tool(new BC_Title(x, y+=ys25, C_("S:"), SMALLFONT));
230 add_tool(new BC_Title(x, y+=ys25, D_("colorpicker_value#V:"), SMALLFONT));
231 add_tool(new BC_Title(x, y+=ys40, C_("R:"), SMALLFONT));
232 add_tool(new BC_Title(x, y+=ys25, C_("G:"), SMALLFONT));
233 add_tool(new BC_Title(x, y+=ys25, C_("B:"), SMALLFONT));
234 add_tool(new BC_Title(x, y+=ys40, C_("Y:"), SMALLFONT));
235 add_tool(new BC_Title(x, y+=ys25, C_("U:"), SMALLFONT));
236 add_tool(new BC_Title(x, y+=ys25, D_("colorpicker_Cr#V:"), SMALLFONT));
238 add_tool(new BC_Title(x, y+=ys40, C_("A:"), SMALLFONT));
240 add_tool(hue = new PaletteHue(this, x, y= y0));
241 add_tool(sat = new PaletteSat(this, x, y+=ys25));
242 add_tool(val = new PaletteVal(this, x, y+=ys25));
243 add_tool(red = new PaletteRed(this, x, y+=ys40));
244 add_tool(grn = new PaletteGrn(this, x, y+=ys25));
245 add_tool(blu = new PaletteBlu(this, x, y+=ys25));
246 add_tool(lum = new PaletteLum(this, x, y+=ys40));
247 add_tool(c_r = new PaletteCr (this, x, y+=ys25));
248 add_tool(c_b = new PaletteCb (this, x, y+=ys25));
250 add_tool(palpha = new PaletteAlpha(this, x, y+=ys40));
252 x += hue->get_w() + xs10;
253 hsv_h = new PaletteHSV(this, x,y= y0, hsv.h, 0, 360);
254 hsv_h->set_increment(1);
255 hsv_h->set_precision(1);
256 hsv_h->create_objects(); hsv_h->set_tooltip(_("Hue"));
257 hsv_s = new PaletteHSV(this, x,y+=ys25, hsv.s, 0, 1);
258 hsv_s->create_objects(); hsv_s->set_tooltip(_("Saturation"));
259 hsv_v = new PaletteHSV(this, x,y+=ys25, hsv.v, 0, 1);
260 hsv_v->create_objects(); hsv_v->set_tooltip(_("Value"));
261 rgb_r = new PaletteRGB(this, x,y+=ys40, rgb.r, 0, 1);
262 rgb_r->create_objects(); rgb_r->set_tooltip(_("Red"));
263 rgb_g = new PaletteRGB(this, x,y+=ys25, rgb.g, 0, 1);
264 rgb_g->create_objects(); rgb_g->set_tooltip(_("Green"));
265 rgb_b = new PaletteRGB(this, x,y+=ys25, rgb.b, 0, 1);
266 rgb_b->create_objects(); rgb_b->set_tooltip(_("Blue"));
267 yuv_y = new PaletteYUV(this, x,y+=ys40, yuv.y, 0, 1);
268 yuv_y->create_objects(); yuv_y->set_tooltip(_("Luminance"));
269 yuv_u = new PaletteYUV(this, x,y+=ys25, yuv.u, 0, 1);
270 yuv_u->create_objects(); yuv_u->set_tooltip(_("Blue Luminance Difference"));
271 yuv_v = new PaletteYUV(this, x,y+=ys25, yuv.v, 0, 1);
272 yuv_v->create_objects(); yuv_v->set_tooltip(_("Red Luminance Difference"));
274 aph_a = new PaletteAPH(this, x,y+=ys40, aph, 0, 1);
275 aph_a->create_objects(); aph_a->set_tooltip(_("Alpha"));
278 add_tool(new ColorOK(this, window));
279 add_tool(new ColorCancel(this, window));
281 create_objects(this);
285 window->show_window(1);
286 window->unlock_window();
290 void ColorGUI::change_values()
292 float r = ((color>>16) & 0xff) / 255.;
293 float g = ((color>>8) & 0xff) / 255.;
294 float b = ((color>>0) & 0xff) / 255.;
295 rgb.r = r; rgb.g = g; rgb.b = b;
296 aph = (float)alpha / 255;
297 update_rgb(rgb.r, rgb.g, rgb.b);
301 ColorOK::ColorOK(ColorGUI *gui, BC_WindowBase *window)
302 : BC_OKButton(window)
305 this->window = window;
307 int ColorOK::handle_event()
311 window->sync_display();
315 ColorCancel::ColorCancel(ColorGUI *gui, BC_WindowBase *window)
316 : BC_CancelButton(window)
319 this->window = window;
321 int ColorCancel::handle_event()
325 window->sync_display();
329 int ColorGUI::close_gui()
331 if( button_grabbed ) {
333 window->ungrab_buttons();
334 window->ungrab_cursor();
336 window->set_done(ok_cancel ? 1 : 0);
341 void ColorGUI::update_rgb()
343 update_rgb(rgb.r, rgb.g, rgb.b);
346 void ColorGUI::update_hsv()
348 update_hsv(hsv.h, hsv.s, hsv.v);
351 void ColorGUI::update_yuv()
353 update_yuv(yuv.y, yuv.u, yuv.v);
357 void ColorGUI::update_display()
359 wheel->draw(wheel->oldhue, wheel->oldsaturation);
360 wheel->oldhue = hsv.h;
361 wheel->oldsaturation = hsv.s;
362 wheel->draw(hsv.h, hsv.s);
364 wheel_value->draw(hsv.h, hsv.s, hsv.v);
365 wheel_value->flash();
369 hue->update((int)hsv.h);
381 hsv_h->update(hsv.h);
382 hsv_s->update(hsv.s);
383 hsv_v->update(hsv.v);
384 rgb_r->update(rgb.r);
385 rgb_g->update(rgb.g);
386 rgb_b->update(rgb.b);
387 yuv_y->update(yuv.y);
388 yuv_u->update(yuv.u);
389 yuv_v->update(yuv.v);
398 int ColorGUI::handle_gui()
400 window->unlock_window();
401 handle_new_color(rgb888(), alpha8());
402 window->lock_window("ColorGUI::handle_event");
406 void ColorGUI::get_screen_sample()
409 window->get_abs_cursor(cx, cy);
410 BC_Capture capture_bitmap(1, 1, 0);
411 VFrame vframe(1,1,BC_RGB888);
412 capture_bitmap.capture_frame(&vframe, cx,cy);
413 unsigned char *data = vframe.get_data();
414 rgb.r = data[0]/255.; rgb.g = data[1]/255.; rgb.b = data[2]/255.;
418 int ColorGUI::cursor_motion_gui()
420 if( button_grabbed && window->get_button_down() ) {
427 int ColorGUI::button_press_gui()
429 if( button_grabbed ) {
436 int ColorGUI::button_release_gui()
438 if( button_grabbed ) {
439 window->ungrab_buttons();
440 window->ungrab_cursor();
449 void ColorGUI::update_rgb_hex(const char *hex)
452 if( sscanf(hex,"%x",&color) == 1 ) {
454 aph = ((color>>24) & 0xff) / 255.;
457 float r = ((color>>16) & 0xff) / 255.;
458 float g = ((color>>8) & 0xff) / 255.;
459 float b = ((color>>0) & 0xff) / 255.;
460 rgb.r = r; rgb.g = g; rgb.b = b;
467 void ColorGUI::update_gui(int color, int alpha)
469 printf("ColorGUI::update_gui undefined.\n");
472 int ColorGUI::handle_new_color(int color, int alpha)
474 printf("ColorGUI::handle_new_color undefined.\n");
480 PaletteWheel::PaletteWheel(ColorGUI *gui, int x, int y)
481 : BC_SubWindow(x, y, xS(170), yS(170))
489 PaletteWheel::~PaletteWheel()
493 int PaletteWheel::button_press_event()
495 if( get_cursor_x() >= 0 && get_cursor_x() < get_w() &&
496 get_cursor_y() >= 0 && get_cursor_y() < get_h() &&
499 cursor_motion_event();
505 int PaletteWheel::cursor_motion_event()
507 int x1, y1, distance;
508 if( button_down && is_event_win() ) {
509 float h = get_angle(get_w()/2, get_h()/2, get_cursor_x(), get_cursor_y());
510 bclamp(h, 0, 359.999); gui->hsv.h = h;
511 x1 = get_w() / 2 - get_cursor_x();
512 y1 = get_h() / 2 - get_cursor_y();
513 distance = (int)sqrt(x1 * x1 + y1 * y1);
514 float s = (float)distance / (get_w() / 2);
515 bclamp(s, 0, 1); gui->hsv.s = s;
524 int PaletteWheel::button_release_event()
533 void PaletteWheel::create_objects()
536 //printf("PaletteWheel::create_objects 1\n");
539 float x1, y1, x2, y2;
541 int default_r, default_g, default_b;
542 VFrame frame(0, -1, get_w(), get_h(), BC_RGBA8888, -1);
545 default_r = (get_resources()->get_bg_color() & 0xff0000) >> 16;
546 default_g = (get_resources()->get_bg_color() & 0xff00) >> 8;
547 default_b = (get_resources()->get_bg_color() & 0xff);
548 //printf("PaletteWheel::create_objects 1\n");
550 int highlight_r = (get_resources()->button_light & 0xff0000) >> 16;
551 int highlight_g = (get_resources()->button_light & 0xff00) >> 8;
552 int highlight_b = (get_resources()->button_light & 0xff);
554 for( y2 = 0; y2 < get_h(); y2++ ) {
555 unsigned char *row = (unsigned char*)frame.get_rows()[(int)y2];
556 for( x2 = 0; x2 < get_w(); x2++ ) {
557 distance = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
558 if( distance > x1 ) {
559 row[(int)x2 * 4] = default_r;
560 row[(int)x2 * 4 + 1] = default_g;
561 row[(int)x2 * 4 + 2] = default_b;
562 row[(int)x2 * 4 + 3] = 0;
565 if( distance > x1 - 1 ) {
567 if( get_h() - y2 < x2 ) {
578 row[(int)x2 * 4] = r_i;
579 row[(int)x2 * 4 + 1] = g_i;
580 row[(int)x2 * 4 + 2] = b_i;
581 row[(int)x2 * 4 + 3] = 255;
584 h = get_angle(x1, y1, x2, y2);
586 HSV::hsv_to_rgb(r, g, b, h, s, v);
587 row[(int)x2 * 4] = (int)(r * 255);
588 row[(int)x2 * 4 + 1] = (int)(g * 255);
589 row[(int)x2 * 4 + 2] = (int)(b * 255);
590 row[(int)x2 * 4 + 3] = 255;
596 0, 0, get_w(), get_h(),
597 0, 0, get_w(), get_h(), 0);
600 oldsaturation = gui->hsv.s;
601 draw(oldhue, oldsaturation);
605 float PaletteWheel::torads(float angle)
607 return (float)angle / 360 * 2 * M_PI;
611 int PaletteWheel::draw(float hue, float saturation)
617 if( hue > 0 && hue < 90 ) {
618 x = (int)(w - w * cos(torads(90 - hue)) * saturation);
619 y = (int)(h - h * sin(torads(90 - hue)) * saturation);
621 else if( hue > 90 && hue < 180 ) {
622 x = (int)(w - w * cos(torads(hue - 90)) * saturation);
623 y = (int)(h + h * sin(torads(hue - 90)) * saturation);
625 else if( hue > 180 && hue < 270 ) {
626 x = (int)(w + w * cos(torads(270 - hue)) * saturation);
627 y = (int)(h + h * sin(torads(270 - hue)) * saturation);
629 else if( hue > 270 && hue < 360 ) {
630 x = (int)(w + w * cos(torads(hue - 270)) * saturation);
631 y = (int)(h - w * sin(torads(hue - 270)) * saturation);
633 else if( hue == 0 ) {
635 y = (int)(h - h * saturation);
637 else if( hue == 90 ) {
638 x = (int)(w - w * saturation);
641 else if( hue == 180 ) {
643 y = (int)(h + h * saturation);
645 else if( hue == 270 ) {
646 x = (int)(w + w * saturation);
652 draw_circle(x - xS(5), y - yS(5), xS(10), yS(10));
657 int PaletteWheel::get_angle(float x1, float y1, float x2, float y2)
659 float result = -atan2(x2 - x1, y1 - y2) * (360 / M_PI / 2);
665 PaletteWheelValue::PaletteWheelValue(ColorGUI *gui, int x, int y)
666 : BC_SubWindow(x, y, xS(40), yS(170), BLACK)
671 PaletteWheelValue::~PaletteWheelValue()
676 void PaletteWheelValue::create_objects()
678 frame = new VFrame(get_w(), get_h(), BC_RGB888);
679 draw(gui->hsv.h, gui->hsv.s, gui->hsv.v);
683 int PaletteWheelValue::button_press_event()
685 //printf("PaletteWheelValue::button_press 1 %d\n", is_event_win());
686 if( get_cursor_x() >= 0 && get_cursor_x() < get_w() &&
687 get_cursor_y() >= 0 && get_cursor_y() < get_h() &&
689 //printf("PaletteWheelValue::button_press 2\n");
691 cursor_motion_event();
697 int PaletteWheelValue::cursor_motion_event()
699 if( button_down && is_event_win() ) {
700 //printf("PaletteWheelValue::cursor_motion 1\n");
701 float v = 1.0 - (float)(get_cursor_y() - 2) / (get_h() - 4);
702 bclamp(v, 0, 1); gui->hsv.v = v;
710 int PaletteWheelValue::button_release_event()
713 //printf("PaletteWheelValue::button_release 1\n");
720 int PaletteWheelValue::draw(float hue, float saturation, float value)
725 for( i = get_h() - 3; i >= 2; i-- ) {
726 unsigned char *row = (unsigned char*)frame->get_rows()[i];
727 HSV::hsv_to_rgb(r_f, g_f, b_f, hue, saturation,
728 1.0 - (float)(i - 2) / (get_h() - 4));
729 r = (int)(r_f * 255);
730 g = (int)(g_f * 255);
731 b = (int)(b_f * 255);
732 for( j = 0; j < get_w(); j++ ) {
738 draw_3d_border(0, 0, get_w(), get_h(), 1);
739 draw_vframe(frame, 2, 2, get_w() - 4, get_h() - 4,
740 2, 2, get_w() - 4, get_h() - 4, 0);
742 draw_line(2, get_h() - 3 - (int)(value * (get_h() - 5)),
743 get_w() - 3, get_h() - 3 - (int)(value * (get_h() - 5)));
744 //printf("PaletteWheelValue::draw %d %f\n", __LINE__, value);
749 PaletteOutput::PaletteOutput(ColorGUI *gui, int x, int y)
750 : BC_SubWindow(x, y, xS(180), yS(30), BLACK)
754 PaletteOutput::~PaletteOutput()
759 void PaletteOutput::create_objects()
765 int PaletteOutput::handle_event()
770 int PaletteOutput::draw()
772 set_color(gui->rgb888());
773 draw_box(2, 2, get_w() - 4, get_h() - 4);
774 draw_3d_border(0, 0, get_w(), get_h(), 1);
778 PaletteHue::PaletteHue(ColorGUI *gui, int x, int y)
779 : BC_ISlider(x, y, 0, xS(150), xS(200), 0, 359, (int)(gui->hsv.h), 0)
783 PaletteHue::~PaletteHue()
787 int PaletteHue::handle_event()
789 gui->hsv.h = get_value();
795 PaletteSat::PaletteSat(ColorGUI *gui, int x, int y)
796 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1.0, gui->hsv.s, 0)
801 PaletteSat::~PaletteSat()
805 int PaletteSat::handle_event()
807 gui->hsv.s = get_value();
814 PaletteVal::PaletteVal(ColorGUI *gui, int x, int y)
815 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1.0, gui->hsv.v, 0)
820 PaletteVal::~PaletteVal()
824 int PaletteVal::handle_event()
826 gui->hsv.v = get_value();
833 PaletteRed::PaletteRed(ColorGUI *gui, int x, int y)
834 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, gui->rgb.r, 0)
839 PaletteRed::~PaletteRed()
843 int PaletteRed::handle_event()
845 gui->rgb.r = get_value();
851 PaletteGrn::PaletteGrn(ColorGUI *gui, int x, int y)
852 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, gui->rgb.g, 0)
857 PaletteGrn::~PaletteGrn()
861 int PaletteGrn::handle_event()
863 gui->rgb.g = get_value();
869 PaletteBlu::PaletteBlu(ColorGUI *gui, int x, int y)
870 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, gui->rgb.b, 0)
875 PaletteBlu::~PaletteBlu()
879 int PaletteBlu::handle_event()
881 gui->rgb.b = get_value();
887 PaletteAlpha::PaletteAlpha(ColorGUI *gui, int x, int y)
888 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, gui->aph, 0)
893 PaletteAlpha::~PaletteAlpha()
897 int PaletteAlpha::handle_event()
899 gui->aph = get_value();
900 gui->aph_a->update(gui->aph);
901 gui->hex_box->update();
906 PaletteLum::PaletteLum(ColorGUI *gui, int x, int y)
907 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, gui->yuv.y, 0)
912 PaletteLum::~PaletteLum()
916 int PaletteLum::handle_event()
918 gui->yuv.y = get_value();
924 PaletteCr::PaletteCr(ColorGUI *gui, int x, int y)
925 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, gui->yuv.u, 0)
930 PaletteCr::~PaletteCr()
934 int PaletteCr::handle_event()
936 gui->yuv.u = get_value();
942 PaletteCb::PaletteCb(ColorGUI *gui, int x, int y)
943 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, gui->yuv.v, 0)
948 PaletteCb::~PaletteCb()
952 int PaletteCb::handle_event()
954 gui->yuv.v = get_value();
960 void ColorGUI::update_rgb(float r, float g, float b)
963 YUV::yuv.rgb_to_yuv_f(r, g, b, y, u, v);
965 bclamp(y, 0, 1); yuv.y = y;
966 bclamp(u, 0, 1); yuv.u = u;
967 bclamp(v, 0, 1); yuv.v = v; }
969 HSV::rgb_to_hsv(r,g,b, h,s,v);
970 bclamp(h, 0, 360); hsv.h = h;
971 bclamp(s, 0, 1); hsv.s = s;
972 bclamp(v, 0, 1); hsv.v = v; }
975 void ColorGUI::update_yuv(float y, float u, float v)
979 YUV::yuv.yuv_to_rgb_f(r, g, b, y, u, v);
980 bclamp(r, 0, 1); rgb.r = r;
981 bclamp(g, 0, 1); rgb.g = g;
982 bclamp(b, 0, 1); rgb.b = b;
984 HSV::rgb_to_hsv(r,g,b, h, s, v);
985 bclamp(h, 0, 360); hsv.h = h;
986 bclamp(s, 0, 1); hsv.s = s;
987 bclamp(v, 0, 1); hsv.v = v; }
990 void ColorGUI::update_hsv(float h, float s, float v)
993 HSV::hsv_to_rgb(r,g,b, h,s,v);
994 bclamp(r, 0, 1); rgb.r = r;
995 bclamp(g, 0, 1); rgb.g = g;
996 bclamp(b, 0, 1); rgb.b = b;
998 YUV::yuv.rgb_to_yuv_f(r, g, b, y, u, v);
1000 bclamp(y, 0, 1); yuv.y = y;
1001 bclamp(u, 0, 1); yuv.u = u;
1002 bclamp(v, 0, 1); yuv.v = v; }
1005 void ColorGUI::load_history()
1007 char history_path[BCTEXTLEN];
1008 MWindow::create_defaults_path(history_path,PALETTE_DATA);
1009 FILE *fp = fopen(history_path,"r");
1012 while( i < PALLETTE_HISTORY_SIZE ) {
1013 char line[BCSTRLEN];
1014 if( !fgets(line,sizeof(line)-1,fp) ) break;
1015 line[sizeof(line)-1] = 0;
1016 if( sscanf(line, "%x",&palette_history[i]) != 1 ) break;
1021 int r = 0, g = 0, b = 0;
1022 float v0 = 0, v1 = 1;
1023 while( i < PALLETTE_HISTORY_SIZE ) {
1024 int grey_code = i ^ (i>>1);
1025 r = 255 * ((grey_code&4) ? v0 : v1);
1026 g = 255 * ((grey_code&2) ? v0 : v1);
1027 b = 255 * ((grey_code&1) ? v0 : v1);
1028 int color = (r<<16) | (g<<8) | (b<<0);
1029 palette_history[i++] = color;
1030 if( i & 7 ) continue;
1031 v0 = 0.5f * (v0+.5f);
1032 v1 = 0.5f * (v1+.5f);
1035 void ColorGUI::save_history()
1037 char history_path[BCTEXTLEN];
1038 MWindow::create_defaults_path(history_path,PALETTE_DATA);
1039 FILE *fp = fopen(history_path,"w");
1041 for( int i=0; i<PALLETTE_HISTORY_SIZE; ++i ) {
1042 fprintf(fp, "%06x\n", palette_history[i]);
1047 void ColorGUI::update_history(int color)
1049 int out = palette_history[0];
1050 palette_history[0] = color;
1051 for( int i=1; out != color && i<PALLETTE_HISTORY_SIZE; ++i ) {
1053 out = palette_history[i];
1054 palette_history[i] = in;
1057 void ColorGUI::update_history()
1059 update_history(rgb888());
1062 int ColorGUI::rgb888()
1064 int r = 255*rgb.r + 0.5, g = 255*rgb.g + 0.5, b = 255*rgb.b + 0.5;
1065 bclamp(r, 0, 255); bclamp(g, 0, 255); bclamp(b, 0, 255);
1066 return (r<<16) | (g<<8) | (b<<0);
1068 int ColorGUI::alpha8()
1070 int a = 255*aph + 0.5;
1075 PaletteNum::PaletteNum(ColorGUI *gui, int x, int y,
1076 float &output, float min, float max)
1077 : BC_TumbleTextBox(gui->window, output, min, max, x, y, xS(64))
1080 this->output = &output;
1081 set_increment(0.01);
1085 PaletteNum::~PaletteNum()
1090 int PaletteHSV::handle_event()
1098 int PaletteRGB::handle_event()
1106 int PaletteYUV::handle_event()
1114 int PaletteAPH::handle_event()
1117 gui->update_display();
1122 PaletteHexButton::PaletteHexButton(ColorGUI *gui, int x, int y)
1123 : BC_GenericButton(x, y, xS(50), "#")
1126 set_tooltip(_("hex rgb color"));
1128 PaletteHexButton::~PaletteHexButton()
1131 int PaletteHexButton::handle_event()
1133 const char *hex = gui->hex_box->get_text();
1134 gui->update_rgb_hex(hex);
1138 PaletteHex::PaletteHex(ColorGUI *gui, int x, int y, const char *hex)
1139 : BC_TextBox(x, y, xS(100), 1, hex)
1143 PaletteHex::~PaletteHex()
1146 void PaletteHex::update()
1148 char hex[BCSTRLEN], *cp = hex;
1150 cp += sprintf(cp,"%02x", gui->alpha8());
1151 sprintf(cp,"%06x",gui->rgb888());
1152 BC_TextBox::update(hex);
1155 int PaletteHex::keypress_event()
1157 if( get_keypress() != RETURN )
1158 return BC_TextBox::keypress_event();
1159 gui->update_rgb_hex(get_text());
1163 #include "grabpick_up_png.h"
1164 #include "grabpick_hi_png.h"
1165 #include "grabpick_dn_png.h"
1167 PaletteGrabButton::PaletteGrabButton(ColorGUI *gui, int x, int y)
1168 : BC_Button(x, y, vframes)
1171 vframes[0] = new VFramePng(grabpick_up_png);
1172 vframes[1] = new VFramePng(grabpick_hi_png);
1173 vframes[2] = new VFramePng(grabpick_dn_png);
1174 set_tooltip(_("grab from anywhere picker"));
1176 PaletteGrabButton::~PaletteGrabButton()
1178 for( int i=0; i<3; ++i )
1181 int PaletteGrabButton::handle_event()
1183 if( gui->window->grab_buttons() ) {
1184 gui->window->grab_cursor();
1185 gui->button_grabbed = 1;
1186 return BC_Button::button_press_event(); // redraw face HI
1191 PaletteHistory::PaletteHistory(ColorGUI *gui, int x, int y)
1192 : BC_SubWindow(x,y, xS(200), yS(24))
1196 set_tooltip(_("color history"));
1198 PaletteHistory::~PaletteHistory()
1201 void PaletteHistory::update(int flush)
1204 for( int i=0; i<PALLETTE_HISTORY_SIZE; x1=x2 ) {
1205 int rgb = gui->palette_history[i];
1206 x2 = (++i * get_w())/PALLETTE_HISTORY_SIZE;
1207 draw_3d_box(x1,0,x2-x1,get_h(),WHITE,BLACK,rgb,LTBLUE,DKBLUE);
1212 int PaletteHistory::button_press_event()
1214 if( button_down || !is_event_win() ) return 0;
1216 cursor_motion_event();
1219 int PaletteHistory::button_release_event()
1221 if( !button_down || !is_event_win() ) return 0;
1222 cursor_motion_event();
1223 if( button_down > 0 ) {
1225 gui->update_display();
1226 gui->update_history();
1231 int PaletteHistory::cursor_motion_event()
1233 if( !button_down || !is_event_win() ) return 0;
1235 int pick = (PALLETTE_HISTORY_SIZE * get_cursor_x()) / get_w();
1236 bclamp(pick, 0, PALLETTE_HISTORY_SIZE-1);
1237 int color = gui->palette_history[pick];
1238 float r = ((color>>16) & 0xff) / 255.;
1239 float g = ((color>>8) & 0xff) / 255.;
1240 float b = ((color>>0) & 0xff) / 255.;
1241 if( gui->rgb.r != r || gui->rgb.g != g || gui->rgb.b != b ) {
1242 gui->rgb.r = r; gui->rgb.g = g; gui->rgb.b = b;
1248 int PaletteHistory::cursor_leave_event()
1253 int PaletteHistory::repeat_event(int64_t duration)
1257 if( duration == get_resources()->tooltip_delay &&
1258 get_tooltip() && *get_tooltip() && cursor_above() ) {
1266 ColorButton::ColorButton(const char *title,
1267 int x, int y, int w, int h,
1268 int color, int alpha, int ok_cancel)
1269 : BC_Button(x, y, w, vframes)
1271 this->title = title;
1272 this->color = this->orig_color = color;
1273 this->alpha = this->orig_alpha = alpha;
1274 this->ok_cancel = ok_cancel;
1276 for( int i=0; i<3; ++i ) {
1277 vframes[i] = new VFrame(w, h, BC_RGBA8888);
1278 vframes[i]->clear_frame();
1284 ColorButton::~ColorButton()
1286 delete color_thread;
1287 delete color_picker;
1288 for( int i=0; i<3; ++i )
1292 void ColorButton::set_color(int color)
1294 printf("ColorButton::set_color %06x\n", color);
1296 void ColorButton::handle_done_event(int result)
1298 color_thread->stop();
1300 int ColorButton::handle_new_color(int color, int alpha)
1302 printf("ColorButton::handle_new_color %02x%06x\n", alpha, color);
1306 int ColorButton::handle_event()
1309 delete color_picker;
1310 color_picker = new ColorButtonPicker(this);
1311 orig_color = color; orig_alpha = alpha;
1312 color_picker->start_window(color, alpha, ok_cancel);
1314 color_thread = new ColorButtonThread(this);
1315 color_thread->start();
1316 lock_window("ColorButtonButton::start_color_thread");
1320 void ColorButton::close_picker()
1322 if( color_thread ) color_thread->stop();
1323 delete color_thread; color_thread = 0;
1324 delete color_picker; color_picker = 0;
1327 void ColorButton::update_gui(int color, int alpha)
1330 color_picker->update_gui(color, alpha);
1331 update_gui(color | (~alpha<<24));
1334 void ColorButton::update_gui(int color)
1340 ColorButtonPicker::ColorButtonPicker(ColorButton *color_button)
1341 : ColorPicker(color_button->alpha >= 0 ? 1 : 0, color_button->title)
1343 this->color_button = color_button;
1346 ColorButtonPicker::~ColorButtonPicker()
1350 void ColorButtonPicker::handle_done_event(int result)
1352 color_button->color_thread->stop();
1353 color_button->handle_done_event(result);
1356 void ColorButtonPicker::update(int color, int alpha)
1358 color_button->color = color;
1359 color_button->alpha = alpha;
1360 color_button->color_thread->update_lock->unlock();
1363 int ColorButtonPicker::handle_new_color(int color, int alpha)
1365 color_button->lock_window("ColorButtonPicker::handle_new_color");
1366 color_button->update_gui(color);
1367 color_button->unlock_window();
1368 return color_button->handle_new_color(color, alpha);
1371 void ColorButtonPicker::update_gui()
1373 color_button->lock_window("ColorButtonPicker::update_gui");
1374 color_button->update_gui(color_button->color, color_button->alpha);
1375 color_button->unlock_window();
1378 void ColorButtonPicker::update_gui(int color, int alpha)
1380 ColorPicker::update_gui(color, alpha);
1381 color_button->handle_new_color(color, alpha);
1384 ColorButtonThread::ColorButtonThread(ColorButton *color_button)
1387 this->color_button = color_button;
1388 this->update_lock = new Condition(0,"ColorButtonThread::update_lock");
1392 ColorButtonThread::~ColorButtonThread()
1398 void ColorButtonThread::start()
1406 void ColorButtonThread::stop()
1410 update_lock->unlock();
1415 void ColorButtonThread::run()
1417 ColorButtonPicker *color_picker = color_button->color_picker;
1418 color_picker->update_gui();
1420 update_lock->lock("ColorButtonThread::run");
1422 color_picker->update_gui();
1427 ColorBoxButton::ColorBoxButton(const char *title,
1428 int x, int y, int w, int h,
1429 int color, int alpha, int ok_cancel)
1430 : ColorButton(title, x, y, w, h, color, alpha, ok_cancel)
1433 ColorBoxButton::~ColorBoxButton()
1437 int ColorBoxButton::handle_new_color(int color, int alpha)
1439 return ColorButton::handle_new_color(color, alpha);
1441 void ColorBoxButton::handle_done_event(int result)
1443 ColorButton::handle_done_event(result);
1445 void ColorBoxButton::create_objects()
1447 update_gui(color, alpha);
1450 void ColorBoxButton::set_color(int color)
1452 this->color = (color & 0xffffff);
1453 if( this->alpha >= 0 )
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 if( this->alpha >= 0 )
1517 this->alpha = (~color>>24) & 0xff;
1518 int r = (color>>16) & 0xff;
1519 int g = (color>>8) & 0xff;
1520 int b = (color>>0) & 0xff;
1521 for( int i=0; i<3; ++i ) {
1522 VFrame *vframe = vframes[i];
1523 int ww = vframe->get_w(), hh = vframe->get_h();
1524 int cx = (ww+1)/2, cy = hh/2;
1525 double cc = (cx*cx + cy*cy) / 4.;
1526 uint8_t *bp = vframe->get_data(), *dp = bp;
1527 uint8_t *ep = dp + vframe->get_data_size();
1528 int rr = r, gg = g, bb = b;
1529 int bpl = vframe->get_bytes_per_line();
1534 if( (rr+=48) > 0xff ) rr = 0xff;
1535 if( (gg+=48) > 0xff ) gg = 0xff;
1536 if( (bb+=48) > 0xff ) bb = 0xff;
1539 if( (rr-=48) < 0x00 ) rr = 0x00;
1540 if( (gg-=48) < 0x00 ) gg = 0x00;
1541 if( (bb-=48) < 0x00 ) bb = 0x00;
1545 int yy = (dp-bp) / bpl, xx = ((dp-bp) % bpl) >> 2;
1546 int dy = cy - yy, dx = cx - xx;
1547 double s = dx*dx + dy*dy - cc;
1548 double ss = s < 0 ? 1 : s >= cc ? 0 : 1 - s/cc;
1550 *dp++ = rr; *dp++ = gg; *dp++ = bb; *dp++ = aa;
1553 set_images(vframes);