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() + 25;
81 int y = display_info.get_abs_cursor_y() - 100;
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 x0 = 10, y0 = 10;
152 lock_window("ColorWindow::create_objects");
156 add_tool(wheel = new PaletteWheel(this, x, y));
157 wheel->create_objects();
159 x += 180; add_tool(wheel_value = new PaletteWheelValue(this, x, y));
160 wheel_value->create_objects();
162 y += 180; add_tool(output = new PaletteOutput(this, x, y));
163 output->create_objects();
164 y += output->get_h() + 20;
166 load_history(); int x1 = x;
167 add_tool(hex_btn = new PaletteHexButton(this, x1, y));
168 char hex[BCSTRLEN]; sprintf(hex,"%06x",thread->output);
169 x1 += hex_btn->get_w() + 5;
170 add_tool(hex_box = new PaletteHex(this, x1, y, hex));
171 x1 += hex_box->get_w() + 15;
172 add_tool(grab_btn = new PaletteGrabButton(this, x1, y));
173 y += hex_box->get_h() + 15;
174 add_tool(history = new PaletteHistory(this, 10, y));
177 add_tool(new BC_Title(x, y =y0, C_("H:"), SMALLFONT));
178 add_tool(new BC_Title(x, y+=25, C_("S:"), SMALLFONT));
179 add_tool(new BC_Title(x, y+=25, D_("colorpicker_value#V:"), SMALLFONT));
180 add_tool(new BC_Title(x, y+=40, C_("R:"), SMALLFONT));
181 add_tool(new BC_Title(x, y+=25, C_("G:"), SMALLFONT));
182 add_tool(new BC_Title(x, y+=25, C_("B:"), SMALLFONT));
183 add_tool(new BC_Title(x, y+=40, C_("Y:"), SMALLFONT));
184 add_tool(new BC_Title(x, y+=25, C_("U:"), SMALLFONT));
185 add_tool(new BC_Title(x, y+=25, D_("colorpicker_Cr#V:"), SMALLFONT));
186 if( thread->do_alpha )
187 add_tool(new BC_Title(x, y+=40, C_("A:"), SMALLFONT));
189 add_tool(hue = new PaletteHue(this, x, y= y0));
190 add_tool(sat = new PaletteSat(this, x, y+=25));
191 add_tool(val = new PaletteVal(this, x, y+=25));
192 add_tool(red = new PaletteRed(this, x, y+=40));
193 add_tool(grn = new PaletteGrn(this, x, y+=25));
194 add_tool(blu = new PaletteBlu(this, x, y+=25));
195 add_tool(lum = new PaletteLum(this, x, y+=40));
196 add_tool(c_r = new PaletteCr (this, x, y+=25));
197 add_tool(c_b = new PaletteCb (this, x, y+=25));
198 if( thread->do_alpha )
199 add_tool(alpha = new PaletteAlpha(this, x, y+=40));
201 x += hue->get_w() + 10;
202 hsv_h = new PaletteHSV(this, x,y= y0, hsv.h, 0, 360);
203 hsv_h->create_objects(); hsv_h->set_tooltip(_("Hue"));
204 hsv_s = new PaletteHSV(this, x,y+=25, hsv.s, 0, 1);
205 hsv_s->create_objects(); hsv_s->set_tooltip(_("Saturation"));
206 hsv_v = new PaletteHSV(this, x,y+=25, hsv.v, 0, 1);
207 hsv_v->create_objects(); hsv_v->set_tooltip(_("Value"));
208 rgb_r = new PaletteRGB(this, x,y+=40, rgb.r, 0, 1);
209 rgb_r->create_objects(); rgb_r->set_tooltip(_("Red"));
210 rgb_g = new PaletteRGB(this, x,y+=25, rgb.g, 0, 1);
211 rgb_g->create_objects(); rgb_g->set_tooltip(_("Green"));
212 rgb_b = new PaletteRGB(this, x,y+=25, rgb.b, 0, 1);
213 rgb_b->create_objects(); rgb_b->set_tooltip(_("Blue"));
214 yuv_y = new PaletteYUV(this, x,y+=40, yuv.y, 0, 1);
215 yuv_y->create_objects(); yuv_y->set_tooltip(_("Luminance"));
216 yuv_u = new PaletteYUV(this, x,y+=25, yuv.u, 0, 1);
217 yuv_u->create_objects(); yuv_u->set_tooltip(_("Blue Luminance Difference"));
218 yuv_v = new PaletteYUV(this, x,y+=25, yuv.v, 0, 1);
219 yuv_v->create_objects(); yuv_v->set_tooltip(_("Red Luminance Difference"));
220 if( thread->do_alpha ) {
221 aph_a = new PaletteAPH(this, x,y+=40, aph, 0, 1);
222 aph_a->create_objects(); aph_a->set_tooltip(_("Alpha"));
224 if( thread->ok_cancel ) {
225 add_tool(new BC_OKButton(this));
226 add_tool(new BC_CancelButton(this));
228 thread->create_objects(this);
237 void ColorWindow::change_values()
239 float r = ((thread->output>>16) & 0xff) / 255.;
240 float g = ((thread->output>>8) & 0xff) / 255.;
241 float b = ((thread->output>>0) & 0xff) / 255.;
242 rgb.r = r; rgb.g = g; rgb.b = b;
243 aph = (float)thread->alpha / 255;
244 update_rgb(rgb.r, rgb.g, rgb.b);
248 int ColorWindow::close_event()
250 set_done(thread->ok_cancel ? 1 : 0);
255 void ColorWindow::update_rgb()
257 update_rgb(rgb.r, rgb.g, rgb.b);
260 void ColorWindow::update_hsv()
262 update_hsv(hsv.h, hsv.s, hsv.v);
265 void ColorWindow::update_yuv()
267 update_yuv(yuv.y, yuv.u, yuv.v);
271 void ColorWindow::update_display()
273 wheel->draw(wheel->oldhue, wheel->oldsaturation);
274 wheel->oldhue = hsv.h;
275 wheel->oldsaturation = hsv.s;
276 wheel->draw(hsv.h, hsv.s);
278 wheel_value->draw(hsv.h, hsv.s, hsv.v);
279 wheel_value->flash();
283 hue->update((int)hsv.h);
295 hsv_h->update(hsv.h);
296 hsv_s->update(hsv.s);
297 hsv_v->update(hsv.v);
298 rgb_r->update(rgb.r);
299 rgb_g->update(rgb.g);
300 rgb_b->update(rgb.b);
301 yuv_y->update(yuv.y);
302 yuv_u->update(yuv.u);
303 yuv_v->update(yuv.v);
306 if( thread->do_alpha ) {
312 int ColorWindow::handle_event()
315 thread->handle_new_color(rgb888(), alpha8());
316 lock_window("ColorWindow::handle_event");
320 void ColorWindow::get_screen_sample()
323 get_abs_cursor(cx, cy);
324 BC_Capture capture_bitmap(1, 1, 0);
325 VFrame vframe(1,1,BC_RGB888);
326 capture_bitmap.capture_frame(&vframe, cx,cy);
327 unsigned char *data = vframe.get_data();
328 rgb.r = data[0]/255.; rgb.g = data[1]/255.; rgb.b = data[2]/255.;
332 int ColorWindow::cursor_motion_event()
334 if( button_grabbed && get_button_down() ) {
341 int ColorWindow::button_press_event()
343 if( button_grabbed ) {
350 int ColorWindow::button_release_event()
352 if( button_grabbed ) {
358 return handle_event();
363 void ColorWindow::update_rgb_hex(const char *hex)
366 if( sscanf(hex,"%x",&color) == 1 ) {
367 if( thread->do_alpha ) {
368 aph = ((color>>24) & 0xff) / 255.;
371 float r = ((color>>16) & 0xff) / 255.;
372 float g = ((color>>8) & 0xff) / 255.;
373 float b = ((color>>0) & 0xff) / 255.;
374 rgb.r = r; rgb.g = g; rgb.b = b;
382 PaletteWheel::PaletteWheel(ColorWindow *window, int x, int y)
383 : BC_SubWindow(x, y, 170, 170)
385 this->window = window;
391 PaletteWheel::~PaletteWheel()
395 int PaletteWheel::button_press_event()
397 if( get_cursor_x() >= 0 && get_cursor_x() < get_w() &&
398 get_cursor_y() >= 0 && get_cursor_y() < get_h() &&
401 cursor_motion_event();
407 int PaletteWheel::cursor_motion_event()
409 int x1, y1, distance;
410 if( button_down && is_event_win() ) {
411 float h = get_angle(get_w()/2, get_h()/2, get_cursor_x(), get_cursor_y());
412 bclamp(h, 0, 359.999); window->hsv.h = h;
413 x1 = get_w() / 2 - get_cursor_x();
414 y1 = get_h() / 2 - get_cursor_y();
415 distance = (int)sqrt(x1 * x1 + y1 * y1);
416 float s = (float)distance / (get_w() / 2);
417 bclamp(s, 0, 1); window->hsv.s = s;
419 window->update_hsv();
420 window->handle_event();
426 int PaletteWheel::button_release_event()
435 void PaletteWheel::create_objects()
438 //printf("PaletteWheel::create_objects 1\n");
441 float x1, y1, x2, y2;
443 int default_r, default_g, default_b;
444 VFrame frame(0, -1, get_w(), get_h(), BC_RGBA8888, -1);
447 default_r = (get_resources()->get_bg_color() & 0xff0000) >> 16;
448 default_g = (get_resources()->get_bg_color() & 0xff00) >> 8;
449 default_b = (get_resources()->get_bg_color() & 0xff);
450 //printf("PaletteWheel::create_objects 1\n");
452 int highlight_r = (get_resources()->button_light & 0xff0000) >> 16;
453 int highlight_g = (get_resources()->button_light & 0xff00) >> 8;
454 int highlight_b = (get_resources()->button_light & 0xff);
456 for( y2 = 0; y2 < get_h(); y2++ ) {
457 unsigned char *row = (unsigned char*)frame.get_rows()[(int)y2];
458 for( x2 = 0; x2 < get_w(); x2++ ) {
459 distance = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
460 if( distance > x1 ) {
461 row[(int)x2 * 4] = default_r;
462 row[(int)x2 * 4 + 1] = default_g;
463 row[(int)x2 * 4 + 2] = default_b;
464 row[(int)x2 * 4 + 3] = 0;
467 if( distance > x1 - 1 ) {
469 if( get_h() - y2 < x2 ) {
480 row[(int)x2 * 4] = r_i;
481 row[(int)x2 * 4 + 1] = g_i;
482 row[(int)x2 * 4 + 2] = b_i;
483 row[(int)x2 * 4 + 3] = 255;
486 h = get_angle(x1, y1, x2, y2);
488 HSV::hsv_to_rgb(r, g, b, h, s, v);
489 row[(int)x2 * 4] = (int)(r * 255);
490 row[(int)x2 * 4 + 1] = (int)(g * 255);
491 row[(int)x2 * 4 + 2] = (int)(b * 255);
492 row[(int)x2 * 4 + 3] = 255;
496 //printf("PaletteWheel::create_objects 1\n");
508 //printf("PaletteWheel::create_objects 1\n");
510 oldhue = window->hsv.h;
511 oldsaturation = window->hsv.s;
512 //printf("PaletteWheel::create_objects 1\n");
513 draw(oldhue, oldsaturation);
514 //printf("PaletteWheel::create_objects 1\n");
516 //printf("PaletteWheel::create_objects 2\n");
519 float PaletteWheel::torads(float angle)
521 return (float)angle / 360 * 2 * M_PI;
525 int PaletteWheel::draw(float hue, float saturation)
531 if( hue > 0 && hue < 90 ) {
532 x = (int)(w - w * cos(torads(90 - hue)) * saturation);
533 y = (int)(h - h * sin(torads(90 - hue)) * saturation);
535 else if( hue > 90 && hue < 180 ) {
536 x = (int)(w - w * cos(torads(hue - 90)) * saturation);
537 y = (int)(h + h * sin(torads(hue - 90)) * saturation);
539 else if( hue > 180 && hue < 270 ) {
540 x = (int)(w + w * cos(torads(270 - hue)) * saturation);
541 y = (int)(h + h * sin(torads(270 - hue)) * saturation);
543 else if( hue > 270 && hue < 360 ) {
544 x = (int)(w + w * cos(torads(hue - 270)) * saturation);
545 y = (int)(h - w * sin(torads(hue - 270)) * saturation);
547 else if( hue == 0 ) {
549 y = (int)(h - h * saturation);
551 else if( hue == 90 ) {
552 x = (int)(w - w * saturation);
555 else if( hue == 180 ) {
557 y = (int)(h + h * saturation);
559 else if( hue == 270 ) {
560 x = (int)(w + w * saturation);
566 draw_circle(x - 5, y - 5, 10, 10);
571 int PaletteWheel::get_angle(float x1, float y1, float x2, float y2)
573 float result = -atan2(x2 - x1, y1 - y2) * (360 / M_PI / 2);
579 PaletteWheelValue::PaletteWheelValue(ColorWindow *window, int x, int y)
580 : BC_SubWindow(x, y, 40, 170, BLACK)
582 this->window = window;
585 PaletteWheelValue::~PaletteWheelValue()
590 void PaletteWheelValue::create_objects()
592 frame = new VFrame(get_w(), get_h(), BC_RGB888);
593 draw(window->hsv.h, window->hsv.s, window->hsv.v);
597 int PaletteWheelValue::button_press_event()
599 //printf("PaletteWheelValue::button_press 1 %d\n", is_event_win());
600 if( get_cursor_x() >= 0 && get_cursor_x() < get_w() &&
601 get_cursor_y() >= 0 && get_cursor_y() < get_h() &&
603 //printf("PaletteWheelValue::button_press 2\n");
605 cursor_motion_event();
611 int PaletteWheelValue::cursor_motion_event()
613 if( button_down && is_event_win() ) {
614 //printf("PaletteWheelValue::cursor_motion 1\n");
615 float v = 1.0 - (float)(get_cursor_y() - 2) / (get_h() - 4);
616 bclamp(v, 0, 1); window->hsv.v = v;
617 window->update_hsv();
618 window->handle_event();
624 int PaletteWheelValue::button_release_event()
627 //printf("PaletteWheelValue::button_release 1\n");
634 int PaletteWheelValue::draw(float hue, float saturation, float value)
639 for( i = get_h() - 3; i >= 2; i-- ) {
640 unsigned char *row = (unsigned char*)frame->get_rows()[i];
641 HSV::hsv_to_rgb(r_f, g_f, b_f, hue, saturation,
642 1.0 - (float)(i - 2) / (get_h() - 4));
643 r = (int)(r_f * 255);
644 g = (int)(g_f * 255);
645 b = (int)(b_f * 255);
646 for( j = 0; j < get_w(); j++ ) {
653 draw_3d_border(0, 0, get_w(), get_h(), 1);
654 draw_vframe(frame, 2, 2, get_w() - 4, get_h() - 4,
655 2, 2, get_w() - 4, get_h() - 4, 0);
657 draw_line(2, get_h() - 3 - (int)(value * (get_h() - 5)),
658 get_w() - 3, get_h() - 3 - (int)(value * (get_h() - 5)));
659 //printf("PaletteWheelValue::draw %d %f\n", __LINE__, value);
664 PaletteOutput::PaletteOutput(ColorWindow *window, int x, int y)
665 : BC_SubWindow(x, y, 180, 30, BLACK)
667 this->window = window;
669 PaletteOutput::~PaletteOutput()
674 void PaletteOutput::create_objects()
680 int PaletteOutput::handle_event()
685 int PaletteOutput::draw()
687 set_color(window->rgb888());
688 draw_box(2, 2, get_w() - 4, get_h() - 4);
689 draw_3d_border(0, 0, get_w(), get_h(), 1);
693 PaletteHue::PaletteHue(ColorWindow *window, int x, int y)
694 : BC_ISlider(x, y, 0, 150, 200, 0, 359, (int)(window->hsv.h), 0)
696 this->window = window;
698 PaletteHue::~PaletteHue()
702 int PaletteHue::handle_event()
704 window->hsv.h = get_value();
705 window->update_hsv();
706 window->handle_event();
710 PaletteSat::PaletteSat(ColorWindow *window, int x, int y)
711 : BC_FSlider(x, y, 0, 150, 200, 0, 1.0, window->hsv.s, 0)
713 this->window = window;
716 PaletteSat::~PaletteSat()
720 int PaletteSat::handle_event()
722 window->hsv.s = get_value();
723 window->update_hsv();
724 window->handle_event();
729 PaletteVal::PaletteVal(ColorWindow *window, int x, int y)
730 : BC_FSlider(x, y, 0, 150, 200, 0, 1.0, window->hsv.v, 0)
732 this->window = window;
735 PaletteVal::~PaletteVal()
739 int PaletteVal::handle_event()
741 window->hsv.v = get_value();
742 window->update_hsv();
743 window->handle_event();
748 PaletteRed::PaletteRed(ColorWindow *window, int x, int y)
749 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->rgb.r, 0)
751 this->window = window;
754 PaletteRed::~PaletteRed()
758 int PaletteRed::handle_event()
760 window->rgb.r = get_value();
761 window->update_rgb();
762 window->handle_event();
766 PaletteGrn::PaletteGrn(ColorWindow *window, int x, int y)
767 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->rgb.g, 0)
769 this->window = window;
772 PaletteGrn::~PaletteGrn()
776 int PaletteGrn::handle_event()
778 window->rgb.g = get_value();
779 window->update_rgb();
780 window->handle_event();
784 PaletteBlu::PaletteBlu(ColorWindow *window, int x, int y)
785 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->rgb.b, 0)
787 this->window = window;
790 PaletteBlu::~PaletteBlu()
794 int PaletteBlu::handle_event()
796 window->rgb.b = get_value();
797 window->update_rgb();
798 window->handle_event();
802 PaletteAlpha::PaletteAlpha(ColorWindow *window, int x, int y)
803 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->aph, 0)
805 this->window = window;
808 PaletteAlpha::~PaletteAlpha()
812 int PaletteAlpha::handle_event()
814 window->aph = get_value();
815 window->aph_a->update(window->aph);
816 window->hex_box->update();
817 window->handle_event();
821 PaletteLum::PaletteLum(ColorWindow *window, int x, int y)
822 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->yuv.y, 0)
824 this->window = window;
827 PaletteLum::~PaletteLum()
831 int PaletteLum::handle_event()
833 window->yuv.y = get_value();
834 window->update_yuv();
835 window->handle_event();
839 PaletteCr::PaletteCr(ColorWindow *window, int x, int y)
840 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->yuv.u, 0)
842 this->window = window;
845 PaletteCr::~PaletteCr()
849 int PaletteCr::handle_event()
851 window->yuv.u = get_value();
852 window->update_yuv();
853 window->handle_event();
857 PaletteCb::PaletteCb(ColorWindow *window, int x, int y)
858 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->yuv.v, 0)
860 this->window = window;
863 PaletteCb::~PaletteCb()
867 int PaletteCb::handle_event()
869 window->yuv.v = get_value();
870 window->update_yuv();
871 window->handle_event();
875 void ColorWindow::update_rgb(float r, float g, float b)
878 YUV::yuv.rgb_to_yuv_f(r, g, b, y, u, v);
880 bclamp(y, 0, 1); yuv.y = y;
881 bclamp(u, 0, 1); yuv.u = u;
882 bclamp(v, 0, 1); yuv.v = v; }
884 HSV::rgb_to_hsv(r,g,b, h,s,v);
885 bclamp(h, 0, 360); hsv.h = h;
886 bclamp(s, 0, 1); hsv.s = s;
887 bclamp(v, 0, 1); hsv.v = v; }
890 void ColorWindow::update_yuv(float y, float u, float v)
894 YUV::yuv.yuv_to_rgb_f(r, g, b, y, u, v);
895 bclamp(r, 0, 1); rgb.r = r;
896 bclamp(g, 0, 1); rgb.g = g;
897 bclamp(b, 0, 1); rgb.b = b;
899 HSV::rgb_to_hsv(r,g,b, h, s, v);
900 bclamp(h, 0, 360); hsv.h = h;
901 bclamp(s, 0, 1); hsv.s = s;
902 bclamp(v, 0, 1); hsv.v = v; }
905 void ColorWindow::update_hsv(float h, float s, float v)
908 HSV::hsv_to_rgb(r,g,b, h,s,v);
909 bclamp(r, 0, 1); rgb.r = r;
910 bclamp(g, 0, 1); rgb.g = g;
911 bclamp(b, 0, 1); rgb.b = b;
913 YUV::yuv.rgb_to_yuv_f(r, g, b, y, u, v);
915 bclamp(y, 0, 1); yuv.y = y;
916 bclamp(u, 0, 1); yuv.u = u;
917 bclamp(v, 0, 1); yuv.v = v; }
920 void ColorWindow::load_history()
922 char history_path[BCTEXTLEN];
923 MWindow::create_defaults_path(history_path,PALETTE_DATA);
924 FILE *fp = fopen(history_path,"r");
927 while( i < PALLETTE_HISTORY_SIZE ) {
929 if( !fgets(line,sizeof(line)-1,fp) ) break;
930 line[sizeof(line)-1] = 0;
931 if( sscanf(line, "%x",&palette_history[i]) != 1 ) break;
936 int r = 0, g = 0, b = 0;
937 float v0 = 0, v1 = 1;
938 while( i < PALLETTE_HISTORY_SIZE ) {
939 int grey_code = i ^ (i>>1);
940 r = 255 * ((grey_code&4) ? v0 : v1);
941 g = 255 * ((grey_code&2) ? v0 : v1);
942 b = 255 * ((grey_code&1) ? v0 : v1);
943 int color = (r<<16) | (g<<8) | (b<<0);
944 palette_history[i++] = color;
945 if( i & 7 ) continue;
946 v0 = 0.5f * (v0+.5f);
947 v1 = 0.5f * (v1+.5f);
950 void ColorWindow::save_history()
952 char history_path[BCTEXTLEN];
953 MWindow::create_defaults_path(history_path,PALETTE_DATA);
954 FILE *fp = fopen(history_path,"w");
956 for( int i=0; i<PALLETTE_HISTORY_SIZE; ++i ) {
957 fprintf(fp, "%06x\n", palette_history[i]);
962 void ColorWindow::update_history(int color)
964 int out = palette_history[0];
965 palette_history[0] = color;
966 for( int i=1; out != color && i<PALLETTE_HISTORY_SIZE; ++i ) {
968 out = palette_history[i];
969 palette_history[i] = in;
972 void ColorWindow::update_history()
974 update_history(rgb888());
977 int ColorWindow::rgb888()
979 int r = 255*rgb.r + 0.5, g = 255*rgb.g + 0.5, b = 255*rgb.b + 0.5;
980 bclamp(r, 0, 255); bclamp(g, 0, 255); bclamp(b, 0, 255);
981 return (r<<16) | (g<<8) | (b<<0);
983 int ColorWindow::alpha8()
985 int a = 255*aph + 0.5;
990 PaletteNum::PaletteNum(ColorWindow *window, int x, int y,
991 float &output, float min, float max)
992 : BC_TumbleTextBox(window, output, min, max, x, y, 64)
994 this->window = window;
995 this->output = &output;
1000 PaletteNum::~PaletteNum()
1005 int PaletteHSV::handle_event()
1008 window->update_hsv();
1009 window->handle_event();
1013 int PaletteRGB::handle_event()
1016 window->update_rgb();
1017 window->handle_event();
1021 int PaletteYUV::handle_event()
1024 window->update_yuv();
1025 window->handle_event();
1029 int PaletteAPH::handle_event()
1032 window->update_display();
1033 window->handle_event();
1037 PaletteHexButton::PaletteHexButton(ColorWindow *window, int x, int y)
1038 : BC_GenericButton(x, y, 50, "#")
1040 this->window = window;
1041 set_tooltip(_("hex rgb color"));
1043 PaletteHexButton::~PaletteHexButton()
1046 int PaletteHexButton::handle_event()
1048 const char *hex = window->hex_box->get_text();
1049 window->update_rgb_hex(hex);
1053 PaletteHex::PaletteHex(ColorWindow *window, int x, int y, const char *hex)
1054 : BC_TextBox(x, y, 100, 1, hex)
1056 this->window = window;
1058 PaletteHex::~PaletteHex()
1061 void PaletteHex::update()
1063 char hex[BCSTRLEN], *cp = hex;
1064 if( window->thread->do_alpha )
1065 cp += sprintf(cp,"%02x", window->alpha8());
1066 sprintf(cp,"%06x",window->rgb888());
1067 BC_TextBox::update(hex);
1070 int PaletteHex::keypress_event()
1072 if( get_keypress() != RETURN )
1073 return BC_TextBox::keypress_event();
1074 window->update_rgb_hex(get_text());
1078 #include "grabpick_up_png.h"
1079 #include "grabpick_hi_png.h"
1080 #include "grabpick_dn_png.h"
1082 PaletteGrabButton::PaletteGrabButton(ColorWindow *window, int x, int y)
1083 : BC_Button(x, y, vframes)
1085 this->window = window;
1086 vframes[0] = new VFramePng(grabpick_up_png);
1087 vframes[1] = new VFramePng(grabpick_hi_png);
1088 vframes[2] = new VFramePng(grabpick_dn_png);
1089 set_tooltip(_("grab from anywhere picker"));
1091 PaletteGrabButton::~PaletteGrabButton()
1093 for( int i=0; i<3; ++i )
1096 int PaletteGrabButton::handle_event()
1098 if( window->grab_buttons() ) {
1100 window->button_grabbed = 1;
1101 button_press_event(); // redraw face HI
1106 PaletteHistory::PaletteHistory(ColorWindow *window, int x, int y)
1107 : BC_SubWindow(x,y, 200, 24)
1109 this->window = window;
1111 set_tooltip(_("color history"));
1113 PaletteHistory::~PaletteHistory()
1116 void PaletteHistory::update(int flush)
1119 for( int i=0; i<PALLETTE_HISTORY_SIZE; x1=x2 ) {
1120 int rgb = window->palette_history[i];
1121 x2 = (++i * get_w())/PALLETTE_HISTORY_SIZE;
1122 draw_3d_box(x1,0,x2-x1,get_h(),WHITE,BLACK,rgb,LTBLUE,DKBLUE);
1127 int PaletteHistory::button_press_event()
1129 if( button_down || !is_event_win() ) return 0;
1131 cursor_motion_event();
1134 int PaletteHistory::button_release_event()
1136 if( !button_down || !is_event_win() ) return 0;
1137 cursor_motion_event();
1138 if( button_down > 0 ) {
1139 window->handle_event();
1140 window->update_display();
1141 window->update_history();
1146 int PaletteHistory::cursor_motion_event()
1148 if( !button_down || !is_event_win() ) return 0;
1150 int pick = (PALLETTE_HISTORY_SIZE * get_cursor_x()) / get_w();
1151 bclamp(pick, 0, PALLETTE_HISTORY_SIZE-1);
1152 int color = window->palette_history[pick];
1153 float r = ((color>>16) & 0xff) / 255.;
1154 float g = ((color>>8) & 0xff) / 255.;
1155 float b = ((color>>0) & 0xff) / 255.;
1156 if( window->rgb.r != r || window->rgb.g != g || window->rgb.b != b ) {
1157 window->rgb.r = r; window->rgb.g = g; window->rgb.b = b;
1158 window->update_rgb();
1163 int PaletteHistory::cursor_leave_event()
1168 int PaletteHistory::repeat_event(int64_t duration)
1172 if( duration == get_resources()->tooltip_delay &&
1173 get_tooltip() && *get_tooltip() && cursor_above() ) {
1181 ColorButton::ColorButton(const char *title,
1182 int x, int y, int w, int h,
1183 int color, int alpha, int ok_cancel)
1184 : BC_Button(x, y, w, vframes)
1186 this->title = title;
1187 this->color = this->orig_color = color;
1188 this->alpha = this->orig_alpha = alpha;
1189 this->ok_cancel = ok_cancel;
1191 for( int i=0; i<3; ++i ) {
1192 vframes[i] = new VFrame(w, h, BC_RGBA8888);
1193 vframes[i]->clear_frame();
1199 ColorButton::~ColorButton()
1201 delete color_thread;
1202 delete color_picker;
1203 for( int i=0; i<3; ++i )
1207 void ColorButton::set_color(int color)
1209 printf("ColorButton::set_color %06x\n", color);
1211 void ColorButton::handle_done_event(int result)
1213 color_thread->stop();
1215 int ColorButton::handle_new_color(int color, int alpha)
1217 printf("ColorButton::handle_new_color %02x%06x\n", alpha, color);
1221 int ColorButton::handle_event()
1224 delete color_picker;
1225 color_picker = new ColorButtonPicker(this);
1226 orig_color = color; orig_alpha = alpha;
1227 color_picker->start_window(color, alpha, ok_cancel);
1229 color_thread = new ColorButtonThread(this);
1230 color_thread->start();
1231 lock_window("ColorButtonButton::start_color_thread");
1235 void ColorButton::close_picker()
1237 if( color_thread ) color_thread->stop();
1238 delete color_thread; color_thread = 0;
1239 delete color_picker; color_picker = 0;
1242 void ColorButton::update_gui(int color, int alpha)
1245 color_picker->update_gui(color, alpha);
1246 update_gui(color | (~alpha<<24));
1249 void ColorButton::update_gui(int color)
1255 ColorButtonPicker::ColorButtonPicker(ColorButton *color_button)
1256 : ColorPicker(color_button->alpha >= 0 ? 1 : 0, color_button->title)
1258 this->color_button = color_button;
1261 ColorButtonPicker::~ColorButtonPicker()
1265 void ColorButtonPicker::handle_done_event(int result)
1267 color_button->color_thread->stop();
1268 color_button->handle_done_event(result);
1271 void ColorButtonPicker::update(int color, int alpha)
1273 color_button->color = color;
1274 color_button->alpha = alpha;
1275 color_button->color_thread->update_lock->unlock();
1278 int ColorButtonPicker::handle_new_color(int color, int alpha)
1280 color_button->lock_window("ColorButtonPicker::handle_new_color");
1281 color_button->update_gui(color, alpha);
1282 color_button->unlock_window();
1283 return color_button->handle_new_color(color, alpha);
1286 void ColorButtonPicker::update_gui()
1288 color_button->lock_window("ColorButtonPicker::update_gui");
1289 color_button->update_gui(color_button->color, color_button->alpha);
1290 color_button->unlock_window();
1293 void ColorButtonPicker::update_gui(int color, int alpha)
1295 ColorPicker::update_gui(color, alpha);
1296 color_button->handle_new_color(color, alpha);
1299 ColorButtonThread::ColorButtonThread(ColorButton *color_button)
1302 this->color_button = color_button;
1303 this->update_lock = new Condition(0,"ColorButtonThread::update_lock");
1307 ColorButtonThread::~ColorButtonThread()
1313 void ColorButtonThread::start()
1321 void ColorButtonThread::stop()
1325 update_lock->unlock();
1330 void ColorButtonThread::run()
1332 ColorButtonPicker *color_picker = color_button->color_picker;
1333 color_picker->update_gui();
1335 update_lock->lock("ColorButtonThread::run");
1337 color_picker->update_gui();
1342 ColorBoxButton::ColorBoxButton(const char *title,
1343 int x, int y, int w, int h,
1344 int color, int alpha, int ok_cancel)
1345 : ColorButton(title, x, y, w, h, color, alpha, ok_cancel)
1348 ColorBoxButton::~ColorBoxButton()
1352 int ColorBoxButton::handle_new_color(int color, int alpha)
1354 return ColorButton::handle_new_color(color, alpha);
1356 void ColorBoxButton::handle_done_event(int result)
1358 ColorButton::handle_done_event(result);
1360 void ColorBoxButton::create_objects()
1362 update_gui(color, alpha);
1365 void ColorBoxButton::set_color(int color)
1367 this->color = (color & 0xffffff);
1368 this->alpha = (~color>>24) & 0xff;
1369 int r = (color>>16) & 0xff;
1370 int g = (color>> 8) & 0xff;
1371 int b = (color>> 0) & 0xff;
1372 int color_model = vframes[0]->get_color_model();
1373 int bpp = BC_CModels::calculate_pixelsize(color_model);
1374 for( int i=0; i<3; ++i ) {
1375 VFrame *vframe = vframes[i];
1376 int ww = vframe->get_w(), hh = vframe->get_h();
1377 uint8_t **rows = vframe->get_rows();
1378 int rr = r, gg = g, bb = b;
1383 if( (rr+=48) > 0xff ) rr = 0xff;
1384 if( (gg+=48) > 0xff ) gg = 0xff;
1385 if( (bb+=48) > 0xff ) bb = 0xff;
1388 if( (rr-=48) < 0x00 ) rr = 0x00;
1389 if( (gg-=48) < 0x00 ) gg = 0x00;
1390 if( (bb-=48) < 0x00 ) bb = 0x00;
1393 for( int y=0; y<hh; ++y ) {
1394 uint8_t *rp = rows[y];
1395 for( int x=0; x<ww; ++x ) {
1396 rp[0] = rr; rp[1] = gg; rp[2] = bb;
1397 if( bpp > 3 ) rp[3] = 0xff;
1402 set_images(vframes);
1405 ColorCircleButton::ColorCircleButton(const char *title,
1406 int x, int y, int w, int h,
1407 int color, int alpha, int ok_cancel)
1408 : ColorButton(title, x, y, w, h, color, alpha, ok_cancel)
1411 ColorCircleButton::~ColorCircleButton()
1414 int ColorCircleButton::handle_new_color(int color, int alpha)
1416 return ColorButton::handle_new_color(color, alpha);
1418 void ColorCircleButton::handle_done_event(int result)
1420 ColorButton::handle_done_event(result);
1422 void ColorCircleButton::create_objects()
1424 update_gui(color, alpha);
1427 void ColorCircleButton::set_color(int color)
1429 this->color = (color & 0xffffff);
1430 this->alpha = (~color>>24) & 0xff;
1431 int r = (color>>16) & 0xff;
1432 int g = (color>>8) & 0xff;
1433 int b = (color>>0) & 0xff;
1434 for( int i=0; i<3; ++i ) {
1435 VFrame *vframe = vframes[i];
1436 int ww = vframe->get_w(), hh = vframe->get_h();
1437 int cx = (ww+1)/2, cy = hh/2;
1438 double cc = (cx*cx + cy*cy) / 4.;
1439 uint8_t *bp = vframe->get_data(), *dp = bp;
1440 uint8_t *ep = dp + vframe->get_data_size();
1441 int rr = r, gg = g, bb = b;
1442 int bpl = vframe->get_bytes_per_line();
1447 if( (rr+=48) > 0xff ) rr = 0xff;
1448 if( (gg+=48) > 0xff ) gg = 0xff;
1449 if( (bb+=48) > 0xff ) bb = 0xff;
1452 if( (rr-=48) < 0x00 ) rr = 0x00;
1453 if( (gg-=48) < 0x00 ) gg = 0x00;
1454 if( (bb-=48) < 0x00 ) bb = 0x00;
1458 int yy = (dp-bp) / bpl, xx = ((dp-bp) % bpl) >> 2;
1459 int dy = cy - yy, dx = cx - xx;
1460 double s = dx*dx + dy*dy - cc;
1461 double ss = s < 0 ? 1 : s >= cc ? 0 : 1 - s/cc;
1463 *dp++ = rr; *dp++ = gg; *dp++ = bb; *dp++ = aa;
1466 set_images(vframes);