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->start_selection(color, !do_alpha ? -1 : alpha, ok_cancel);
93 void ColorPicker::update_gui(int color, int alpha)
95 ColorWindow *window = (ColorWindow *)get_gui();
97 window->update_gui(color, alpha);
100 int ColorPicker::handle_new_color(int color, int alpha)
102 printf("ColorPicker::handle_new_color undefined.\n");
106 ColorWindow::ColorWindow(ColorPicker *thread, int x, int y, int w, int h, const char *title)
107 : BC_Window(title, x, y, w, h, w, h, 0, 0, 1),
110 this->thread = thread;
113 ColorWindow::~ColorWindow()
117 void ColorWindow::update_gui(int color, int alpha)
119 lock_window("ColorWindow::update_gui");
126 int ColorWindow::handle_new_color(int color, int alpha)
128 lock_window("ColorWindow::handle_new_color");
129 thread->handle_new_color(color, alpha);
135 ColorGUI::ColorGUI(BC_WindowBase *window)
137 this->window = window;
141 this->color = this->orig_color = BLACK;
142 this->alpha = this->orig_alpha = 255;
148 hue = 0; sat = 0; val = 0;
149 red = 0; grn = 0; blu = 0;
150 lum = 0; c_r = 0; c_b = 0;
153 hsv_h = 0; hsv_s = 0; hsv_v = 0;
154 rgb_r = 0; rgb_g = 0; rgb_b = 0;
155 yuv_y = 0; yuv_u = 0; yuv_v = 0;
160 ColorGUI::~ColorGUI()
162 delete hsv_h; delete hsv_s; delete hsv_v;
163 delete rgb_r; delete rgb_g; delete rgb_b;
164 delete yuv_y; delete yuv_u; delete yuv_v;
167 if( button_grabbed ) {
168 window->ungrab_buttons();
169 window->ungrab_cursor();
171 update_history(rgb888());
175 void ColorGUI::add_tool(BC_WindowBase *sub_wdw)
177 window->add_tool(sub_wdw);
180 void ColorGUI::start_selection(int color, int alpha, int ok_cancel)
182 this->orig_color = color;
183 this->orig_alpha = alpha;
186 this->do_alpha = alpha < 0 ? 0 : 1;
187 this->ok_cancel = ok_cancel;
191 void ColorGUI::create_objects()
193 int xs5 = xS(5), xs10 = xS(10), xs15 = xS(15);
194 int ys10 = yS(10), ys15 = yS(15), ys25 = yS(25), ys40 = yS(40);
195 int x0 = xs10, y0 = ys10;
196 window->lock_window("ColorGUI::create_objects");
200 add_tool(wheel = new PaletteWheel(this, x, y));
201 wheel->create_objects();
203 x += xS(180); add_tool(wheel_value = new PaletteWheelValue(this, x, y));
204 wheel_value->create_objects();
206 y += yS(180); add_tool(poutput = new PaletteOutput(this, x, y));
207 poutput->create_objects();
208 y += poutput->get_h() + yS(20);
210 load_history(); int x1 = x;
211 add_tool(hex_btn = new PaletteHexButton(this, x1, y));
212 char hex[BCSTRLEN]; sprintf(hex,"%06x",color);
213 x1 += hex_btn->get_w() + xs5;
214 add_tool(hex_box = new PaletteHex(this, x1, y, hex));
215 x1 += hex_box->get_w() + xs15;
216 add_tool(grab_btn = new PaletteGrabButton(this, x1, y));
217 y += hex_box->get_h() + ys15;
218 add_tool(history = new PaletteHistory(this, xs10, y));
221 add_tool(new BC_Title(x, y =y0, C_("H:"), SMALLFONT));
222 add_tool(new BC_Title(x, y+=ys25, C_("S:"), SMALLFONT));
223 add_tool(new BC_Title(x, y+=ys25, D_("colorpicker_value#V:"), SMALLFONT));
224 add_tool(new BC_Title(x, y+=ys40, C_("R:"), SMALLFONT));
225 add_tool(new BC_Title(x, y+=ys25, C_("G:"), SMALLFONT));
226 add_tool(new BC_Title(x, y+=ys25, C_("B:"), SMALLFONT));
227 add_tool(new BC_Title(x, y+=ys40, C_("Y:"), SMALLFONT));
228 add_tool(new BC_Title(x, y+=ys25, C_("U:"), SMALLFONT));
229 add_tool(new BC_Title(x, y+=ys25, D_("colorpicker_Cr#V:"), SMALLFONT));
231 add_tool(new BC_Title(x, y+=ys40, C_("A:"), SMALLFONT));
233 add_tool(hue = new PaletteHue(this, x, y= y0));
234 add_tool(sat = new PaletteSat(this, x, y+=ys25));
235 add_tool(val = new PaletteVal(this, x, y+=ys25));
236 add_tool(red = new PaletteRed(this, x, y+=ys40));
237 add_tool(grn = new PaletteGrn(this, x, y+=ys25));
238 add_tool(blu = new PaletteBlu(this, x, y+=ys25));
239 add_tool(lum = new PaletteLum(this, x, y+=ys40));
240 add_tool(c_r = new PaletteCr (this, x, y+=ys25));
241 add_tool(c_b = new PaletteCb (this, x, y+=ys25));
243 add_tool(palpha = new PaletteAlpha(this, x, y+=ys40));
245 x += hue->get_w() + xs10;
246 hsv_h = new PaletteHSV(this, x,y= y0, hsv.h, 0, 360);
247 hsv_h->create_objects(); hsv_h->set_tooltip(_("Hue"));
248 hsv_s = new PaletteHSV(this, x,y+=ys25, hsv.s, 0, 1);
249 hsv_s->create_objects(); hsv_s->set_tooltip(_("Saturation"));
250 hsv_v = new PaletteHSV(this, x,y+=ys25, hsv.v, 0, 1);
251 hsv_v->create_objects(); hsv_v->set_tooltip(_("Value"));
252 rgb_r = new PaletteRGB(this, x,y+=ys40, rgb.r, 0, 1);
253 rgb_r->create_objects(); rgb_r->set_tooltip(_("Red"));
254 rgb_g = new PaletteRGB(this, x,y+=ys25, rgb.g, 0, 1);
255 rgb_g->create_objects(); rgb_g->set_tooltip(_("Green"));
256 rgb_b = new PaletteRGB(this, x,y+=ys25, rgb.b, 0, 1);
257 rgb_b->create_objects(); rgb_b->set_tooltip(_("Blue"));
258 yuv_y = new PaletteYUV(this, x,y+=ys40, yuv.y, 0, 1);
259 yuv_y->create_objects(); yuv_y->set_tooltip(_("Luminance"));
260 yuv_u = new PaletteYUV(this, x,y+=ys25, yuv.u, 0, 1);
261 yuv_u->create_objects(); yuv_u->set_tooltip(_("Blue Luminance Difference"));
262 yuv_v = new PaletteYUV(this, x,y+=ys25, yuv.v, 0, 1);
263 yuv_v->create_objects(); yuv_v->set_tooltip(_("Red Luminance Difference"));
265 aph_a = new PaletteAPH(this, x,y+=ys40, aph, 0, 1);
266 aph_a->create_objects(); aph_a->set_tooltip(_("Alpha"));
269 add_tool(new ColorOK(this, window));
270 add_tool(new ColorCancel(this, window));
272 create_objects(this);
276 window->show_window(1);
277 window->unlock_window();
281 void ColorGUI::change_values()
283 float r = ((color>>16) & 0xff) / 255.;
284 float g = ((color>>8) & 0xff) / 255.;
285 float b = ((color>>0) & 0xff) / 255.;
286 rgb.r = r; rgb.g = g; rgb.b = b;
287 aph = (float)alpha / 255;
288 update_rgb(rgb.r, rgb.g, rgb.b);
292 ColorOK::ColorOK(ColorGUI *gui, BC_WindowBase *window)
293 : BC_OKButton(window)
296 this->window = window;
298 int ColorOK::handle_event()
302 window->sync_display();
306 ColorCancel::ColorCancel(ColorGUI *gui, BC_WindowBase *window)
307 : BC_CancelButton(window)
310 this->window = window;
312 int ColorCancel::handle_event()
316 window->sync_display();
320 int ColorGUI::close_gui()
322 if( button_grabbed ) {
324 window->ungrab_buttons();
325 window->ungrab_cursor();
327 window->set_done(ok_cancel ? 1 : 0);
332 void ColorGUI::update_rgb()
334 update_rgb(rgb.r, rgb.g, rgb.b);
337 void ColorGUI::update_hsv()
339 update_hsv(hsv.h, hsv.s, hsv.v);
342 void ColorGUI::update_yuv()
344 update_yuv(yuv.y, yuv.u, yuv.v);
348 void ColorGUI::update_display()
350 wheel->draw(wheel->oldhue, wheel->oldsaturation);
351 wheel->oldhue = hsv.h;
352 wheel->oldsaturation = hsv.s;
353 wheel->draw(hsv.h, hsv.s);
355 wheel_value->draw(hsv.h, hsv.s, hsv.v);
356 wheel_value->flash();
360 hue->update((int)hsv.h);
372 hsv_h->update(hsv.h);
373 hsv_s->update(hsv.s);
374 hsv_v->update(hsv.v);
375 rgb_r->update(rgb.r);
376 rgb_g->update(rgb.g);
377 rgb_b->update(rgb.b);
378 yuv_y->update(yuv.y);
379 yuv_u->update(yuv.u);
380 yuv_v->update(yuv.v);
389 int ColorGUI::handle_gui()
391 window->unlock_window();
392 handle_new_color(rgb888(), alpha8());
393 window->lock_window("ColorGUI::handle_event");
397 void ColorGUI::get_screen_sample()
400 window->get_abs_cursor(cx, cy);
401 BC_Capture capture_bitmap(1, 1, 0);
402 VFrame vframe(1,1,BC_RGB888);
403 capture_bitmap.capture_frame(&vframe, cx,cy);
404 unsigned char *data = vframe.get_data();
405 rgb.r = data[0]/255.; rgb.g = data[1]/255.; rgb.b = data[2]/255.;
409 int ColorGUI::cursor_motion_gui()
411 if( button_grabbed && window->get_button_down() ) {
418 int ColorGUI::button_press_gui()
420 if( button_grabbed ) {
427 int ColorGUI::button_release_gui()
429 if( button_grabbed ) {
430 window->ungrab_buttons();
431 window->ungrab_cursor();
440 void ColorGUI::update_rgb_hex(const char *hex)
443 if( sscanf(hex,"%x",&color) == 1 ) {
445 aph = ((color>>24) & 0xff) / 255.;
448 float r = ((color>>16) & 0xff) / 255.;
449 float g = ((color>>8) & 0xff) / 255.;
450 float b = ((color>>0) & 0xff) / 255.;
451 rgb.r = r; rgb.g = g; rgb.b = b;
458 void ColorGUI::update_gui(int color, int alpha)
460 printf("ColorGUI::update_gui undefined.\n");
463 int ColorGUI::handle_new_color(int color, int alpha)
465 printf("ColorGUI::handle_new_color undefined.\n");
471 PaletteWheel::PaletteWheel(ColorGUI *gui, int x, int y)
472 : BC_SubWindow(x, y, xS(170), yS(170))
480 PaletteWheel::~PaletteWheel()
484 int PaletteWheel::button_press_event()
486 if( get_cursor_x() >= 0 && get_cursor_x() < get_w() &&
487 get_cursor_y() >= 0 && get_cursor_y() < get_h() &&
490 cursor_motion_event();
496 int PaletteWheel::cursor_motion_event()
498 int x1, y1, distance;
499 if( button_down && is_event_win() ) {
500 float h = get_angle(get_w()/2, get_h()/2, get_cursor_x(), get_cursor_y());
501 bclamp(h, 0, 359.999); gui->hsv.h = h;
502 x1 = get_w() / 2 - get_cursor_x();
503 y1 = get_h() / 2 - get_cursor_y();
504 distance = (int)sqrt(x1 * x1 + y1 * y1);
505 float s = (float)distance / (get_w() / 2);
506 bclamp(s, 0, 1); gui->hsv.s = s;
515 int PaletteWheel::button_release_event()
524 void PaletteWheel::create_objects()
527 //printf("PaletteWheel::create_objects 1\n");
530 float x1, y1, x2, y2;
532 int default_r, default_g, default_b;
533 VFrame frame(0, -1, get_w(), get_h(), BC_RGBA8888, -1);
536 default_r = (get_resources()->get_bg_color() & 0xff0000) >> 16;
537 default_g = (get_resources()->get_bg_color() & 0xff00) >> 8;
538 default_b = (get_resources()->get_bg_color() & 0xff);
539 //printf("PaletteWheel::create_objects 1\n");
541 int highlight_r = (get_resources()->button_light & 0xff0000) >> 16;
542 int highlight_g = (get_resources()->button_light & 0xff00) >> 8;
543 int highlight_b = (get_resources()->button_light & 0xff);
545 for( y2 = 0; y2 < get_h(); y2++ ) {
546 unsigned char *row = (unsigned char*)frame.get_rows()[(int)y2];
547 for( x2 = 0; x2 < get_w(); x2++ ) {
548 distance = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
549 if( distance > x1 ) {
550 row[(int)x2 * 4] = default_r;
551 row[(int)x2 * 4 + 1] = default_g;
552 row[(int)x2 * 4 + 2] = default_b;
553 row[(int)x2 * 4 + 3] = 0;
556 if( distance > x1 - 1 ) {
558 if( get_h() - y2 < x2 ) {
569 row[(int)x2 * 4] = r_i;
570 row[(int)x2 * 4 + 1] = g_i;
571 row[(int)x2 * 4 + 2] = b_i;
572 row[(int)x2 * 4 + 3] = 255;
575 h = get_angle(x1, y1, x2, y2);
577 HSV::hsv_to_rgb(r, g, b, h, s, v);
578 row[(int)x2 * 4] = (int)(r * 255);
579 row[(int)x2 * 4 + 1] = (int)(g * 255);
580 row[(int)x2 * 4 + 2] = (int)(b * 255);
581 row[(int)x2 * 4 + 3] = 255;
587 0, 0, get_w(), get_h(),
588 0, 0, get_w(), get_h(), 0);
591 oldsaturation = gui->hsv.s;
592 draw(oldhue, oldsaturation);
596 float PaletteWheel::torads(float angle)
598 return (float)angle / 360 * 2 * M_PI;
602 int PaletteWheel::draw(float hue, float saturation)
608 if( hue > 0 && hue < 90 ) {
609 x = (int)(w - w * cos(torads(90 - hue)) * saturation);
610 y = (int)(h - h * sin(torads(90 - hue)) * saturation);
612 else if( hue > 90 && hue < 180 ) {
613 x = (int)(w - w * cos(torads(hue - 90)) * saturation);
614 y = (int)(h + h * sin(torads(hue - 90)) * saturation);
616 else if( hue > 180 && hue < 270 ) {
617 x = (int)(w + w * cos(torads(270 - hue)) * saturation);
618 y = (int)(h + h * sin(torads(270 - hue)) * saturation);
620 else if( hue > 270 && hue < 360 ) {
621 x = (int)(w + w * cos(torads(hue - 270)) * saturation);
622 y = (int)(h - w * sin(torads(hue - 270)) * saturation);
624 else if( hue == 0 ) {
626 y = (int)(h - h * saturation);
628 else if( hue == 90 ) {
629 x = (int)(w - w * saturation);
632 else if( hue == 180 ) {
634 y = (int)(h + h * saturation);
636 else if( hue == 270 ) {
637 x = (int)(w + w * saturation);
643 draw_circle(x - xS(5), y - yS(5), xS(10), yS(10));
648 int PaletteWheel::get_angle(float x1, float y1, float x2, float y2)
650 float result = -atan2(x2 - x1, y1 - y2) * (360 / M_PI / 2);
656 PaletteWheelValue::PaletteWheelValue(ColorGUI *gui, int x, int y)
657 : BC_SubWindow(x, y, xS(40), yS(170), BLACK)
662 PaletteWheelValue::~PaletteWheelValue()
667 void PaletteWheelValue::create_objects()
669 frame = new VFrame(get_w(), get_h(), BC_RGB888);
670 draw(gui->hsv.h, gui->hsv.s, gui->hsv.v);
674 int PaletteWheelValue::button_press_event()
676 //printf("PaletteWheelValue::button_press 1 %d\n", is_event_win());
677 if( get_cursor_x() >= 0 && get_cursor_x() < get_w() &&
678 get_cursor_y() >= 0 && get_cursor_y() < get_h() &&
680 //printf("PaletteWheelValue::button_press 2\n");
682 cursor_motion_event();
688 int PaletteWheelValue::cursor_motion_event()
690 if( button_down && is_event_win() ) {
691 //printf("PaletteWheelValue::cursor_motion 1\n");
692 float v = 1.0 - (float)(get_cursor_y() - 2) / (get_h() - 4);
693 bclamp(v, 0, 1); gui->hsv.v = v;
701 int PaletteWheelValue::button_release_event()
704 //printf("PaletteWheelValue::button_release 1\n");
711 int PaletteWheelValue::draw(float hue, float saturation, float value)
716 for( i = get_h() - 3; i >= 2; i-- ) {
717 unsigned char *row = (unsigned char*)frame->get_rows()[i];
718 HSV::hsv_to_rgb(r_f, g_f, b_f, hue, saturation,
719 1.0 - (float)(i - 2) / (get_h() - 4));
720 r = (int)(r_f * 255);
721 g = (int)(g_f * 255);
722 b = (int)(b_f * 255);
723 for( j = 0; j < get_w(); j++ ) {
729 draw_3d_border(0, 0, get_w(), get_h(), 1);
730 draw_vframe(frame, 2, 2, get_w() - 4, get_h() - 4,
731 2, 2, get_w() - 4, get_h() - 4, 0);
733 draw_line(2, get_h() - 3 - (int)(value * (get_h() - 5)),
734 get_w() - 3, get_h() - 3 - (int)(value * (get_h() - 5)));
735 //printf("PaletteWheelValue::draw %d %f\n", __LINE__, value);
740 PaletteOutput::PaletteOutput(ColorGUI *gui, int x, int y)
741 : BC_SubWindow(x, y, xS(180), yS(30), BLACK)
745 PaletteOutput::~PaletteOutput()
750 void PaletteOutput::create_objects()
756 int PaletteOutput::handle_event()
761 int PaletteOutput::draw()
763 set_color(gui->rgb888());
764 draw_box(2, 2, get_w() - 4, get_h() - 4);
765 draw_3d_border(0, 0, get_w(), get_h(), 1);
769 PaletteHue::PaletteHue(ColorGUI *gui, int x, int y)
770 : BC_ISlider(x, y, 0, xS(150), xS(200), 0, 359, (int)(gui->hsv.h), 0)
774 PaletteHue::~PaletteHue()
778 int PaletteHue::handle_event()
780 gui->hsv.h = get_value();
786 PaletteSat::PaletteSat(ColorGUI *gui, int x, int y)
787 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1.0, gui->hsv.s, 0)
792 PaletteSat::~PaletteSat()
796 int PaletteSat::handle_event()
798 gui->hsv.s = get_value();
805 PaletteVal::PaletteVal(ColorGUI *gui, int x, int y)
806 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1.0, gui->hsv.v, 0)
811 PaletteVal::~PaletteVal()
815 int PaletteVal::handle_event()
817 gui->hsv.v = get_value();
824 PaletteRed::PaletteRed(ColorGUI *gui, int x, int y)
825 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, gui->rgb.r, 0)
830 PaletteRed::~PaletteRed()
834 int PaletteRed::handle_event()
836 gui->rgb.r = get_value();
842 PaletteGrn::PaletteGrn(ColorGUI *gui, int x, int y)
843 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, gui->rgb.g, 0)
848 PaletteGrn::~PaletteGrn()
852 int PaletteGrn::handle_event()
854 gui->rgb.g = get_value();
860 PaletteBlu::PaletteBlu(ColorGUI *gui, int x, int y)
861 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, gui->rgb.b, 0)
866 PaletteBlu::~PaletteBlu()
870 int PaletteBlu::handle_event()
872 gui->rgb.b = get_value();
878 PaletteAlpha::PaletteAlpha(ColorGUI *gui, int x, int y)
879 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, gui->aph, 0)
884 PaletteAlpha::~PaletteAlpha()
888 int PaletteAlpha::handle_event()
890 gui->aph = get_value();
891 gui->aph_a->update(gui->aph);
892 gui->hex_box->update();
897 PaletteLum::PaletteLum(ColorGUI *gui, int x, int y)
898 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, gui->yuv.y, 0)
903 PaletteLum::~PaletteLum()
907 int PaletteLum::handle_event()
909 gui->yuv.y = get_value();
915 PaletteCr::PaletteCr(ColorGUI *gui, int x, int y)
916 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, gui->yuv.u, 0)
921 PaletteCr::~PaletteCr()
925 int PaletteCr::handle_event()
927 gui->yuv.u = get_value();
933 PaletteCb::PaletteCb(ColorGUI *gui, int x, int y)
934 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, gui->yuv.v, 0)
939 PaletteCb::~PaletteCb()
943 int PaletteCb::handle_event()
945 gui->yuv.v = get_value();
951 void ColorGUI::update_rgb(float r, float g, float b)
954 YUV::yuv.rgb_to_yuv_f(r, g, b, y, u, v);
956 bclamp(y, 0, 1); yuv.y = y;
957 bclamp(u, 0, 1); yuv.u = u;
958 bclamp(v, 0, 1); yuv.v = v; }
960 HSV::rgb_to_hsv(r,g,b, h,s,v);
961 bclamp(h, 0, 360); hsv.h = h;
962 bclamp(s, 0, 1); hsv.s = s;
963 bclamp(v, 0, 1); hsv.v = v; }
966 void ColorGUI::update_yuv(float y, float u, float v)
970 YUV::yuv.yuv_to_rgb_f(r, g, b, y, u, v);
971 bclamp(r, 0, 1); rgb.r = r;
972 bclamp(g, 0, 1); rgb.g = g;
973 bclamp(b, 0, 1); rgb.b = b;
975 HSV::rgb_to_hsv(r,g,b, h, s, v);
976 bclamp(h, 0, 360); hsv.h = h;
977 bclamp(s, 0, 1); hsv.s = s;
978 bclamp(v, 0, 1); hsv.v = v; }
981 void ColorGUI::update_hsv(float h, float s, float v)
984 HSV::hsv_to_rgb(r,g,b, h,s,v);
985 bclamp(r, 0, 1); rgb.r = r;
986 bclamp(g, 0, 1); rgb.g = g;
987 bclamp(b, 0, 1); rgb.b = b;
989 YUV::yuv.rgb_to_yuv_f(r, g, b, y, u, v);
991 bclamp(y, 0, 1); yuv.y = y;
992 bclamp(u, 0, 1); yuv.u = u;
993 bclamp(v, 0, 1); yuv.v = v; }
996 void ColorGUI::load_history()
998 char history_path[BCTEXTLEN];
999 MWindow::create_defaults_path(history_path,PALETTE_DATA);
1000 FILE *fp = fopen(history_path,"r");
1003 while( i < PALLETTE_HISTORY_SIZE ) {
1004 char line[BCSTRLEN];
1005 if( !fgets(line,sizeof(line)-1,fp) ) break;
1006 line[sizeof(line)-1] = 0;
1007 if( sscanf(line, "%x",&palette_history[i]) != 1 ) break;
1012 int r = 0, g = 0, b = 0;
1013 float v0 = 0, v1 = 1;
1014 while( i < PALLETTE_HISTORY_SIZE ) {
1015 int grey_code = i ^ (i>>1);
1016 r = 255 * ((grey_code&4) ? v0 : v1);
1017 g = 255 * ((grey_code&2) ? v0 : v1);
1018 b = 255 * ((grey_code&1) ? v0 : v1);
1019 int color = (r<<16) | (g<<8) | (b<<0);
1020 palette_history[i++] = color;
1021 if( i & 7 ) continue;
1022 v0 = 0.5f * (v0+.5f);
1023 v1 = 0.5f * (v1+.5f);
1026 void ColorGUI::save_history()
1028 char history_path[BCTEXTLEN];
1029 MWindow::create_defaults_path(history_path,PALETTE_DATA);
1030 FILE *fp = fopen(history_path,"w");
1032 for( int i=0; i<PALLETTE_HISTORY_SIZE; ++i ) {
1033 fprintf(fp, "%06x\n", palette_history[i]);
1038 void ColorGUI::update_history(int color)
1040 int out = palette_history[0];
1041 palette_history[0] = color;
1042 for( int i=1; out != color && i<PALLETTE_HISTORY_SIZE; ++i ) {
1044 out = palette_history[i];
1045 palette_history[i] = in;
1048 void ColorGUI::update_history()
1050 update_history(rgb888());
1053 int ColorGUI::rgb888()
1055 int r = 255*rgb.r + 0.5, g = 255*rgb.g + 0.5, b = 255*rgb.b + 0.5;
1056 bclamp(r, 0, 255); bclamp(g, 0, 255); bclamp(b, 0, 255);
1057 return (r<<16) | (g<<8) | (b<<0);
1059 int ColorGUI::alpha8()
1061 int a = 255*aph + 0.5;
1066 PaletteNum::PaletteNum(ColorGUI *gui, int x, int y,
1067 float &output, float min, float max)
1068 : BC_TumbleTextBox(gui->window, output, min, max, x, y, xS(64))
1071 this->output = &output;
1072 set_increment(0.01);
1076 PaletteNum::~PaletteNum()
1081 int PaletteHSV::handle_event()
1089 int PaletteRGB::handle_event()
1097 int PaletteYUV::handle_event()
1105 int PaletteAPH::handle_event()
1108 gui->update_display();
1113 PaletteHexButton::PaletteHexButton(ColorGUI *gui, int x, int y)
1114 : BC_GenericButton(x, y, xS(50), "#")
1117 set_tooltip(_("hex rgb color"));
1119 PaletteHexButton::~PaletteHexButton()
1122 int PaletteHexButton::handle_event()
1124 const char *hex = gui->hex_box->get_text();
1125 gui->update_rgb_hex(hex);
1129 PaletteHex::PaletteHex(ColorGUI *gui, int x, int y, const char *hex)
1130 : BC_TextBox(x, y, xS(100), 1, hex)
1134 PaletteHex::~PaletteHex()
1137 void PaletteHex::update()
1139 char hex[BCSTRLEN], *cp = hex;
1141 cp += sprintf(cp,"%02x", gui->alpha8());
1142 sprintf(cp,"%06x",gui->rgb888());
1143 BC_TextBox::update(hex);
1146 int PaletteHex::keypress_event()
1148 if( get_keypress() != RETURN )
1149 return BC_TextBox::keypress_event();
1150 gui->update_rgb_hex(get_text());
1154 #include "grabpick_up_png.h"
1155 #include "grabpick_hi_png.h"
1156 #include "grabpick_dn_png.h"
1158 PaletteGrabButton::PaletteGrabButton(ColorGUI *gui, int x, int y)
1159 : BC_Button(x, y, vframes)
1162 vframes[0] = new VFramePng(grabpick_up_png);
1163 vframes[1] = new VFramePng(grabpick_hi_png);
1164 vframes[2] = new VFramePng(grabpick_dn_png);
1165 set_tooltip(_("grab from anywhere picker"));
1167 PaletteGrabButton::~PaletteGrabButton()
1169 for( int i=0; i<3; ++i )
1172 int PaletteGrabButton::handle_event()
1174 if( gui->window->grab_buttons() ) {
1175 gui->window->grab_cursor();
1176 gui->button_grabbed = 1;
1177 return BC_Button::button_press_event(); // redraw face HI
1182 PaletteHistory::PaletteHistory(ColorGUI *gui, int x, int y)
1183 : BC_SubWindow(x,y, xS(200), yS(24))
1187 set_tooltip(_("color history"));
1189 PaletteHistory::~PaletteHistory()
1192 void PaletteHistory::update(int flush)
1195 for( int i=0; i<PALLETTE_HISTORY_SIZE; x1=x2 ) {
1196 int rgb = gui->palette_history[i];
1197 x2 = (++i * get_w())/PALLETTE_HISTORY_SIZE;
1198 draw_3d_box(x1,0,x2-x1,get_h(),WHITE,BLACK,rgb,LTBLUE,DKBLUE);
1203 int PaletteHistory::button_press_event()
1205 if( button_down || !is_event_win() ) return 0;
1207 cursor_motion_event();
1210 int PaletteHistory::button_release_event()
1212 if( !button_down || !is_event_win() ) return 0;
1213 cursor_motion_event();
1214 if( button_down > 0 ) {
1216 gui->update_display();
1217 gui->update_history();
1222 int PaletteHistory::cursor_motion_event()
1224 if( !button_down || !is_event_win() ) return 0;
1226 int pick = (PALLETTE_HISTORY_SIZE * get_cursor_x()) / get_w();
1227 bclamp(pick, 0, PALLETTE_HISTORY_SIZE-1);
1228 int color = gui->palette_history[pick];
1229 float r = ((color>>16) & 0xff) / 255.;
1230 float g = ((color>>8) & 0xff) / 255.;
1231 float b = ((color>>0) & 0xff) / 255.;
1232 if( gui->rgb.r != r || gui->rgb.g != g || gui->rgb.b != b ) {
1233 gui->rgb.r = r; gui->rgb.g = g; gui->rgb.b = b;
1239 int PaletteHistory::cursor_leave_event()
1244 int PaletteHistory::repeat_event(int64_t duration)
1248 if( duration == get_resources()->tooltip_delay &&
1249 get_tooltip() && *get_tooltip() && cursor_above() ) {
1257 ColorButton::ColorButton(const char *title,
1258 int x, int y, int w, int h,
1259 int color, int alpha, int ok_cancel)
1260 : BC_Button(x, y, w, vframes)
1262 this->title = title;
1263 this->color = this->orig_color = color;
1264 this->alpha = this->orig_alpha = alpha;
1265 this->ok_cancel = ok_cancel;
1267 for( int i=0; i<3; ++i ) {
1268 vframes[i] = new VFrame(w, h, BC_RGBA8888);
1269 vframes[i]->clear_frame();
1275 ColorButton::~ColorButton()
1277 delete color_thread;
1278 delete color_picker;
1279 for( int i=0; i<3; ++i )
1283 void ColorButton::set_color(int color)
1285 printf("ColorButton::set_color %06x\n", color);
1287 void ColorButton::handle_done_event(int result)
1289 color_thread->stop();
1291 int ColorButton::handle_new_color(int color, int alpha)
1293 printf("ColorButton::handle_new_color %02x%06x\n", alpha, color);
1297 int ColorButton::handle_event()
1300 delete color_picker;
1301 color_picker = new ColorButtonPicker(this);
1302 orig_color = color; orig_alpha = alpha;
1303 color_picker->start_window(color, alpha, ok_cancel);
1305 color_thread = new ColorButtonThread(this);
1306 color_thread->start();
1307 lock_window("ColorButtonButton::start_color_thread");
1311 void ColorButton::close_picker()
1313 if( color_thread ) color_thread->stop();
1314 delete color_thread; color_thread = 0;
1315 delete color_picker; color_picker = 0;
1318 void ColorButton::update_gui(int color, int alpha)
1321 color_picker->update_gui(color, alpha);
1322 update_gui(color | (~alpha<<24));
1325 void ColorButton::update_gui(int color)
1331 ColorButtonPicker::ColorButtonPicker(ColorButton *color_button)
1332 : ColorPicker(color_button->alpha >= 0 ? 1 : 0, color_button->title)
1334 this->color_button = color_button;
1337 ColorButtonPicker::~ColorButtonPicker()
1341 void ColorButtonPicker::handle_done_event(int result)
1343 color_button->color_thread->stop();
1344 color_button->handle_done_event(result);
1347 void ColorButtonPicker::update(int color, int alpha)
1349 color_button->color = color;
1350 color_button->alpha = alpha;
1351 color_button->color_thread->update_lock->unlock();
1354 int ColorButtonPicker::handle_new_color(int color, int alpha)
1356 color_button->lock_window("ColorButtonPicker::handle_new_color");
1357 color_button->update_gui(color, alpha);
1358 color_button->unlock_window();
1359 return color_button->handle_new_color(color, alpha);
1362 void ColorButtonPicker::update_gui()
1364 color_button->lock_window("ColorButtonPicker::update_gui");
1365 color_button->update_gui(color_button->color, color_button->alpha);
1366 color_button->unlock_window();
1369 void ColorButtonPicker::update_gui(int color, int alpha)
1371 ColorPicker::update_gui(color, alpha);
1372 color_button->handle_new_color(color, alpha);
1375 ColorButtonThread::ColorButtonThread(ColorButton *color_button)
1378 this->color_button = color_button;
1379 this->update_lock = new Condition(0,"ColorButtonThread::update_lock");
1383 ColorButtonThread::~ColorButtonThread()
1389 void ColorButtonThread::start()
1397 void ColorButtonThread::stop()
1401 update_lock->unlock();
1406 void ColorButtonThread::run()
1408 ColorButtonPicker *color_picker = color_button->color_picker;
1409 color_picker->update_gui();
1411 update_lock->lock("ColorButtonThread::run");
1413 color_picker->update_gui();
1418 ColorBoxButton::ColorBoxButton(const char *title,
1419 int x, int y, int w, int h,
1420 int color, int alpha, int ok_cancel)
1421 : ColorButton(title, x, y, w, h, color, alpha, ok_cancel)
1424 ColorBoxButton::~ColorBoxButton()
1428 int ColorBoxButton::handle_new_color(int color, int alpha)
1430 return ColorButton::handle_new_color(color, alpha);
1432 void ColorBoxButton::handle_done_event(int result)
1434 ColorButton::handle_done_event(result);
1436 void ColorBoxButton::create_objects()
1438 update_gui(color, alpha);
1441 void ColorBoxButton::set_color(int color)
1443 this->color = (color & 0xffffff);
1444 this->alpha = (~color>>24) & 0xff;
1445 int r = (color>>16) & 0xff;
1446 int g = (color>> 8) & 0xff;
1447 int b = (color>> 0) & 0xff;
1448 int color_model = vframes[0]->get_color_model();
1449 int bpp = BC_CModels::calculate_pixelsize(color_model);
1450 for( int i=0; i<3; ++i ) {
1451 VFrame *vframe = vframes[i];
1452 int ww = vframe->get_w(), hh = vframe->get_h();
1453 uint8_t **rows = vframe->get_rows();
1454 int rr = r, gg = g, bb = b;
1459 if( (rr+=48) > 0xff ) rr = 0xff;
1460 if( (gg+=48) > 0xff ) gg = 0xff;
1461 if( (bb+=48) > 0xff ) bb = 0xff;
1464 if( (rr-=48) < 0x00 ) rr = 0x00;
1465 if( (gg-=48) < 0x00 ) gg = 0x00;
1466 if( (bb-=48) < 0x00 ) bb = 0x00;
1469 for( int y=0; y<hh; ++y ) {
1470 uint8_t *rp = rows[y];
1471 for( int x=0; x<ww; ++x ) {
1472 rp[0] = rr; rp[1] = gg; rp[2] = bb;
1473 if( bpp > 3 ) rp[3] = 0xff;
1478 set_images(vframes);
1481 ColorCircleButton::ColorCircleButton(const char *title,
1482 int x, int y, int w, int h,
1483 int color, int alpha, int ok_cancel)
1484 : ColorButton(title, x, y, w, h, color, alpha, ok_cancel)
1487 ColorCircleButton::~ColorCircleButton()
1490 int ColorCircleButton::handle_new_color(int color, int alpha)
1492 return ColorButton::handle_new_color(color, alpha);
1494 void ColorCircleButton::handle_done_event(int result)
1496 ColorButton::handle_done_event(result);
1498 void ColorCircleButton::create_objects()
1500 update_gui(color, alpha);
1503 void ColorCircleButton::set_color(int color)
1505 this->color = (color & 0xffffff);
1506 this->alpha = (~color>>24) & 0xff;
1507 int r = (color>>16) & 0xff;
1508 int g = (color>>8) & 0xff;
1509 int b = (color>>0) & 0xff;
1510 for( int i=0; i<3; ++i ) {
1511 VFrame *vframe = vframes[i];
1512 int ww = vframe->get_w(), hh = vframe->get_h();
1513 int cx = (ww+1)/2, cy = hh/2;
1514 double cc = (cx*cx + cy*cy) / 4.;
1515 uint8_t *bp = vframe->get_data(), *dp = bp;
1516 uint8_t *ep = dp + vframe->get_data_size();
1517 int rr = r, gg = g, bb = b;
1518 int bpl = vframe->get_bytes_per_line();
1523 if( (rr+=48) > 0xff ) rr = 0xff;
1524 if( (gg+=48) > 0xff ) gg = 0xff;
1525 if( (bb+=48) > 0xff ) bb = 0xff;
1528 if( (rr-=48) < 0x00 ) rr = 0x00;
1529 if( (gg-=48) < 0x00 ) gg = 0x00;
1530 if( (bb-=48) < 0x00 ) bb = 0x00;
1534 int yy = (dp-bp) / bpl, xx = ((dp-bp) % bpl) >> 2;
1535 int dy = cy - yy, dx = cx - xx;
1536 double s = dx*dx + dy*dy - cc;
1537 double ss = s < 0 ? 1 : s >= cc ? 0 : 1 - s/cc;
1539 *dp++ = rr; *dp++ = gg; *dp++ = bb; *dp++ = aa;
1542 set_images(vframes);