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;
45 this->do_okcancel = 0;
50 ColorPicker::~ColorPicker()
55 void ColorPicker::start_window(int output, int alpha, int do_okcancel)
58 ColorWindow *gui = (ColorWindow *)get_gui();
60 gui->lock_window("ColorPicker::start_window");
66 this->output = output;
68 this->do_okcancel = do_okcancel;
72 BC_Window* ColorPicker::new_gui()
74 char window_title[BCTEXTLEN];
75 strcpy(window_title, _(PROGRAM_NAME ": "));
76 strcat(window_title, title ? title : _("Color Picker"));
77 BC_DisplayInfo display_info;
78 int x = display_info.get_abs_cursor_x() + 25;
79 int y = display_info.get_abs_cursor_y() - 100;
82 h += bmax(BC_OKButton::calculate_h(),BC_CancelButton::calculate_h());
83 int root_w = display_info.get_root_w(), root_h = display_info.get_root_h();
84 if( x+w > root_w ) x = root_w - w;
85 if( y+h > root_h ) y = root_h - h;
88 ColorWindow *gui = new ColorWindow(this, x, y, w, h, window_title);
89 gui->create_objects();
93 void ColorPicker::update_gui(int output, int alpha)
95 ColorWindow *gui = (ColorWindow *)get_gui();
98 this->output = output;
100 gui->change_values();
101 gui->update_display();
102 gui->unlock_window();
105 int ColorPicker::handle_new_color(int output, int alpha)
107 printf("ColorPicker::handle_new_color undefined.\n");
112 ColorWindow::ColorWindow(ColorPicker *thread, int x, int y, int w, int h, const char *title)
113 : BC_Window(title, x, y, w, h, w, h, 0, 0, 1)
115 this->thread = thread;
120 hue = 0; sat = 0; val = 0;
121 red = 0; grn = 0; blu = 0;
122 lum = 0; c_r = 0; c_b = 0;
125 hsv_h = 0; hsv_s = 0; hsv_v = 0;
126 rgb_r = 0; rgb_g = 0; rgb_b = 0;
127 yuv_y = 0; yuv_u = 0; yuv_v = 0;
132 ColorWindow::~ColorWindow()
134 delete hsv_h; delete hsv_s; delete hsv_v;
135 delete rgb_r; delete rgb_g; delete rgb_b;
136 delete yuv_y; delete yuv_u; delete yuv_v;
139 if( button_grabbed ) {
143 update_history(rgb888());
147 void ColorWindow::create_objects()
149 int x0 = 10, y0 = 10;
150 lock_window("ColorWindow::create_objects");
154 add_tool(wheel = new PaletteWheel(this, x, y));
155 wheel->create_objects();
157 x += 180; add_tool(wheel_value = new PaletteWheelValue(this, x, y));
158 wheel_value->create_objects();
160 y += 180; add_tool(output = new PaletteOutput(this, x, y));
161 output->create_objects();
162 y += output->get_h() + 20;
164 load_history(); int x1 = x;
165 add_tool(hex_btn = new PaletteHexButton(this, x1, y));
166 char hex[BCSTRLEN]; sprintf(hex,"%06x",thread->output);
167 x1 += hex_btn->get_w() + 5;
168 add_tool(hex_box = new PaletteHex(this, x1, y, hex));
169 x1 += hex_box->get_w() + 15;
170 add_tool(grab_btn = new PaletteGrabButton(this, x1, y));
171 y += hex_box->get_h() + 15;
172 add_tool(history = new PaletteHistory(this, 10, y));
175 add_tool(new BC_Title(x, y =y0, C_("H:"), SMALLFONT));
176 add_tool(new BC_Title(x, y+=25, C_("S:"), SMALLFONT));
177 add_tool(new BC_Title(x, y+=25, D_("colorpicker_value#V:"), SMALLFONT));
178 add_tool(new BC_Title(x, y+=40, C_("R:"), SMALLFONT));
179 add_tool(new BC_Title(x, y+=25, C_("G:"), SMALLFONT));
180 add_tool(new BC_Title(x, y+=25, C_("B:"), SMALLFONT));
181 add_tool(new BC_Title(x, y+=40, C_("Y:"), SMALLFONT));
182 add_tool(new BC_Title(x, y+=25, C_("U:"), SMALLFONT));
183 add_tool(new BC_Title(x, y+=25, D_("colorpicker_Cr#V:"), SMALLFONT));
184 if( thread->do_alpha )
185 add_tool(new BC_Title(x, y+=40, C_("A:"), SMALLFONT));
187 add_tool(hue = new PaletteHue(this, x, y= y0));
188 add_tool(sat = new PaletteSat(this, x, y+=25));
189 add_tool(val = new PaletteVal(this, x, y+=25));
190 add_tool(red = new PaletteRed(this, x, y+=40));
191 add_tool(grn = new PaletteGrn(this, x, y+=25));
192 add_tool(blu = new PaletteBlu(this, x, y+=25));
193 add_tool(lum = new PaletteLum(this, x, y+=40));
194 add_tool(c_r = new PaletteCr (this, x, y+=25));
195 add_tool(c_b = new PaletteCb (this, x, y+=25));
196 if( thread->do_alpha )
197 add_tool(alpha = new PaletteAlpha(this, x, y+=40));
199 x += hue->get_w() + 10;
200 hsv_h = new PaletteHSV(this, x,y= y0, hsv.h, 0, 360);
201 hsv_h->create_objects(); hsv_h->set_tooltip(_("Hue"));
202 hsv_s = new PaletteHSV(this, x,y+=25, hsv.s, 0, 1);
203 hsv_s->create_objects(); hsv_s->set_tooltip(_("Saturation"));
204 hsv_v = new PaletteHSV(this, x,y+=25, hsv.v, 0, 1);
205 hsv_v->create_objects(); hsv_v->set_tooltip(_("Value"));
206 rgb_r = new PaletteRGB(this, x,y+=40, rgb.r, 0, 1);
207 rgb_r->create_objects(); rgb_r->set_tooltip(_("Red"));
208 rgb_g = new PaletteRGB(this, x,y+=25, rgb.g, 0, 1);
209 rgb_g->create_objects(); rgb_g->set_tooltip(_("Green"));
210 rgb_b = new PaletteRGB(this, x,y+=25, rgb.b, 0, 1);
211 rgb_b->create_objects(); rgb_b->set_tooltip(_("Blue"));
212 yuv_y = new PaletteYUV(this, x,y+=40, yuv.y, 0, 1);
213 yuv_y->create_objects(); yuv_y->set_tooltip(_("Luminance"));
214 yuv_u = new PaletteYUV(this, x,y+=25, yuv.u, 0, 1);
215 yuv_u->create_objects(); yuv_u->set_tooltip(_("Blue Luminance Difference"));
216 yuv_v = new PaletteYUV(this, x,y+=25, yuv.v, 0, 1);
217 yuv_v->create_objects(); yuv_v->set_tooltip(_("Red Luminance Difference"));
218 if( thread->do_alpha ) {
219 aph_a = new PaletteAPH(this, x,y+=40, aph, 0, 1);
220 aph_a->create_objects(); aph_a->set_tooltip(_("Alpha"));
222 if( thread->do_okcancel ) {
223 add_tool(new BC_OKButton(this));
224 add_tool(new BC_CancelButton(this));
234 void ColorWindow::change_values()
236 float r = ((thread->output>>16) & 0xff) / 255.;
237 float g = ((thread->output>>8) & 0xff) / 255.;
238 float b = ((thread->output>>0) & 0xff) / 255.;
239 rgb.r = r; rgb.g = g; rgb.b = b;
240 aph = (float)thread->alpha / 255;
241 update_rgb(rgb.r, rgb.g, rgb.b);
245 int ColorWindow::close_event()
247 set_done(thread->do_okcancel ? 1 : 0);
252 void ColorWindow::update_rgb()
254 update_rgb(rgb.r, rgb.g, rgb.b);
257 void ColorWindow::update_hsv()
259 update_hsv(hsv.h, hsv.s, hsv.v);
262 void ColorWindow::update_yuv()
264 update_yuv(yuv.y, yuv.u, yuv.v);
268 void ColorWindow::update_display()
270 wheel->draw(wheel->oldhue, wheel->oldsaturation);
271 wheel->oldhue = hsv.h;
272 wheel->oldsaturation = hsv.s;
273 wheel->draw(hsv.h, hsv.s);
275 wheel_value->draw(hsv.h, hsv.s, hsv.v);
276 wheel_value->flash();
280 hue->update((int)hsv.h);
292 hsv_h->update(hsv.h);
293 hsv_s->update(hsv.s);
294 hsv_v->update(hsv.v);
295 rgb_r->update(rgb.r);
296 rgb_g->update(rgb.g);
297 rgb_b->update(rgb.b);
298 yuv_y->update(yuv.y);
299 yuv_u->update(yuv.u);
300 yuv_v->update(yuv.v);
303 if( thread->do_alpha )
307 int ColorWindow::handle_event()
309 thread->handle_new_color(rgb888(), (int)(255*aph + 0.5));
313 void ColorWindow::get_screen_sample()
316 get_abs_cursor(cx, cy);
317 BC_Capture capture_bitmap(1, 1, 0);
318 VFrame vframe(1,1,BC_RGB888);
319 capture_bitmap.capture_frame(&vframe, cx,cy);
320 unsigned char *data = vframe.get_data();
321 rgb.r = data[0]/255.; rgb.g = data[1]/255.; rgb.b = data[2]/255.;
325 int ColorWindow::cursor_motion_event()
327 if( button_grabbed && get_button_down() ) {
334 int ColorWindow::button_press_event()
336 if( button_grabbed ) {
343 int ColorWindow::button_release_event()
345 if( button_grabbed ) {
351 return handle_event();
356 void ColorWindow::update_rgb_hex(const char *hex)
359 if( sscanf(hex,"%x",&color) == 1 ) {
360 float r = ((color>>16) & 0xff) / 255.;
361 float g = ((color>>8) & 0xff) / 255.;
362 float b = ((color>>0) & 0xff) / 255.;
363 rgb.r = r; rgb.g = g; rgb.b = b;
371 PaletteWheel::PaletteWheel(ColorWindow *window, int x, int y)
372 : BC_SubWindow(x, y, 170, 170)
374 this->window = window;
380 PaletteWheel::~PaletteWheel()
384 int PaletteWheel::button_press_event()
386 if( get_cursor_x() >= 0 && get_cursor_x() < get_w() &&
387 get_cursor_y() >= 0 && get_cursor_y() < get_h() &&
390 cursor_motion_event();
396 int PaletteWheel::cursor_motion_event()
398 int x1, y1, distance;
399 if( button_down && is_event_win() ) {
400 float h = get_angle(get_w()/2, get_h()/2, get_cursor_x(), get_cursor_y());
401 bclamp(h, 0, 359.999); window->hsv.h = h;
402 x1 = get_w() / 2 - get_cursor_x();
403 y1 = get_h() / 2 - get_cursor_y();
404 distance = (int)sqrt(x1 * x1 + y1 * y1);
405 float s = (float)distance / (get_w() / 2);
406 bclamp(s, 0, 1); window->hsv.s = s;
408 window->update_hsv();
409 window->handle_event();
415 int PaletteWheel::button_release_event()
424 void PaletteWheel::create_objects()
427 //printf("PaletteWheel::create_objects 1\n");
430 float x1, y1, x2, y2;
432 int default_r, default_g, default_b;
433 VFrame frame(0, -1, get_w(), get_h(), BC_RGBA8888, -1);
436 default_r = (get_resources()->get_bg_color() & 0xff0000) >> 16;
437 default_g = (get_resources()->get_bg_color() & 0xff00) >> 8;
438 default_b = (get_resources()->get_bg_color() & 0xff);
439 //printf("PaletteWheel::create_objects 1\n");
441 int highlight_r = (get_resources()->button_light & 0xff0000) >> 16;
442 int highlight_g = (get_resources()->button_light & 0xff00) >> 8;
443 int highlight_b = (get_resources()->button_light & 0xff);
445 for( y2 = 0; y2 < get_h(); y2++ ) {
446 unsigned char *row = (unsigned char*)frame.get_rows()[(int)y2];
447 for( x2 = 0; x2 < get_w(); x2++ ) {
448 distance = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
449 if( distance > x1 ) {
450 row[(int)x2 * 4] = default_r;
451 row[(int)x2 * 4 + 1] = default_g;
452 row[(int)x2 * 4 + 2] = default_b;
453 row[(int)x2 * 4 + 3] = 0;
456 if( distance > x1 - 1 ) {
458 if( get_h() - y2 < x2 ) {
469 row[(int)x2 * 4] = r_i;
470 row[(int)x2 * 4 + 1] = g_i;
471 row[(int)x2 * 4 + 2] = b_i;
472 row[(int)x2 * 4 + 3] = 255;
475 h = get_angle(x1, y1, x2, y2);
477 HSV::hsv_to_rgb(r, g, b, h, s, v);
478 row[(int)x2 * 4] = (int)(r * 255);
479 row[(int)x2 * 4 + 1] = (int)(g * 255);
480 row[(int)x2 * 4 + 2] = (int)(b * 255);
481 row[(int)x2 * 4 + 3] = 255;
485 //printf("PaletteWheel::create_objects 1\n");
497 //printf("PaletteWheel::create_objects 1\n");
499 oldhue = window->hsv.h;
500 oldsaturation = window->hsv.s;
501 //printf("PaletteWheel::create_objects 1\n");
502 draw(oldhue, oldsaturation);
503 //printf("PaletteWheel::create_objects 1\n");
505 //printf("PaletteWheel::create_objects 2\n");
508 float PaletteWheel::torads(float angle)
510 return (float)angle / 360 * 2 * M_PI;
514 int PaletteWheel::draw(float hue, float saturation)
520 if( hue > 0 && hue < 90 ) {
521 x = (int)(w - w * cos(torads(90 - hue)) * saturation);
522 y = (int)(h - h * sin(torads(90 - hue)) * saturation);
524 else if( hue > 90 && hue < 180 ) {
525 x = (int)(w - w * cos(torads(hue - 90)) * saturation);
526 y = (int)(h + h * sin(torads(hue - 90)) * saturation);
528 else if( hue > 180 && hue < 270 ) {
529 x = (int)(w + w * cos(torads(270 - hue)) * saturation);
530 y = (int)(h + h * sin(torads(270 - hue)) * saturation);
532 else if( hue > 270 && hue < 360 ) {
533 x = (int)(w + w * cos(torads(hue - 270)) * saturation);
534 y = (int)(h - w * sin(torads(hue - 270)) * saturation);
536 else if( hue == 0 ) {
538 y = (int)(h - h * saturation);
540 else if( hue == 90 ) {
541 x = (int)(w - w * saturation);
544 else if( hue == 180 ) {
546 y = (int)(h + h * saturation);
548 else if( hue == 270 ) {
549 x = (int)(w + w * saturation);
555 draw_circle(x - 5, y - 5, 10, 10);
560 int PaletteWheel::get_angle(float x1, float y1, float x2, float y2)
562 float result = -atan2(x2 - x1, y1 - y2) * (360 / M_PI / 2);
568 PaletteWheelValue::PaletteWheelValue(ColorWindow *window, int x, int y)
569 : BC_SubWindow(x, y, 40, 170, BLACK)
571 this->window = window;
574 PaletteWheelValue::~PaletteWheelValue()
579 void PaletteWheelValue::create_objects()
581 frame = new VFrame(get_w(), get_h(), BC_RGB888);
582 draw(window->hsv.h, window->hsv.s, window->hsv.v);
586 int PaletteWheelValue::button_press_event()
588 //printf("PaletteWheelValue::button_press 1 %d\n", is_event_win());
589 if( get_cursor_x() >= 0 && get_cursor_x() < get_w() &&
590 get_cursor_y() >= 0 && get_cursor_y() < get_h() &&
592 //printf("PaletteWheelValue::button_press 2\n");
594 cursor_motion_event();
600 int PaletteWheelValue::cursor_motion_event()
602 if( button_down && is_event_win() ) {
603 //printf("PaletteWheelValue::cursor_motion 1\n");
604 float v = 1.0 - (float)(get_cursor_y() - 2) / (get_h() - 4);
605 bclamp(v, 0, 1); window->hsv.v = v;
606 window->update_hsv();
607 window->handle_event();
613 int PaletteWheelValue::button_release_event()
616 //printf("PaletteWheelValue::button_release 1\n");
623 int PaletteWheelValue::draw(float hue, float saturation, float value)
628 for( i = get_h() - 3; i >= 2; i-- ) {
629 unsigned char *row = (unsigned char*)frame->get_rows()[i];
630 HSV::hsv_to_rgb(r_f, g_f, b_f, hue, saturation,
631 1.0 - (float)(i - 2) / (get_h() - 4));
632 r = (int)(r_f * 255);
633 g = (int)(g_f * 255);
634 b = (int)(b_f * 255);
635 for( j = 0; j < get_w(); j++ ) {
642 draw_3d_border(0, 0, get_w(), get_h(), 1);
643 draw_vframe(frame, 2, 2, get_w() - 4, get_h() - 4,
644 2, 2, get_w() - 4, get_h() - 4, 0);
646 draw_line(2, get_h() - 3 - (int)(value * (get_h() - 5)),
647 get_w() - 3, get_h() - 3 - (int)(value * (get_h() - 5)));
648 //printf("PaletteWheelValue::draw %d %f\n", __LINE__, value);
653 PaletteOutput::PaletteOutput(ColorWindow *window, int x, int y)
654 : BC_SubWindow(x, y, 180, 30, BLACK)
656 this->window = window;
658 PaletteOutput::~PaletteOutput()
663 void PaletteOutput::create_objects()
669 int PaletteOutput::handle_event()
674 int PaletteOutput::draw()
676 set_color(window->rgb888());
677 draw_box(2, 2, get_w() - 4, get_h() - 4);
678 draw_3d_border(0, 0, get_w(), get_h(), 1);
682 PaletteHue::PaletteHue(ColorWindow *window, int x, int y)
683 : BC_ISlider(x, y, 0, 150, 200, 0, 359, (int)(window->hsv.h), 0)
685 this->window = window;
687 PaletteHue::~PaletteHue()
691 int PaletteHue::handle_event()
693 window->hsv.h = get_value();
694 window->update_hsv();
695 window->handle_event();
699 PaletteSat::PaletteSat(ColorWindow *window, int x, int y)
700 : BC_FSlider(x, y, 0, 150, 200, 0, 1.0, window->hsv.s, 0)
702 this->window = window;
705 PaletteSat::~PaletteSat()
709 int PaletteSat::handle_event()
711 window->hsv.s = get_value();
712 window->update_hsv();
713 window->handle_event();
718 PaletteVal::PaletteVal(ColorWindow *window, int x, int y)
719 : BC_FSlider(x, y, 0, 150, 200, 0, 1.0, window->hsv.v, 0)
721 this->window = window;
724 PaletteVal::~PaletteVal()
728 int PaletteVal::handle_event()
730 window->hsv.v = get_value();
731 window->update_hsv();
732 window->handle_event();
737 PaletteRed::PaletteRed(ColorWindow *window, int x, int y)
738 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->rgb.r, 0)
740 this->window = window;
743 PaletteRed::~PaletteRed()
747 int PaletteRed::handle_event()
749 window->rgb.r = get_value();
750 window->update_rgb();
751 window->handle_event();
755 PaletteGrn::PaletteGrn(ColorWindow *window, int x, int y)
756 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->rgb.g, 0)
758 this->window = window;
761 PaletteGrn::~PaletteGrn()
765 int PaletteGrn::handle_event()
767 window->rgb.g = get_value();
768 window->update_rgb();
769 window->handle_event();
773 PaletteBlu::PaletteBlu(ColorWindow *window, int x, int y)
774 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->rgb.b, 0)
776 this->window = window;
779 PaletteBlu::~PaletteBlu()
783 int PaletteBlu::handle_event()
785 window->rgb.b = get_value();
786 window->update_rgb();
787 window->handle_event();
791 PaletteAlpha::PaletteAlpha(ColorWindow *window, int x, int y)
792 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->aph, 0)
794 this->window = window;
797 PaletteAlpha::~PaletteAlpha()
801 int PaletteAlpha::handle_event()
803 window->aph = get_value();
804 window->handle_event();
808 PaletteLum::PaletteLum(ColorWindow *window, int x, int y)
809 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->yuv.y, 0)
811 this->window = window;
814 PaletteLum::~PaletteLum()
818 int PaletteLum::handle_event()
820 window->yuv.y = get_value();
821 window->update_yuv();
822 window->handle_event();
826 PaletteCr::PaletteCr(ColorWindow *window, int x, int y)
827 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->yuv.u, 0)
829 this->window = window;
832 PaletteCr::~PaletteCr()
836 int PaletteCr::handle_event()
838 window->yuv.u = get_value();
839 window->update_yuv();
840 window->handle_event();
844 PaletteCb::PaletteCb(ColorWindow *window, int x, int y)
845 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->yuv.v, 0)
847 this->window = window;
850 PaletteCb::~PaletteCb()
854 int PaletteCb::handle_event()
856 window->yuv.v = get_value();
857 window->update_yuv();
858 window->handle_event();
862 void ColorWindow::update_rgb(float r, float g, float b)
865 YUV::yuv.rgb_to_yuv_f(r, g, b, y, u, v);
867 bclamp(y, 0, 1); yuv.y = y;
868 bclamp(u, 0, 1); yuv.u = u;
869 bclamp(v, 0, 1); yuv.v = v; }
871 HSV::rgb_to_hsv(r,g,b, h,s,v);
872 bclamp(h, 0, 360); hsv.h = h;
873 bclamp(s, 0, 1); hsv.s = s;
874 bclamp(v, 0, 1); hsv.v = v; }
877 void ColorWindow::update_yuv(float y, float u, float v)
881 YUV::yuv.yuv_to_rgb_f(r, g, b, y, u, v);
882 bclamp(r, 0, 1); rgb.r = r;
883 bclamp(g, 0, 1); rgb.g = g;
884 bclamp(b, 0, 1); rgb.b = b;
886 HSV::rgb_to_hsv(r,g,b, h, s, v);
887 bclamp(h, 0, 360); hsv.h = h;
888 bclamp(s, 0, 1); hsv.s = s;
889 bclamp(v, 0, 1); hsv.v = v; }
892 void ColorWindow::update_hsv(float h, float s, float v)
895 HSV::hsv_to_rgb(r,g,b, h,s,v);
896 bclamp(r, 0, 1); rgb.r = r;
897 bclamp(g, 0, 1); rgb.g = g;
898 bclamp(b, 0, 1); rgb.b = b;
900 YUV::yuv.rgb_to_yuv_f(r, g, b, y, u, v);
902 bclamp(y, 0, 1); yuv.y = y;
903 bclamp(u, 0, 1); yuv.u = u;
904 bclamp(v, 0, 1); yuv.v = v; }
907 void ColorWindow::load_history()
909 char history_path[BCTEXTLEN];
910 MWindow::create_defaults_path(history_path,PALETTE_DATA);
911 FILE *fp = fopen(history_path,"r");
914 while( i < PALLETTE_HISTORY_SIZE ) {
916 if( !fgets(line,sizeof(line)-1,fp) ) break;
917 line[sizeof(line)-1] = 0;
918 if( sscanf(line, "%x",&palette_history[i]) != 1 ) break;
923 int r = 0, g = 0, b = 0;
924 float v0 = 0, v1 = 1;
925 while( i < PALLETTE_HISTORY_SIZE ) {
926 int grey_code = i ^ (i>>1);
927 r = 255 * ((grey_code&4) ? v0 : v1);
928 g = 255 * ((grey_code&2) ? v0 : v1);
929 b = 255 * ((grey_code&1) ? v0 : v1);
930 int color = (r<<16) | (g<<8) | (b<<0);
931 palette_history[i++] = color;
932 if( i & 7 ) continue;
933 v0 = 0.5f * (v0+.5f);
934 v1 = 0.5f * (v1+.5f);
937 void ColorWindow::save_history()
939 char history_path[BCTEXTLEN];
940 MWindow::create_defaults_path(history_path,PALETTE_DATA);
941 FILE *fp = fopen(history_path,"w");
943 for( int i=0; i<PALLETTE_HISTORY_SIZE; ++i ) {
944 fprintf(fp, "%06x\n", palette_history[i]);
949 void ColorWindow::update_history(int color)
951 int out = palette_history[0];
952 palette_history[0] = color;
953 for( int i=1; out != color && i<PALLETTE_HISTORY_SIZE; ++i ) {
955 out = palette_history[i];
956 palette_history[i] = in;
959 void ColorWindow::update_history()
961 update_history(rgb888());
964 int ColorWindow::rgb888()
966 int r = 255*rgb.r + 0.5, g = 255*rgb.g + 0.5, b = 255*rgb.b + 0.5;
967 bclamp(r, 0, 255); bclamp(g, 0, 255); bclamp(b, 0, 255);
968 return (r<<16) | (g<<8) | (b<<0);
971 PaletteNum::PaletteNum(ColorWindow *window, int x, int y,
972 float &output, float min, float max)
973 : BC_TumbleTextBox(window, output, min, max, x, y, 64)
975 this->window = window;
976 this->output = &output;
981 PaletteNum::~PaletteNum()
986 int PaletteHSV::handle_event()
989 window->update_hsv();
990 window->handle_event();
994 int PaletteRGB::handle_event()
997 window->update_rgb();
998 window->handle_event();
1002 int PaletteYUV::handle_event()
1005 window->update_yuv();
1006 window->handle_event();
1010 int PaletteAPH::handle_event()
1013 window->update_display();
1014 window->handle_event();
1018 PaletteHexButton::PaletteHexButton(ColorWindow *window, int x, int y)
1019 : BC_GenericButton(x, y, 50, "#")
1021 this->window = window;
1022 set_tooltip(_("hex rgb color"));
1024 PaletteHexButton::~PaletteHexButton()
1027 int PaletteHexButton::handle_event()
1029 const char *hex = window->hex_box->get_text();
1030 window->update_rgb_hex(hex);
1034 PaletteHex::PaletteHex(ColorWindow *window, int x, int y, const char *hex)
1035 : BC_TextBox(x, y, 100, 1, hex)
1037 this->window = window;
1039 PaletteHex::~PaletteHex()
1042 void PaletteHex::update()
1044 char hex[BCSTRLEN]; sprintf(hex,"%06x",window->rgb888());
1045 BC_TextBox::update(hex);
1048 int PaletteHex::keypress_event()
1050 if( get_keypress() != RETURN )
1051 return BC_TextBox::keypress_event();
1052 window->update_rgb_hex(get_text());
1056 #include "grabpick_up_png.h"
1057 #include "grabpick_hi_png.h"
1058 #include "grabpick_dn_png.h"
1060 PaletteGrabButton::PaletteGrabButton(ColorWindow *window, int x, int y)
1061 : BC_Button(x, y, vframes)
1063 this->window = window;
1064 vframes[0] = new VFramePng(grabpick_up_png);
1065 vframes[1] = new VFramePng(grabpick_hi_png);
1066 vframes[2] = new VFramePng(grabpick_dn_png);
1067 set_tooltip(_("grab from anywhere picker"));
1069 PaletteGrabButton::~PaletteGrabButton()
1071 for( int i=0; i<3; ++i )
1074 int PaletteGrabButton::handle_event()
1076 if( window->grab_buttons() ) {
1078 window->button_grabbed = 1;
1079 button_press_event(); // redraw face HI
1084 PaletteHistory::PaletteHistory(ColorWindow *window, int x, int y)
1085 : BC_SubWindow(x,y, 200, 24)
1087 this->window = window;
1089 set_tooltip(_("color history"));
1091 PaletteHistory::~PaletteHistory()
1094 void PaletteHistory::update(int flush)
1097 for( int i=0; i<PALLETTE_HISTORY_SIZE; x1=x2 ) {
1098 int rgb = window->palette_history[i];
1099 x2 = (++i * get_w())/PALLETTE_HISTORY_SIZE;
1100 draw_3d_box(x1,0,x2-x1,get_h(),WHITE,BLACK,rgb,LTBLUE,DKBLUE);
1105 int PaletteHistory::button_press_event()
1107 if( button_down || !is_event_win() ) return 0;
1109 cursor_motion_event();
1112 int PaletteHistory::button_release_event()
1114 if( !button_down || !is_event_win() ) return 0;
1115 cursor_motion_event();
1116 if( button_down > 0 ) {
1117 window->handle_event();
1118 window->update_display();
1119 window->update_history();
1124 int PaletteHistory::cursor_motion_event()
1126 if( !button_down || !is_event_win() ) return 0;
1128 int pick = (PALLETTE_HISTORY_SIZE * get_cursor_x()) / get_w();
1129 bclamp(pick, 0, PALLETTE_HISTORY_SIZE-1);
1130 int color = window->palette_history[pick];
1131 float r = ((color>>16) & 0xff) / 255.;
1132 float g = ((color>>8) & 0xff) / 255.;
1133 float b = ((color>>0) & 0xff) / 255.;
1134 if( window->rgb.r != r || window->rgb.g != g || window->rgb.b != b ) {
1135 window->rgb.r = r; window->rgb.g = g; window->rgb.b = b;
1136 window->update_rgb();
1141 int PaletteHistory::cursor_leave_event()
1146 int PaletteHistory::repeat_event(int64_t duration)
1150 if( duration == get_resources()->tooltip_delay &&
1151 get_tooltip() && *get_tooltip() && cursor_above() ) {