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"
37 #define PALETTE_DATA "palette.dat"
39 ColorThread::ColorThread(int do_alpha, const char *title)
43 this->do_alpha = do_alpha;
44 this->do_okcancel = 0;
49 ColorThread::~ColorThread()
54 void ColorThread::start_window(int output, int alpha, int do_okcancel)
57 ColorWindow *gui = (ColorWindow *)get_gui();
59 gui->lock_window("ColorThread::start_window");
65 this->output = output;
67 this->do_okcancel = do_okcancel;
71 BC_Window* ColorThread::new_gui()
73 char window_title[BCTEXTLEN];
74 strcpy(window_title, _(PROGRAM_NAME ": "));
75 strcat(window_title, title ? title : _("Color Picker"));
76 BC_DisplayInfo display_info;
77 int x = display_info.get_abs_cursor_x() + 25;
78 int y = display_info.get_abs_cursor_y() - 100;
81 h += bmax(BC_OKButton::calculate_h(),BC_CancelButton::calculate_h());
82 int root_w = display_info.get_root_w(), root_h = display_info.get_root_h();
83 if( x+w > root_w ) x = root_w - w;
84 if( y+h > root_h ) y = root_h - h;
87 ColorWindow *gui = new ColorWindow(this, x, y, w, h, window_title);
88 gui->create_objects();
92 void ColorThread::update_gui(int output, int alpha)
94 ColorWindow *gui = (ColorWindow *)get_gui();
97 this->output = output;
100 gui->update_display();
101 gui->unlock_window();
104 int ColorThread::handle_new_color(int output, int alpha)
106 printf("ColorThread::handle_new_color undefined.\n");
111 ColorWindow::ColorWindow(ColorThread *thread, int x, int y, int w, int h, const char *title)
112 : BC_Window(title, x, y, w, h, w, h, 0, 0, 1)
114 this->thread = thread;
119 hue = 0; sat = 0; val = 0;
120 red = 0; grn = 0; blu = 0;
121 lum = 0; c_r = 0; c_b = 0;
124 hsv_h = 0; hsv_s = 0; hsv_v = 0;
125 rgb_r = 0; rgb_g = 0; rgb_b = 0;
126 yuv_y = 0; yuv_u = 0; yuv_v = 0;
131 ColorWindow::~ColorWindow()
133 delete hsv_h; delete hsv_s; delete hsv_v;
134 delete rgb_r; delete rgb_g; delete rgb_b;
135 delete yuv_y; delete yuv_u; delete yuv_v;
138 if( button_grabbed ) {
142 update_history(rgb888());
146 void ColorWindow::create_objects()
148 int x0 = 10, y0 = 10;
149 lock_window("ColorWindow::create_objects");
153 add_tool(wheel = new PaletteWheel(this, x, y));
154 wheel->create_objects();
156 x += 180; add_tool(wheel_value = new PaletteWheelValue(this, x, y));
157 wheel_value->create_objects();
159 y += 180; add_tool(output = new PaletteOutput(this, x, y));
160 output->create_objects();
161 y += output->get_h() + 20;
163 load_history(); int x1 = x;
164 add_tool(hex_btn = new PaletteHexButton(this, x1, y));
165 char hex[BCSTRLEN]; sprintf(hex,"%06x",thread->output);
166 x1 += hex_btn->get_w() + 5;
167 add_tool(hex_box = new PaletteHex(this, x1, y, hex));
168 x1 += hex_box->get_w() + 15;
169 add_tool(grab_btn = new PaletteGrabButton(this, x1, y));
170 y += hex_box->get_h() + 15;
171 add_tool(history = new PaletteHistory(this, 10, y));
174 add_tool(new BC_Title(x, y =y0, _("H:"), SMALLFONT));
175 add_tool(new BC_Title(x, y+=25, _("S:"), SMALLFONT));
176 add_tool(new BC_Title(x, y+=25, _("V:"), SMALLFONT));
177 add_tool(new BC_Title(x, y+=40, _("R:"), SMALLFONT));
178 add_tool(new BC_Title(x, y+=25, _("G:"), SMALLFONT));
179 add_tool(new BC_Title(x, y+=25, _("B:"), SMALLFONT));
180 add_tool(new BC_Title(x, y+=40, _("Y:"), SMALLFONT));
181 add_tool(new BC_Title(x, y+=25, _("U:"), SMALLFONT));
182 add_tool(new BC_Title(x, y+=25, _("V:"), SMALLFONT));
183 if( thread->do_alpha )
184 add_tool(new BC_Title(x, y+=40, _("A:"), SMALLFONT));
186 add_tool(hue = new PaletteHue(this, x, y= y0));
187 add_tool(sat = new PaletteSat(this, x, y+=25));
188 add_tool(val = new PaletteVal(this, x, y+=25));
189 add_tool(red = new PaletteRed(this, x, y+=40));
190 add_tool(grn = new PaletteGrn(this, x, y+=25));
191 add_tool(blu = new PaletteBlu(this, x, y+=25));
192 add_tool(lum = new PaletteLum(this, x, y+=40));
193 add_tool(c_r = new PaletteCr (this, x, y+=25));
194 add_tool(c_b = new PaletteCb (this, x, y+=25));
195 if( thread->do_alpha )
196 add_tool(alpha = new PaletteAlpha(this, x, y+=40));
198 x += hue->get_w() + 10;
199 hsv_h = new PaletteHSV(this, x,y= y0, hsv.h, 0, 360);
200 hsv_h->create_objects(); hsv_h->set_tooltip(_("Hue"));
201 hsv_s = new PaletteHSV(this, x,y+=25, hsv.s, 0, 1);
202 hsv_s->create_objects(); hsv_s->set_tooltip(_("Saturation"));
203 hsv_v = new PaletteHSV(this, x,y+=25, hsv.v, 0, 1);
204 hsv_v->create_objects(); hsv_v->set_tooltip(_("Value"));
205 rgb_r = new PaletteRGB(this, x,y+=40, rgb.r, 0, 1);
206 rgb_r->create_objects(); rgb_r->set_tooltip(_("Red"));
207 rgb_g = new PaletteRGB(this, x,y+=25, rgb.g, 0, 1);
208 rgb_g->create_objects(); rgb_g->set_tooltip(_("Green"));
209 rgb_b = new PaletteRGB(this, x,y+=25, rgb.b, 0, 1);
210 rgb_b->create_objects(); rgb_b->set_tooltip(_("Blue"));
211 yuv_y = new PaletteYUV(this, x,y+=40, yuv.y, 0, 1);
212 yuv_y->create_objects(); yuv_y->set_tooltip(_("Luminance"));
213 yuv_u = new PaletteYUV(this, x,y+=25, yuv.u, 0, 1);
214 yuv_u->create_objects(); yuv_u->set_tooltip(_("Complement Blue"));
215 yuv_v = new PaletteYUV(this, x,y+=25, yuv.v, 0, 1);
216 yuv_v->create_objects(); yuv_v->set_tooltip(_("Complement Red"));
217 if( thread->do_alpha ) {
218 aph_a = new PaletteAPH(this, x,y+=40, aph, 0, 1);
219 aph_a->create_objects(); aph_a->set_tooltip(_("Alpha"));
221 if( thread->do_okcancel ) {
222 add_tool(new BC_OKButton(this));
223 add_tool(new BC_CancelButton(this));
233 void ColorWindow::change_values()
235 float r = ((thread->output>>16) & 0xff) / 255.;
236 float g = ((thread->output>>8) & 0xff) / 255.;
237 float b = ((thread->output>>0) & 0xff) / 255.;
238 rgb.r = r; rgb.g = g; rgb.b = b;
239 aph = (float)thread->alpha / 255;
240 update_rgb(rgb.r, rgb.g, rgb.b);
244 int ColorWindow::close_event()
246 set_done(thread->do_okcancel ? 1 : 0);
251 void ColorWindow::update_rgb()
253 update_rgb(rgb.r, rgb.g, rgb.b);
256 void ColorWindow::update_hsv()
258 update_hsv(hsv.h, hsv.s, hsv.v);
261 void ColorWindow::update_yuv()
263 update_yuv(yuv.y, yuv.u, yuv.v);
267 void ColorWindow::update_display()
269 wheel->draw(wheel->oldhue, wheel->oldsaturation);
270 wheel->oldhue = hsv.h;
271 wheel->oldsaturation = hsv.s;
272 wheel->draw(hsv.h, hsv.s);
274 wheel_value->draw(hsv.h, hsv.s, hsv.v);
275 wheel_value->flash();
279 hue->update((int)hsv.h);
291 hsv_h->update(hsv.h);
292 hsv_s->update(hsv.s);
293 hsv_v->update(hsv.v);
294 rgb_r->update(rgb.r);
295 rgb_g->update(rgb.g);
296 rgb_b->update(rgb.b);
297 yuv_y->update(yuv.y);
298 yuv_u->update(yuv.u);
299 yuv_v->update(yuv.v);
302 if( thread->do_alpha )
306 int ColorWindow::handle_event()
308 thread->handle_new_color(rgb888(), (int)(255*aph + 0.5));
312 int ColorWindow::button_press_event()
314 if( button_grabbed ) {
319 int ColorWindow::button_release_event()
321 if( button_grabbed ) {
328 get_abs_cursor_xy(cx, cy);
329 //printf("grabbed button %d,%d\n",cx,cy);
330 BC_Capture capture_bitmap(1, 1, 0);
331 VFrame vframe(1,1,BC_RGB888);
332 capture_bitmap.capture_frame(&vframe, cx,cy);
333 unsigned char *data = vframe.get_data();
334 rgb.r = data[0]/255.; rgb.g = data[1]/255.; rgb.b = data[2]/255.;
338 return handle_event();
343 void ColorWindow::update_rgb_hex(const char *hex)
346 if( sscanf(hex,"%x",&color) == 1 ) {
347 float r = ((color>>16) & 0xff) / 255.;
348 float g = ((color>>8) & 0xff) / 255.;
349 float b = ((color>>0) & 0xff) / 255.;
350 rgb.r = r; rgb.g = g; rgb.b = b;
359 PaletteWheel::PaletteWheel(ColorWindow *window, int x, int y)
360 : BC_SubWindow(x, y, 170, 170)
362 this->window = window;
368 PaletteWheel::~PaletteWheel()
372 int PaletteWheel::button_press_event()
374 if( get_cursor_x() >= 0 && get_cursor_x() < get_w() &&
375 get_cursor_y() >= 0 && get_cursor_y() < get_h() &&
378 cursor_motion_event();
384 int PaletteWheel::cursor_motion_event()
386 int x1, y1, distance;
387 if( button_down && is_event_win() ) {
388 float h = get_angle(get_w()/2, get_h()/2, get_cursor_x(), get_cursor_y());
389 bclamp(h, 0, 359.999); window->hsv.h = h;
390 x1 = get_w() / 2 - get_cursor_x();
391 y1 = get_h() / 2 - get_cursor_y();
392 distance = (int)sqrt(x1 * x1 + y1 * y1);
393 float s = (float)distance / (get_w() / 2);
394 bclamp(s, 0, 1); window->hsv.s = s;
395 window->update_hsv();
396 window->update_display();
397 window->handle_event();
403 int PaletteWheel::button_release_event()
412 void PaletteWheel::create_objects()
415 //printf("PaletteWheel::create_objects 1\n");
418 float x1, y1, x2, y2;
420 int default_r, default_g, default_b;
421 VFrame frame(0, -1, get_w(), get_h(), BC_RGBA8888, -1);
424 default_r = (get_resources()->get_bg_color() & 0xff0000) >> 16;
425 default_g = (get_resources()->get_bg_color() & 0xff00) >> 8;
426 default_b = (get_resources()->get_bg_color() & 0xff);
427 //printf("PaletteWheel::create_objects 1\n");
429 int highlight_r = (get_resources()->button_light & 0xff0000) >> 16;
430 int highlight_g = (get_resources()->button_light & 0xff00) >> 8;
431 int highlight_b = (get_resources()->button_light & 0xff);
433 for( y2 = 0; y2 < get_h(); y2++ ) {
434 unsigned char *row = (unsigned char*)frame.get_rows()[(int)y2];
435 for( x2 = 0; x2 < get_w(); x2++ ) {
436 distance = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
437 if( distance > x1 ) {
438 row[(int)x2 * 4] = default_r;
439 row[(int)x2 * 4 + 1] = default_g;
440 row[(int)x2 * 4 + 2] = default_b;
441 row[(int)x2 * 4 + 3] = 0;
444 if( distance > x1 - 1 ) {
446 if( get_h() - y2 < x2 ) {
457 row[(int)x2 * 4] = r_i;
458 row[(int)x2 * 4 + 1] = g_i;
459 row[(int)x2 * 4 + 2] = b_i;
460 row[(int)x2 * 4 + 3] = 255;
463 h = get_angle(x1, y1, x2, y2);
465 HSV::hsv_to_rgb(r, g, b, h, s, v);
466 row[(int)x2 * 4] = (int)(r * 255);
467 row[(int)x2 * 4 + 1] = (int)(g * 255);
468 row[(int)x2 * 4 + 2] = (int)(b * 255);
469 row[(int)x2 * 4 + 3] = 255;
473 //printf("PaletteWheel::create_objects 1\n");
485 //printf("PaletteWheel::create_objects 1\n");
487 oldhue = window->hsv.h;
488 oldsaturation = window->hsv.s;
489 //printf("PaletteWheel::create_objects 1\n");
490 draw(oldhue, oldsaturation);
491 //printf("PaletteWheel::create_objects 1\n");
493 //printf("PaletteWheel::create_objects 2\n");
496 float PaletteWheel::torads(float angle)
498 return (float)angle / 360 * 2 * M_PI;
502 int PaletteWheel::draw(float hue, float saturation)
508 if( hue > 0 && hue < 90 ) {
509 x = (int)(w - w * cos(torads(90 - hue)) * saturation);
510 y = (int)(h - h * sin(torads(90 - hue)) * saturation);
512 else if( hue > 90 && hue < 180 ) {
513 x = (int)(w - w * cos(torads(hue - 90)) * saturation);
514 y = (int)(h + h * sin(torads(hue - 90)) * saturation);
516 else if( hue > 180 && hue < 270 ) {
517 x = (int)(w + w * cos(torads(270 - hue)) * saturation);
518 y = (int)(h + h * sin(torads(270 - hue)) * saturation);
520 else if( hue > 270 && hue < 360 ) {
521 x = (int)(w + w * cos(torads(hue - 270)) * saturation);
522 y = (int)(h - w * sin(torads(hue - 270)) * saturation);
524 else if( hue == 0 ) {
526 y = (int)(h - h * saturation);
528 else if( hue == 90 ) {
529 x = (int)(w - w * saturation);
532 else if( hue == 180 ) {
534 y = (int)(h + h * saturation);
536 else if( hue == 270 ) {
537 x = (int)(w + w * saturation);
543 draw_circle(x - 5, y - 5, 10, 10);
548 int PaletteWheel::get_angle(float x1, float y1, float x2, float y2)
550 float result = -atan2(x2 - x1, y1 - y2) * (360 / M_PI / 2);
556 PaletteWheelValue::PaletteWheelValue(ColorWindow *window, int x, int y)
557 : BC_SubWindow(x, y, 40, 170, BLACK)
559 this->window = window;
562 PaletteWheelValue::~PaletteWheelValue()
567 void PaletteWheelValue::create_objects()
569 frame = new VFrame(0, -1, get_w(), get_h(), BC_RGB888, -1);
570 draw(window->hsv.h, window->hsv.s, window->hsv.v);
574 int PaletteWheelValue::button_press_event()
576 //printf("PaletteWheelValue::button_press 1 %d\n", is_event_win());
577 if( get_cursor_x() >= 0 && get_cursor_x() < get_w() &&
578 get_cursor_y() >= 0 && get_cursor_y() < get_h() &&
580 //printf("PaletteWheelValue::button_press 2\n");
582 cursor_motion_event();
588 int PaletteWheelValue::cursor_motion_event()
590 if( button_down && is_event_win() ) {
591 //printf("PaletteWheelValue::cursor_motion 1\n");
592 float v = 1.0 - (float)(get_cursor_y() - 2) / (get_h() - 4);
593 bclamp(v, 0, 1); window->hsv.v = v;
594 window->update_hsv();
595 window->update_display();
596 window->handle_event();
602 int PaletteWheelValue::button_release_event()
605 //printf("PaletteWheelValue::button_release 1\n");
612 int PaletteWheelValue::draw(float hue, float saturation, float value)
617 for( i = get_h() - 3; i >= 2; i-- ) {
618 unsigned char *row = (unsigned char*)frame->get_rows()[i];
619 HSV::hsv_to_rgb(r_f, g_f, b_f, hue, saturation,
620 1.0 - (float)(i - 2) / (get_h() - 4));
621 r = (int)(r_f * 255);
622 g = (int)(g_f * 255);
623 b = (int)(b_f * 255);
624 for( j = 0; j < get_w(); j++ ) {
631 draw_3d_border(0, 0, get_w(), get_h(), 1);
632 draw_vframe(frame, 2, 2, get_w() - 4, get_h() - 4,
633 2, 2, get_w() - 4, get_h() - 4, 0);
635 draw_line(2, get_h() - 3 - (int)(value * (get_h() - 5)),
636 get_w() - 3, get_h() - 3 - (int)(value * (get_h() - 5)));
637 //printf("PaletteWheelValue::draw %d %f\n", __LINE__, value);
642 PaletteOutput::PaletteOutput(ColorWindow *window, int x, int y)
643 : BC_SubWindow(x, y, 180, 30, BLACK)
645 this->window = window;
647 PaletteOutput::~PaletteOutput()
652 void PaletteOutput::create_objects()
658 int PaletteOutput::handle_event()
663 int PaletteOutput::draw()
665 set_color(window->rgb888());
666 draw_box(2, 2, get_w() - 4, get_h() - 4);
667 draw_3d_border(0, 0, get_w(), get_h(), 1);
671 PaletteHue::PaletteHue(ColorWindow *window, int x, int y)
672 : BC_ISlider(x, y, 0, 150, 200, 0, 359, (int)(window->hsv.h), 0)
674 this->window = window;
676 PaletteHue::~PaletteHue()
680 int PaletteHue::handle_event()
682 window->hsv.h = get_value();
683 window->update_hsv();
684 window->handle_event();
688 PaletteSat::PaletteSat(ColorWindow *window, int x, int y)
689 : BC_FSlider(x, y, 0, 150, 200, 0, 1.0, window->hsv.s, 0)
691 this->window = window;
694 PaletteSat::~PaletteSat()
698 int PaletteSat::handle_event()
700 window->hsv.s = get_value();
701 window->update_hsv();
702 window->handle_event();
707 PaletteVal::PaletteVal(ColorWindow *window, int x, int y)
708 : BC_FSlider(x, y, 0, 150, 200, 0, 1.0, window->hsv.v, 0)
710 this->window = window;
713 PaletteVal::~PaletteVal()
717 int PaletteVal::handle_event()
719 window->hsv.v = get_value();
720 window->update_hsv();
721 window->handle_event();
726 PaletteRed::PaletteRed(ColorWindow *window, int x, int y)
727 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->rgb.r, 0)
729 this->window = window;
732 PaletteRed::~PaletteRed()
736 int PaletteRed::handle_event()
738 window->rgb.r = get_value();
739 window->update_rgb();
740 window->handle_event();
744 PaletteGrn::PaletteGrn(ColorWindow *window, int x, int y)
745 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->rgb.g, 0)
747 this->window = window;
750 PaletteGrn::~PaletteGrn()
754 int PaletteGrn::handle_event()
756 window->rgb.g = get_value();
757 window->update_rgb();
758 window->handle_event();
762 PaletteBlu::PaletteBlu(ColorWindow *window, int x, int y)
763 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->rgb.b, 0)
765 this->window = window;
768 PaletteBlu::~PaletteBlu()
772 int PaletteBlu::handle_event()
774 window->rgb.b = get_value();
775 window->update_rgb();
776 window->handle_event();
780 PaletteAlpha::PaletteAlpha(ColorWindow *window, int x, int y)
781 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->aph, 0)
783 this->window = window;
786 PaletteAlpha::~PaletteAlpha()
790 int PaletteAlpha::handle_event()
792 window->aph = get_value();
793 window->handle_event();
797 PaletteLum::PaletteLum(ColorWindow *window, int x, int y)
798 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->yuv.y, 0)
800 this->window = window;
803 PaletteLum::~PaletteLum()
807 int PaletteLum::handle_event()
809 window->yuv.y = get_value();
810 window->update_yuv();
811 window->handle_event();
815 PaletteCr::PaletteCr(ColorWindow *window, int x, int y)
816 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->yuv.u, 0)
818 this->window = window;
821 PaletteCr::~PaletteCr()
825 int PaletteCr::handle_event()
827 window->yuv.u = get_value();
828 window->update_yuv();
829 window->handle_event();
833 PaletteCb::PaletteCb(ColorWindow *window, int x, int y)
834 : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->yuv.v, 0)
836 this->window = window;
839 PaletteCb::~PaletteCb()
843 int PaletteCb::handle_event()
845 window->yuv.v = get_value();
846 window->update_yuv();
847 window->handle_event();
851 void ColorWindow::update_rgb(float r, float g, float b)
854 YUV::rgb_to_yuv_f(r, g, b, y, u, v);
856 bclamp(y, 0, 1); yuv.y = y;
857 bclamp(u, 0, 1); yuv.u = u;
858 bclamp(v, 0, 1); yuv.v = v; }
860 HSV::rgb_to_hsv(r,g,b, h,s,v);
861 bclamp(h, 0, 360); hsv.h = h;
862 bclamp(s, 0, 1); hsv.s = s;
863 bclamp(v, 0, 1); hsv.v = v; }
866 void ColorWindow::update_yuv(float y, float u, float v)
870 YUV::yuv_to_rgb_f(r, g, b, y, u, v);
871 bclamp(r, 0, 1); rgb.r = r;
872 bclamp(g, 0, 1); rgb.g = g;
873 bclamp(b, 0, 1); rgb.b = b;
875 HSV::rgb_to_hsv(r,g,b, h, s, v);
876 bclamp(h, 0, 360); hsv.h = h;
877 bclamp(s, 0, 1); hsv.s = s;
878 bclamp(v, 0, 1); hsv.v = v; }
881 void ColorWindow::update_hsv(float h, float s, float v)
884 HSV::hsv_to_rgb(r,g,b, h,s,v);
885 bclamp(r, 0, 1); rgb.r = r;
886 bclamp(g, 0, 1); rgb.g = g;
887 bclamp(b, 0, 1); rgb.b = b;
889 YUV::rgb_to_yuv_f(r, g, b, y, u, v);
891 bclamp(y, 0, 1); yuv.y = y;
892 bclamp(u, 0, 1); yuv.u = u;
893 bclamp(v, 0, 1); yuv.v = v; }
896 void ColorWindow::load_history()
898 char history_path[BCTEXTLEN];
899 MWindow::create_defaults_path(history_path,PALETTE_DATA);
900 FILE *fp = fopen(history_path,"r");
903 while( i < PALLETTE_HISTORY_SIZE ) {
905 if( !fgets(line,sizeof(line)-1,fp) ) break;
906 line[sizeof(line)-1] = 0;
907 if( sscanf(line, "%x",&palette_history[i]) != 1 ) break;
912 int r = 0, g = 0, b = 0;
913 float v0 = 0, v1 = 1;
914 while( i < PALLETTE_HISTORY_SIZE ) {
915 int grey_code = i ^ (i>>1);
916 r = 255 * ((grey_code&4) ? v0 : v1);
917 g = 255 * ((grey_code&2) ? v0 : v1);
918 b = 255 * ((grey_code&1) ? v0 : v1);
919 int color = (r<<16) | (g<<8) | (b<<0);
920 palette_history[i++] = color;
921 if( i & 7 ) continue;
922 v0 = 0.5f * (v0+.5f);
923 v1 = 0.5f * (v1+.5f);
926 void ColorWindow::save_history()
928 char history_path[BCTEXTLEN];
929 MWindow::create_defaults_path(history_path,PALETTE_DATA);
930 FILE *fp = fopen(history_path,"w");
932 for( int i=0; i<PALLETTE_HISTORY_SIZE; ++i ) {
933 fprintf(fp, "%06x\n", palette_history[i]);
938 void ColorWindow::update_history(int color)
940 int out = palette_history[0];
941 palette_history[0] = color;
942 for( int i=1; out != color && i<PALLETTE_HISTORY_SIZE; ++i ) {
944 out = palette_history[i];
945 palette_history[i] = in;
948 void ColorWindow::update_history()
950 update_history(rgb888());
953 int ColorWindow::rgb888()
955 int r = 255*rgb.r + 0.5, g = 255*rgb.g + 0.5, b = 255*rgb.b + 0.5;
956 bclamp(r, 0, 255); bclamp(g, 0, 255); bclamp(b, 0, 255);
957 return (r<<16) | (g<<8) | (b<<0);
960 PaletteNum::PaletteNum(ColorWindow *window, int x, int y,
961 float &output, float min, float max)
962 : BC_TumbleTextBox(window, output, min, max, x, y, 64)
964 this->window = window;
965 this->output = &output;
970 PaletteNum::~PaletteNum()
975 int PaletteHSV::handle_event()
978 window->update_hsv();
979 window->update_display();
980 window->handle_event();
984 int PaletteRGB::handle_event()
987 window->update_rgb();
988 window->update_display();
989 window->handle_event();
993 int PaletteYUV::handle_event()
996 window->update_yuv();
997 window->update_display();
998 window->handle_event();
1002 int PaletteAPH::handle_event()
1005 window->update_display();
1006 window->handle_event();
1010 PaletteHexButton::PaletteHexButton(ColorWindow *window, int x, int y)
1011 : BC_GenericButton(x, y, 50, "#")
1013 this->window = window;
1014 set_tooltip(_("hex rgb color"));
1016 PaletteHexButton::~PaletteHexButton()
1019 int PaletteHexButton::handle_event()
1021 const char *hex = window->hex_box->get_text();
1022 window->update_rgb_hex(hex);
1026 PaletteHex::PaletteHex(ColorWindow *window, int x, int y, const char *hex)
1027 : BC_TextBox(x, y, 100, 1, hex)
1029 this->window = window;
1031 PaletteHex::~PaletteHex()
1034 void PaletteHex::update()
1036 char hex[BCSTRLEN]; sprintf(hex,"%06x",window->rgb888());
1037 BC_TextBox::update(hex);
1040 int PaletteHex::keypress_event()
1042 if( get_keypress() != RETURN )
1043 return BC_TextBox::keypress_event();
1044 window->update_rgb_hex(get_text());
1048 #include "grabpick_up_png.h"
1049 #include "grabpick_hi_png.h"
1050 #include "grabpick_dn_png.h"
1052 PaletteGrabButton::PaletteGrabButton(ColorWindow *window, int x, int y)
1053 : BC_Button(x, y, vframes)
1055 this->window = window;
1056 vframes[0] = new VFramePng(grabpick_up_png);
1057 vframes[1] = new VFramePng(grabpick_hi_png);
1058 vframes[2] = new VFramePng(grabpick_dn_png);
1059 set_tooltip(_("grab from anywhere picker"));
1061 PaletteGrabButton::~PaletteGrabButton()
1063 for( int i=0; i<3; ++i )
1066 int PaletteGrabButton::handle_event()
1068 if( window->grab_buttons() ) {
1069 window->button_grabbed = 1;
1070 button_press_event(); // redraw face HI
1075 PaletteHistory::PaletteHistory(ColorWindow *window, int x, int y)
1076 : BC_SubWindow(x,y, 200, 24)
1078 this->window = window;
1080 set_tooltip(_("color history"));
1082 PaletteHistory::~PaletteHistory()
1085 void PaletteHistory::update(int flush)
1088 for( int i=0; i<PALLETTE_HISTORY_SIZE; x1=x2 ) {
1089 int rgb = window->palette_history[i];
1090 x2 = (++i * get_w())/PALLETTE_HISTORY_SIZE;
1091 draw_3d_box(x1,0,x2-x1,get_h(),WHITE,BLACK,rgb,LTBLUE,DKBLUE);
1096 int PaletteHistory::button_press_event()
1098 if( button_down || !is_event_win() ) return 0;
1100 cursor_motion_event();
1103 int PaletteHistory::button_release_event()
1105 if( !button_down || !is_event_win() ) return 0;
1106 cursor_motion_event();
1107 if( button_down > 0 ) {
1108 window->handle_event();
1109 window->update_display();
1110 window->update_history();
1115 int PaletteHistory::cursor_motion_event()
1117 if( !button_down || !is_event_win() ) return 0;
1119 int pick = (PALLETTE_HISTORY_SIZE * get_cursor_x()) / get_w();
1120 bclamp(pick, 0, PALLETTE_HISTORY_SIZE-1);
1121 int color = window->palette_history[pick];
1122 float r = ((color>>16) & 0xff) / 255.;
1123 float g = ((color>>8) & 0xff) / 255.;
1124 float b = ((color>>0) & 0xff) / 255.;
1125 if( window->rgb.r != r || window->rgb.g != g || window->rgb.b != b ) {
1126 window->rgb.r = r; window->rgb.g = g; window->rgb.b = b;
1127 window->update_rgb();
1128 window->update_display();
1133 int PaletteHistory::cursor_leave_event()
1138 int PaletteHistory::repeat_event(int64_t duration)
1142 if( duration == get_resources()->tooltip_delay &&
1143 get_tooltip() && *get_tooltip() && cursor_above() ) {