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"
26 #include "colorpicker.h"
27 #include "condition.h"
38 #define PALETTE_DATA "palette.dat"
40 ColorPicker::ColorPicker(int do_alpha, const char *title)
44 this->do_alpha = do_alpha;
46 this->output = this->orig_color = BLACK;
47 this->alpha = this->orig_alpha = 255;
50 ColorPicker::~ColorPicker()
55 void ColorPicker::start_window(int output, int alpha, int ok_cancel)
58 ColorWindow *gui = (ColorWindow *)get_gui();
60 gui->lock_window("ColorPicker::start_window");
66 this->orig_color = output;
67 this->orig_alpha = alpha;
68 this->output = output;
70 this->ok_cancel = ok_cancel;
74 BC_Window* ColorPicker::new_gui()
76 char window_title[BCTEXTLEN];
77 strcpy(window_title, _(PROGRAM_NAME ": "));
78 strcat(window_title, title ? title : _("Color Picker"));
79 BC_DisplayInfo display_info;
80 int x = display_info.get_abs_cursor_x() + xS(25);
81 int y = display_info.get_abs_cursor_y() - yS(100);
82 int w = xS(540), h = yS(330);
84 h += bmax(BC_OKButton::calculate_h(),BC_CancelButton::calculate_h());
85 int root_w = display_info.get_root_w(), root_h = display_info.get_root_h();
86 if( x+w > root_w ) x = root_w - w;
87 if( y+h > root_h ) y = root_h - h;
90 ColorWindow *gui = new ColorWindow(this, x, y, w, h, window_title);
91 gui->create_objects();
95 void ColorPicker::update_gui(int output, int alpha)
97 ColorWindow *gui = (ColorWindow *)get_gui();
99 gui->lock_window("ColorPicker::update_gui");
100 this->output = output;
102 gui->change_values();
103 gui->update_display();
104 gui->unlock_window();
107 int ColorPicker::handle_new_color(int output, int alpha)
109 printf("ColorPicker::handle_new_color undefined.\n");
114 ColorWindow::ColorWindow(ColorPicker *thread, int x, int y, int w, int h, const char *title)
115 : BC_Window(title, x, y, w, h, w, h, 0, 0, 1)
117 this->thread = thread;
122 hue = 0; sat = 0; val = 0;
123 red = 0; grn = 0; blu = 0;
124 lum = 0; c_r = 0; c_b = 0;
127 hsv_h = 0; hsv_s = 0; hsv_v = 0;
128 rgb_r = 0; rgb_g = 0; rgb_b = 0;
129 yuv_y = 0; yuv_u = 0; yuv_v = 0;
134 ColorWindow::~ColorWindow()
136 delete hsv_h; delete hsv_s; delete hsv_v;
137 delete rgb_r; delete rgb_g; delete rgb_b;
138 delete yuv_y; delete yuv_u; delete yuv_v;
141 if( button_grabbed ) {
145 update_history(rgb888());
149 void ColorWindow::create_objects()
151 int xs5 = xS(5), xs10 = xS(10), xs15 = xS(15);
152 int ys10 = yS(10), ys15 = yS(15), ys25 = yS(25), ys40 = yS(40);
153 int x0 = xs10, y0 = ys10;
154 lock_window("ColorWindow::create_objects");
158 add_tool(wheel = new PaletteWheel(this, x, y));
159 wheel->create_objects();
161 x += xS(180); add_tool(wheel_value = new PaletteWheelValue(this, x, y));
162 wheel_value->create_objects();
164 y += yS(180); add_tool(output = new PaletteOutput(this, x, y));
165 output->create_objects();
166 y += output->get_h() + yS(20);
168 load_history(); int x1 = x;
169 add_tool(hex_btn = new PaletteHexButton(this, x1, y));
170 char hex[BCSTRLEN]; sprintf(hex,"%06x",thread->output);
171 x1 += hex_btn->get_w() + xs5;
172 add_tool(hex_box = new PaletteHex(this, x1, y, hex));
173 x1 += hex_box->get_w() + xs15;
174 add_tool(grab_btn = new PaletteGrabButton(this, x1, y));
175 y += hex_box->get_h() + ys15;
176 add_tool(history = new PaletteHistory(this, xs10, y));
179 add_tool(new BC_Title(x, y =y0, C_("H:"), SMALLFONT));
180 add_tool(new BC_Title(x, y+=ys25, C_("S:"), SMALLFONT));
181 add_tool(new BC_Title(x, y+=ys25, D_("colorpicker_value#V:"), SMALLFONT));
182 add_tool(new BC_Title(x, y+=ys40, C_("R:"), SMALLFONT));
183 add_tool(new BC_Title(x, y+=ys25, C_("G:"), SMALLFONT));
184 add_tool(new BC_Title(x, y+=ys25, C_("B:"), SMALLFONT));
185 add_tool(new BC_Title(x, y+=ys40, C_("Y:"), SMALLFONT));
186 add_tool(new BC_Title(x, y+=ys25, C_("U:"), SMALLFONT));
187 add_tool(new BC_Title(x, y+=ys25, D_("colorpicker_Cr#V:"), SMALLFONT));
188 if( thread->do_alpha )
189 add_tool(new BC_Title(x, y+=ys40, C_("A:"), SMALLFONT));
191 add_tool(hue = new PaletteHue(this, x, y= y0));
192 add_tool(sat = new PaletteSat(this, x, y+=ys25));
193 add_tool(val = new PaletteVal(this, x, y+=ys25));
194 add_tool(red = new PaletteRed(this, x, y+=ys40));
195 add_tool(grn = new PaletteGrn(this, x, y+=ys25));
196 add_tool(blu = new PaletteBlu(this, x, y+=ys25));
197 add_tool(lum = new PaletteLum(this, x, y+=ys40));
198 add_tool(c_r = new PaletteCr (this, x, y+=ys25));
199 add_tool(c_b = new PaletteCb (this, x, y+=ys25));
200 if( thread->do_alpha )
201 add_tool(alpha = new PaletteAlpha(this, x, y+=ys40));
203 x += hue->get_w() + xs10;
204 hsv_h = new PaletteHSV(this, x,y= y0, hsv.h, 0, 360);
205 hsv_h->create_objects(); hsv_h->set_tooltip(_("Hue"));
206 hsv_s = new PaletteHSV(this, x,y+=ys25, hsv.s, 0, 1);
207 hsv_s->create_objects(); hsv_s->set_tooltip(_("Saturation"));
208 hsv_v = new PaletteHSV(this, x,y+=ys25, hsv.v, 0, 1);
209 hsv_v->create_objects(); hsv_v->set_tooltip(_("Value"));
210 rgb_r = new PaletteRGB(this, x,y+=ys40, rgb.r, 0, 1);
211 rgb_r->create_objects(); rgb_r->set_tooltip(_("Red"));
212 rgb_g = new PaletteRGB(this, x,y+=ys25, rgb.g, 0, 1);
213 rgb_g->create_objects(); rgb_g->set_tooltip(_("Green"));
214 rgb_b = new PaletteRGB(this, x,y+=ys25, rgb.b, 0, 1);
215 rgb_b->create_objects(); rgb_b->set_tooltip(_("Blue"));
216 yuv_y = new PaletteYUV(this, x,y+=ys40, yuv.y, 0, 1);
217 yuv_y->create_objects(); yuv_y->set_tooltip(_("Luminance"));
218 yuv_u = new PaletteYUV(this, x,y+=ys25, yuv.u, 0, 1);
219 yuv_u->create_objects(); yuv_u->set_tooltip(_("Blue Luminance Difference"));
220 yuv_v = new PaletteYUV(this, x,y+=ys25, yuv.v, 0, 1);
221 yuv_v->create_objects(); yuv_v->set_tooltip(_("Red Luminance Difference"));
222 if( thread->do_alpha ) {
223 aph_a = new PaletteAPH(this, x,y+=ys40, aph, 0, 1);
224 aph_a->create_objects(); aph_a->set_tooltip(_("Alpha"));
226 if( thread->ok_cancel ) {
227 add_tool(new BC_OKButton(this));
228 add_tool(new BC_CancelButton(this));
230 thread->create_objects(this);
239 void ColorWindow::change_values()
241 float r = ((thread->output>>16) & 0xff) / 255.;
242 float g = ((thread->output>>8) & 0xff) / 255.;
243 float b = ((thread->output>>0) & 0xff) / 255.;
244 rgb.r = r; rgb.g = g; rgb.b = b;
245 aph = (float)thread->alpha / 255;
246 update_rgb(rgb.r, rgb.g, rgb.b);
250 int ColorWindow::close_event()
252 set_done(thread->ok_cancel ? 1 : 0);
257 void ColorWindow::update_rgb()
259 update_rgb(rgb.r, rgb.g, rgb.b);
262 void ColorWindow::update_hsv()
264 update_hsv(hsv.h, hsv.s, hsv.v);
267 void ColorWindow::update_yuv()
269 update_yuv(yuv.y, yuv.u, yuv.v);
273 void ColorWindow::update_display()
275 wheel->draw(wheel->oldhue, wheel->oldsaturation);
276 wheel->oldhue = hsv.h;
277 wheel->oldsaturation = hsv.s;
278 wheel->draw(hsv.h, hsv.s);
280 wheel_value->draw(hsv.h, hsv.s, hsv.v);
281 wheel_value->flash();
285 hue->update((int)hsv.h);
297 hsv_h->update(hsv.h);
298 hsv_s->update(hsv.s);
299 hsv_v->update(hsv.v);
300 rgb_r->update(rgb.r);
301 rgb_g->update(rgb.g);
302 rgb_b->update(rgb.b);
303 yuv_y->update(yuv.y);
304 yuv_u->update(yuv.u);
305 yuv_v->update(yuv.v);
308 if( thread->do_alpha ) {
314 int ColorWindow::handle_event()
317 thread->handle_new_color(rgb888(), alpha8());
318 lock_window("ColorWindow::handle_event");
322 void ColorWindow::get_screen_sample()
325 get_abs_cursor(cx, cy);
326 BC_Capture capture_bitmap(1, 1, 0);
327 VFrame vframe(1,1,BC_RGB888);
328 capture_bitmap.capture_frame(&vframe, cx,cy);
329 unsigned char *data = vframe.get_data();
330 rgb.r = data[0]/255.; rgb.g = data[1]/255.; rgb.b = data[2]/255.;
334 int ColorWindow::cursor_motion_event()
336 if( button_grabbed && get_button_down() ) {
343 int ColorWindow::button_press_event()
345 if( button_grabbed ) {
352 int ColorWindow::button_release_event()
354 if( button_grabbed ) {
360 return handle_event();
365 void ColorWindow::update_rgb_hex(const char *hex)
368 if( sscanf(hex,"%x",&color) == 1 ) {
369 if( thread->do_alpha ) {
370 aph = ((color>>24) & 0xff) / 255.;
373 float r = ((color>>16) & 0xff) / 255.;
374 float g = ((color>>8) & 0xff) / 255.;
375 float b = ((color>>0) & 0xff) / 255.;
376 rgb.r = r; rgb.g = g; rgb.b = b;
384 PaletteWheel::PaletteWheel(ColorWindow *window, int x, int y)
385 : BC_SubWindow(x, y, xS(170), yS(170))
387 this->window = window;
393 PaletteWheel::~PaletteWheel()
397 int PaletteWheel::button_press_event()
399 if( get_cursor_x() >= 0 && get_cursor_x() < get_w() &&
400 get_cursor_y() >= 0 && get_cursor_y() < get_h() &&
403 cursor_motion_event();
409 int PaletteWheel::cursor_motion_event()
411 int x1, y1, distance;
412 if( button_down && is_event_win() ) {
413 float h = get_angle(get_w()/2, get_h()/2, get_cursor_x(), get_cursor_y());
414 bclamp(h, 0, 359.999); window->hsv.h = h;
415 x1 = get_w() / 2 - get_cursor_x();
416 y1 = get_h() / 2 - get_cursor_y();
417 distance = (int)sqrt(x1 * x1 + y1 * y1);
418 float s = (float)distance / (get_w() / 2);
419 bclamp(s, 0, 1); window->hsv.s = s;
421 window->update_hsv();
422 window->handle_event();
428 int PaletteWheel::button_release_event()
437 void PaletteWheel::create_objects()
440 //printf("PaletteWheel::create_objects 1\n");
443 float x1, y1, x2, y2;
445 int default_r, default_g, default_b;
446 VFrame frame(0, -1, get_w(), get_h(), BC_RGBA8888, -1);
449 default_r = (get_resources()->get_bg_color() & 0xff0000) >> 16;
450 default_g = (get_resources()->get_bg_color() & 0xff00) >> 8;
451 default_b = (get_resources()->get_bg_color() & 0xff);
452 //printf("PaletteWheel::create_objects 1\n");
454 int highlight_r = (get_resources()->button_light & 0xff0000) >> 16;
455 int highlight_g = (get_resources()->button_light & 0xff00) >> 8;
456 int highlight_b = (get_resources()->button_light & 0xff);
458 for( y2 = 0; y2 < get_h(); y2++ ) {
459 unsigned char *row = (unsigned char*)frame.get_rows()[(int)y2];
460 for( x2 = 0; x2 < get_w(); x2++ ) {
461 distance = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
462 if( distance > x1 ) {
463 row[(int)x2 * 4] = default_r;
464 row[(int)x2 * 4 + 1] = default_g;
465 row[(int)x2 * 4 + 2] = default_b;
466 row[(int)x2 * 4 + 3] = 0;
469 if( distance > x1 - 1 ) {
471 if( get_h() - y2 < x2 ) {
482 row[(int)x2 * 4] = r_i;
483 row[(int)x2 * 4 + 1] = g_i;
484 row[(int)x2 * 4 + 2] = b_i;
485 row[(int)x2 * 4 + 3] = 255;
488 h = get_angle(x1, y1, x2, y2);
490 HSV::hsv_to_rgb(r, g, b, h, s, v);
491 row[(int)x2 * 4] = (int)(r * 255);
492 row[(int)x2 * 4 + 1] = (int)(g * 255);
493 row[(int)x2 * 4 + 2] = (int)(b * 255);
494 row[(int)x2 * 4 + 3] = 255;
500 0, 0, get_w(), get_h(),
501 0, 0, get_w(), get_h(), 0);
503 oldhue = window->hsv.h;
504 oldsaturation = window->hsv.s;
505 draw(oldhue, oldsaturation);
509 float PaletteWheel::torads(float angle)
511 return (float)angle / 360 * 2 * M_PI;
515 int PaletteWheel::draw(float hue, float saturation)
521 if( hue > 0 && hue < 90 ) {
522 x = (int)(w - w * cos(torads(90 - hue)) * saturation);
523 y = (int)(h - h * sin(torads(90 - hue)) * saturation);
525 else if( hue > 90 && hue < 180 ) {
526 x = (int)(w - w * cos(torads(hue - 90)) * saturation);
527 y = (int)(h + h * sin(torads(hue - 90)) * saturation);
529 else if( hue > 180 && hue < 270 ) {
530 x = (int)(w + w * cos(torads(270 - hue)) * saturation);
531 y = (int)(h + h * sin(torads(270 - hue)) * saturation);
533 else if( hue > 270 && hue < 360 ) {
534 x = (int)(w + w * cos(torads(hue - 270)) * saturation);
535 y = (int)(h - w * sin(torads(hue - 270)) * saturation);
537 else if( hue == 0 ) {
539 y = (int)(h - h * saturation);
541 else if( hue == 90 ) {
542 x = (int)(w - w * saturation);
545 else if( hue == 180 ) {
547 y = (int)(h + h * saturation);
549 else if( hue == 270 ) {
550 x = (int)(w + w * saturation);
556 draw_circle(x - xS(5), y - yS(5), xS(10), yS(10));
561 int PaletteWheel::get_angle(float x1, float y1, float x2, float y2)
563 float result = -atan2(x2 - x1, y1 - y2) * (360 / M_PI / 2);
569 PaletteWheelValue::PaletteWheelValue(ColorWindow *window, int x, int y)
570 : BC_SubWindow(x, y, xS(40), yS(170), BLACK)
572 this->window = window;
575 PaletteWheelValue::~PaletteWheelValue()
580 void PaletteWheelValue::create_objects()
582 frame = new VFrame(get_w(), get_h(), BC_RGB888);
583 draw(window->hsv.h, window->hsv.s, window->hsv.v);
587 int PaletteWheelValue::button_press_event()
589 //printf("PaletteWheelValue::button_press 1 %d\n", is_event_win());
590 if( get_cursor_x() >= 0 && get_cursor_x() < get_w() &&
591 get_cursor_y() >= 0 && get_cursor_y() < get_h() &&
593 //printf("PaletteWheelValue::button_press 2\n");
595 cursor_motion_event();
601 int PaletteWheelValue::cursor_motion_event()
603 if( button_down && is_event_win() ) {
604 //printf("PaletteWheelValue::cursor_motion 1\n");
605 float v = 1.0 - (float)(get_cursor_y() - 2) / (get_h() - 4);
606 bclamp(v, 0, 1); window->hsv.v = v;
607 window->update_hsv();
608 window->handle_event();
614 int PaletteWheelValue::button_release_event()
617 //printf("PaletteWheelValue::button_release 1\n");
624 int PaletteWheelValue::draw(float hue, float saturation, float value)
629 for( i = get_h() - 3; i >= 2; i-- ) {
630 unsigned char *row = (unsigned char*)frame->get_rows()[i];
631 HSV::hsv_to_rgb(r_f, g_f, b_f, hue, saturation,
632 1.0 - (float)(i - 2) / (get_h() - 4));
633 r = (int)(r_f * 255);
634 g = (int)(g_f * 255);
635 b = (int)(b_f * 255);
636 for( j = 0; j < get_w(); j++ ) {
642 draw_3d_border(0, 0, get_w(), get_h(), 1);
643 draw_vframe(frame, 2, 2, get_w() - 4, get_h() - 4,
644 2, 2, get_w() - 4, get_h() - 4, 0);
646 draw_line(2, get_h() - 3 - (int)(value * (get_h() - 5)),
647 get_w() - 3, get_h() - 3 - (int)(value * (get_h() - 5)));
648 //printf("PaletteWheelValue::draw %d %f\n", __LINE__, value);
653 PaletteOutput::PaletteOutput(ColorWindow *window, int x, int y)
654 : BC_SubWindow(x, y, xS(180), yS(30), BLACK)
656 this->window = window;
658 PaletteOutput::~PaletteOutput()
663 void PaletteOutput::create_objects()
669 int PaletteOutput::handle_event()
674 int PaletteOutput::draw()
676 set_color(window->rgb888());
677 draw_box(2, 2, get_w() - 4, get_h() - 4);
678 draw_3d_border(0, 0, get_w(), get_h(), 1);
682 PaletteHue::PaletteHue(ColorWindow *window, int x, int y)
683 : BC_ISlider(x, y, 0, xS(150), xS(200), 0, 359, (int)(window->hsv.h), 0)
685 this->window = window;
687 PaletteHue::~PaletteHue()
691 int PaletteHue::handle_event()
693 window->hsv.h = get_value();
694 window->update_hsv();
695 window->handle_event();
699 PaletteSat::PaletteSat(ColorWindow *window, int x, int y)
700 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1.0, window->hsv.s, 0)
702 this->window = window;
705 PaletteSat::~PaletteSat()
709 int PaletteSat::handle_event()
711 window->hsv.s = get_value();
712 window->update_hsv();
713 window->handle_event();
718 PaletteVal::PaletteVal(ColorWindow *window, int x, int y)
719 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1.0, window->hsv.v, 0)
721 this->window = window;
724 PaletteVal::~PaletteVal()
728 int PaletteVal::handle_event()
730 window->hsv.v = get_value();
731 window->update_hsv();
732 window->handle_event();
737 PaletteRed::PaletteRed(ColorWindow *window, int x, int y)
738 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, window->rgb.r, 0)
740 this->window = window;
743 PaletteRed::~PaletteRed()
747 int PaletteRed::handle_event()
749 window->rgb.r = get_value();
750 window->update_rgb();
751 window->handle_event();
755 PaletteGrn::PaletteGrn(ColorWindow *window, int x, int y)
756 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, window->rgb.g, 0)
758 this->window = window;
761 PaletteGrn::~PaletteGrn()
765 int PaletteGrn::handle_event()
767 window->rgb.g = get_value();
768 window->update_rgb();
769 window->handle_event();
773 PaletteBlu::PaletteBlu(ColorWindow *window, int x, int y)
774 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, window->rgb.b, 0)
776 this->window = window;
779 PaletteBlu::~PaletteBlu()
783 int PaletteBlu::handle_event()
785 window->rgb.b = get_value();
786 window->update_rgb();
787 window->handle_event();
791 PaletteAlpha::PaletteAlpha(ColorWindow *window, int x, int y)
792 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, window->aph, 0)
794 this->window = window;
797 PaletteAlpha::~PaletteAlpha()
801 int PaletteAlpha::handle_event()
803 window->aph = get_value();
804 window->aph_a->update(window->aph);
805 window->hex_box->update();
806 window->handle_event();
810 PaletteLum::PaletteLum(ColorWindow *window, int x, int y)
811 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, window->yuv.y, 0)
813 this->window = window;
816 PaletteLum::~PaletteLum()
820 int PaletteLum::handle_event()
822 window->yuv.y = get_value();
823 window->update_yuv();
824 window->handle_event();
828 PaletteCr::PaletteCr(ColorWindow *window, int x, int y)
829 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, window->yuv.u, 0)
831 this->window = window;
834 PaletteCr::~PaletteCr()
838 int PaletteCr::handle_event()
840 window->yuv.u = get_value();
841 window->update_yuv();
842 window->handle_event();
846 PaletteCb::PaletteCb(ColorWindow *window, int x, int y)
847 : BC_FSlider(x, y, 0, xS(150), xS(200), 0, 1, window->yuv.v, 0)
849 this->window = window;
852 PaletteCb::~PaletteCb()
856 int PaletteCb::handle_event()
858 window->yuv.v = get_value();
859 window->update_yuv();
860 window->handle_event();
864 void ColorWindow::update_rgb(float r, float g, float b)
867 YUV::yuv.rgb_to_yuv_f(r, g, b, y, u, v);
869 bclamp(y, 0, 1); yuv.y = y;
870 bclamp(u, 0, 1); yuv.u = u;
871 bclamp(v, 0, 1); yuv.v = v; }
873 HSV::rgb_to_hsv(r,g,b, h,s,v);
874 bclamp(h, 0, 360); hsv.h = h;
875 bclamp(s, 0, 1); hsv.s = s;
876 bclamp(v, 0, 1); hsv.v = v; }
879 void ColorWindow::update_yuv(float y, float u, float v)
883 YUV::yuv.yuv_to_rgb_f(r, g, b, y, u, v);
884 bclamp(r, 0, 1); rgb.r = r;
885 bclamp(g, 0, 1); rgb.g = g;
886 bclamp(b, 0, 1); rgb.b = b;
888 HSV::rgb_to_hsv(r,g,b, h, s, v);
889 bclamp(h, 0, 360); hsv.h = h;
890 bclamp(s, 0, 1); hsv.s = s;
891 bclamp(v, 0, 1); hsv.v = v; }
894 void ColorWindow::update_hsv(float h, float s, float v)
897 HSV::hsv_to_rgb(r,g,b, h,s,v);
898 bclamp(r, 0, 1); rgb.r = r;
899 bclamp(g, 0, 1); rgb.g = g;
900 bclamp(b, 0, 1); rgb.b = b;
902 YUV::yuv.rgb_to_yuv_f(r, g, b, y, u, v);
904 bclamp(y, 0, 1); yuv.y = y;
905 bclamp(u, 0, 1); yuv.u = u;
906 bclamp(v, 0, 1); yuv.v = v; }
909 void ColorWindow::load_history()
911 char history_path[BCTEXTLEN];
912 MWindow::create_defaults_path(history_path,PALETTE_DATA);
913 FILE *fp = fopen(history_path,"r");
916 while( i < PALLETTE_HISTORY_SIZE ) {
918 if( !fgets(line,sizeof(line)-1,fp) ) break;
919 line[sizeof(line)-1] = 0;
920 if( sscanf(line, "%x",&palette_history[i]) != 1 ) break;
925 int r = 0, g = 0, b = 0;
926 float v0 = 0, v1 = 1;
927 while( i < PALLETTE_HISTORY_SIZE ) {
928 int grey_code = i ^ (i>>1);
929 r = 255 * ((grey_code&4) ? v0 : v1);
930 g = 255 * ((grey_code&2) ? v0 : v1);
931 b = 255 * ((grey_code&1) ? v0 : v1);
932 int color = (r<<16) | (g<<8) | (b<<0);
933 palette_history[i++] = color;
934 if( i & 7 ) continue;
935 v0 = 0.5f * (v0+.5f);
936 v1 = 0.5f * (v1+.5f);
939 void ColorWindow::save_history()
941 char history_path[BCTEXTLEN];
942 MWindow::create_defaults_path(history_path,PALETTE_DATA);
943 FILE *fp = fopen(history_path,"w");
945 for( int i=0; i<PALLETTE_HISTORY_SIZE; ++i ) {
946 fprintf(fp, "%06x\n", palette_history[i]);
951 void ColorWindow::update_history(int color)
953 int out = palette_history[0];
954 palette_history[0] = color;
955 for( int i=1; out != color && i<PALLETTE_HISTORY_SIZE; ++i ) {
957 out = palette_history[i];
958 palette_history[i] = in;
961 void ColorWindow::update_history()
963 update_history(rgb888());
966 int ColorWindow::rgb888()
968 int r = 255*rgb.r + 0.5, g = 255*rgb.g + 0.5, b = 255*rgb.b + 0.5;
969 bclamp(r, 0, 255); bclamp(g, 0, 255); bclamp(b, 0, 255);
970 return (r<<16) | (g<<8) | (b<<0);
972 int ColorWindow::alpha8()
974 int a = 255*aph + 0.5;
979 PaletteNum::PaletteNum(ColorWindow *window, int x, int y,
980 float &output, float min, float max)
981 : BC_TumbleTextBox(window, output, min, max, x, y, xS(64))
983 this->window = window;
984 this->output = &output;
989 PaletteNum::~PaletteNum()
994 int PaletteHSV::handle_event()
997 window->update_hsv();
998 window->handle_event();
1002 int PaletteRGB::handle_event()
1005 window->update_rgb();
1006 window->handle_event();
1010 int PaletteYUV::handle_event()
1013 window->update_yuv();
1014 window->handle_event();
1018 int PaletteAPH::handle_event()
1021 window->update_display();
1022 window->handle_event();
1026 PaletteHexButton::PaletteHexButton(ColorWindow *window, int x, int y)
1027 : BC_GenericButton(x, y, xS(50), "#")
1029 this->window = window;
1030 set_tooltip(_("hex rgb color"));
1032 PaletteHexButton::~PaletteHexButton()
1035 int PaletteHexButton::handle_event()
1037 const char *hex = window->hex_box->get_text();
1038 window->update_rgb_hex(hex);
1042 PaletteHex::PaletteHex(ColorWindow *window, int x, int y, const char *hex)
1043 : BC_TextBox(x, y, xS(100), 1, hex)
1045 this->window = window;
1047 PaletteHex::~PaletteHex()
1050 void PaletteHex::update()
1052 char hex[BCSTRLEN], *cp = hex;
1053 if( window->thread->do_alpha )
1054 cp += sprintf(cp,"%02x", window->alpha8());
1055 sprintf(cp,"%06x",window->rgb888());
1056 BC_TextBox::update(hex);
1059 int PaletteHex::keypress_event()
1061 if( get_keypress() != RETURN )
1062 return BC_TextBox::keypress_event();
1063 window->update_rgb_hex(get_text());
1067 #include "grabpick_up_png.h"
1068 #include "grabpick_hi_png.h"
1069 #include "grabpick_dn_png.h"
1071 PaletteGrabButton::PaletteGrabButton(ColorWindow *window, int x, int y)
1072 : BC_Button(x, y, vframes)
1074 this->window = window;
1075 vframes[0] = new VFramePng(grabpick_up_png);
1076 vframes[1] = new VFramePng(grabpick_hi_png);
1077 vframes[2] = new VFramePng(grabpick_dn_png);
1078 set_tooltip(_("grab from anywhere picker"));
1080 PaletteGrabButton::~PaletteGrabButton()
1082 for( int i=0; i<3; ++i )
1085 int PaletteGrabButton::handle_event()
1087 if( window->grab_buttons() ) {
1089 window->button_grabbed = 1;
1090 button_press_event(); // redraw face HI
1095 PaletteHistory::PaletteHistory(ColorWindow *window, int x, int y)
1096 : BC_SubWindow(x,y, xS(200), yS(24))
1098 this->window = window;
1100 set_tooltip(_("color history"));
1102 PaletteHistory::~PaletteHistory()
1105 void PaletteHistory::update(int flush)
1108 for( int i=0; i<PALLETTE_HISTORY_SIZE; x1=x2 ) {
1109 int rgb = window->palette_history[i];
1110 x2 = (++i * get_w())/PALLETTE_HISTORY_SIZE;
1111 draw_3d_box(x1,0,x2-x1,get_h(),WHITE,BLACK,rgb,LTBLUE,DKBLUE);
1116 int PaletteHistory::button_press_event()
1118 if( button_down || !is_event_win() ) return 0;
1120 cursor_motion_event();
1123 int PaletteHistory::button_release_event()
1125 if( !button_down || !is_event_win() ) return 0;
1126 cursor_motion_event();
1127 if( button_down > 0 ) {
1128 window->handle_event();
1129 window->update_display();
1130 window->update_history();
1135 int PaletteHistory::cursor_motion_event()
1137 if( !button_down || !is_event_win() ) return 0;
1139 int pick = (PALLETTE_HISTORY_SIZE * get_cursor_x()) / get_w();
1140 bclamp(pick, 0, PALLETTE_HISTORY_SIZE-1);
1141 int color = window->palette_history[pick];
1142 float r = ((color>>16) & 0xff) / 255.;
1143 float g = ((color>>8) & 0xff) / 255.;
1144 float b = ((color>>0) & 0xff) / 255.;
1145 if( window->rgb.r != r || window->rgb.g != g || window->rgb.b != b ) {
1146 window->rgb.r = r; window->rgb.g = g; window->rgb.b = b;
1147 window->update_rgb();
1152 int PaletteHistory::cursor_leave_event()
1157 int PaletteHistory::repeat_event(int64_t duration)
1161 if( duration == get_resources()->tooltip_delay &&
1162 get_tooltip() && *get_tooltip() && cursor_above() ) {
1170 ColorButton::ColorButton(const char *title,
1171 int x, int y, int w, int h,
1172 int color, int alpha, int ok_cancel)
1173 : BC_Button(x, y, w, vframes)
1175 this->title = title;
1176 this->color = this->orig_color = color;
1177 this->alpha = this->orig_alpha = alpha;
1178 this->ok_cancel = ok_cancel;
1180 for( int i=0; i<3; ++i ) {
1181 vframes[i] = new VFrame(w, h, BC_RGBA8888);
1182 vframes[i]->clear_frame();
1188 ColorButton::~ColorButton()
1190 delete color_thread;
1191 delete color_picker;
1192 for( int i=0; i<3; ++i )
1196 void ColorButton::set_color(int color)
1198 printf("ColorButton::set_color %06x\n", color);
1200 void ColorButton::handle_done_event(int result)
1202 color_thread->stop();
1204 int ColorButton::handle_new_color(int color, int alpha)
1206 printf("ColorButton::handle_new_color %02x%06x\n", alpha, color);
1210 int ColorButton::handle_event()
1213 delete color_picker;
1214 color_picker = new ColorButtonPicker(this);
1215 orig_color = color; orig_alpha = alpha;
1216 color_picker->start_window(color, alpha, ok_cancel);
1218 color_thread = new ColorButtonThread(this);
1219 color_thread->start();
1220 lock_window("ColorButtonButton::start_color_thread");
1224 void ColorButton::close_picker()
1226 if( color_thread ) color_thread->stop();
1227 delete color_thread; color_thread = 0;
1228 delete color_picker; color_picker = 0;
1231 void ColorButton::update_gui(int color, int alpha)
1234 color_picker->update_gui(color, alpha);
1235 update_gui(color | (~alpha<<24));
1238 void ColorButton::update_gui(int color)
1244 ColorButtonPicker::ColorButtonPicker(ColorButton *color_button)
1245 : ColorPicker(color_button->alpha >= 0 ? 1 : 0, color_button->title)
1247 this->color_button = color_button;
1250 ColorButtonPicker::~ColorButtonPicker()
1254 void ColorButtonPicker::handle_done_event(int result)
1256 color_button->color_thread->stop();
1257 color_button->handle_done_event(result);
1260 void ColorButtonPicker::update(int color, int alpha)
1262 color_button->color = color;
1263 color_button->alpha = alpha;
1264 color_button->color_thread->update_lock->unlock();
1267 int ColorButtonPicker::handle_new_color(int color, int alpha)
1269 color_button->lock_window("ColorButtonPicker::handle_new_color");
1270 color_button->update_gui(color, alpha);
1271 color_button->unlock_window();
1272 return color_button->handle_new_color(color, alpha);
1275 void ColorButtonPicker::update_gui()
1277 color_button->lock_window("ColorButtonPicker::update_gui");
1278 color_button->update_gui(color_button->color, color_button->alpha);
1279 color_button->unlock_window();
1282 void ColorButtonPicker::update_gui(int color, int alpha)
1284 ColorPicker::update_gui(color, alpha);
1285 color_button->handle_new_color(color, alpha);
1288 ColorButtonThread::ColorButtonThread(ColorButton *color_button)
1291 this->color_button = color_button;
1292 this->update_lock = new Condition(0,"ColorButtonThread::update_lock");
1296 ColorButtonThread::~ColorButtonThread()
1302 void ColorButtonThread::start()
1310 void ColorButtonThread::stop()
1314 update_lock->unlock();
1319 void ColorButtonThread::run()
1321 ColorButtonPicker *color_picker = color_button->color_picker;
1322 color_picker->update_gui();
1324 update_lock->lock("ColorButtonThread::run");
1326 color_picker->update_gui();
1331 ColorBoxButton::ColorBoxButton(const char *title,
1332 int x, int y, int w, int h,
1333 int color, int alpha, int ok_cancel)
1334 : ColorButton(title, x, y, w, h, color, alpha, ok_cancel)
1337 ColorBoxButton::~ColorBoxButton()
1341 int ColorBoxButton::handle_new_color(int color, int alpha)
1343 return ColorButton::handle_new_color(color, alpha);
1345 void ColorBoxButton::handle_done_event(int result)
1347 ColorButton::handle_done_event(result);
1349 void ColorBoxButton::create_objects()
1351 update_gui(color, alpha);
1354 void ColorBoxButton::set_color(int color)
1356 this->color = (color & 0xffffff);
1357 this->alpha = (~color>>24) & 0xff;
1358 int r = (color>>16) & 0xff;
1359 int g = (color>> 8) & 0xff;
1360 int b = (color>> 0) & 0xff;
1361 int color_model = vframes[0]->get_color_model();
1362 int bpp = BC_CModels::calculate_pixelsize(color_model);
1363 for( int i=0; i<3; ++i ) {
1364 VFrame *vframe = vframes[i];
1365 int ww = vframe->get_w(), hh = vframe->get_h();
1366 uint8_t **rows = vframe->get_rows();
1367 int rr = r, gg = g, bb = b;
1372 if( (rr+=48) > 0xff ) rr = 0xff;
1373 if( (gg+=48) > 0xff ) gg = 0xff;
1374 if( (bb+=48) > 0xff ) bb = 0xff;
1377 if( (rr-=48) < 0x00 ) rr = 0x00;
1378 if( (gg-=48) < 0x00 ) gg = 0x00;
1379 if( (bb-=48) < 0x00 ) bb = 0x00;
1382 for( int y=0; y<hh; ++y ) {
1383 uint8_t *rp = rows[y];
1384 for( int x=0; x<ww; ++x ) {
1385 rp[0] = rr; rp[1] = gg; rp[2] = bb;
1386 if( bpp > 3 ) rp[3] = 0xff;
1391 set_images(vframes);
1394 ColorCircleButton::ColorCircleButton(const char *title,
1395 int x, int y, int w, int h,
1396 int color, int alpha, int ok_cancel)
1397 : ColorButton(title, x, y, w, h, color, alpha, ok_cancel)
1400 ColorCircleButton::~ColorCircleButton()
1403 int ColorCircleButton::handle_new_color(int color, int alpha)
1405 return ColorButton::handle_new_color(color, alpha);
1407 void ColorCircleButton::handle_done_event(int result)
1409 ColorButton::handle_done_event(result);
1411 void ColorCircleButton::create_objects()
1413 update_gui(color, alpha);
1416 void ColorCircleButton::set_color(int color)
1418 this->color = (color & 0xffffff);
1419 this->alpha = (~color>>24) & 0xff;
1420 int r = (color>>16) & 0xff;
1421 int g = (color>>8) & 0xff;
1422 int b = (color>>0) & 0xff;
1423 for( int i=0; i<3; ++i ) {
1424 VFrame *vframe = vframes[i];
1425 int ww = vframe->get_w(), hh = vframe->get_h();
1426 int cx = (ww+1)/2, cy = hh/2;
1427 double cc = (cx*cx + cy*cy) / 4.;
1428 uint8_t *bp = vframe->get_data(), *dp = bp;
1429 uint8_t *ep = dp + vframe->get_data_size();
1430 int rr = r, gg = g, bb = b;
1431 int bpl = vframe->get_bytes_per_line();
1436 if( (rr+=48) > 0xff ) rr = 0xff;
1437 if( (gg+=48) > 0xff ) gg = 0xff;
1438 if( (bb+=48) > 0xff ) bb = 0xff;
1441 if( (rr-=48) < 0x00 ) rr = 0x00;
1442 if( (gg-=48) < 0x00 ) gg = 0x00;
1443 if( (bb-=48) < 0x00 ) bb = 0x00;
1447 int yy = (dp-bp) / bpl, xx = ((dp-bp) % bpl) >> 2;
1448 int dy = cy - yy, dx = cx - xx;
1449 double s = dx*dx + dy*dy - cc;
1450 double ss = s < 0 ? 1 : s >= cc ? 0 : 1 - s/cc;
1452 *dp++ = rr; *dp++ = gg; *dp++ = bb; *dp++ = aa;
1455 set_images(vframes);