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"
36 #define PALETTE_DATA "palette.dat"
38 ColorThread::ColorThread(int do_alpha, const char *title)
42 this->do_alpha = do_alpha;
43 this->do_okcancel = 0;
48 ColorThread::~ColorThread()
53 void ColorThread::start_window(int output, int alpha, int do_okcancel)
56 ColorWindow *gui = (ColorWindow *)get_gui();
58 gui->lock_window("ColorThread::start_window");
64 this->output = output;
66 this->do_okcancel = do_okcancel;
70 BC_Window* ColorThread::new_gui()
72 char window_title[BCTEXTLEN];
73 strcpy(window_title, _(PROGRAM_NAME ": "));
74 strcat(window_title, title ? title : _("Color Picker"));
75 BC_DisplayInfo display_info;
76 int x = display_info.get_abs_cursor_x() + 25;
77 int y = display_info.get_abs_cursor_y() - 100;
80 h += bmax(BC_OKButton::calculate_h(),BC_CancelButton::calculate_h());
81 int root_w = display_info.get_root_w(), root_h = display_info.get_root_h();
82 if( x+w > root_w ) x = root_w - w;
83 if( y+h > root_h ) y = root_h - h;
86 ColorWindow *gui = new ColorWindow(this, x, y, w, h, window_title);
87 gui->create_objects();
91 void ColorThread::update_gui(int output, int alpha)
93 ColorWindow *gui = (ColorWindow *)get_gui();
96 this->output = output;
99 gui->update_display();
100 gui->unlock_window();
103 int ColorThread::handle_new_color(int output, int alpha)
105 printf("ColorThread::handle_new_color undefined.\n");
110 ColorWindow::ColorWindow(ColorThread *thread, int x, int y, int w, int h, const char *title)
111 : BC_Window(title, x, y, w, h, w, h, 0, 0, 1)
113 this->thread = thread;
118 hue = 0; sat = 0; val = 0;
119 red = 0; grn = 0; blu = 0;
120 lum = 0; c_r = 0; c_b = 0;
123 hsv_h = 0; hsv_s = 0; hsv_v = 0;
124 rgb_r = 0; rgb_g = 0; rgb_b = 0;
125 yuv_y = 0; yuv_u = 0; yuv_v = 0;
130 ColorWindow::~ColorWindow()
132 delete hsv_h; delete hsv_s; delete hsv_v;
133 delete rgb_r; delete rgb_g; delete rgb_b;
134 delete yuv_y; delete yuv_u; delete yuv_v;
137 update_history(rgb888());
141 void ColorWindow::create_objects()
143 int x0 = 10, y0 = 10;
144 lock_window("ColorWindow::create_objects");
148 add_tool(wheel = new PaletteWheel(this, x, y));
149 wheel->create_objects();
151 x += 180; add_tool(wheel_value = new PaletteWheelValue(this, x, y));
152 wheel_value->create_objects();
154 y += 180; add_tool(output = new PaletteOutput(this, x, y));
155 output->create_objects();
156 y += output->get_h() + 20;
158 load_history(); int x1 = x;
159 add_tool(hex_btn = new PaletteHexButton(this, x1, y));
160 char hex[BCSTRLEN]; sprintf(hex,"%06x",thread->output);
161 x1 += hex_btn->get_w() + 5;
162 add_tool(hex_box = new PaletteHex(this, x1, y, hex));
163 x1 += hex_box->get_w() + 15;
164 add_tool(grab_btn = new PaletteGrabButton(this, x1, y));
165 y += hex_box->get_h() + 15;
166 add_tool(history = new PaletteHistory(this, 10, y));
169 add_tool(new BC_Title(x, y =y0, _("H:"), SMALLFONT));
170 add_tool(new BC_Title(x, y+=25, _("S:"), SMALLFONT));
171 add_tool(new BC_Title(x, y+=25, _("V:"), SMALLFONT));
172 add_tool(new BC_Title(x, y+=40, _("R:"), SMALLFONT));
173 add_tool(new BC_Title(x, y+=25, _("G:"), SMALLFONT));
174 add_tool(new BC_Title(x, y+=25, _("B:"), SMALLFONT));
175 add_tool(new BC_Title(x, y+=40, _("Y:"), SMALLFONT));
176 add_tool(new BC_Title(x, y+=25, _("U:"), SMALLFONT));
177 add_tool(new BC_Title(x, y+=25, _("V:"), SMALLFONT));
178 if( thread->do_alpha )
179 add_tool(new BC_Title(x, y+=40, _("A:"), SMALLFONT));
181 add_tool(hue = new PaletteHue(this, x, y= y0));
182 add_tool(sat = new PaletteSat(this, x, y+=25));
183 add_tool(val = new PaletteVal(this, x, y+=25));
184 add_tool(red = new PaletteRed(this, x, y+=40));
185 add_tool(grn = new PaletteGrn(this, x, y+=25));
186 add_tool(blu = new PaletteBlu(this, x, y+=25));
187 add_tool(lum = new PaletteLum(this, x, y+=40));
188 add_tool(c_r = new PaletteCr (this, x, y+=25));
189 add_tool(c_b = new PaletteCb (this, x, y+=25));
190 if( thread->do_alpha )
191 add_tool(alpha = new PaletteAlpha(this, x, y+=40));
193 x += hue->get_w() + 10;
194 hsv_h = new PaletteHSV(this, x,y= y0, hsv.h, 0, 360);
195 hsv_h->create_objects(); hsv_h->set_tooltip(_("Hue"));
196 hsv_s = new PaletteHSV(this, x,y+=25, hsv.s, 0, 1);
197 hsv_s->create_objects(); hsv_s->set_tooltip(_("Saturation"));
198 hsv_v = new PaletteHSV(this, x,y+=25, hsv.v, 0, 1);
199 hsv_v->create_objects(); hsv_v->set_tooltip(_("Value"));
200 rgb_r = new PaletteRGB(this, x,y+=40, rgb.r, 0, 1);
201 rgb_r->create_objects(); rgb_r->set_tooltip(_("Red"));
202 rgb_g = new PaletteRGB(this, x,y+=25, rgb.g, 0, 1);
203 rgb_g->create_objects(); rgb_g->set_tooltip(_("Green"));
204 rgb_b = new PaletteRGB(this, x,y+=25, rgb.b, 0, 1);
205 rgb_b->create_objects(); rgb_b->set_tooltip(_("Blue"));
206 yuv_y = new PaletteYUV(this, x,y+=40, yuv.y, 0, 1);
207 yuv_y->create_objects(); yuv_y->set_tooltip(_("Luminance"));
208 yuv_u = new PaletteYUV(this, x,y+=25, yuv.u, 0, 1);
209 yuv_u->create_objects(); yuv_u->set_tooltip(_("Compliment Red"));
210 yuv_v = new PaletteYUV(this, x,y+=25, yuv.v, 0, 1);
211 yuv_v->create_objects(); yuv_v->set_tooltip(_("Compliment Blue"));
212 if( thread->do_alpha ) {
213 aph_a = new PaletteAPH(this, x,y+=40, aph, 0, 1);
214 aph_a->create_objects(); aph_a->set_tooltip(_("Alpha"));
216 if( thread->do_okcancel ) {
217 add_tool(new BC_OKButton(this));
218 add_tool(new BC_CancelButton(this));
228 void ColorWindow::change_values()
230 float r = ((thread->output>>16) & 0xff) / 255.;
231 float g = ((thread->output>>8) & 0xff) / 255.;
232 float b = ((thread->output>>0) & 0xff) / 255.;
233 rgb.r = r; rgb.g = g; rgb.b = b;
234 aph = (float)thread->alpha / 255;
235 update_rgb(rgb.r, rgb.g, rgb.b);
239 int ColorWindow::close_event()
241 set_done(thread->do_okcancel ? 1 : 0);
246 void ColorWindow::update_rgb()
248 update_rgb(rgb.r, rgb.g, rgb.b);
251 void ColorWindow::update_hsv()
253 update_hsv(hsv.h, hsv.s, hsv.v);
256 void ColorWindow::update_yuv()
258 update_yuv(yuv.y, yuv.u, yuv.v);
262 void ColorWindow::update_display()
264 wheel->draw(wheel->oldhue, wheel->oldsaturation);
265 wheel->oldhue = hsv.h;
266 wheel->oldsaturation = hsv.s;
267 wheel->draw(hsv.h, hsv.s);
269 wheel_value->draw(hsv.h, hsv.s, hsv.v);
270 wheel_value->flash();
274 hue->update((int)hsv.h);
286 hsv_h->update(hsv.h);
287 hsv_s->update(hsv.s);
288 hsv_v->update(hsv.v);
289 rgb_r->update(rgb.r);
290 rgb_g->update(rgb.g);
291 rgb_b->update(rgb.b);
292 yuv_y->update(yuv.y);
293 yuv_u->update(yuv.u);
294 yuv_v->update(yuv.v);
297 if( thread->do_alpha )
301 int ColorWindow::handle_event()
303 thread->handle_new_color(rgb888(), (int)(255*aph + 0.5));
307 int ColorWindow::button_press_event()
309 if( button_grabbed ) {
314 int ColorWindow::button_release_event()
316 if( button_grabbed ) {
323 get_abs_cursor_xy(cx, cy);
324 //printf("grabbed button %d,%d\n",cx,cy);
325 BC_Capture capture_bitmap(1, 1, 0);
326 VFrame vframe(1,1,BC_RGB888);
327 capture_bitmap.capture_frame(&vframe, cx,cy);
328 unsigned char *data = vframe.get_data();
329 rgb.r = data[0]/255.; rgb.g = data[1]/255.; rgb.b = data[2]/255.;
333 return handle_event();
339 PaletteWheel::PaletteWheel(ColorWindow *window, int x, int y)
340 : BC_SubWindow(x, y, 170, 170)
342 this->window = window;
348 PaletteWheel::~PaletteWheel()
352 int PaletteWheel::button_press_event()
354 if( get_cursor_x() >= 0 && get_cursor_x() < get_w() &&
355 get_cursor_y() >= 0 && get_cursor_y() < get_h() &&
358 cursor_motion_event();
364 int PaletteWheel::cursor_motion_event()
366 int x1, y1, distance;
367 if( button_down && is_event_win() ) {
368 float h = get_angle(get_w()/2, get_h()/2, get_cursor_x(), get_cursor_y());
369 bclamp(h, 0, 359.999); window->hsv.h = h;
370 x1 = get_w() / 2 - get_cursor_x();
371 y1 = get_h() / 2 - get_cursor_y();
372 distance = (int)sqrt(x1 * x1 + y1 * y1);
373 float s = (float)distance / (get_w() / 2);
374 bclamp(s, 0, 1); window->hsv.s = s;
375 window->update_hsv();
376 window->update_display();
377 window->handle_event();
383 int PaletteWheel::button_release_event()
392 void PaletteWheel::create_objects()
395 //printf("PaletteWheel::create_objects 1\n");
398 float x1, y1, x2, y2;
400 int default_r, default_g, default_b;
401 VFrame frame(0, -1, get_w(), get_h(), BC_RGBA8888, -1);
404 default_r = (get_resources()->get_bg_color() & 0xff0000) >> 16;
405 default_g = (get_resources()->get_bg_color() & 0xff00) >> 8;
406 default_b = (get_resources()->get_bg_color() & 0xff);
407 //printf("PaletteWheel::create_objects 1\n");
409 int highlight_r = (get_resources()->button_light & 0xff0000) >> 16;
410 int highlight_g = (get_resources()->button_light & 0xff00) >> 8;
411 int highlight_b = (get_resources()->button_light & 0xff);
413 for( y2 = 0; y2 < get_h(); y2++ ) {
414 unsigned char *row = (unsigned char*)frame.get_rows()[(int)y2];
415 for( x2 = 0; x2 < get_w(); x2++ ) {
416 distance = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
417 if( distance > x1 ) {
418 row[(int)x2 * 4] = default_r;
419 row[(int)x2 * 4 + 1] = default_g;
420 row[(int)x2 * 4 + 2] = default_b;
421 row[(int)x2 * 4 + 3] = 0;
424 if( distance > x1 - 1 ) {
426 if( get_h() - y2 < x2 ) {
437 row[(int)x2 * 4] = r_i;
438 row[(int)x2 * 4 + 1] = g_i;
439 row[(int)x2 * 4 + 2] = b_i;
440 row[(int)x2 * 4 + 3] = 255;
443 h = get_angle(x1, y1, x2, y2);
445 HSV::hsv_to_rgb(r, g, b, h, s, v);
446 row[(int)x2 * 4] = (int)(r * 255);
447 row[(int)x2 * 4 + 1] = (int)(g * 255);
448 row[(int)x2 * 4 + 2] = (int)(b * 255);
449 row[(int)x2 * 4 + 3] = 255;
453 //printf("PaletteWheel::create_objects 1\n");
465 //printf("PaletteWheel::create_objects 1\n");
467 oldhue = window->hsv.h;
468 oldsaturation = window->hsv.s;
469 //printf("PaletteWheel::create_objects 1\n");
470 draw(oldhue, oldsaturation);
471 //printf("PaletteWheel::create_objects 1\n");
473 //printf("PaletteWheel::create_objects 2\n");
476 float PaletteWheel::torads(float angle)
478 return (float)angle / 360 * 2 * M_PI;
482 int PaletteWheel::draw(float hue, float saturation)
488 if( hue > 0 && hue < 90 ) {
489 x = (int)(w - w * cos(torads(90 - hue)) * saturation);
490 y = (int)(h - h * sin(torads(90 - hue)) * saturation);
492 else if( hue > 90 && hue < 180 ) {
493 x = (int)(w - w * cos(torads(hue - 90)) * saturation);
494 y = (int)(h + h * sin(torads(hue - 90)) * saturation);
496 else if( hue > 180 && hue < 270 ) {
497 x = (int)(w + w * cos(torads(270 - hue)) * saturation);
498 y = (int)(h + h * sin(torads(270 - hue)) * saturation);
500 else if( hue > 270 && hue < 360 ) {
501 x = (int)(w + w * cos(torads(hue - 270)) * saturation);
502 y = (int)(h - w * sin(torads(hue - 270)) * saturation);
504 else if( hue == 0 ) {
506 y = (int)(h - h * saturation);
508 else if( hue == 90 ) {
509 x = (int)(w - w * saturation);
512 else if( hue == 180 ) {
514 y = (int)(h + h * saturation);
516 else if( hue == 270 ) {
517 x = (int)(w + w * saturation);
523 draw_circle(x - 5, y - 5, 10, 10);
528 int PaletteWheel::get_angle(float x1, float y1, float x2, float y2)
530 float result = -atan2(x2 - x1, y1 - y2) * (360 / M_PI / 2);
536 PaletteWheelValue::PaletteWheelValue(ColorWindow *window, int x, int y)
537 : BC_SubWindow(x, y, 40, 170, BLACK)
539 this->window = window;
542 PaletteWheelValue::~PaletteWheelValue()
547 void PaletteWheelValue::create_objects()
549 frame = new VFrame(0, -1, get_w(), get_h(), BC_RGB888, -1);
550 draw(window->hsv.h, window->hsv.s, window->hsv.v);
554 int PaletteWheelValue::button_press_event()
556 //printf("PaletteWheelValue::button_press 1 %d\n", is_event_win());
557 if( get_cursor_x() >= 0 && get_cursor_x() < get_w() &&
558 get_cursor_y() >= 0 && get_cursor_y() < get_h() &&
560 //printf("PaletteWheelValue::button_press 2\n");
562 cursor_motion_event();
568 int PaletteWheelValue::cursor_motion_event()
570 if( button_down && is_event_win() ) {
571 //printf("PaletteWheelValue::cursor_motion 1\n");
572 float v = 1.0 - (float)(get_cursor_y() - 2) / (get_h() - 4);
573 bclamp(v, 0, 1); window->hsv.v = v;
574 window->update_hsv();
575 window->update_display();
576 window->handle_event();
582 int PaletteWheelValue::button_release_event()
585 //printf("PaletteWheelValue::button_release 1\n");
592 int PaletteWheelValue::draw(float hue, float saturation, float value)
597 for( i = get_h() - 3; i >= 2; i-- ) {
598 unsigned char *row = (unsigned char*)frame->get_rows()[i];
599 HSV::hsv_to_rgb(r_f, g_f, b_f, hue, saturation,
600 1.0 - (float)(i - 2) / (get_h() - 4));
601 r = (int)(r_f * 255);
602 g = (int)(g_f * 255);
603 b = (int)(b_f * 255);
604 for( j = 0; j < get_w(); j++ ) {
611 draw_3d_border(0, 0, get_w(), get_h(), 1);
612 draw_vframe(frame, 2, 2, get_w() - 4, get_h() - 4,
613 2, 2, get_w() - 4, get_h() - 4, 0);
615 draw_line(2, get_h() - 3 - (int)(value * (get_h() - 5)),
616 get_w() - 3, get_h() - 3 - (int)(value * (get_h() - 5)));
617 //printf("PaletteWheelValue::draw %d %f\n", __LINE__, value);
622 PaletteOutput::PaletteOutput(ColorWindow *window, int x, int y)
623 : BC_SubWindow(x, y, 180, 30, BLACK)
625 this->window = window;
627 PaletteOutput::~PaletteOutput()
632 void PaletteOutput::create_objects()
638 int PaletteOutput::handle_event()
643 int PaletteOutput::draw()
645 set_color(window->rgb888());
646 draw_box(2, 2, get_w() - 4, get_h() - 4);
647 draw_3d_border(0, 0, get_w(), get_h(), 1);
651 PaletteHue::PaletteHue(ColorWindow *window, int x, int y)
652 : BC_ISlider(x, y, 0, 150, 200, 0, 359, (int)(window->hsv.h), 0)
654 this->window = window;
656 PaletteHue::~PaletteHue()
660 int PaletteHue::handle_event()
662 window->hsv.h = get_value();
663 window->update_hsv();
664 window->handle_event();
668 PaletteSat::PaletteSat(ColorWindow *window, int x, int y)
669 : BC_FSlider(x, y, 0, 150, 200, 0, 1.0, window->hsv.s, 0)
671 this->window = window;
674 PaletteSat::~PaletteSat()
678 int PaletteSat::handle_event()
680 window->hsv.s = get_value();
681 window->update_hsv();
682 window->handle_event();
687 PaletteVal::PaletteVal(ColorWindow *window, int x, int y)
688 : BC_FSlider(x, y, 0, 150, 200, 0, 1.0, window->hsv.v, 0)
690 this->window = window;
693 PaletteVal::~PaletteVal()
697 int PaletteVal::handle_event()
699 window->hsv.v = get_value();
700 window->update_hsv();
701 window->handle_event();
706 PaletteRed::PaletteRed(ColorWindow *window, int x, int y)
707 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->rgb.r, 0)
709 this->window = window;
712 PaletteRed::~PaletteRed()
716 int PaletteRed::handle_event()
718 window->rgb.r = get_value();
719 window->update_rgb();
720 window->handle_event();
724 PaletteGrn::PaletteGrn(ColorWindow *window, int x, int y)
725 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->rgb.g, 0)
727 this->window = window;
730 PaletteGrn::~PaletteGrn()
734 int PaletteGrn::handle_event()
736 window->rgb.g = get_value();
737 window->update_rgb();
738 window->handle_event();
742 PaletteBlu::PaletteBlu(ColorWindow *window, int x, int y)
743 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->rgb.b, 0)
745 this->window = window;
748 PaletteBlu::~PaletteBlu()
752 int PaletteBlu::handle_event()
754 window->rgb.b = get_value();
755 window->update_rgb();
756 window->handle_event();
760 PaletteAlpha::PaletteAlpha(ColorWindow *window, int x, int y)
761 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->aph, 0)
763 this->window = window;
766 PaletteAlpha::~PaletteAlpha()
770 int PaletteAlpha::handle_event()
772 window->aph = get_value();
773 window->handle_event();
777 PaletteLum::PaletteLum(ColorWindow *window, int x, int y)
778 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->yuv.y, 0)
780 this->window = window;
783 PaletteLum::~PaletteLum()
787 int PaletteLum::handle_event()
789 window->yuv.y = get_value();
790 window->update_yuv();
791 window->handle_event();
795 PaletteCr::PaletteCr(ColorWindow *window, int x, int y)
796 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->yuv.u, 0)
798 this->window = window;
801 PaletteCr::~PaletteCr()
805 int PaletteCr::handle_event()
807 window->yuv.u = get_value();
808 window->update_yuv();
809 window->handle_event();
813 PaletteCb::PaletteCb(ColorWindow *window, int x, int y)
814 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->yuv.v, 0)
816 this->window = window;
819 PaletteCb::~PaletteCb()
823 int PaletteCb::handle_event()
825 window->yuv.v = get_value();
826 window->update_yuv();
827 window->handle_event();
831 void ColorWindow::update_rgb(float r, float g, float b)
834 YUV::rgb_to_yuv_f(r, g, b, y, u, v);
836 bclamp(y, 0, 1); yuv.y = y;
837 bclamp(u, 0, 1); yuv.u = u;
838 bclamp(v, 0, 1); yuv.v = v; }
840 HSV::rgb_to_hsv(r,g,b, h,s,v);
841 bclamp(h, 0, 360); hsv.h = h;
842 bclamp(s, 0, 1); hsv.s = s;
843 bclamp(v, 0, 1); hsv.v = v; }
846 void ColorWindow::update_yuv(float y, float u, float v)
850 YUV::yuv_to_rgb_f(r, g, b, y, u, v);
851 bclamp(r, 0, 1); rgb.r = r;
852 bclamp(g, 0, 1); rgb.g = g;
853 bclamp(b, 0, 1); rgb.b = b;
855 HSV::rgb_to_hsv(r,g,b, h, s, v);
856 bclamp(h, 0, 360); hsv.h = h;
857 bclamp(s, 0, 1); hsv.s = s;
858 bclamp(v, 0, 1); hsv.v = v; }
861 void ColorWindow::update_hsv(float h, float s, float v)
864 HSV::hsv_to_rgb(r,g,b, h,s,v);
865 bclamp(r, 0, 1); rgb.r = r;
866 bclamp(g, 0, 1); rgb.g = g;
867 bclamp(b, 0, 1); rgb.b = b;
869 YUV::rgb_to_yuv_f(r, g, b, y, u, v);
871 bclamp(y, 0, 1); yuv.y = y;
872 bclamp(u, 0, 1); yuv.u = u;
873 bclamp(v, 0, 1); yuv.v = v; }
876 void ColorWindow::load_history()
878 char history_path[BCTEXTLEN];
879 MWindow::create_defaults_path(history_path,PALETTE_DATA);
880 FILE *fp = fopen(history_path,"r");
883 while( i < PALLETTE_HISTORY_SIZE ) {
885 if( !fgets(line,sizeof(line)-1,fp) ) break;
886 line[sizeof(line)-1] = 0;
887 if( sscanf(line, "%x",&palette_history[i]) != 1 ) break;
892 int r = 0, g = 0, b = 0;
893 float v0 = 0, v1 = 1;
894 while( i < PALLETTE_HISTORY_SIZE ) {
895 int grey_code = i ^ (i>>1);
896 r = 255 * ((grey_code&4) ? v0 : v1);
897 g = 255 * ((grey_code&2) ? v0 : v1);
898 b = 255 * ((grey_code&1) ? v0 : v1);
899 int color = (r<<16) | (g<<8) | (b<<0);
900 palette_history[i++] = color;
901 if( i & 7 ) continue;
902 v0 = 0.5f * (v0+.5f);
903 v1 = 0.5f * (v1+.5f);
906 void ColorWindow::save_history()
908 char history_path[BCTEXTLEN];
909 MWindow::create_defaults_path(history_path,PALETTE_DATA);
910 FILE *fp = fopen(history_path,"w");
912 for( int i=0; i<PALLETTE_HISTORY_SIZE; ++i ) {
913 fprintf(fp, "%06x\n", palette_history[i]);
918 void ColorWindow::update_history(int color)
920 int out = palette_history[0];
921 palette_history[0] = color;
922 for( int i=1; out != color && i<PALLETTE_HISTORY_SIZE; ++i ) {
924 out = palette_history[i];
925 palette_history[i] = in;
928 void ColorWindow::update_history()
930 update_history(rgb888());
933 int ColorWindow::rgb888()
935 int r = 255*rgb.r + 0.5, g = 255*rgb.g + 0.5, b = 255*rgb.b + 0.5;
936 bclamp(r, 0, 255); bclamp(g, 0, 255); bclamp(b, 0, 255);
937 return (r<<16) | (g<<8) | (b<<0);
940 PaletteNum::PaletteNum(ColorWindow *window, int x, int y,
941 float &output, float min, float max)
942 : BC_TumbleTextBox(window, output, min, max, x, y, 64)
944 this->window = window;
945 this->output = &output;
950 PaletteNum::~PaletteNum()
955 int PaletteHSV::handle_event()
958 window->update_hsv();
959 window->update_display();
960 window->handle_event();
964 int PaletteRGB::handle_event()
967 window->update_rgb();
968 window->update_display();
969 window->handle_event();
973 int PaletteYUV::handle_event()
976 window->update_yuv();
977 window->update_display();
978 window->handle_event();
982 int PaletteAPH::handle_event()
985 window->update_display();
986 window->handle_event();
990 PaletteHexButton::PaletteHexButton(ColorWindow *window, int x, int y)
991 : BC_GenericButton(x, y, 50, "#")
993 this->window = window;
994 set_tooltip(_("hex rgb color"));
996 PaletteHexButton::~PaletteHexButton()
999 int PaletteHexButton::handle_event()
1001 const char *hex = window->hex_box->get_text();
1003 if( sscanf(hex,"%x",&color) == 1 ) {
1004 float r = ((color>>16) & 0xff) / 255.;
1005 float g = ((color>>8) & 0xff) / 255.;
1006 float b = ((color>>0) & 0xff) / 255.;
1007 window->rgb.r = r; window->rgb.g = g; window->rgb.b = b;
1008 window->update_rgb();
1009 window->update_display();
1010 window->update_history();
1011 window->handle_event();
1016 PaletteHex::PaletteHex(ColorWindow *window, int x, int y, const char *hex)
1017 : BC_TextBox(x, y, 100, 1, hex)
1019 this->window = window;
1021 PaletteHex::~PaletteHex()
1024 void PaletteHex::update()
1026 char hex[BCSTRLEN]; sprintf(hex,"%06x",window->rgb888());
1027 BC_TextBox::update(hex);
1030 int PaletteHex::handle_event()
1035 #include "grabpick_up_png.h"
1036 #include "grabpick_hi_png.h"
1037 #include "grabpick_dn_png.h"
1039 PaletteGrabButton::PaletteGrabButton(ColorWindow *window, int x, int y)
1040 : BC_Button(x, y, vframes)
1042 this->window = window;
1043 vframes[0] = new VFramePng(grabpick_up_png);
1044 vframes[1] = new VFramePng(grabpick_hi_png);
1045 vframes[2] = new VFramePng(grabpick_dn_png);
1046 set_tooltip(_("grab from anywhere picker"));
1048 PaletteGrabButton::~PaletteGrabButton()
1050 for( int i=0; i<3; ++i )
1053 int PaletteGrabButton::handle_event()
1055 if( window->grab_buttons() ) {
1056 window->button_grabbed = 1;
1057 button_press_event(); // redraw face HI
1062 PaletteHistory::PaletteHistory(ColorWindow *window, int x, int y)
1063 : BC_SubWindow(x,y, 200, 24)
1065 this->window = window;
1067 set_tooltip(_("color history"));
1069 PaletteHistory::~PaletteHistory()
1072 void PaletteHistory::update(int flush)
1075 for( int i=0; i<PALLETTE_HISTORY_SIZE; x1=x2 ) {
1076 int rgb = window->palette_history[i];
1077 x2 = (++i * get_w())/PALLETTE_HISTORY_SIZE;
1078 draw_3d_box(x1,0,x2-x1,get_h(),WHITE,BLACK,rgb,LTBLUE,DKBLUE);
1083 int PaletteHistory::button_press_event()
1085 if( button_down || !is_event_win() ) return 0;
1087 cursor_motion_event();
1090 int PaletteHistory::button_release_event()
1092 if( !button_down || !is_event_win() ) return 0;
1093 cursor_motion_event();
1094 if( button_down > 0 ) {
1095 window->handle_event();
1096 window->update_display();
1097 window->update_history();
1102 int PaletteHistory::cursor_motion_event()
1104 if( !button_down || !is_event_win() ) return 0;
1106 int pick = (PALLETTE_HISTORY_SIZE * get_cursor_x()) / get_w();
1107 bclamp(pick, 0, PALLETTE_HISTORY_SIZE-1);
1108 int color = window->palette_history[pick];
1109 float r = ((color>>16) & 0xff) / 255.;
1110 float g = ((color>>8) & 0xff) / 255.;
1111 float b = ((color>>0) & 0xff) / 255.;
1112 if( window->rgb.r != r || window->rgb.g != g || window->rgb.b != b ) {
1113 window->rgb.r = r; window->rgb.g = g; window->rgb.b = b;
1114 window->update_rgb();
1115 window->update_display();
1120 int PaletteHistory::cursor_enter_event()
1122 set_tooltip_done(0);
1125 int PaletteHistory::cursor_leave_event()
1128 set_tooltip_done(0);
1131 int PaletteHistory::repeat_event(int64_t duration)
1135 if( duration == get_resources()->tooltip_delay &&
1136 get_tooltip() && *get_tooltip() && cursor_above() ) {
1138 set_tooltip_done(1);