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"
24 #include "bcdisplayinfo.h"
25 #include "colorpicker.h"
26 #include "condition.h"
38 #define MSGQUAL "colorpicker"
40 #define PALETTE_DATA "palette.dat"
42 ColorThread::ColorThread(int do_alpha, const char *title)
46 this->do_alpha = do_alpha;
47 this->do_okcancel = 0;
52 ColorThread::~ColorThread()
57 void ColorThread::start_window(int output, int alpha, int do_okcancel)
60 ColorWindow *gui = (ColorWindow *)get_gui();
62 gui->lock_window("ColorThread::start_window");
68 this->output = output;
70 this->do_okcancel = do_okcancel;
74 BC_Window* ColorThread::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 ColorThread::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 ColorThread::handle_new_color(int output, int alpha)
109 printf("ColorThread::handle_new_color undefined.\n");
114 ColorWindow::ColorWindow(ColorThread *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->do_okcancel ) {
225 add_tool(new BC_OKButton(this));
226 add_tool(new BC_CancelButton(this));
236 void ColorWindow::change_values()
238 float r = ((thread->output>>16) & 0xff) / 255.;
239 float g = ((thread->output>>8) & 0xff) / 255.;
240 float b = ((thread->output>>0) & 0xff) / 255.;
241 rgb.r = r; rgb.g = g; rgb.b = b;
242 aph = (float)thread->alpha / 255;
243 update_rgb(rgb.r, rgb.g, rgb.b);
247 int ColorWindow::close_event()
249 set_done(thread->do_okcancel ? 1 : 0);
254 void ColorWindow::update_rgb()
256 update_rgb(rgb.r, rgb.g, rgb.b);
259 void ColorWindow::update_hsv()
261 update_hsv(hsv.h, hsv.s, hsv.v);
264 void ColorWindow::update_yuv()
266 update_yuv(yuv.y, yuv.u, yuv.v);
270 void ColorWindow::update_display()
272 wheel->draw(wheel->oldhue, wheel->oldsaturation);
273 wheel->oldhue = hsv.h;
274 wheel->oldsaturation = hsv.s;
275 wheel->draw(hsv.h, hsv.s);
277 wheel_value->draw(hsv.h, hsv.s, hsv.v);
278 wheel_value->flash();
282 hue->update((int)hsv.h);
294 hsv_h->update(hsv.h);
295 hsv_s->update(hsv.s);
296 hsv_v->update(hsv.v);
297 rgb_r->update(rgb.r);
298 rgb_g->update(rgb.g);
299 rgb_b->update(rgb.b);
300 yuv_y->update(yuv.y);
301 yuv_u->update(yuv.u);
302 yuv_v->update(yuv.v);
305 if( thread->do_alpha )
309 int ColorWindow::handle_event()
311 thread->handle_new_color(rgb888(), (int)(255*aph + 0.5));
315 void ColorWindow::get_screen_sample()
318 get_abs_cursor_xy(cx, cy);
319 BC_Capture capture_bitmap(1, 1, 0);
320 VFrame vframe(1,1,BC_RGB888);
321 capture_bitmap.capture_frame(&vframe, cx,cy);
322 unsigned char *data = vframe.get_data();
323 rgb.r = data[0]/255.; rgb.g = data[1]/255.; rgb.b = data[2]/255.;
327 int ColorWindow::cursor_motion_event()
329 if( button_grabbed && get_button_down() ) {
336 int ColorWindow::button_press_event()
338 if( button_grabbed ) {
345 int ColorWindow::button_release_event()
347 if( button_grabbed ) {
353 return handle_event();
358 void ColorWindow::update_rgb_hex(const char *hex)
361 if( sscanf(hex,"%x",&color) == 1 ) {
362 float r = ((color>>16) & 0xff) / 255.;
363 float g = ((color>>8) & 0xff) / 255.;
364 float b = ((color>>0) & 0xff) / 255.;
365 rgb.r = r; rgb.g = g; rgb.b = b;
373 PaletteWheel::PaletteWheel(ColorWindow *window, int x, int y)
374 : BC_SubWindow(x, y, 170, 170)
376 this->window = window;
382 PaletteWheel::~PaletteWheel()
386 int PaletteWheel::button_press_event()
388 if( get_cursor_x() >= 0 && get_cursor_x() < get_w() &&
389 get_cursor_y() >= 0 && get_cursor_y() < get_h() &&
392 cursor_motion_event();
398 int PaletteWheel::cursor_motion_event()
400 int x1, y1, distance;
401 if( button_down && is_event_win() ) {
402 float h = get_angle(get_w()/2, get_h()/2, get_cursor_x(), get_cursor_y());
403 bclamp(h, 0, 359.999); window->hsv.h = h;
404 x1 = get_w() / 2 - get_cursor_x();
405 y1 = get_h() / 2 - get_cursor_y();
406 distance = (int)sqrt(x1 * x1 + y1 * y1);
407 float s = (float)distance / (get_w() / 2);
408 bclamp(s, 0, 1); window->hsv.s = s;
410 window->update_hsv();
411 window->handle_event();
417 int PaletteWheel::button_release_event()
426 void PaletteWheel::create_objects()
429 //printf("PaletteWheel::create_objects 1\n");
432 float x1, y1, x2, y2;
434 int default_r, default_g, default_b;
435 VFrame frame(0, -1, get_w(), get_h(), BC_RGBA8888, -1);
438 default_r = (get_resources()->get_bg_color() & 0xff0000) >> 16;
439 default_g = (get_resources()->get_bg_color() & 0xff00) >> 8;
440 default_b = (get_resources()->get_bg_color() & 0xff);
441 //printf("PaletteWheel::create_objects 1\n");
443 int highlight_r = (get_resources()->button_light & 0xff0000) >> 16;
444 int highlight_g = (get_resources()->button_light & 0xff00) >> 8;
445 int highlight_b = (get_resources()->button_light & 0xff);
447 for( y2 = 0; y2 < get_h(); y2++ ) {
448 unsigned char *row = (unsigned char*)frame.get_rows()[(int)y2];
449 for( x2 = 0; x2 < get_w(); x2++ ) {
450 distance = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
451 if( distance > x1 ) {
452 row[(int)x2 * 4] = default_r;
453 row[(int)x2 * 4 + 1] = default_g;
454 row[(int)x2 * 4 + 2] = default_b;
455 row[(int)x2 * 4 + 3] = 0;
458 if( distance > x1 - 1 ) {
460 if( get_h() - y2 < x2 ) {
471 row[(int)x2 * 4] = r_i;
472 row[(int)x2 * 4 + 1] = g_i;
473 row[(int)x2 * 4 + 2] = b_i;
474 row[(int)x2 * 4 + 3] = 255;
477 h = get_angle(x1, y1, x2, y2);
479 HSV::hsv_to_rgb(r, g, b, h, s, v);
480 row[(int)x2 * 4] = (int)(r * 255);
481 row[(int)x2 * 4 + 1] = (int)(g * 255);
482 row[(int)x2 * 4 + 2] = (int)(b * 255);
483 row[(int)x2 * 4 + 3] = 255;
487 //printf("PaletteWheel::create_objects 1\n");
499 //printf("PaletteWheel::create_objects 1\n");
501 oldhue = window->hsv.h;
502 oldsaturation = window->hsv.s;
503 //printf("PaletteWheel::create_objects 1\n");
504 draw(oldhue, oldsaturation);
505 //printf("PaletteWheel::create_objects 1\n");
507 //printf("PaletteWheel::create_objects 2\n");
510 float PaletteWheel::torads(float angle)
512 return (float)angle / 360 * 2 * M_PI;
516 int PaletteWheel::draw(float hue, float saturation)
522 if( hue > 0 && hue < 90 ) {
523 x = (int)(w - w * cos(torads(90 - hue)) * saturation);
524 y = (int)(h - h * sin(torads(90 - hue)) * saturation);
526 else if( hue > 90 && hue < 180 ) {
527 x = (int)(w - w * cos(torads(hue - 90)) * saturation);
528 y = (int)(h + h * sin(torads(hue - 90)) * saturation);
530 else if( hue > 180 && hue < 270 ) {
531 x = (int)(w + w * cos(torads(270 - hue)) * saturation);
532 y = (int)(h + h * sin(torads(270 - hue)) * saturation);
534 else if( hue > 270 && hue < 360 ) {
535 x = (int)(w + w * cos(torads(hue - 270)) * saturation);
536 y = (int)(h - w * sin(torads(hue - 270)) * saturation);
538 else if( hue == 0 ) {
540 y = (int)(h - h * saturation);
542 else if( hue == 90 ) {
543 x = (int)(w - w * saturation);
546 else if( hue == 180 ) {
548 y = (int)(h + h * saturation);
550 else if( hue == 270 ) {
551 x = (int)(w + w * saturation);
557 draw_circle(x - 5, y - 5, 10, 10);
562 int PaletteWheel::get_angle(float x1, float y1, float x2, float y2)
564 float result = -atan2(x2 - x1, y1 - y2) * (360 / M_PI / 2);
570 PaletteWheelValue::PaletteWheelValue(ColorWindow *window, int x, int y)
571 : BC_SubWindow(x, y, 40, 170, BLACK)
573 this->window = window;
576 PaletteWheelValue::~PaletteWheelValue()
581 void PaletteWheelValue::create_objects()
583 frame = new VFrame(0, -1, get_w(), get_h(), BC_RGB888, -1);
584 draw(window->hsv.h, window->hsv.s, window->hsv.v);
588 int PaletteWheelValue::button_press_event()
590 //printf("PaletteWheelValue::button_press 1 %d\n", is_event_win());
591 if( get_cursor_x() >= 0 && get_cursor_x() < get_w() &&
592 get_cursor_y() >= 0 && get_cursor_y() < get_h() &&
594 //printf("PaletteWheelValue::button_press 2\n");
596 cursor_motion_event();
602 int PaletteWheelValue::cursor_motion_event()
604 if( button_down && is_event_win() ) {
605 //printf("PaletteWheelValue::cursor_motion 1\n");
606 float v = 1.0 - (float)(get_cursor_y() - 2) / (get_h() - 4);
607 bclamp(v, 0, 1); window->hsv.v = v;
608 window->update_hsv();
609 window->handle_event();
615 int PaletteWheelValue::button_release_event()
618 //printf("PaletteWheelValue::button_release 1\n");
625 int PaletteWheelValue::draw(float hue, float saturation, float value)
630 for( i = get_h() - 3; i >= 2; i-- ) {
631 unsigned char *row = (unsigned char*)frame->get_rows()[i];
632 HSV::hsv_to_rgb(r_f, g_f, b_f, hue, saturation,
633 1.0 - (float)(i - 2) / (get_h() - 4));
634 r = (int)(r_f * 255);
635 g = (int)(g_f * 255);
636 b = (int)(b_f * 255);
637 for( j = 0; j < get_w(); j++ ) {
644 draw_3d_border(0, 0, get_w(), get_h(), 1);
645 draw_vframe(frame, 2, 2, get_w() - 4, get_h() - 4,
646 2, 2, get_w() - 4, get_h() - 4, 0);
648 draw_line(2, get_h() - 3 - (int)(value * (get_h() - 5)),
649 get_w() - 3, get_h() - 3 - (int)(value * (get_h() - 5)));
650 //printf("PaletteWheelValue::draw %d %f\n", __LINE__, value);
655 PaletteOutput::PaletteOutput(ColorWindow *window, int x, int y)
656 : BC_SubWindow(x, y, 180, 30, BLACK)
658 this->window = window;
660 PaletteOutput::~PaletteOutput()
665 void PaletteOutput::create_objects()
671 int PaletteOutput::handle_event()
676 int PaletteOutput::draw()
678 set_color(window->rgb888());
679 draw_box(2, 2, get_w() - 4, get_h() - 4);
680 draw_3d_border(0, 0, get_w(), get_h(), 1);
684 PaletteHue::PaletteHue(ColorWindow *window, int x, int y)
685 : BC_ISlider(x, y, 0, 150, 200, 0, 359, (int)(window->hsv.h), 0)
687 this->window = window;
689 PaletteHue::~PaletteHue()
693 int PaletteHue::handle_event()
695 window->hsv.h = get_value();
696 window->update_hsv();
697 window->handle_event();
701 PaletteSat::PaletteSat(ColorWindow *window, int x, int y)
702 : BC_FSlider(x, y, 0, 150, 200, 0, 1.0, window->hsv.s, 0)
704 this->window = window;
707 PaletteSat::~PaletteSat()
711 int PaletteSat::handle_event()
713 window->hsv.s = get_value();
714 window->update_hsv();
715 window->handle_event();
720 PaletteVal::PaletteVal(ColorWindow *window, int x, int y)
721 : BC_FSlider(x, y, 0, 150, 200, 0, 1.0, window->hsv.v, 0)
723 this->window = window;
726 PaletteVal::~PaletteVal()
730 int PaletteVal::handle_event()
732 window->hsv.v = get_value();
733 window->update_hsv();
734 window->handle_event();
739 PaletteRed::PaletteRed(ColorWindow *window, int x, int y)
740 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->rgb.r, 0)
742 this->window = window;
745 PaletteRed::~PaletteRed()
749 int PaletteRed::handle_event()
751 window->rgb.r = get_value();
752 window->update_rgb();
753 window->handle_event();
757 PaletteGrn::PaletteGrn(ColorWindow *window, int x, int y)
758 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->rgb.g, 0)
760 this->window = window;
763 PaletteGrn::~PaletteGrn()
767 int PaletteGrn::handle_event()
769 window->rgb.g = get_value();
770 window->update_rgb();
771 window->handle_event();
775 PaletteBlu::PaletteBlu(ColorWindow *window, int x, int y)
776 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->rgb.b, 0)
778 this->window = window;
781 PaletteBlu::~PaletteBlu()
785 int PaletteBlu::handle_event()
787 window->rgb.b = get_value();
788 window->update_rgb();
789 window->handle_event();
793 PaletteAlpha::PaletteAlpha(ColorWindow *window, int x, int y)
794 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->aph, 0)
796 this->window = window;
799 PaletteAlpha::~PaletteAlpha()
803 int PaletteAlpha::handle_event()
805 window->aph = get_value();
806 window->handle_event();
810 PaletteLum::PaletteLum(ColorWindow *window, int x, int y)
811 : BC_FSlider(x, y, 0, 150, 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, 150, 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, 150, 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::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_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::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);
973 PaletteNum::PaletteNum(ColorWindow *window, int x, int y,
974 float &output, float min, float max)
975 : BC_TumbleTextBox(window, output, min, max, x, y, 64)
977 this->window = window;
978 this->output = &output;
983 PaletteNum::~PaletteNum()
988 int PaletteHSV::handle_event()
991 window->update_hsv();
992 window->handle_event();
996 int PaletteRGB::handle_event()
999 window->update_rgb();
1000 window->handle_event();
1004 int PaletteYUV::handle_event()
1007 window->update_yuv();
1008 window->handle_event();
1012 int PaletteAPH::handle_event()
1015 window->update_display();
1016 window->handle_event();
1020 PaletteHexButton::PaletteHexButton(ColorWindow *window, int x, int y)
1021 : BC_GenericButton(x, y, 50, "#")
1023 this->window = window;
1024 set_tooltip(_("hex rgb color"));
1026 PaletteHexButton::~PaletteHexButton()
1029 int PaletteHexButton::handle_event()
1031 const char *hex = window->hex_box->get_text();
1032 window->update_rgb_hex(hex);
1036 PaletteHex::PaletteHex(ColorWindow *window, int x, int y, const char *hex)
1037 : BC_TextBox(x, y, 100, 1, hex)
1039 this->window = window;
1041 PaletteHex::~PaletteHex()
1044 void PaletteHex::update()
1046 char hex[BCSTRLEN]; sprintf(hex,"%06x",window->rgb888());
1047 BC_TextBox::update(hex);
1050 int PaletteHex::keypress_event()
1052 if( get_keypress() != RETURN )
1053 return BC_TextBox::keypress_event();
1054 window->update_rgb_hex(get_text());
1058 #include "grabpick_up_png.h"
1059 #include "grabpick_hi_png.h"
1060 #include "grabpick_dn_png.h"
1062 PaletteGrabButton::PaletteGrabButton(ColorWindow *window, int x, int y)
1063 : BC_Button(x, y, vframes)
1065 this->window = window;
1066 vframes[0] = new VFramePng(grabpick_up_png);
1067 vframes[1] = new VFramePng(grabpick_hi_png);
1068 vframes[2] = new VFramePng(grabpick_dn_png);
1069 set_tooltip(_("grab from anywhere picker"));
1071 PaletteGrabButton::~PaletteGrabButton()
1073 for( int i=0; i<3; ++i )
1076 int PaletteGrabButton::handle_event()
1078 if( window->grab_buttons() ) {
1080 window->button_grabbed = 1;
1081 button_press_event(); // redraw face HI
1086 PaletteHistory::PaletteHistory(ColorWindow *window, int x, int y)
1087 : BC_SubWindow(x,y, 200, 24)
1089 this->window = window;
1091 set_tooltip(_("color history"));
1093 PaletteHistory::~PaletteHistory()
1096 void PaletteHistory::update(int flush)
1099 for( int i=0; i<PALLETTE_HISTORY_SIZE; x1=x2 ) {
1100 int rgb = window->palette_history[i];
1101 x2 = (++i * get_w())/PALLETTE_HISTORY_SIZE;
1102 draw_3d_box(x1,0,x2-x1,get_h(),WHITE,BLACK,rgb,LTBLUE,DKBLUE);
1107 int PaletteHistory::button_press_event()
1109 if( button_down || !is_event_win() ) return 0;
1111 cursor_motion_event();
1114 int PaletteHistory::button_release_event()
1116 if( !button_down || !is_event_win() ) return 0;
1117 cursor_motion_event();
1118 if( button_down > 0 ) {
1119 window->handle_event();
1120 window->update_display();
1121 window->update_history();
1126 int PaletteHistory::cursor_motion_event()
1128 if( !button_down || !is_event_win() ) return 0;
1130 int pick = (PALLETTE_HISTORY_SIZE * get_cursor_x()) / get_w();
1131 bclamp(pick, 0, PALLETTE_HISTORY_SIZE-1);
1132 int color = window->palette_history[pick];
1133 float r = ((color>>16) & 0xff) / 255.;
1134 float g = ((color>>8) & 0xff) / 255.;
1135 float b = ((color>>0) & 0xff) / 255.;
1136 if( window->rgb.r != r || window->rgb.g != g || window->rgb.b != b ) {
1137 window->rgb.r = r; window->rgb.g = g; window->rgb.b = b;
1138 window->update_rgb();
1143 int PaletteHistory::cursor_leave_event()
1148 int PaletteHistory::repeat_event(int64_t duration)
1152 if( duration == get_resources()->tooltip_delay &&
1153 get_tooltip() && *get_tooltip() && cursor_above() ) {