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;
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 do_okcancel)
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->do_okcancel = do_okcancel;
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->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 ) {
311 int ColorWindow::handle_event()
313 thread->handle_new_color(rgb888(), (int)(255*aph + 0.5));
317 void ColorWindow::get_screen_sample()
320 get_abs_cursor(cx, cy);
321 BC_Capture capture_bitmap(1, 1, 0);
322 VFrame vframe(1,1,BC_RGB888);
323 capture_bitmap.capture_frame(&vframe, cx,cy);
324 unsigned char *data = vframe.get_data();
325 rgb.r = data[0]/255.; rgb.g = data[1]/255.; rgb.b = data[2]/255.;
329 int ColorWindow::cursor_motion_event()
331 if( button_grabbed && get_button_down() ) {
338 int ColorWindow::button_press_event()
340 if( button_grabbed ) {
347 int ColorWindow::button_release_event()
349 if( button_grabbed ) {
355 return handle_event();
360 void ColorWindow::update_rgb_hex(const char *hex)
363 if( sscanf(hex,"%x",&color) == 1 ) {
364 float r = ((color>>16) & 0xff) / 255.;
365 float g = ((color>>8) & 0xff) / 255.;
366 float b = ((color>>0) & 0xff) / 255.;
367 rgb.r = r; rgb.g = g; rgb.b = b;
375 PaletteWheel::PaletteWheel(ColorWindow *window, int x, int y)
376 : BC_SubWindow(x, y, 170, 170)
378 this->window = window;
384 PaletteWheel::~PaletteWheel()
388 int PaletteWheel::button_press_event()
390 if( get_cursor_x() >= 0 && get_cursor_x() < get_w() &&
391 get_cursor_y() >= 0 && get_cursor_y() < get_h() &&
394 cursor_motion_event();
400 int PaletteWheel::cursor_motion_event()
402 int x1, y1, distance;
403 if( button_down && is_event_win() ) {
404 float h = get_angle(get_w()/2, get_h()/2, get_cursor_x(), get_cursor_y());
405 bclamp(h, 0, 359.999); window->hsv.h = h;
406 x1 = get_w() / 2 - get_cursor_x();
407 y1 = get_h() / 2 - get_cursor_y();
408 distance = (int)sqrt(x1 * x1 + y1 * y1);
409 float s = (float)distance / (get_w() / 2);
410 bclamp(s, 0, 1); window->hsv.s = s;
412 window->update_hsv();
413 window->handle_event();
419 int PaletteWheel::button_release_event()
428 void PaletteWheel::create_objects()
431 //printf("PaletteWheel::create_objects 1\n");
434 float x1, y1, x2, y2;
436 int default_r, default_g, default_b;
437 VFrame frame(0, -1, get_w(), get_h(), BC_RGBA8888, -1);
440 default_r = (get_resources()->get_bg_color() & 0xff0000) >> 16;
441 default_g = (get_resources()->get_bg_color() & 0xff00) >> 8;
442 default_b = (get_resources()->get_bg_color() & 0xff);
443 //printf("PaletteWheel::create_objects 1\n");
445 int highlight_r = (get_resources()->button_light & 0xff0000) >> 16;
446 int highlight_g = (get_resources()->button_light & 0xff00) >> 8;
447 int highlight_b = (get_resources()->button_light & 0xff);
449 for( y2 = 0; y2 < get_h(); y2++ ) {
450 unsigned char *row = (unsigned char*)frame.get_rows()[(int)y2];
451 for( x2 = 0; x2 < get_w(); x2++ ) {
452 distance = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
453 if( distance > x1 ) {
454 row[(int)x2 * 4] = default_r;
455 row[(int)x2 * 4 + 1] = default_g;
456 row[(int)x2 * 4 + 2] = default_b;
457 row[(int)x2 * 4 + 3] = 0;
460 if( distance > x1 - 1 ) {
462 if( get_h() - y2 < x2 ) {
473 row[(int)x2 * 4] = r_i;
474 row[(int)x2 * 4 + 1] = g_i;
475 row[(int)x2 * 4 + 2] = b_i;
476 row[(int)x2 * 4 + 3] = 255;
479 h = get_angle(x1, y1, x2, y2);
481 HSV::hsv_to_rgb(r, g, b, h, s, v);
482 row[(int)x2 * 4] = (int)(r * 255);
483 row[(int)x2 * 4 + 1] = (int)(g * 255);
484 row[(int)x2 * 4 + 2] = (int)(b * 255);
485 row[(int)x2 * 4 + 3] = 255;
489 //printf("PaletteWheel::create_objects 1\n");
501 //printf("PaletteWheel::create_objects 1\n");
503 oldhue = window->hsv.h;
504 oldsaturation = window->hsv.s;
505 //printf("PaletteWheel::create_objects 1\n");
506 draw(oldhue, oldsaturation);
507 //printf("PaletteWheel::create_objects 1\n");
509 //printf("PaletteWheel::create_objects 2\n");
512 float PaletteWheel::torads(float angle)
514 return (float)angle / 360 * 2 * M_PI;
518 int PaletteWheel::draw(float hue, float saturation)
524 if( hue > 0 && hue < 90 ) {
525 x = (int)(w - w * cos(torads(90 - hue)) * saturation);
526 y = (int)(h - h * sin(torads(90 - hue)) * saturation);
528 else if( hue > 90 && hue < 180 ) {
529 x = (int)(w - w * cos(torads(hue - 90)) * saturation);
530 y = (int)(h + h * sin(torads(hue - 90)) * saturation);
532 else if( hue > 180 && hue < 270 ) {
533 x = (int)(w + w * cos(torads(270 - hue)) * saturation);
534 y = (int)(h + h * sin(torads(270 - hue)) * saturation);
536 else if( hue > 270 && hue < 360 ) {
537 x = (int)(w + w * cos(torads(hue - 270)) * saturation);
538 y = (int)(h - w * sin(torads(hue - 270)) * saturation);
540 else if( hue == 0 ) {
542 y = (int)(h - h * saturation);
544 else if( hue == 90 ) {
545 x = (int)(w - w * saturation);
548 else if( hue == 180 ) {
550 y = (int)(h + h * saturation);
552 else if( hue == 270 ) {
553 x = (int)(w + w * saturation);
559 draw_circle(x - 5, y - 5, 10, 10);
564 int PaletteWheel::get_angle(float x1, float y1, float x2, float y2)
566 float result = -atan2(x2 - x1, y1 - y2) * (360 / M_PI / 2);
572 PaletteWheelValue::PaletteWheelValue(ColorWindow *window, int x, int y)
573 : BC_SubWindow(x, y, 40, 170, BLACK)
575 this->window = window;
578 PaletteWheelValue::~PaletteWheelValue()
583 void PaletteWheelValue::create_objects()
585 frame = new VFrame(get_w(), get_h(), BC_RGB888);
586 draw(window->hsv.h, window->hsv.s, window->hsv.v);
590 int PaletteWheelValue::button_press_event()
592 //printf("PaletteWheelValue::button_press 1 %d\n", is_event_win());
593 if( get_cursor_x() >= 0 && get_cursor_x() < get_w() &&
594 get_cursor_y() >= 0 && get_cursor_y() < get_h() &&
596 //printf("PaletteWheelValue::button_press 2\n");
598 cursor_motion_event();
604 int PaletteWheelValue::cursor_motion_event()
606 if( button_down && is_event_win() ) {
607 //printf("PaletteWheelValue::cursor_motion 1\n");
608 float v = 1.0 - (float)(get_cursor_y() - 2) / (get_h() - 4);
609 bclamp(v, 0, 1); window->hsv.v = v;
610 window->update_hsv();
611 window->handle_event();
617 int PaletteWheelValue::button_release_event()
620 //printf("PaletteWheelValue::button_release 1\n");
627 int PaletteWheelValue::draw(float hue, float saturation, float value)
632 for( i = get_h() - 3; i >= 2; i-- ) {
633 unsigned char *row = (unsigned char*)frame->get_rows()[i];
634 HSV::hsv_to_rgb(r_f, g_f, b_f, hue, saturation,
635 1.0 - (float)(i - 2) / (get_h() - 4));
636 r = (int)(r_f * 255);
637 g = (int)(g_f * 255);
638 b = (int)(b_f * 255);
639 for( j = 0; j < get_w(); j++ ) {
646 draw_3d_border(0, 0, get_w(), get_h(), 1);
647 draw_vframe(frame, 2, 2, get_w() - 4, get_h() - 4,
648 2, 2, get_w() - 4, get_h() - 4, 0);
650 draw_line(2, get_h() - 3 - (int)(value * (get_h() - 5)),
651 get_w() - 3, get_h() - 3 - (int)(value * (get_h() - 5)));
652 //printf("PaletteWheelValue::draw %d %f\n", __LINE__, value);
657 PaletteOutput::PaletteOutput(ColorWindow *window, int x, int y)
658 : BC_SubWindow(x, y, 180, 30, BLACK)
660 this->window = window;
662 PaletteOutput::~PaletteOutput()
667 void PaletteOutput::create_objects()
673 int PaletteOutput::handle_event()
678 int PaletteOutput::draw()
680 set_color(window->rgb888());
681 draw_box(2, 2, get_w() - 4, get_h() - 4);
682 draw_3d_border(0, 0, get_w(), get_h(), 1);
686 PaletteHue::PaletteHue(ColorWindow *window, int x, int y)
687 : BC_ISlider(x, y, 0, 150, 200, 0, 359, (int)(window->hsv.h), 0)
689 this->window = window;
691 PaletteHue::~PaletteHue()
695 int PaletteHue::handle_event()
697 window->hsv.h = get_value();
698 window->update_hsv();
699 window->handle_event();
703 PaletteSat::PaletteSat(ColorWindow *window, int x, int y)
704 : BC_FSlider(x, y, 0, 150, 200, 0, 1.0, window->hsv.s, 0)
706 this->window = window;
709 PaletteSat::~PaletteSat()
713 int PaletteSat::handle_event()
715 window->hsv.s = get_value();
716 window->update_hsv();
717 window->handle_event();
722 PaletteVal::PaletteVal(ColorWindow *window, int x, int y)
723 : BC_FSlider(x, y, 0, 150, 200, 0, 1.0, window->hsv.v, 0)
725 this->window = window;
728 PaletteVal::~PaletteVal()
732 int PaletteVal::handle_event()
734 window->hsv.v = get_value();
735 window->update_hsv();
736 window->handle_event();
741 PaletteRed::PaletteRed(ColorWindow *window, int x, int y)
742 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->rgb.r, 0)
744 this->window = window;
747 PaletteRed::~PaletteRed()
751 int PaletteRed::handle_event()
753 window->rgb.r = get_value();
754 window->update_rgb();
755 window->handle_event();
759 PaletteGrn::PaletteGrn(ColorWindow *window, int x, int y)
760 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->rgb.g, 0)
762 this->window = window;
765 PaletteGrn::~PaletteGrn()
769 int PaletteGrn::handle_event()
771 window->rgb.g = get_value();
772 window->update_rgb();
773 window->handle_event();
777 PaletteBlu::PaletteBlu(ColorWindow *window, int x, int y)
778 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->rgb.b, 0)
780 this->window = window;
783 PaletteBlu::~PaletteBlu()
787 int PaletteBlu::handle_event()
789 window->rgb.b = get_value();
790 window->update_rgb();
791 window->handle_event();
795 PaletteAlpha::PaletteAlpha(ColorWindow *window, int x, int y)
796 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->aph, 0)
798 this->window = window;
801 PaletteAlpha::~PaletteAlpha()
805 int PaletteAlpha::handle_event()
807 window->aph = get_value();
808 window->aph_a->update(window->aph);
809 window->handle_event();
813 PaletteLum::PaletteLum(ColorWindow *window, int x, int y)
814 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->yuv.y, 0)
816 this->window = window;
819 PaletteLum::~PaletteLum()
823 int PaletteLum::handle_event()
825 window->yuv.y = get_value();
826 window->update_yuv();
827 window->handle_event();
831 PaletteCr::PaletteCr(ColorWindow *window, int x, int y)
832 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->yuv.u, 0)
834 this->window = window;
837 PaletteCr::~PaletteCr()
841 int PaletteCr::handle_event()
843 window->yuv.u = get_value();
844 window->update_yuv();
845 window->handle_event();
849 PaletteCb::PaletteCb(ColorWindow *window, int x, int y)
850 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->yuv.v, 0)
852 this->window = window;
855 PaletteCb::~PaletteCb()
859 int PaletteCb::handle_event()
861 window->yuv.v = get_value();
862 window->update_yuv();
863 window->handle_event();
867 void ColorWindow::update_rgb(float r, float g, float b)
870 YUV::yuv.rgb_to_yuv_f(r, g, b, y, u, v);
872 bclamp(y, 0, 1); yuv.y = y;
873 bclamp(u, 0, 1); yuv.u = u;
874 bclamp(v, 0, 1); yuv.v = v; }
876 HSV::rgb_to_hsv(r,g,b, h,s,v);
877 bclamp(h, 0, 360); hsv.h = h;
878 bclamp(s, 0, 1); hsv.s = s;
879 bclamp(v, 0, 1); hsv.v = v; }
882 void ColorWindow::update_yuv(float y, float u, float v)
886 YUV::yuv.yuv_to_rgb_f(r, g, b, y, u, v);
887 bclamp(r, 0, 1); rgb.r = r;
888 bclamp(g, 0, 1); rgb.g = g;
889 bclamp(b, 0, 1); rgb.b = b;
891 HSV::rgb_to_hsv(r,g,b, h, s, v);
892 bclamp(h, 0, 360); hsv.h = h;
893 bclamp(s, 0, 1); hsv.s = s;
894 bclamp(v, 0, 1); hsv.v = v; }
897 void ColorWindow::update_hsv(float h, float s, float v)
900 HSV::hsv_to_rgb(r,g,b, h,s,v);
901 bclamp(r, 0, 1); rgb.r = r;
902 bclamp(g, 0, 1); rgb.g = g;
903 bclamp(b, 0, 1); rgb.b = b;
905 YUV::yuv.rgb_to_yuv_f(r, g, b, y, u, v);
907 bclamp(y, 0, 1); yuv.y = y;
908 bclamp(u, 0, 1); yuv.u = u;
909 bclamp(v, 0, 1); yuv.v = v; }
912 void ColorWindow::load_history()
914 char history_path[BCTEXTLEN];
915 MWindow::create_defaults_path(history_path,PALETTE_DATA);
916 FILE *fp = fopen(history_path,"r");
919 while( i < PALLETTE_HISTORY_SIZE ) {
921 if( !fgets(line,sizeof(line)-1,fp) ) break;
922 line[sizeof(line)-1] = 0;
923 if( sscanf(line, "%x",&palette_history[i]) != 1 ) break;
928 int r = 0, g = 0, b = 0;
929 float v0 = 0, v1 = 1;
930 while( i < PALLETTE_HISTORY_SIZE ) {
931 int grey_code = i ^ (i>>1);
932 r = 255 * ((grey_code&4) ? v0 : v1);
933 g = 255 * ((grey_code&2) ? v0 : v1);
934 b = 255 * ((grey_code&1) ? v0 : v1);
935 int color = (r<<16) | (g<<8) | (b<<0);
936 palette_history[i++] = color;
937 if( i & 7 ) continue;
938 v0 = 0.5f * (v0+.5f);
939 v1 = 0.5f * (v1+.5f);
942 void ColorWindow::save_history()
944 char history_path[BCTEXTLEN];
945 MWindow::create_defaults_path(history_path,PALETTE_DATA);
946 FILE *fp = fopen(history_path,"w");
948 for( int i=0; i<PALLETTE_HISTORY_SIZE; ++i ) {
949 fprintf(fp, "%06x\n", palette_history[i]);
954 void ColorWindow::update_history(int color)
956 int out = palette_history[0];
957 palette_history[0] = color;
958 for( int i=1; out != color && i<PALLETTE_HISTORY_SIZE; ++i ) {
960 out = palette_history[i];
961 palette_history[i] = in;
964 void ColorWindow::update_history()
966 update_history(rgb888());
969 int ColorWindow::rgb888()
971 int r = 255*rgb.r + 0.5, g = 255*rgb.g + 0.5, b = 255*rgb.b + 0.5;
972 bclamp(r, 0, 255); bclamp(g, 0, 255); bclamp(b, 0, 255);
973 return (r<<16) | (g<<8) | (b<<0);
976 PaletteNum::PaletteNum(ColorWindow *window, int x, int y,
977 float &output, float min, float max)
978 : BC_TumbleTextBox(window, output, min, max, x, y, 64)
980 this->window = window;
981 this->output = &output;
986 PaletteNum::~PaletteNum()
991 int PaletteHSV::handle_event()
994 window->update_hsv();
995 window->handle_event();
999 int PaletteRGB::handle_event()
1002 window->update_rgb();
1003 window->handle_event();
1007 int PaletteYUV::handle_event()
1010 window->update_yuv();
1011 window->handle_event();
1015 int PaletteAPH::handle_event()
1018 window->update_display();
1019 window->handle_event();
1023 PaletteHexButton::PaletteHexButton(ColorWindow *window, int x, int y)
1024 : BC_GenericButton(x, y, 50, "#")
1026 this->window = window;
1027 set_tooltip(_("hex rgb color"));
1029 PaletteHexButton::~PaletteHexButton()
1032 int PaletteHexButton::handle_event()
1034 const char *hex = window->hex_box->get_text();
1035 window->update_rgb_hex(hex);
1039 PaletteHex::PaletteHex(ColorWindow *window, int x, int y, const char *hex)
1040 : BC_TextBox(x, y, 100, 1, hex)
1042 this->window = window;
1044 PaletteHex::~PaletteHex()
1047 void PaletteHex::update()
1049 char hex[BCSTRLEN]; sprintf(hex,"%06x",window->rgb888());
1050 BC_TextBox::update(hex);
1053 int PaletteHex::keypress_event()
1055 if( get_keypress() != RETURN )
1056 return BC_TextBox::keypress_event();
1057 window->update_rgb_hex(get_text());
1061 #include "grabpick_up_png.h"
1062 #include "grabpick_hi_png.h"
1063 #include "grabpick_dn_png.h"
1065 PaletteGrabButton::PaletteGrabButton(ColorWindow *window, int x, int y)
1066 : BC_Button(x, y, vframes)
1068 this->window = window;
1069 vframes[0] = new VFramePng(grabpick_up_png);
1070 vframes[1] = new VFramePng(grabpick_hi_png);
1071 vframes[2] = new VFramePng(grabpick_dn_png);
1072 set_tooltip(_("grab from anywhere picker"));
1074 PaletteGrabButton::~PaletteGrabButton()
1076 for( int i=0; i<3; ++i )
1079 int PaletteGrabButton::handle_event()
1081 if( window->grab_buttons() ) {
1083 window->button_grabbed = 1;
1084 button_press_event(); // redraw face HI
1089 PaletteHistory::PaletteHistory(ColorWindow *window, int x, int y)
1090 : BC_SubWindow(x,y, 200, 24)
1092 this->window = window;
1094 set_tooltip(_("color history"));
1096 PaletteHistory::~PaletteHistory()
1099 void PaletteHistory::update(int flush)
1102 for( int i=0; i<PALLETTE_HISTORY_SIZE; x1=x2 ) {
1103 int rgb = window->palette_history[i];
1104 x2 = (++i * get_w())/PALLETTE_HISTORY_SIZE;
1105 draw_3d_box(x1,0,x2-x1,get_h(),WHITE,BLACK,rgb,LTBLUE,DKBLUE);
1110 int PaletteHistory::button_press_event()
1112 if( button_down || !is_event_win() ) return 0;
1114 cursor_motion_event();
1117 int PaletteHistory::button_release_event()
1119 if( !button_down || !is_event_win() ) return 0;
1120 cursor_motion_event();
1121 if( button_down > 0 ) {
1122 window->handle_event();
1123 window->update_display();
1124 window->update_history();
1129 int PaletteHistory::cursor_motion_event()
1131 if( !button_down || !is_event_win() ) return 0;
1133 int pick = (PALLETTE_HISTORY_SIZE * get_cursor_x()) / get_w();
1134 bclamp(pick, 0, PALLETTE_HISTORY_SIZE-1);
1135 int color = window->palette_history[pick];
1136 float r = ((color>>16) & 0xff) / 255.;
1137 float g = ((color>>8) & 0xff) / 255.;
1138 float b = ((color>>0) & 0xff) / 255.;
1139 if( window->rgb.r != r || window->rgb.g != g || window->rgb.b != b ) {
1140 window->rgb.r = r; window->rgb.g = g; window->rgb.b = b;
1141 window->update_rgb();
1146 int PaletteHistory::cursor_leave_event()
1151 int PaletteHistory::repeat_event(int64_t duration)
1155 if( duration == get_resources()->tooltip_delay &&
1156 get_tooltip() && *get_tooltip() && cursor_above() ) {