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(BC_OKButton::calculate_h(),BC_CancelButton::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 BC_OKButton(window));
270 add_tool(new BC_CancelButton(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 int ColorGUI::close_gui()
294 window->set_done(ok_cancel ? 1 : 0);
299 void ColorGUI::update_rgb()
301 update_rgb(rgb.r, rgb.g, rgb.b);
304 void ColorGUI::update_hsv()
306 update_hsv(hsv.h, hsv.s, hsv.v);
309 void ColorGUI::update_yuv()
311 update_yuv(yuv.y, yuv.u, yuv.v);
315 void ColorGUI::update_display()
317 wheel->draw(wheel->oldhue, wheel->oldsaturation);
318 wheel->oldhue = hsv.h;
319 wheel->oldsaturation = hsv.s;
320 wheel->draw(hsv.h, hsv.s);
322 wheel_value->draw(hsv.h, hsv.s, hsv.v);
323 wheel_value->flash();
327 hue->update((int)hsv.h);
339 hsv_h->update(hsv.h);
340 hsv_s->update(hsv.s);
341 hsv_v->update(hsv.v);
342 rgb_r->update(rgb.r);
343 rgb_g->update(rgb.g);
344 rgb_b->update(rgb.b);
345 yuv_y->update(yuv.y);
346 yuv_u->update(yuv.u);
347 yuv_v->update(yuv.v);
356 int ColorGUI::handle_gui()
358 window->unlock_window();
359 handle_new_color(rgb888(), alpha8());
360 window->lock_window("ColorGUI::handle_event");
364 void ColorGUI::get_screen_sample()
367 window->get_abs_cursor(cx, cy);
368 BC_Capture capture_bitmap(1, 1, 0);
369 VFrame vframe(1,1,BC_RGB888);
370 capture_bitmap.capture_frame(&vframe, cx,cy);
371 unsigned char *data = vframe.get_data();
372 rgb.r = data[0]/255.; rgb.g = data[1]/255.; rgb.b = data[2]/255.;
376 int ColorGUI::cursor_motion_gui()
378 if( button_grabbed && window->get_button_down() ) {
385 int ColorGUI::button_press_gui()
387 if( button_grabbed ) {
394 int ColorGUI::button_release_gui()
396 if( button_grabbed ) {
397 window->ungrab_buttons();
398 window->ungrab_cursor();
407 void ColorGUI::update_rgb_hex(const char *hex)
410 if( sscanf(hex,"%x",&color) == 1 ) {
412 aph = ((color>>24) & 0xff) / 255.;
415 float r = ((color>>16) & 0xff) / 255.;
416 float g = ((color>>8) & 0xff) / 255.;
417 float b = ((color>>0) & 0xff) / 255.;
418 rgb.r = r; rgb.g = g; rgb.b = b;
425 void ColorGUI::update_gui(int color, int alpha)
427 printf("ColorGUI::update_gui undefined.\n");
430 int ColorGUI::handle_new_color(int color, int alpha)
432 printf("ColorGUI::handle_new_color undefined.\n");
438 PaletteWheel::PaletteWheel(ColorGUI *gui, int x, int y)
439 : BC_SubWindow(x, y, xS(170), yS(170))
447 PaletteWheel::~PaletteWheel()
451 int PaletteWheel::button_press_event()
453 if( get_cursor_x() >= 0 && get_cursor_x() < get_w() &&
454 get_cursor_y() >= 0 && get_cursor_y() < get_h() &&
457 cursor_motion_event();
463 int PaletteWheel::cursor_motion_event()
465 int x1, y1, distance;
466 if( button_down && is_event_win() ) {
467 float h = get_angle(get_w()/2, get_h()/2, get_cursor_x(), get_cursor_y());
468 bclamp(h, 0, 359.999); gui->hsv.h = h;
469 x1 = get_w() / 2 - get_cursor_x();
470 y1 = get_h() / 2 - get_cursor_y();
471 distance = (int)sqrt(x1 * x1 + y1 * y1);
472 float s = (float)distance / (get_w() / 2);
473 bclamp(s, 0, 1); gui->hsv.s = s;
482 int PaletteWheel::button_release_event()
491 void PaletteWheel::create_objects()
494 //printf("PaletteWheel::create_objects 1\n");
497 float x1, y1, x2, y2;
499 int default_r, default_g, default_b;
500 VFrame frame(0, -1, get_w(), get_h(), BC_RGBA8888, -1);
503 default_r = (get_resources()->get_bg_color() & 0xff0000) >> 16;
504 default_g = (get_resources()->get_bg_color() & 0xff00) >> 8;
505 default_b = (get_resources()->get_bg_color() & 0xff);
506 //printf("PaletteWheel::create_objects 1\n");
508 int highlight_r = (get_resources()->button_light & 0xff0000) >> 16;
509 int highlight_g = (get_resources()->button_light & 0xff00) >> 8;
510 int highlight_b = (get_resources()->button_light & 0xff);
512 for( y2 = 0; y2 < get_h(); y2++ ) {
513 unsigned char *row = (unsigned char*)frame.get_rows()[(int)y2];
514 for( x2 = 0; x2 < get_w(); x2++ ) {
515 distance = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
516 if( distance > x1 ) {
517 row[(int)x2 * 4] = default_r;
518 row[(int)x2 * 4 + 1] = default_g;
519 row[(int)x2 * 4 + 2] = default_b;
520 row[(int)x2 * 4 + 3] = 0;
523 if( distance > x1 - 1 ) {
525 if( get_h() - y2 < x2 ) {
536 row[(int)x2 * 4] = r_i;
537 row[(int)x2 * 4 + 1] = g_i;
538 row[(int)x2 * 4 + 2] = b_i;
539 row[(int)x2 * 4 + 3] = 255;
542 h = get_angle(x1, y1, x2, y2);
544 HSV::hsv_to_rgb(r, g, b, h, s, v);
545 row[(int)x2 * 4] = (int)(r * 255);
546 row[(int)x2 * 4 + 1] = (int)(g * 255);
547 row[(int)x2 * 4 + 2] = (int)(b * 255);
548 row[(int)x2 * 4 + 3] = 255;
554 0, 0, get_w(), get_h(),
555 0, 0, get_w(), get_h(), 0);
558 oldsaturation = gui->hsv.s;
559 draw(oldhue, oldsaturation);
563 float PaletteWheel::torads(float angle)
565 return (float)angle / 360 * 2 * M_PI;
569 int PaletteWheel::draw(float hue, float saturation)
575 if( hue > 0 && hue < 90 ) {
576 x = (int)(w - w * cos(torads(90 - hue)) * saturation);
577 y = (int)(h - h * sin(torads(90 - hue)) * saturation);
579 else if( hue > 90 && hue < 180 ) {
580 x = (int)(w - w * cos(torads(hue - 90)) * saturation);
581 y = (int)(h + h * sin(torads(hue - 90)) * saturation);
583 else if( hue > 180 && hue < 270 ) {
584 x = (int)(w + w * cos(torads(270 - hue)) * saturation);
585 y = (int)(h + h * sin(torads(270 - hue)) * saturation);
587 else if( hue > 270 && hue < 360 ) {
588 x = (int)(w + w * cos(torads(hue - 270)) * saturation);
589 y = (int)(h - w * sin(torads(hue - 270)) * saturation);
591 else if( hue == 0 ) {
593 y = (int)(h - h * saturation);
595 else if( hue == 90 ) {
596 x = (int)(w - w * saturation);
599 else if( hue == 180 ) {
601 y = (int)(h + h * saturation);
603 else if( hue == 270 ) {
604 x = (int)(w + w * saturation);
610 draw_circle(x - xS(5), y - yS(5), xS(10), yS(10));
615 int PaletteWheel::get_angle(float x1, float y1, float x2, float y2)
617 float result = -atan2(x2 - x1, y1 - y2) * (360 / M_PI / 2);
623 PaletteWheelValue::PaletteWheelValue(ColorGUI *gui, int x, int y)
624 : BC_SubWindow(x, y, xS(40), yS(170), BLACK)
629 PaletteWheelValue::~PaletteWheelValue()
634 void PaletteWheelValue::create_objects()
636 frame = new VFrame(get_w(), get_h(), BC_RGB888);
637 draw(gui->hsv.h, gui->hsv.s, gui->hsv.v);
641 int PaletteWheelValue::button_press_event()
643 //printf("PaletteWheelValue::button_press 1 %d\n", is_event_win());
644 if( get_cursor_x() >= 0 && get_cursor_x() < get_w() &&
645 get_cursor_y() >= 0 && get_cursor_y() < get_h() &&
647 //printf("PaletteWheelValue::button_press 2\n");
649 cursor_motion_event();
655 int PaletteWheelValue::cursor_motion_event()
657 if( button_down && is_event_win() ) {
658 //printf("PaletteWheelValue::cursor_motion 1\n");
659 float v = 1.0 - (float)(get_cursor_y() - 2) / (get_h() - 4);
660 bclamp(v, 0, 1); gui->hsv.v = v;
668 int PaletteWheelValue::button_release_event()
671 //printf("PaletteWheelValue::button_release 1\n");
678 int PaletteWheelValue::draw(float hue, float saturation, float value)
683 for( i = get_h() - 3; i >= 2; i-- ) {
684 unsigned char *row = (unsigned char*)frame->get_rows()[i];
685 HSV::hsv_to_rgb(r_f, g_f, b_f, hue, saturation,
686 1.0 - (float)(i - 2) / (get_h() - 4));
687 r = (int)(r_f * 255);
688 g = (int)(g_f * 255);
689 b = (int)(b_f * 255);
690 for( j = 0; j < get_w(); j++ ) {
696 draw_3d_border(0, 0, get_w(), get_h(), 1);
697 draw_vframe(frame, 2, 2, get_w() - 4, get_h() - 4,
698 2, 2, get_w() - 4, get_h() - 4, 0);
700 draw_line(2, get_h() - 3 - (int)(value * (get_h() - 5)),
701 get_w() - 3, get_h() - 3 - (int)(value * (get_h() - 5)));
702 //printf("PaletteWheelValue::draw %d %f\n", __LINE__, value);
707 PaletteOutput::PaletteOutput(ColorGUI *gui, int x, int y)
708 : BC_SubWindow(x, y, xS(180), yS(30), BLACK)
712 PaletteOutput::~PaletteOutput()
717 void PaletteOutput::create_objects()
723 int PaletteOutput::handle_event()
728 int PaletteOutput::draw()
730 set_color(gui->rgb888());
731 draw_box(2, 2, get_w() - 4, get_h() - 4);
732 draw_3d_border(0, 0, get_w(), get_h(), 1);
736 PaletteHue::PaletteHue(ColorGUI *gui, int x, int y)
737 : BC_ISlider(x, y, 0, xS(150), xS(200), 0, 359, (int)(gui->hsv.h), 0)
741 PaletteHue::~PaletteHue()
745 int PaletteHue::handle_event()
747 gui->hsv.h = get_value();
753 PaletteSat::PaletteSat(ColorGUI *gui, int x, int y)
754 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1.0, gui->hsv.s, 0)
759 PaletteSat::~PaletteSat()
763 int PaletteSat::handle_event()
765 gui->hsv.s = get_value();
772 PaletteVal::PaletteVal(ColorGUI *gui, int x, int y)
773 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1.0, gui->hsv.v, 0)
778 PaletteVal::~PaletteVal()
782 int PaletteVal::handle_event()
784 gui->hsv.v = get_value();
791 PaletteRed::PaletteRed(ColorGUI *gui, int x, int y)
792 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, gui->rgb.r, 0)
797 PaletteRed::~PaletteRed()
801 int PaletteRed::handle_event()
803 gui->rgb.r = get_value();
809 PaletteGrn::PaletteGrn(ColorGUI *gui, int x, int y)
810 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, gui->rgb.g, 0)
815 PaletteGrn::~PaletteGrn()
819 int PaletteGrn::handle_event()
821 gui->rgb.g = get_value();
827 PaletteBlu::PaletteBlu(ColorGUI *gui, int x, int y)
828 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, gui->rgb.b, 0)
833 PaletteBlu::~PaletteBlu()
837 int PaletteBlu::handle_event()
839 gui->rgb.b = get_value();
845 PaletteAlpha::PaletteAlpha(ColorGUI *gui, int x, int y)
846 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, gui->aph, 0)
851 PaletteAlpha::~PaletteAlpha()
855 int PaletteAlpha::handle_event()
857 gui->aph = get_value();
858 gui->aph_a->update(gui->aph);
859 gui->hex_box->update();
864 PaletteLum::PaletteLum(ColorGUI *gui, int x, int y)
865 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, gui->yuv.y, 0)
870 PaletteLum::~PaletteLum()
874 int PaletteLum::handle_event()
876 gui->yuv.y = get_value();
882 PaletteCr::PaletteCr(ColorGUI *gui, int x, int y)
883 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, gui->yuv.u, 0)
888 PaletteCr::~PaletteCr()
892 int PaletteCr::handle_event()
894 gui->yuv.u = get_value();
900 PaletteCb::PaletteCb(ColorGUI *gui, int x, int y)
901 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, gui->yuv.v, 0)
906 PaletteCb::~PaletteCb()
910 int PaletteCb::handle_event()
912 gui->yuv.v = get_value();
918 void ColorGUI::update_rgb(float r, float g, float b)
921 YUV::yuv.rgb_to_yuv_f(r, g, b, y, u, v);
923 bclamp(y, 0, 1); yuv.y = y;
924 bclamp(u, 0, 1); yuv.u = u;
925 bclamp(v, 0, 1); yuv.v = v; }
927 HSV::rgb_to_hsv(r,g,b, h,s,v);
928 bclamp(h, 0, 360); hsv.h = h;
929 bclamp(s, 0, 1); hsv.s = s;
930 bclamp(v, 0, 1); hsv.v = v; }
933 void ColorGUI::update_yuv(float y, float u, float v)
937 YUV::yuv.yuv_to_rgb_f(r, g, b, y, u, v);
938 bclamp(r, 0, 1); rgb.r = r;
939 bclamp(g, 0, 1); rgb.g = g;
940 bclamp(b, 0, 1); rgb.b = b;
942 HSV::rgb_to_hsv(r,g,b, h, s, v);
943 bclamp(h, 0, 360); hsv.h = h;
944 bclamp(s, 0, 1); hsv.s = s;
945 bclamp(v, 0, 1); hsv.v = v; }
948 void ColorGUI::update_hsv(float h, float s, float v)
951 HSV::hsv_to_rgb(r,g,b, h,s,v);
952 bclamp(r, 0, 1); rgb.r = r;
953 bclamp(g, 0, 1); rgb.g = g;
954 bclamp(b, 0, 1); rgb.b = b;
956 YUV::yuv.rgb_to_yuv_f(r, g, b, y, u, v);
958 bclamp(y, 0, 1); yuv.y = y;
959 bclamp(u, 0, 1); yuv.u = u;
960 bclamp(v, 0, 1); yuv.v = v; }
963 void ColorGUI::load_history()
965 char history_path[BCTEXTLEN];
966 MWindow::create_defaults_path(history_path,PALETTE_DATA);
967 FILE *fp = fopen(history_path,"r");
970 while( i < PALLETTE_HISTORY_SIZE ) {
972 if( !fgets(line,sizeof(line)-1,fp) ) break;
973 line[sizeof(line)-1] = 0;
974 if( sscanf(line, "%x",&palette_history[i]) != 1 ) break;
979 int r = 0, g = 0, b = 0;
980 float v0 = 0, v1 = 1;
981 while( i < PALLETTE_HISTORY_SIZE ) {
982 int grey_code = i ^ (i>>1);
983 r = 255 * ((grey_code&4) ? v0 : v1);
984 g = 255 * ((grey_code&2) ? v0 : v1);
985 b = 255 * ((grey_code&1) ? v0 : v1);
986 int color = (r<<16) | (g<<8) | (b<<0);
987 palette_history[i++] = color;
988 if( i & 7 ) continue;
989 v0 = 0.5f * (v0+.5f);
990 v1 = 0.5f * (v1+.5f);
993 void ColorGUI::save_history()
995 char history_path[BCTEXTLEN];
996 MWindow::create_defaults_path(history_path,PALETTE_DATA);
997 FILE *fp = fopen(history_path,"w");
999 for( int i=0; i<PALLETTE_HISTORY_SIZE; ++i ) {
1000 fprintf(fp, "%06x\n", palette_history[i]);
1005 void ColorGUI::update_history(int color)
1007 int out = palette_history[0];
1008 palette_history[0] = color;
1009 for( int i=1; out != color && i<PALLETTE_HISTORY_SIZE; ++i ) {
1011 out = palette_history[i];
1012 palette_history[i] = in;
1015 void ColorGUI::update_history()
1017 update_history(rgb888());
1020 int ColorGUI::rgb888()
1022 int r = 255*rgb.r + 0.5, g = 255*rgb.g + 0.5, b = 255*rgb.b + 0.5;
1023 bclamp(r, 0, 255); bclamp(g, 0, 255); bclamp(b, 0, 255);
1024 return (r<<16) | (g<<8) | (b<<0);
1026 int ColorGUI::alpha8()
1028 int a = 255*aph + 0.5;
1033 PaletteNum::PaletteNum(ColorGUI *gui, int x, int y,
1034 float &output, float min, float max)
1035 : BC_TumbleTextBox(gui->window, output, min, max, x, y, xS(64))
1038 this->output = &output;
1039 set_increment(0.01);
1043 PaletteNum::~PaletteNum()
1048 int PaletteHSV::handle_event()
1056 int PaletteRGB::handle_event()
1064 int PaletteYUV::handle_event()
1072 int PaletteAPH::handle_event()
1075 gui->update_display();
1080 PaletteHexButton::PaletteHexButton(ColorGUI *gui, int x, int y)
1081 : BC_GenericButton(x, y, xS(50), "#")
1084 set_tooltip(_("hex rgb color"));
1086 PaletteHexButton::~PaletteHexButton()
1089 int PaletteHexButton::handle_event()
1091 const char *hex = gui->hex_box->get_text();
1092 gui->update_rgb_hex(hex);
1096 PaletteHex::PaletteHex(ColorGUI *gui, int x, int y, const char *hex)
1097 : BC_TextBox(x, y, xS(100), 1, hex)
1101 PaletteHex::~PaletteHex()
1104 void PaletteHex::update()
1106 char hex[BCSTRLEN], *cp = hex;
1108 cp += sprintf(cp,"%02x", gui->alpha8());
1109 sprintf(cp,"%06x",gui->rgb888());
1110 BC_TextBox::update(hex);
1113 int PaletteHex::keypress_event()
1115 if( get_keypress() != RETURN )
1116 return BC_TextBox::keypress_event();
1117 gui->update_rgb_hex(get_text());
1121 #include "grabpick_up_png.h"
1122 #include "grabpick_hi_png.h"
1123 #include "grabpick_dn_png.h"
1125 PaletteGrabButton::PaletteGrabButton(ColorGUI *gui, int x, int y)
1126 : BC_Button(x, y, vframes)
1129 vframes[0] = new VFramePng(grabpick_up_png);
1130 vframes[1] = new VFramePng(grabpick_hi_png);
1131 vframes[2] = new VFramePng(grabpick_dn_png);
1132 set_tooltip(_("grab from anywhere picker"));
1134 PaletteGrabButton::~PaletteGrabButton()
1136 for( int i=0; i<3; ++i )
1139 int PaletteGrabButton::handle_event()
1141 if( gui->window->grab_buttons() ) {
1142 gui->window->grab_cursor();
1143 gui->button_grabbed = 1;
1144 gui->button_press_gui(); // redraw face HI
1149 PaletteHistory::PaletteHistory(ColorGUI *gui, int x, int y)
1150 : BC_SubWindow(x,y, xS(200), yS(24))
1154 set_tooltip(_("color history"));
1156 PaletteHistory::~PaletteHistory()
1159 void PaletteHistory::update(int flush)
1162 for( int i=0; i<PALLETTE_HISTORY_SIZE; x1=x2 ) {
1163 int rgb = gui->palette_history[i];
1164 x2 = (++i * get_w())/PALLETTE_HISTORY_SIZE;
1165 draw_3d_box(x1,0,x2-x1,get_h(),WHITE,BLACK,rgb,LTBLUE,DKBLUE);
1170 int PaletteHistory::button_press_event()
1172 if( button_down || !is_event_win() ) return 0;
1174 cursor_motion_event();
1177 int PaletteHistory::button_release_event()
1179 if( !button_down || !is_event_win() ) return 0;
1180 cursor_motion_event();
1181 if( button_down > 0 ) {
1183 gui->update_display();
1184 gui->update_history();
1189 int PaletteHistory::cursor_motion_event()
1191 if( !button_down || !is_event_win() ) return 0;
1193 int pick = (PALLETTE_HISTORY_SIZE * get_cursor_x()) / get_w();
1194 bclamp(pick, 0, PALLETTE_HISTORY_SIZE-1);
1195 int color = gui->palette_history[pick];
1196 float r = ((color>>16) & 0xff) / 255.;
1197 float g = ((color>>8) & 0xff) / 255.;
1198 float b = ((color>>0) & 0xff) / 255.;
1199 if( gui->rgb.r != r || gui->rgb.g != g || gui->rgb.b != b ) {
1200 gui->rgb.r = r; gui->rgb.g = g; gui->rgb.b = b;
1206 int PaletteHistory::cursor_leave_event()
1211 int PaletteHistory::repeat_event(int64_t duration)
1215 if( duration == get_resources()->tooltip_delay &&
1216 get_tooltip() && *get_tooltip() && cursor_above() ) {
1224 ColorButton::ColorButton(const char *title,
1225 int x, int y, int w, int h,
1226 int color, int alpha, int ok_cancel)
1227 : BC_Button(x, y, w, vframes)
1229 this->title = title;
1230 this->color = this->orig_color = color;
1231 this->alpha = this->orig_alpha = alpha;
1232 this->ok_cancel = ok_cancel;
1234 for( int i=0; i<3; ++i ) {
1235 vframes[i] = new VFrame(w, h, BC_RGBA8888);
1236 vframes[i]->clear_frame();
1242 ColorButton::~ColorButton()
1244 delete color_thread;
1245 delete color_picker;
1246 for( int i=0; i<3; ++i )
1250 void ColorButton::set_color(int color)
1252 printf("ColorButton::set_color %06x\n", color);
1254 void ColorButton::handle_done_event(int result)
1256 color_thread->stop();
1258 int ColorButton::handle_new_color(int color, int alpha)
1260 printf("ColorButton::handle_new_color %02x%06x\n", alpha, color);
1264 int ColorButton::handle_event()
1267 delete color_picker;
1268 color_picker = new ColorButtonPicker(this);
1269 orig_color = color; orig_alpha = alpha;
1270 color_picker->start_window(color, alpha, ok_cancel);
1272 color_thread = new ColorButtonThread(this);
1273 color_thread->start();
1274 lock_window("ColorButtonButton::start_color_thread");
1278 void ColorButton::close_picker()
1280 if( color_thread ) color_thread->stop();
1281 delete color_thread; color_thread = 0;
1282 delete color_picker; color_picker = 0;
1285 void ColorButton::update_gui(int color, int alpha)
1288 color_picker->update_gui(color, alpha);
1289 update_gui(color | (~alpha<<24));
1292 void ColorButton::update_gui(int color)
1298 ColorButtonPicker::ColorButtonPicker(ColorButton *color_button)
1299 : ColorPicker(color_button->alpha >= 0 ? 1 : 0, color_button->title)
1301 this->color_button = color_button;
1304 ColorButtonPicker::~ColorButtonPicker()
1308 void ColorButtonPicker::handle_done_event(int result)
1310 color_button->color_thread->stop();
1311 color_button->handle_done_event(result);
1314 void ColorButtonPicker::update(int color, int alpha)
1316 color_button->color = color;
1317 color_button->alpha = alpha;
1318 color_button->color_thread->update_lock->unlock();
1321 int ColorButtonPicker::handle_new_color(int color, int alpha)
1323 color_button->lock_window("ColorButtonPicker::handle_new_color");
1324 color_button->update_gui(color, alpha);
1325 color_button->unlock_window();
1326 return color_button->handle_new_color(color, alpha);
1329 void ColorButtonPicker::update_gui()
1331 color_button->lock_window("ColorButtonPicker::update_gui");
1332 color_button->update_gui(color_button->color, color_button->alpha);
1333 color_button->unlock_window();
1336 void ColorButtonPicker::update_gui(int color, int alpha)
1338 ColorPicker::update_gui(color, alpha);
1339 color_button->handle_new_color(color, alpha);
1342 ColorButtonThread::ColorButtonThread(ColorButton *color_button)
1345 this->color_button = color_button;
1346 this->update_lock = new Condition(0,"ColorButtonThread::update_lock");
1350 ColorButtonThread::~ColorButtonThread()
1356 void ColorButtonThread::start()
1364 void ColorButtonThread::stop()
1368 update_lock->unlock();
1373 void ColorButtonThread::run()
1375 ColorButtonPicker *color_picker = color_button->color_picker;
1376 color_picker->update_gui();
1378 update_lock->lock("ColorButtonThread::run");
1380 color_picker->update_gui();
1385 ColorBoxButton::ColorBoxButton(const char *title,
1386 int x, int y, int w, int h,
1387 int color, int alpha, int ok_cancel)
1388 : ColorButton(title, x, y, w, h, color, alpha, ok_cancel)
1391 ColorBoxButton::~ColorBoxButton()
1395 int ColorBoxButton::handle_new_color(int color, int alpha)
1397 return ColorButton::handle_new_color(color, alpha);
1399 void ColorBoxButton::handle_done_event(int result)
1401 ColorButton::handle_done_event(result);
1403 void ColorBoxButton::create_objects()
1405 update_gui(color, alpha);
1408 void ColorBoxButton::set_color(int color)
1410 this->color = (color & 0xffffff);
1411 this->alpha = (~color>>24) & 0xff;
1412 int r = (color>>16) & 0xff;
1413 int g = (color>> 8) & 0xff;
1414 int b = (color>> 0) & 0xff;
1415 int color_model = vframes[0]->get_color_model();
1416 int bpp = BC_CModels::calculate_pixelsize(color_model);
1417 for( int i=0; i<3; ++i ) {
1418 VFrame *vframe = vframes[i];
1419 int ww = vframe->get_w(), hh = vframe->get_h();
1420 uint8_t **rows = vframe->get_rows();
1421 int rr = r, gg = g, bb = b;
1426 if( (rr+=48) > 0xff ) rr = 0xff;
1427 if( (gg+=48) > 0xff ) gg = 0xff;
1428 if( (bb+=48) > 0xff ) bb = 0xff;
1431 if( (rr-=48) < 0x00 ) rr = 0x00;
1432 if( (gg-=48) < 0x00 ) gg = 0x00;
1433 if( (bb-=48) < 0x00 ) bb = 0x00;
1436 for( int y=0; y<hh; ++y ) {
1437 uint8_t *rp = rows[y];
1438 for( int x=0; x<ww; ++x ) {
1439 rp[0] = rr; rp[1] = gg; rp[2] = bb;
1440 if( bpp > 3 ) rp[3] = 0xff;
1445 set_images(vframes);
1448 ColorCircleButton::ColorCircleButton(const char *title,
1449 int x, int y, int w, int h,
1450 int color, int alpha, int ok_cancel)
1451 : ColorButton(title, x, y, w, h, color, alpha, ok_cancel)
1454 ColorCircleButton::~ColorCircleButton()
1457 int ColorCircleButton::handle_new_color(int color, int alpha)
1459 return ColorButton::handle_new_color(color, alpha);
1461 void ColorCircleButton::handle_done_event(int result)
1463 ColorButton::handle_done_event(result);
1465 void ColorCircleButton::create_objects()
1467 update_gui(color, alpha);
1470 void ColorCircleButton::set_color(int color)
1472 this->color = (color & 0xffffff);
1473 this->alpha = (~color>>24) & 0xff;
1474 int r = (color>>16) & 0xff;
1475 int g = (color>>8) & 0xff;
1476 int b = (color>>0) & 0xff;
1477 for( int i=0; i<3; ++i ) {
1478 VFrame *vframe = vframes[i];
1479 int ww = vframe->get_w(), hh = vframe->get_h();
1480 int cx = (ww+1)/2, cy = hh/2;
1481 double cc = (cx*cx + cy*cy) / 4.;
1482 uint8_t *bp = vframe->get_data(), *dp = bp;
1483 uint8_t *ep = dp + vframe->get_data_size();
1484 int rr = r, gg = g, bb = b;
1485 int bpl = vframe->get_bytes_per_line();
1490 if( (rr+=48) > 0xff ) rr = 0xff;
1491 if( (gg+=48) > 0xff ) gg = 0xff;
1492 if( (bb+=48) > 0xff ) bb = 0xff;
1495 if( (rr-=48) < 0x00 ) rr = 0x00;
1496 if( (gg-=48) < 0x00 ) gg = 0x00;
1497 if( (bb-=48) < 0x00 ) bb = 0x00;
1501 int yy = (dp-bp) / bpl, xx = ((dp-bp) % bpl) >> 2;
1502 int dy = cy - yy, dx = cx - xx;
1503 double s = dx*dx + dy*dy - cc;
1504 double ss = s < 0 ? 1 : s >= cc ? 0 : 1 - s/cc;
1506 *dp++ = rr; *dp++ = gg; *dp++ = bb; *dp++ = aa;
1509 set_images(vframes);