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"
39 #define MSGQUAL "colorpicker"
41 #define PALETTE_DATA "palette.dat"
43 ColorPicker::ColorPicker(int do_alpha, const char *title)
47 this->do_alpha = do_alpha;
48 this->do_okcancel = 0;
53 ColorPicker::~ColorPicker()
58 void ColorPicker::start_window(int output, int alpha, int do_okcancel)
61 ColorWindow *gui = (ColorWindow *)get_gui();
63 gui->lock_window("ColorPicker::start_window");
69 this->output = output;
71 this->do_okcancel = do_okcancel;
75 BC_Window* ColorPicker::new_gui()
77 char window_title[BCTEXTLEN];
78 strcpy(window_title, _(PROGRAM_NAME ": "));
79 strcat(window_title, title ? title : _("Color Picker"));
80 BC_DisplayInfo display_info;
81 int x = display_info.get_abs_cursor_x() + 25;
82 int y = display_info.get_abs_cursor_y() - 100;
85 h += bmax(BC_OKButton::calculate_h(),BC_CancelButton::calculate_h());
86 int root_w = display_info.get_root_w(), root_h = display_info.get_root_h();
87 if( x+w > root_w ) x = root_w - w;
88 if( y+h > root_h ) y = root_h - h;
91 ColorWindow *gui = new ColorWindow(this, x, y, w, h, window_title);
92 gui->create_objects();
96 void ColorPicker::update_gui(int output, int alpha)
98 ColorWindow *gui = (ColorWindow *)get_gui();
101 this->output = output;
103 gui->change_values();
104 gui->update_display();
105 gui->unlock_window();
108 int ColorPicker::handle_new_color(int output, int alpha)
110 printf("ColorPicker::handle_new_color undefined.\n");
115 ColorWindow::ColorWindow(ColorPicker *thread, int x, int y, int w, int h, const char *title)
116 : BC_Window(title, x, y, w, h, w, h, 0, 0, 1)
118 this->thread = thread;
123 hue = 0; sat = 0; val = 0;
124 red = 0; grn = 0; blu = 0;
125 lum = 0; c_r = 0; c_b = 0;
128 hsv_h = 0; hsv_s = 0; hsv_v = 0;
129 rgb_r = 0; rgb_g = 0; rgb_b = 0;
130 yuv_y = 0; yuv_u = 0; yuv_v = 0;
135 ColorWindow::~ColorWindow()
137 delete hsv_h; delete hsv_s; delete hsv_v;
138 delete rgb_r; delete rgb_g; delete rgb_b;
139 delete yuv_y; delete yuv_u; delete yuv_v;
142 if( button_grabbed ) {
146 update_history(rgb888());
150 void ColorWindow::create_objects()
152 int x0 = 10, y0 = 10;
153 lock_window("ColorWindow::create_objects");
157 add_tool(wheel = new PaletteWheel(this, x, y));
158 wheel->create_objects();
160 x += 180; add_tool(wheel_value = new PaletteWheelValue(this, x, y));
161 wheel_value->create_objects();
163 y += 180; add_tool(output = new PaletteOutput(this, x, y));
164 output->create_objects();
165 y += output->get_h() + 20;
167 load_history(); int x1 = x;
168 add_tool(hex_btn = new PaletteHexButton(this, x1, y));
169 char hex[BCSTRLEN]; sprintf(hex,"%06x",thread->output);
170 x1 += hex_btn->get_w() + 5;
171 add_tool(hex_box = new PaletteHex(this, x1, y, hex));
172 x1 += hex_box->get_w() + 15;
173 add_tool(grab_btn = new PaletteGrabButton(this, x1, y));
174 y += hex_box->get_h() + 15;
175 add_tool(history = new PaletteHistory(this, 10, y));
178 add_tool(new BC_Title(x, y =y0, C_("H:"), SMALLFONT));
179 add_tool(new BC_Title(x, y+=25, C_("S:"), SMALLFONT));
180 add_tool(new BC_Title(x, y+=25, D_("colorpicker_value#V:"), SMALLFONT));
181 add_tool(new BC_Title(x, y+=40, C_("R:"), SMALLFONT));
182 add_tool(new BC_Title(x, y+=25, C_("G:"), SMALLFONT));
183 add_tool(new BC_Title(x, y+=25, C_("B:"), SMALLFONT));
184 add_tool(new BC_Title(x, y+=40, C_("Y:"), SMALLFONT));
185 add_tool(new BC_Title(x, y+=25, C_("U:"), SMALLFONT));
186 add_tool(new BC_Title(x, y+=25, D_("colorpicker_Cr#V:"), SMALLFONT));
187 if( thread->do_alpha )
188 add_tool(new BC_Title(x, y+=40, C_("A:"), SMALLFONT));
190 add_tool(hue = new PaletteHue(this, x, y= y0));
191 add_tool(sat = new PaletteSat(this, x, y+=25));
192 add_tool(val = new PaletteVal(this, x, y+=25));
193 add_tool(red = new PaletteRed(this, x, y+=40));
194 add_tool(grn = new PaletteGrn(this, x, y+=25));
195 add_tool(blu = new PaletteBlu(this, x, y+=25));
196 add_tool(lum = new PaletteLum(this, x, y+=40));
197 add_tool(c_r = new PaletteCr (this, x, y+=25));
198 add_tool(c_b = new PaletteCb (this, x, y+=25));
199 if( thread->do_alpha )
200 add_tool(alpha = new PaletteAlpha(this, x, y+=40));
202 x += hue->get_w() + 10;
203 hsv_h = new PaletteHSV(this, x,y= y0, hsv.h, 0, 360);
204 hsv_h->create_objects(); hsv_h->set_tooltip(_("Hue"));
205 hsv_s = new PaletteHSV(this, x,y+=25, hsv.s, 0, 1);
206 hsv_s->create_objects(); hsv_s->set_tooltip(_("Saturation"));
207 hsv_v = new PaletteHSV(this, x,y+=25, hsv.v, 0, 1);
208 hsv_v->create_objects(); hsv_v->set_tooltip(_("Value"));
209 rgb_r = new PaletteRGB(this, x,y+=40, rgb.r, 0, 1);
210 rgb_r->create_objects(); rgb_r->set_tooltip(_("Red"));
211 rgb_g = new PaletteRGB(this, x,y+=25, rgb.g, 0, 1);
212 rgb_g->create_objects(); rgb_g->set_tooltip(_("Green"));
213 rgb_b = new PaletteRGB(this, x,y+=25, rgb.b, 0, 1);
214 rgb_b->create_objects(); rgb_b->set_tooltip(_("Blue"));
215 yuv_y = new PaletteYUV(this, x,y+=40, yuv.y, 0, 1);
216 yuv_y->create_objects(); yuv_y->set_tooltip(_("Luminance"));
217 yuv_u = new PaletteYUV(this, x,y+=25, yuv.u, 0, 1);
218 yuv_u->create_objects(); yuv_u->set_tooltip(_("Blue Luminance Difference"));
219 yuv_v = new PaletteYUV(this, x,y+=25, yuv.v, 0, 1);
220 yuv_v->create_objects(); yuv_v->set_tooltip(_("Red Luminance Difference"));
221 if( thread->do_alpha ) {
222 aph_a = new PaletteAPH(this, x,y+=40, aph, 0, 1);
223 aph_a->create_objects(); aph_a->set_tooltip(_("Alpha"));
225 if( thread->do_okcancel ) {
226 add_tool(new BC_OKButton(this));
227 add_tool(new BC_CancelButton(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->do_okcancel ? 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 )
310 int ColorWindow::handle_event()
312 thread->handle_new_color(rgb888(), (int)(255*aph + 0.5));
316 void ColorWindow::get_screen_sample()
319 get_abs_cursor_xy(cx, cy);
320 BC_Capture capture_bitmap(1, 1, 0);
321 VFrame vframe(1,1,BC_RGB888);
322 capture_bitmap.capture_frame(&vframe, cx,cy);
323 unsigned char *data = vframe.get_data();
324 rgb.r = data[0]/255.; rgb.g = data[1]/255.; rgb.b = data[2]/255.;
328 int ColorWindow::cursor_motion_event()
330 if( button_grabbed && get_button_down() ) {
337 int ColorWindow::button_press_event()
339 if( button_grabbed ) {
346 int ColorWindow::button_release_event()
348 if( button_grabbed ) {
354 return handle_event();
359 void ColorWindow::update_rgb_hex(const char *hex)
362 if( sscanf(hex,"%x",&color) == 1 ) {
363 float r = ((color>>16) & 0xff) / 255.;
364 float g = ((color>>8) & 0xff) / 255.;
365 float b = ((color>>0) & 0xff) / 255.;
366 rgb.r = r; rgb.g = g; rgb.b = b;
374 PaletteWheel::PaletteWheel(ColorWindow *window, int x, int y)
375 : BC_SubWindow(x, y, 170, 170)
377 this->window = window;
383 PaletteWheel::~PaletteWheel()
387 int PaletteWheel::button_press_event()
389 if( get_cursor_x() >= 0 && get_cursor_x() < get_w() &&
390 get_cursor_y() >= 0 && get_cursor_y() < get_h() &&
393 cursor_motion_event();
399 int PaletteWheel::cursor_motion_event()
401 int x1, y1, distance;
402 if( button_down && is_event_win() ) {
403 float h = get_angle(get_w()/2, get_h()/2, get_cursor_x(), get_cursor_y());
404 bclamp(h, 0, 359.999); window->hsv.h = h;
405 x1 = get_w() / 2 - get_cursor_x();
406 y1 = get_h() / 2 - get_cursor_y();
407 distance = (int)sqrt(x1 * x1 + y1 * y1);
408 float s = (float)distance / (get_w() / 2);
409 bclamp(s, 0, 1); window->hsv.s = s;
411 window->update_hsv();
412 window->handle_event();
418 int PaletteWheel::button_release_event()
427 void PaletteWheel::create_objects()
430 //printf("PaletteWheel::create_objects 1\n");
433 float x1, y1, x2, y2;
435 int default_r, default_g, default_b;
436 VFrame frame(0, -1, get_w(), get_h(), BC_RGBA8888, -1);
439 default_r = (get_resources()->get_bg_color() & 0xff0000) >> 16;
440 default_g = (get_resources()->get_bg_color() & 0xff00) >> 8;
441 default_b = (get_resources()->get_bg_color() & 0xff);
442 //printf("PaletteWheel::create_objects 1\n");
444 int highlight_r = (get_resources()->button_light & 0xff0000) >> 16;
445 int highlight_g = (get_resources()->button_light & 0xff00) >> 8;
446 int highlight_b = (get_resources()->button_light & 0xff);
448 for( y2 = 0; y2 < get_h(); y2++ ) {
449 unsigned char *row = (unsigned char*)frame.get_rows()[(int)y2];
450 for( x2 = 0; x2 < get_w(); x2++ ) {
451 distance = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
452 if( distance > x1 ) {
453 row[(int)x2 * 4] = default_r;
454 row[(int)x2 * 4 + 1] = default_g;
455 row[(int)x2 * 4 + 2] = default_b;
456 row[(int)x2 * 4 + 3] = 0;
459 if( distance > x1 - 1 ) {
461 if( get_h() - y2 < x2 ) {
472 row[(int)x2 * 4] = r_i;
473 row[(int)x2 * 4 + 1] = g_i;
474 row[(int)x2 * 4 + 2] = b_i;
475 row[(int)x2 * 4 + 3] = 255;
478 h = get_angle(x1, y1, x2, y2);
480 HSV::hsv_to_rgb(r, g, b, h, s, v);
481 row[(int)x2 * 4] = (int)(r * 255);
482 row[(int)x2 * 4 + 1] = (int)(g * 255);
483 row[(int)x2 * 4 + 2] = (int)(b * 255);
484 row[(int)x2 * 4 + 3] = 255;
488 //printf("PaletteWheel::create_objects 1\n");
500 //printf("PaletteWheel::create_objects 1\n");
502 oldhue = window->hsv.h;
503 oldsaturation = window->hsv.s;
504 //printf("PaletteWheel::create_objects 1\n");
505 draw(oldhue, oldsaturation);
506 //printf("PaletteWheel::create_objects 1\n");
508 //printf("PaletteWheel::create_objects 2\n");
511 float PaletteWheel::torads(float angle)
513 return (float)angle / 360 * 2 * M_PI;
517 int PaletteWheel::draw(float hue, float saturation)
523 if( hue > 0 && hue < 90 ) {
524 x = (int)(w - w * cos(torads(90 - hue)) * saturation);
525 y = (int)(h - h * sin(torads(90 - hue)) * saturation);
527 else if( hue > 90 && hue < 180 ) {
528 x = (int)(w - w * cos(torads(hue - 90)) * saturation);
529 y = (int)(h + h * sin(torads(hue - 90)) * saturation);
531 else if( hue > 180 && hue < 270 ) {
532 x = (int)(w + w * cos(torads(270 - hue)) * saturation);
533 y = (int)(h + h * sin(torads(270 - hue)) * saturation);
535 else if( hue > 270 && hue < 360 ) {
536 x = (int)(w + w * cos(torads(hue - 270)) * saturation);
537 y = (int)(h - w * sin(torads(hue - 270)) * saturation);
539 else if( hue == 0 ) {
541 y = (int)(h - h * saturation);
543 else if( hue == 90 ) {
544 x = (int)(w - w * saturation);
547 else if( hue == 180 ) {
549 y = (int)(h + h * saturation);
551 else if( hue == 270 ) {
552 x = (int)(w + w * saturation);
558 draw_circle(x - 5, y - 5, 10, 10);
563 int PaletteWheel::get_angle(float x1, float y1, float x2, float y2)
565 float result = -atan2(x2 - x1, y1 - y2) * (360 / M_PI / 2);
571 PaletteWheelValue::PaletteWheelValue(ColorWindow *window, int x, int y)
572 : BC_SubWindow(x, y, 40, 170, BLACK)
574 this->window = window;
577 PaletteWheelValue::~PaletteWheelValue()
582 void PaletteWheelValue::create_objects()
584 frame = new VFrame(0, -1, get_w(), get_h(), BC_RGB888, -1);
585 draw(window->hsv.h, window->hsv.s, window->hsv.v);
589 int PaletteWheelValue::button_press_event()
591 //printf("PaletteWheelValue::button_press 1 %d\n", is_event_win());
592 if( get_cursor_x() >= 0 && get_cursor_x() < get_w() &&
593 get_cursor_y() >= 0 && get_cursor_y() < get_h() &&
595 //printf("PaletteWheelValue::button_press 2\n");
597 cursor_motion_event();
603 int PaletteWheelValue::cursor_motion_event()
605 if( button_down && is_event_win() ) {
606 //printf("PaletteWheelValue::cursor_motion 1\n");
607 float v = 1.0 - (float)(get_cursor_y() - 2) / (get_h() - 4);
608 bclamp(v, 0, 1); window->hsv.v = v;
609 window->update_hsv();
610 window->handle_event();
616 int PaletteWheelValue::button_release_event()
619 //printf("PaletteWheelValue::button_release 1\n");
626 int PaletteWheelValue::draw(float hue, float saturation, float value)
631 for( i = get_h() - 3; i >= 2; i-- ) {
632 unsigned char *row = (unsigned char*)frame->get_rows()[i];
633 HSV::hsv_to_rgb(r_f, g_f, b_f, hue, saturation,
634 1.0 - (float)(i - 2) / (get_h() - 4));
635 r = (int)(r_f * 255);
636 g = (int)(g_f * 255);
637 b = (int)(b_f * 255);
638 for( j = 0; j < get_w(); j++ ) {
645 draw_3d_border(0, 0, get_w(), get_h(), 1);
646 draw_vframe(frame, 2, 2, get_w() - 4, get_h() - 4,
647 2, 2, get_w() - 4, get_h() - 4, 0);
649 draw_line(2, get_h() - 3 - (int)(value * (get_h() - 5)),
650 get_w() - 3, get_h() - 3 - (int)(value * (get_h() - 5)));
651 //printf("PaletteWheelValue::draw %d %f\n", __LINE__, value);
656 PaletteOutput::PaletteOutput(ColorWindow *window, int x, int y)
657 : BC_SubWindow(x, y, 180, 30, BLACK)
659 this->window = window;
661 PaletteOutput::~PaletteOutput()
666 void PaletteOutput::create_objects()
672 int PaletteOutput::handle_event()
677 int PaletteOutput::draw()
679 set_color(window->rgb888());
680 draw_box(2, 2, get_w() - 4, get_h() - 4);
681 draw_3d_border(0, 0, get_w(), get_h(), 1);
685 PaletteHue::PaletteHue(ColorWindow *window, int x, int y)
686 : BC_ISlider(x, y, 0, 150, 200, 0, 359, (int)(window->hsv.h), 0)
688 this->window = window;
690 PaletteHue::~PaletteHue()
694 int PaletteHue::handle_event()
696 window->hsv.h = get_value();
697 window->update_hsv();
698 window->handle_event();
702 PaletteSat::PaletteSat(ColorWindow *window, int x, int y)
703 : BC_FSlider(x, y, 0, 150, 200, 0, 1.0, window->hsv.s, 0)
705 this->window = window;
708 PaletteSat::~PaletteSat()
712 int PaletteSat::handle_event()
714 window->hsv.s = get_value();
715 window->update_hsv();
716 window->handle_event();
721 PaletteVal::PaletteVal(ColorWindow *window, int x, int y)
722 : BC_FSlider(x, y, 0, 150, 200, 0, 1.0, window->hsv.v, 0)
724 this->window = window;
727 PaletteVal::~PaletteVal()
731 int PaletteVal::handle_event()
733 window->hsv.v = get_value();
734 window->update_hsv();
735 window->handle_event();
740 PaletteRed::PaletteRed(ColorWindow *window, int x, int y)
741 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->rgb.r, 0)
743 this->window = window;
746 PaletteRed::~PaletteRed()
750 int PaletteRed::handle_event()
752 window->rgb.r = get_value();
753 window->update_rgb();
754 window->handle_event();
758 PaletteGrn::PaletteGrn(ColorWindow *window, int x, int y)
759 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->rgb.g, 0)
761 this->window = window;
764 PaletteGrn::~PaletteGrn()
768 int PaletteGrn::handle_event()
770 window->rgb.g = get_value();
771 window->update_rgb();
772 window->handle_event();
776 PaletteBlu::PaletteBlu(ColorWindow *window, int x, int y)
777 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->rgb.b, 0)
779 this->window = window;
782 PaletteBlu::~PaletteBlu()
786 int PaletteBlu::handle_event()
788 window->rgb.b = get_value();
789 window->update_rgb();
790 window->handle_event();
794 PaletteAlpha::PaletteAlpha(ColorWindow *window, int x, int y)
795 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->aph, 0)
797 this->window = window;
800 PaletteAlpha::~PaletteAlpha()
804 int PaletteAlpha::handle_event()
806 window->aph = get_value();
807 window->handle_event();
811 PaletteLum::PaletteLum(ColorWindow *window, int x, int y)
812 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->yuv.y, 0)
814 this->window = window;
817 PaletteLum::~PaletteLum()
821 int PaletteLum::handle_event()
823 window->yuv.y = get_value();
824 window->update_yuv();
825 window->handle_event();
829 PaletteCr::PaletteCr(ColorWindow *window, int x, int y)
830 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->yuv.u, 0)
832 this->window = window;
835 PaletteCr::~PaletteCr()
839 int PaletteCr::handle_event()
841 window->yuv.u = get_value();
842 window->update_yuv();
843 window->handle_event();
847 PaletteCb::PaletteCb(ColorWindow *window, int x, int y)
848 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->yuv.v, 0)
850 this->window = window;
853 PaletteCb::~PaletteCb()
857 int PaletteCb::handle_event()
859 window->yuv.v = get_value();
860 window->update_yuv();
861 window->handle_event();
865 void ColorWindow::update_rgb(float r, float g, float b)
868 YUV::rgb_to_yuv_f(r, g, b, y, u, v);
870 bclamp(y, 0, 1); yuv.y = y;
871 bclamp(u, 0, 1); yuv.u = u;
872 bclamp(v, 0, 1); yuv.v = v; }
874 HSV::rgb_to_hsv(r,g,b, h,s,v);
875 bclamp(h, 0, 360); hsv.h = h;
876 bclamp(s, 0, 1); hsv.s = s;
877 bclamp(v, 0, 1); hsv.v = v; }
880 void ColorWindow::update_yuv(float y, float u, float v)
884 YUV::yuv_to_rgb_f(r, g, b, y, u, v);
885 bclamp(r, 0, 1); rgb.r = r;
886 bclamp(g, 0, 1); rgb.g = g;
887 bclamp(b, 0, 1); rgb.b = b;
889 HSV::rgb_to_hsv(r,g,b, h, s, v);
890 bclamp(h, 0, 360); hsv.h = h;
891 bclamp(s, 0, 1); hsv.s = s;
892 bclamp(v, 0, 1); hsv.v = v; }
895 void ColorWindow::update_hsv(float h, float s, float v)
898 HSV::hsv_to_rgb(r,g,b, h,s,v);
899 bclamp(r, 0, 1); rgb.r = r;
900 bclamp(g, 0, 1); rgb.g = g;
901 bclamp(b, 0, 1); rgb.b = b;
903 YUV::rgb_to_yuv_f(r, g, b, y, u, v);
905 bclamp(y, 0, 1); yuv.y = y;
906 bclamp(u, 0, 1); yuv.u = u;
907 bclamp(v, 0, 1); yuv.v = v; }
910 void ColorWindow::load_history()
912 char history_path[BCTEXTLEN];
913 MWindow::create_defaults_path(history_path,PALETTE_DATA);
914 FILE *fp = fopen(history_path,"r");
917 while( i < PALLETTE_HISTORY_SIZE ) {
919 if( !fgets(line,sizeof(line)-1,fp) ) break;
920 line[sizeof(line)-1] = 0;
921 if( sscanf(line, "%x",&palette_history[i]) != 1 ) break;
926 int r = 0, g = 0, b = 0;
927 float v0 = 0, v1 = 1;
928 while( i < PALLETTE_HISTORY_SIZE ) {
929 int grey_code = i ^ (i>>1);
930 r = 255 * ((grey_code&4) ? v0 : v1);
931 g = 255 * ((grey_code&2) ? v0 : v1);
932 b = 255 * ((grey_code&1) ? v0 : v1);
933 int color = (r<<16) | (g<<8) | (b<<0);
934 palette_history[i++] = color;
935 if( i & 7 ) continue;
936 v0 = 0.5f * (v0+.5f);
937 v1 = 0.5f * (v1+.5f);
940 void ColorWindow::save_history()
942 char history_path[BCTEXTLEN];
943 MWindow::create_defaults_path(history_path,PALETTE_DATA);
944 FILE *fp = fopen(history_path,"w");
946 for( int i=0; i<PALLETTE_HISTORY_SIZE; ++i ) {
947 fprintf(fp, "%06x\n", palette_history[i]);
952 void ColorWindow::update_history(int color)
954 int out = palette_history[0];
955 palette_history[0] = color;
956 for( int i=1; out != color && i<PALLETTE_HISTORY_SIZE; ++i ) {
958 out = palette_history[i];
959 palette_history[i] = in;
962 void ColorWindow::update_history()
964 update_history(rgb888());
967 int ColorWindow::rgb888()
969 int r = 255*rgb.r + 0.5, g = 255*rgb.g + 0.5, b = 255*rgb.b + 0.5;
970 bclamp(r, 0, 255); bclamp(g, 0, 255); bclamp(b, 0, 255);
971 return (r<<16) | (g<<8) | (b<<0);
974 PaletteNum::PaletteNum(ColorWindow *window, int x, int y,
975 float &output, float min, float max)
976 : BC_TumbleTextBox(window, output, min, max, x, y, 64)
978 this->window = window;
979 this->output = &output;
984 PaletteNum::~PaletteNum()
989 int PaletteHSV::handle_event()
992 window->update_hsv();
993 window->handle_event();
997 int PaletteRGB::handle_event()
1000 window->update_rgb();
1001 window->handle_event();
1005 int PaletteYUV::handle_event()
1008 window->update_yuv();
1009 window->handle_event();
1013 int PaletteAPH::handle_event()
1016 window->update_display();
1017 window->handle_event();
1021 PaletteHexButton::PaletteHexButton(ColorWindow *window, int x, int y)
1022 : BC_GenericButton(x, y, 50, "#")
1024 this->window = window;
1025 set_tooltip(_("hex rgb color"));
1027 PaletteHexButton::~PaletteHexButton()
1030 int PaletteHexButton::handle_event()
1032 const char *hex = window->hex_box->get_text();
1033 window->update_rgb_hex(hex);
1037 PaletteHex::PaletteHex(ColorWindow *window, int x, int y, const char *hex)
1038 : BC_TextBox(x, y, 100, 1, hex)
1040 this->window = window;
1042 PaletteHex::~PaletteHex()
1045 void PaletteHex::update()
1047 char hex[BCSTRLEN]; sprintf(hex,"%06x",window->rgb888());
1048 BC_TextBox::update(hex);
1051 int PaletteHex::keypress_event()
1053 if( get_keypress() != RETURN )
1054 return BC_TextBox::keypress_event();
1055 window->update_rgb_hex(get_text());
1059 #include "grabpick_up_png.h"
1060 #include "grabpick_hi_png.h"
1061 #include "grabpick_dn_png.h"
1063 PaletteGrabButton::PaletteGrabButton(ColorWindow *window, int x, int y)
1064 : BC_Button(x, y, vframes)
1066 this->window = window;
1067 vframes[0] = new VFramePng(grabpick_up_png);
1068 vframes[1] = new VFramePng(grabpick_hi_png);
1069 vframes[2] = new VFramePng(grabpick_dn_png);
1070 set_tooltip(_("grab from anywhere picker"));
1072 PaletteGrabButton::~PaletteGrabButton()
1074 for( int i=0; i<3; ++i )
1077 int PaletteGrabButton::handle_event()
1079 if( window->grab_buttons() ) {
1081 window->button_grabbed = 1;
1082 button_press_event(); // redraw face HI
1087 PaletteHistory::PaletteHistory(ColorWindow *window, int x, int y)
1088 : BC_SubWindow(x,y, 200, 24)
1090 this->window = window;
1092 set_tooltip(_("color history"));
1094 PaletteHistory::~PaletteHistory()
1097 void PaletteHistory::update(int flush)
1100 for( int i=0; i<PALLETTE_HISTORY_SIZE; x1=x2 ) {
1101 int rgb = window->palette_history[i];
1102 x2 = (++i * get_w())/PALLETTE_HISTORY_SIZE;
1103 draw_3d_box(x1,0,x2-x1,get_h(),WHITE,BLACK,rgb,LTBLUE,DKBLUE);
1108 int PaletteHistory::button_press_event()
1110 if( button_down || !is_event_win() ) return 0;
1112 cursor_motion_event();
1115 int PaletteHistory::button_release_event()
1117 if( !button_down || !is_event_win() ) return 0;
1118 cursor_motion_event();
1119 if( button_down > 0 ) {
1120 window->handle_event();
1121 window->update_display();
1122 window->update_history();
1127 int PaletteHistory::cursor_motion_event()
1129 if( !button_down || !is_event_win() ) return 0;
1131 int pick = (PALLETTE_HISTORY_SIZE * get_cursor_x()) / get_w();
1132 bclamp(pick, 0, PALLETTE_HISTORY_SIZE-1);
1133 int color = window->palette_history[pick];
1134 float r = ((color>>16) & 0xff) / 255.;
1135 float g = ((color>>8) & 0xff) / 255.;
1136 float b = ((color>>0) & 0xff) / 255.;
1137 if( window->rgb.r != r || window->rgb.g != g || window->rgb.b != b ) {
1138 window->rgb.r = r; window->rgb.g = g; window->rgb.b = b;
1139 window->update_rgb();
1144 int PaletteHistory::cursor_leave_event()
1149 int PaletteHistory::repeat_event(int64_t duration)
1153 if( duration == get_resources()->tooltip_delay &&
1154 get_tooltip() && *get_tooltip() && cursor_above() ) {