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();
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()
314 thread->handle_new_color(rgb888(), (int)(255*aph + 0.5));
318 void ColorWindow::get_screen_sample()
321 get_abs_cursor(cx, cy);
322 BC_Capture capture_bitmap(1, 1, 0);
323 VFrame vframe(1,1,BC_RGB888);
324 capture_bitmap.capture_frame(&vframe, cx,cy);
325 unsigned char *data = vframe.get_data();
326 rgb.r = data[0]/255.; rgb.g = data[1]/255.; rgb.b = data[2]/255.;
330 int ColorWindow::cursor_motion_event()
332 if( button_grabbed && get_button_down() ) {
339 int ColorWindow::button_press_event()
341 if( button_grabbed ) {
348 int ColorWindow::button_release_event()
350 if( button_grabbed ) {
356 return handle_event();
361 void ColorWindow::update_rgb_hex(const char *hex)
364 if( sscanf(hex,"%x",&color) == 1 ) {
365 float r = ((color>>16) & 0xff) / 255.;
366 float g = ((color>>8) & 0xff) / 255.;
367 float b = ((color>>0) & 0xff) / 255.;
368 rgb.r = r; rgb.g = g; rgb.b = b;
376 PaletteWheel::PaletteWheel(ColorWindow *window, int x, int y)
377 : BC_SubWindow(x, y, 170, 170)
379 this->window = window;
385 PaletteWheel::~PaletteWheel()
389 int PaletteWheel::button_press_event()
391 if( get_cursor_x() >= 0 && get_cursor_x() < get_w() &&
392 get_cursor_y() >= 0 && get_cursor_y() < get_h() &&
395 cursor_motion_event();
401 int PaletteWheel::cursor_motion_event()
403 int x1, y1, distance;
404 if( button_down && is_event_win() ) {
405 float h = get_angle(get_w()/2, get_h()/2, get_cursor_x(), get_cursor_y());
406 bclamp(h, 0, 359.999); window->hsv.h = h;
407 x1 = get_w() / 2 - get_cursor_x();
408 y1 = get_h() / 2 - get_cursor_y();
409 distance = (int)sqrt(x1 * x1 + y1 * y1);
410 float s = (float)distance / (get_w() / 2);
411 bclamp(s, 0, 1); window->hsv.s = s;
413 window->update_hsv();
414 window->handle_event();
420 int PaletteWheel::button_release_event()
429 void PaletteWheel::create_objects()
432 //printf("PaletteWheel::create_objects 1\n");
435 float x1, y1, x2, y2;
437 int default_r, default_g, default_b;
438 VFrame frame(0, -1, get_w(), get_h(), BC_RGBA8888, -1);
441 default_r = (get_resources()->get_bg_color() & 0xff0000) >> 16;
442 default_g = (get_resources()->get_bg_color() & 0xff00) >> 8;
443 default_b = (get_resources()->get_bg_color() & 0xff);
444 //printf("PaletteWheel::create_objects 1\n");
446 int highlight_r = (get_resources()->button_light & 0xff0000) >> 16;
447 int highlight_g = (get_resources()->button_light & 0xff00) >> 8;
448 int highlight_b = (get_resources()->button_light & 0xff);
450 for( y2 = 0; y2 < get_h(); y2++ ) {
451 unsigned char *row = (unsigned char*)frame.get_rows()[(int)y2];
452 for( x2 = 0; x2 < get_w(); x2++ ) {
453 distance = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
454 if( distance > x1 ) {
455 row[(int)x2 * 4] = default_r;
456 row[(int)x2 * 4 + 1] = default_g;
457 row[(int)x2 * 4 + 2] = default_b;
458 row[(int)x2 * 4 + 3] = 0;
461 if( distance > x1 - 1 ) {
463 if( get_h() - y2 < x2 ) {
474 row[(int)x2 * 4] = r_i;
475 row[(int)x2 * 4 + 1] = g_i;
476 row[(int)x2 * 4 + 2] = b_i;
477 row[(int)x2 * 4 + 3] = 255;
480 h = get_angle(x1, y1, x2, y2);
482 HSV::hsv_to_rgb(r, g, b, h, s, v);
483 row[(int)x2 * 4] = (int)(r * 255);
484 row[(int)x2 * 4 + 1] = (int)(g * 255);
485 row[(int)x2 * 4 + 2] = (int)(b * 255);
486 row[(int)x2 * 4 + 3] = 255;
490 //printf("PaletteWheel::create_objects 1\n");
502 //printf("PaletteWheel::create_objects 1\n");
504 oldhue = window->hsv.h;
505 oldsaturation = window->hsv.s;
506 //printf("PaletteWheel::create_objects 1\n");
507 draw(oldhue, oldsaturation);
508 //printf("PaletteWheel::create_objects 1\n");
510 //printf("PaletteWheel::create_objects 2\n");
513 float PaletteWheel::torads(float angle)
515 return (float)angle / 360 * 2 * M_PI;
519 int PaletteWheel::draw(float hue, float saturation)
525 if( hue > 0 && hue < 90 ) {
526 x = (int)(w - w * cos(torads(90 - hue)) * saturation);
527 y = (int)(h - h * sin(torads(90 - hue)) * saturation);
529 else if( hue > 90 && hue < 180 ) {
530 x = (int)(w - w * cos(torads(hue - 90)) * saturation);
531 y = (int)(h + h * sin(torads(hue - 90)) * saturation);
533 else if( hue > 180 && hue < 270 ) {
534 x = (int)(w + w * cos(torads(270 - hue)) * saturation);
535 y = (int)(h + h * sin(torads(270 - hue)) * saturation);
537 else if( hue > 270 && hue < 360 ) {
538 x = (int)(w + w * cos(torads(hue - 270)) * saturation);
539 y = (int)(h - w * sin(torads(hue - 270)) * saturation);
541 else if( hue == 0 ) {
543 y = (int)(h - h * saturation);
545 else if( hue == 90 ) {
546 x = (int)(w - w * saturation);
549 else if( hue == 180 ) {
551 y = (int)(h + h * saturation);
553 else if( hue == 270 ) {
554 x = (int)(w + w * saturation);
560 draw_circle(x - 5, y - 5, 10, 10);
565 int PaletteWheel::get_angle(float x1, float y1, float x2, float y2)
567 float result = -atan2(x2 - x1, y1 - y2) * (360 / M_PI / 2);
573 PaletteWheelValue::PaletteWheelValue(ColorWindow *window, int x, int y)
574 : BC_SubWindow(x, y, 40, 170, BLACK)
576 this->window = window;
579 PaletteWheelValue::~PaletteWheelValue()
584 void PaletteWheelValue::create_objects()
586 frame = new VFrame(get_w(), get_h(), BC_RGB888);
587 draw(window->hsv.h, window->hsv.s, window->hsv.v);
591 int PaletteWheelValue::button_press_event()
593 //printf("PaletteWheelValue::button_press 1 %d\n", is_event_win());
594 if( get_cursor_x() >= 0 && get_cursor_x() < get_w() &&
595 get_cursor_y() >= 0 && get_cursor_y() < get_h() &&
597 //printf("PaletteWheelValue::button_press 2\n");
599 cursor_motion_event();
605 int PaletteWheelValue::cursor_motion_event()
607 if( button_down && is_event_win() ) {
608 //printf("PaletteWheelValue::cursor_motion 1\n");
609 float v = 1.0 - (float)(get_cursor_y() - 2) / (get_h() - 4);
610 bclamp(v, 0, 1); window->hsv.v = v;
611 window->update_hsv();
612 window->handle_event();
618 int PaletteWheelValue::button_release_event()
621 //printf("PaletteWheelValue::button_release 1\n");
628 int PaletteWheelValue::draw(float hue, float saturation, float value)
633 for( i = get_h() - 3; i >= 2; i-- ) {
634 unsigned char *row = (unsigned char*)frame->get_rows()[i];
635 HSV::hsv_to_rgb(r_f, g_f, b_f, hue, saturation,
636 1.0 - (float)(i - 2) / (get_h() - 4));
637 r = (int)(r_f * 255);
638 g = (int)(g_f * 255);
639 b = (int)(b_f * 255);
640 for( j = 0; j < get_w(); j++ ) {
647 draw_3d_border(0, 0, get_w(), get_h(), 1);
648 draw_vframe(frame, 2, 2, get_w() - 4, get_h() - 4,
649 2, 2, get_w() - 4, get_h() - 4, 0);
651 draw_line(2, get_h() - 3 - (int)(value * (get_h() - 5)),
652 get_w() - 3, get_h() - 3 - (int)(value * (get_h() - 5)));
653 //printf("PaletteWheelValue::draw %d %f\n", __LINE__, value);
658 PaletteOutput::PaletteOutput(ColorWindow *window, int x, int y)
659 : BC_SubWindow(x, y, 180, 30, BLACK)
661 this->window = window;
663 PaletteOutput::~PaletteOutput()
668 void PaletteOutput::create_objects()
674 int PaletteOutput::handle_event()
679 int PaletteOutput::draw()
681 set_color(window->rgb888());
682 draw_box(2, 2, get_w() - 4, get_h() - 4);
683 draw_3d_border(0, 0, get_w(), get_h(), 1);
687 PaletteHue::PaletteHue(ColorWindow *window, int x, int y)
688 : BC_ISlider(x, y, 0, 150, 200, 0, 359, (int)(window->hsv.h), 0)
690 this->window = window;
692 PaletteHue::~PaletteHue()
696 int PaletteHue::handle_event()
698 window->hsv.h = get_value();
699 window->update_hsv();
700 window->handle_event();
704 PaletteSat::PaletteSat(ColorWindow *window, int x, int y)
705 : BC_FSlider(x, y, 0, 150, 200, 0, 1.0, window->hsv.s, 0)
707 this->window = window;
710 PaletteSat::~PaletteSat()
714 int PaletteSat::handle_event()
716 window->hsv.s = get_value();
717 window->update_hsv();
718 window->handle_event();
723 PaletteVal::PaletteVal(ColorWindow *window, int x, int y)
724 : BC_FSlider(x, y, 0, 150, 200, 0, 1.0, window->hsv.v, 0)
726 this->window = window;
729 PaletteVal::~PaletteVal()
733 int PaletteVal::handle_event()
735 window->hsv.v = get_value();
736 window->update_hsv();
737 window->handle_event();
742 PaletteRed::PaletteRed(ColorWindow *window, int x, int y)
743 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->rgb.r, 0)
745 this->window = window;
748 PaletteRed::~PaletteRed()
752 int PaletteRed::handle_event()
754 window->rgb.r = get_value();
755 window->update_rgb();
756 window->handle_event();
760 PaletteGrn::PaletteGrn(ColorWindow *window, int x, int y)
761 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->rgb.g, 0)
763 this->window = window;
766 PaletteGrn::~PaletteGrn()
770 int PaletteGrn::handle_event()
772 window->rgb.g = get_value();
773 window->update_rgb();
774 window->handle_event();
778 PaletteBlu::PaletteBlu(ColorWindow *window, int x, int y)
779 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->rgb.b, 0)
781 this->window = window;
784 PaletteBlu::~PaletteBlu()
788 int PaletteBlu::handle_event()
790 window->rgb.b = get_value();
791 window->update_rgb();
792 window->handle_event();
796 PaletteAlpha::PaletteAlpha(ColorWindow *window, int x, int y)
797 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->aph, 0)
799 this->window = window;
802 PaletteAlpha::~PaletteAlpha()
806 int PaletteAlpha::handle_event()
808 window->aph = get_value();
809 window->aph_a->update(window->aph);
810 window->handle_event();
814 PaletteLum::PaletteLum(ColorWindow *window, int x, int y)
815 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->yuv.y, 0)
817 this->window = window;
820 PaletteLum::~PaletteLum()
824 int PaletteLum::handle_event()
826 window->yuv.y = get_value();
827 window->update_yuv();
828 window->handle_event();
832 PaletteCr::PaletteCr(ColorWindow *window, int x, int y)
833 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->yuv.u, 0)
835 this->window = window;
838 PaletteCr::~PaletteCr()
842 int PaletteCr::handle_event()
844 window->yuv.u = get_value();
845 window->update_yuv();
846 window->handle_event();
850 PaletteCb::PaletteCb(ColorWindow *window, int x, int y)
851 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->yuv.v, 0)
853 this->window = window;
856 PaletteCb::~PaletteCb()
860 int PaletteCb::handle_event()
862 window->yuv.v = get_value();
863 window->update_yuv();
864 window->handle_event();
868 void ColorWindow::update_rgb(float r, float g, float b)
871 YUV::yuv.rgb_to_yuv_f(r, g, b, y, u, v);
873 bclamp(y, 0, 1); yuv.y = y;
874 bclamp(u, 0, 1); yuv.u = u;
875 bclamp(v, 0, 1); yuv.v = v; }
877 HSV::rgb_to_hsv(r,g,b, h,s,v);
878 bclamp(h, 0, 360); hsv.h = h;
879 bclamp(s, 0, 1); hsv.s = s;
880 bclamp(v, 0, 1); hsv.v = v; }
883 void ColorWindow::update_yuv(float y, float u, float v)
887 YUV::yuv.yuv_to_rgb_f(r, g, b, y, u, v);
888 bclamp(r, 0, 1); rgb.r = r;
889 bclamp(g, 0, 1); rgb.g = g;
890 bclamp(b, 0, 1); rgb.b = b;
892 HSV::rgb_to_hsv(r,g,b, h, s, v);
893 bclamp(h, 0, 360); hsv.h = h;
894 bclamp(s, 0, 1); hsv.s = s;
895 bclamp(v, 0, 1); hsv.v = v; }
898 void ColorWindow::update_hsv(float h, float s, float v)
901 HSV::hsv_to_rgb(r,g,b, h,s,v);
902 bclamp(r, 0, 1); rgb.r = r;
903 bclamp(g, 0, 1); rgb.g = g;
904 bclamp(b, 0, 1); rgb.b = b;
906 YUV::yuv.rgb_to_yuv_f(r, g, b, y, u, v);
908 bclamp(y, 0, 1); yuv.y = y;
909 bclamp(u, 0, 1); yuv.u = u;
910 bclamp(v, 0, 1); yuv.v = v; }
913 void ColorWindow::load_history()
915 char history_path[BCTEXTLEN];
916 MWindow::create_defaults_path(history_path,PALETTE_DATA);
917 FILE *fp = fopen(history_path,"r");
920 while( i < PALLETTE_HISTORY_SIZE ) {
922 if( !fgets(line,sizeof(line)-1,fp) ) break;
923 line[sizeof(line)-1] = 0;
924 if( sscanf(line, "%x",&palette_history[i]) != 1 ) break;
929 int r = 0, g = 0, b = 0;
930 float v0 = 0, v1 = 1;
931 while( i < PALLETTE_HISTORY_SIZE ) {
932 int grey_code = i ^ (i>>1);
933 r = 255 * ((grey_code&4) ? v0 : v1);
934 g = 255 * ((grey_code&2) ? v0 : v1);
935 b = 255 * ((grey_code&1) ? v0 : v1);
936 int color = (r<<16) | (g<<8) | (b<<0);
937 palette_history[i++] = color;
938 if( i & 7 ) continue;
939 v0 = 0.5f * (v0+.5f);
940 v1 = 0.5f * (v1+.5f);
943 void ColorWindow::save_history()
945 char history_path[BCTEXTLEN];
946 MWindow::create_defaults_path(history_path,PALETTE_DATA);
947 FILE *fp = fopen(history_path,"w");
949 for( int i=0; i<PALLETTE_HISTORY_SIZE; ++i ) {
950 fprintf(fp, "%06x\n", palette_history[i]);
955 void ColorWindow::update_history(int color)
957 int out = palette_history[0];
958 palette_history[0] = color;
959 for( int i=1; out != color && i<PALLETTE_HISTORY_SIZE; ++i ) {
961 out = palette_history[i];
962 palette_history[i] = in;
965 void ColorWindow::update_history()
967 update_history(rgb888());
970 int ColorWindow::rgb888()
972 int r = 255*rgb.r + 0.5, g = 255*rgb.g + 0.5, b = 255*rgb.b + 0.5;
973 bclamp(r, 0, 255); bclamp(g, 0, 255); bclamp(b, 0, 255);
974 return (r<<16) | (g<<8) | (b<<0);
977 PaletteNum::PaletteNum(ColorWindow *window, int x, int y,
978 float &output, float min, float max)
979 : BC_TumbleTextBox(window, output, min, max, x, y, 64)
981 this->window = window;
982 this->output = &output;
987 PaletteNum::~PaletteNum()
992 int PaletteHSV::handle_event()
995 window->update_hsv();
996 window->handle_event();
1000 int PaletteRGB::handle_event()
1003 window->update_rgb();
1004 window->handle_event();
1008 int PaletteYUV::handle_event()
1011 window->update_yuv();
1012 window->handle_event();
1016 int PaletteAPH::handle_event()
1019 window->update_display();
1020 window->handle_event();
1024 PaletteHexButton::PaletteHexButton(ColorWindow *window, int x, int y)
1025 : BC_GenericButton(x, y, 50, "#")
1027 this->window = window;
1028 set_tooltip(_("hex rgb color"));
1030 PaletteHexButton::~PaletteHexButton()
1033 int PaletteHexButton::handle_event()
1035 const char *hex = window->hex_box->get_text();
1036 window->update_rgb_hex(hex);
1040 PaletteHex::PaletteHex(ColorWindow *window, int x, int y, const char *hex)
1041 : BC_TextBox(x, y, 100, 1, hex)
1043 this->window = window;
1045 PaletteHex::~PaletteHex()
1048 void PaletteHex::update()
1050 char hex[BCSTRLEN]; sprintf(hex,"%06x",window->rgb888());
1051 BC_TextBox::update(hex);
1054 int PaletteHex::keypress_event()
1056 if( get_keypress() != RETURN )
1057 return BC_TextBox::keypress_event();
1058 window->update_rgb_hex(get_text());
1062 #include "grabpick_up_png.h"
1063 #include "grabpick_hi_png.h"
1064 #include "grabpick_dn_png.h"
1066 PaletteGrabButton::PaletteGrabButton(ColorWindow *window, int x, int y)
1067 : BC_Button(x, y, vframes)
1069 this->window = window;
1070 vframes[0] = new VFramePng(grabpick_up_png);
1071 vframes[1] = new VFramePng(grabpick_hi_png);
1072 vframes[2] = new VFramePng(grabpick_dn_png);
1073 set_tooltip(_("grab from anywhere picker"));
1075 PaletteGrabButton::~PaletteGrabButton()
1077 for( int i=0; i<3; ++i )
1080 int PaletteGrabButton::handle_event()
1082 if( window->grab_buttons() ) {
1084 window->button_grabbed = 1;
1085 button_press_event(); // redraw face HI
1090 PaletteHistory::PaletteHistory(ColorWindow *window, int x, int y)
1091 : BC_SubWindow(x,y, 200, 24)
1093 this->window = window;
1095 set_tooltip(_("color history"));
1097 PaletteHistory::~PaletteHistory()
1100 void PaletteHistory::update(int flush)
1103 for( int i=0; i<PALLETTE_HISTORY_SIZE; x1=x2 ) {
1104 int rgb = window->palette_history[i];
1105 x2 = (++i * get_w())/PALLETTE_HISTORY_SIZE;
1106 draw_3d_box(x1,0,x2-x1,get_h(),WHITE,BLACK,rgb,LTBLUE,DKBLUE);
1111 int PaletteHistory::button_press_event()
1113 if( button_down || !is_event_win() ) return 0;
1115 cursor_motion_event();
1118 int PaletteHistory::button_release_event()
1120 if( !button_down || !is_event_win() ) return 0;
1121 cursor_motion_event();
1122 if( button_down > 0 ) {
1123 window->handle_event();
1124 window->update_display();
1125 window->update_history();
1130 int PaletteHistory::cursor_motion_event()
1132 if( !button_down || !is_event_win() ) return 0;
1134 int pick = (PALLETTE_HISTORY_SIZE * get_cursor_x()) / get_w();
1135 bclamp(pick, 0, PALLETTE_HISTORY_SIZE-1);
1136 int color = window->palette_history[pick];
1137 float r = ((color>>16) & 0xff) / 255.;
1138 float g = ((color>>8) & 0xff) / 255.;
1139 float b = ((color>>0) & 0xff) / 255.;
1140 if( window->rgb.r != r || window->rgb.g != g || window->rgb.b != b ) {
1141 window->rgb.r = r; window->rgb.g = g; window->rgb.b = b;
1142 window->update_rgb();
1147 int PaletteHistory::cursor_leave_event()
1152 int PaletteHistory::repeat_event(int64_t duration)
1156 if( duration == get_resources()->tooltip_delay &&
1157 get_tooltip() && *get_tooltip() && cursor_above() ) {
1165 ColorButton::ColorButton(const char *title,
1166 int x, int y, int w, int h,
1167 int color, int alpha, int ok_cancel)
1168 : BC_Button(x, y, w, vframes)
1170 this->title = title;
1171 this->color = this->orig_color = color;
1172 this->alpha = this->orig_alpha = alpha;
1173 this->ok_cancel = ok_cancel;
1175 for( int i=0; i<3; ++i ) {
1176 vframes[i] = new VFrame(w, h, BC_RGBA8888);
1177 vframes[i]->clear_frame();
1183 ColorButton::~ColorButton()
1185 delete color_thread;
1186 delete color_picker;
1187 for( int i=0; i<3; ++i )
1191 void ColorButton::set_color(int color)
1193 printf("ColorButton::set_color %06x\n", color);
1195 void ColorButton::handle_done_event(int result)
1197 color_thread->stop();
1199 int ColorButton::handle_new_color(int color, int alpha)
1201 printf("ColorButton::handle_new_color %02x%06x\n", alpha, color);
1205 int ColorButton::handle_event()
1208 delete color_picker;
1209 color_picker = new ColorButtonPicker(this);
1210 orig_color = color; orig_alpha = alpha;
1211 color_picker->start_window(color, alpha, ok_cancel);
1213 color_thread = new ColorButtonThread(this);
1214 color_thread->start();
1215 lock_window("ColorButtonButton::start_color_thread");
1219 void ColorButton::close_picker()
1221 if( color_thread ) color_thread->stop();
1222 delete color_thread; color_thread = 0;
1223 delete color_picker; color_picker = 0;
1226 void ColorButton::update_gui(int color)
1232 ColorButtonPicker::ColorButtonPicker(ColorButton *color_button)
1233 : ColorPicker(color_button->alpha >= 0 ? 1 : 0, color_button->title)
1235 this->color_button = color_button;
1238 ColorButtonPicker::~ColorButtonPicker()
1242 void ColorButtonPicker::handle_done_event(int result)
1244 color_button->color_thread->stop();
1245 color_button->handle_done_event(result);
1248 int ColorButtonPicker::handle_new_color(int color, int alpha)
1250 color_button->color = color;
1251 color_button->color_thread->update_lock->unlock();
1252 color_button->handle_new_color(color, alpha);
1256 void ColorButtonPicker::update_gui()
1258 color_button->lock_window("ColorButtonPicker::update_gui");
1259 color_button->update_gui(color_button->color);
1260 color_button->unlock_window();
1263 ColorButtonThread::ColorButtonThread(ColorButton *color_button)
1266 this->color_button = color_button;
1267 this->update_lock = new Condition(0,"ColorButtonThread::update_lock");
1271 ColorButtonThread::~ColorButtonThread()
1277 void ColorButtonThread::start()
1285 void ColorButtonThread::stop()
1289 update_lock->unlock();
1294 void ColorButtonThread::run()
1296 ColorButtonPicker *color_picker = color_button->color_picker;
1297 color_picker->update_gui();
1299 update_lock->lock("ColorButtonThread::run");
1301 color_picker->update_gui();
1306 ColorBoxButton::ColorBoxButton(const char *title,
1307 int x, int y, int w, int h,
1308 int color, int alpha, int ok_cancel)
1309 : ColorButton(title, x, y, w, h, color, alpha, ok_cancel)
1312 ColorBoxButton::~ColorBoxButton()
1316 int ColorBoxButton::handle_new_color(int color, int alpha)
1318 return ColorButton::handle_new_color(color, alpha);
1320 void ColorBoxButton::handle_done_event(int result)
1322 ColorButton::handle_done_event(result);
1324 void ColorBoxButton::create_objects()
1329 void ColorBoxButton::set_color(int color)
1331 this->color = color;
1332 int r = (color>>16) & 0xff;
1333 int g = (color>> 8) & 0xff;
1334 int b = (color>> 0) & 0xff;
1335 int color_model = vframes[0]->get_color_model();
1336 int bpp = BC_CModels::calculate_pixelsize(color_model);
1337 for( int i=0; i<3; ++i ) {
1338 VFrame *vframe = vframes[i];
1339 int ww = vframe->get_w(), hh = vframe->get_h();
1340 uint8_t **rows = vframe->get_rows();
1341 int rr = r, gg = g, bb = b;
1346 if( (rr+=48) > 0xff ) rr = 0xff;
1347 if( (gg+=48) > 0xff ) gg = 0xff;
1348 if( (bb+=48) > 0xff ) bb = 0xff;
1351 if( (rr-=48) < 0x00 ) rr = 0x00;
1352 if( (gg-=48) < 0x00 ) gg = 0x00;
1353 if( (bb-=48) < 0x00 ) bb = 0x00;
1356 for( int y=0; y<hh; ++y ) {
1357 uint8_t *rp = rows[y];
1358 for( int x=0; x<ww; ++x ) {
1359 rp[0] = rr; rp[1] = gg; rp[2] = bb;
1360 if( bpp > 3 ) rp[3] = 0xff;
1365 set_images(vframes);
1368 ColorCircleButton::ColorCircleButton(const char *title,
1369 int x, int y, int w, int h,
1370 int color, int alpha, int ok_cancel)
1371 : ColorButton(title, x, y, w, h, color, alpha, ok_cancel)
1374 ColorCircleButton::~ColorCircleButton()
1377 int ColorCircleButton::handle_new_color(int color, int alpha)
1379 return ColorButton::handle_new_color(color, alpha);
1381 void ColorCircleButton::handle_done_event(int result)
1383 ColorButton::handle_done_event(result);
1385 void ColorCircleButton::create_objects()
1390 void ColorCircleButton::set_color(int color)
1392 this->color = color;
1393 int r = (color>>16) & 0xff;
1394 int g = (color>>8) & 0xff;
1395 int b = (color>>0) & 0xff;
1396 for( int i=0; i<3; ++i ) {
1397 VFrame *vframe = vframes[i];
1398 int ww = vframe->get_w(), hh = vframe->get_h();
1399 int cx = (ww+1)/2, cy = hh/2;
1400 double cc = (cx*cx + cy*cy) / 4.;
1401 uint8_t *bp = vframe->get_data(), *dp = bp;
1402 uint8_t *ep = dp + vframe->get_data_size();
1403 int rr = r, gg = g, bb = b;
1404 int bpl = vframe->get_bytes_per_line();
1409 if( (rr+=48) > 0xff ) rr = 0xff;
1410 if( (gg+=48) > 0xff ) gg = 0xff;
1411 if( (bb+=48) > 0xff ) bb = 0xff;
1414 if( (rr-=48) < 0x00 ) rr = 0x00;
1415 if( (gg-=48) < 0x00 ) gg = 0x00;
1416 if( (bb-=48) < 0x00 ) bb = 0x00;
1420 int yy = (dp-bp) / bpl, xx = ((dp-bp) % bpl) >> 2;
1421 int dy = cy - yy, dx = cx - xx;
1422 double s = dx*dx + dy*dy - cc;
1423 double ss = s < 0 ? 1 : s >= cc ? 0 : 1 - s/cc;
1425 *dp++ = rr; *dp++ = gg; *dp++ = bb; *dp++ = aa;
1428 set_images(vframes);