olaf neophyte and de.po updates, valgrind tweaks, delete green lady, inkscape dpi=96
[goodguy/history.git] / cinelerra-5.1 / cinelerra / colorpicker.C
index f51069e1a8145423914a6b59b1842b696fc6f94c..1b40fcf0c38f8d00bca6f4b35972422918d71c3c 100644 (file)
 /*
  * CINELERRA
  * Copyright (C) 1997-2011 Adam Williams <broadcast at earthling dot net>
- * 
+ *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
- * 
+ *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- * 
+ *
  */
 
+#include "bcbutton.h"
+#include "bccapture.h"
+#include "bccolors.h"
 #include "bcdisplayinfo.h"
 #include "colorpicker.h"
 #include "condition.h"
+#include "keys.h"
 #include "language.h"
 #include "mutex.h"
-#include "mwindow.inc"
-#include "cicolors.h"
+#include "mwindow.h"
+#include "bccolors.h"
 #include "vframe.h"
 
 #include <string.h>
 #include <unistd.h>
 
+#define PALETTE_DATA "palette.dat"
 
-ColorThread::ColorThread(int do_alpha, char *title)
- : Thread()
+ColorPicker::ColorPicker(int do_alpha, const char *title)
+ : BC_DialogThread()
 {
-       window = 0;
        this->title = title;
        this->do_alpha = do_alpha;
-       set_synchronous(0);
-       mutex = new Mutex("ColorThread::mutex");
-       completion = new Condition(1, "ColorThread::completion");
+       this->do_okcancel = 0;
+       this->output = BLACK;
+       this->alpha = 255;
 }
 
-ColorThread::~ColorThread()
+ColorPicker::~ColorPicker()
 {
-       if(running())
-       {
-               window->set_done(0);
-               completion->lock("ColorThread::~ColorThread");
-               completion->unlock();
-       }
-       delete mutex;
-       delete completion;
+       close_window();
 }
 
-void ColorThread::start_window(int output, int alpha)
+void ColorPicker::start_window(int output, int alpha, int do_okcancel)
 {
-       mutex->lock("ColorThread::start_window 1");
+       if( running() ) {
+               ColorWindow *gui = (ColorWindow *)get_gui();
+               if( gui ) {
+                       gui->lock_window("ColorPicker::start_window");
+                       gui->raise_window(1);
+                       gui->unlock_window();
+               }
+               return;
+       }
        this->output = output;
        this->alpha = alpha;
-       mutex->unlock();
-
-       if(!running())
-       {
-               completion->lock("ColorThread::start_window");
-               Thread::start();
-       }
-       else
-       {
-               window->raise_window();
-               window->flush();
-       }
+       this->do_okcancel = do_okcancel;
+       start();
 }
 
-void ColorThread::run()
+BC_Window* ColorPicker::new_gui()
 {
-       BC_DisplayInfo info;
-//printf("ColorThread::run 1\n");
        char window_title[BCTEXTLEN];
-
        strcpy(window_title, _(PROGRAM_NAME ": "));
-       if(title)
-               strcat(window_title, title);
-       else
-               strcat(window_title, _("Color Picker"));
-
-
-       mutex->lock("ColorThread::run 1");
-       window = new ColorWindow(this, 
-               info.get_abs_cursor_x() - 200, 
-               info.get_abs_cursor_y() - 200,
-               window_title);
-       window->create_objects();
-       mutex->unlock();
-       window->run_window();
-       mutex->lock("lorThread::run 2");
-       delete window;
-       window = 0;
-       mutex->unlock();
-       completion->unlock();
-}
-
-void ColorThread::update_gui(int output, int alpha)
-{
-       mutex->lock("ColorThread::update_gui");
-       if (window)
-       {
-               this->output = output;
-               this->alpha = alpha;
-               window->change_values();
-               window->lock_window();
-               window->update_display();
-               window->unlock_window();
-       }
-       mutex->unlock();
+       strcat(window_title, title ? title : _("Color Picker"));
+       BC_DisplayInfo display_info;
+       int x = display_info.get_abs_cursor_x() + 25;
+       int y = display_info.get_abs_cursor_y() - 100;
+       int w = 540, h = 330;
+       if( do_okcancel )
+               h += bmax(BC_OKButton::calculate_h(),BC_CancelButton::calculate_h());
+       int root_w = display_info.get_root_w(), root_h = display_info.get_root_h();
+       if( x+w > root_w ) x = root_w - w;
+       if( y+h > root_h ) y = root_h - h;
+       if( x < 0 ) x = 0;
+       if( y < 0 ) y = 0;
+       ColorWindow *gui = new ColorWindow(this, x, y, w, h, window_title);
+       gui->create_objects();
+       return gui;
 }
 
-int ColorThread::handle_new_color(int output, int alpha)
+void ColorPicker::update_gui(int output, int alpha)
 {
-       printf("ColorThread::handle_new_color undefined.\n");
-       return 0;
+       ColorWindow *gui = (ColorWindow *)get_gui();
+       if( !gui ) return;
+       gui->lock_window();
+       this->output = output;
+       this->alpha = alpha;
+       gui->change_values();
+       gui->update_display();
+       gui->unlock_window();
 }
 
-ColorWindow* ColorThread::get_gui()
+int ColorPicker::handle_new_color(int output, int alpha)
 {
-       return window;
+       printf("ColorPicker::handle_new_color undefined.\n");
+       return 0;
 }
 
 
-
-ColorWindow::ColorWindow(ColorThread *thread, int x, int y, char *title)
- : BC_Window(title, 
-       x,
-       y,
-       410, 
-       320, 
-       410, 
-       320, 
-       0, 
-       0,
-       1)
+ColorWindow::ColorWindow(ColorPicker *thread, int x, int y, int w, int h, const char *title)
+ : BC_Window(title, x, y, w, h, w, h, 0, 0, 1)
 {
        this->thread = thread;
+       wheel = 0;
+       wheel_value = 0;
+       output = 0;
+
+       hue = 0; sat = 0; val = 0;
+       red = 0; grn = 0; blu = 0;
+       lum = 0; c_r = 0; c_b = 0;
+       alpha = 0;
+
+       hsv_h = 0;  hsv_s = 0;  hsv_v = 0;
+       rgb_r = 0;  rgb_g = 0;  rgb_b = 0;
+       yuv_y = 0;  yuv_u = 0;  yuv_v = 0;
+       aph_a = 0;
+
+       button_grabbed = 0;
+}
+ColorWindow::~ColorWindow()
+{
+       delete hsv_h;  delete hsv_s;  delete hsv_v;
+       delete rgb_r;  delete rgb_g;  delete rgb_b;
+       delete yuv_y;  delete yuv_u;  delete yuv_v;
+       delete aph_a;
+
+       if( button_grabbed ) {
+               ungrab_buttons();
+               ungrab_cursor();
+       }
+       update_history(rgb888());
+       save_history();
 }
 
 void ColorWindow::create_objects()
 {
-       int x = 10, init_x = 10, y = 10, init_y = 10;
-       
+       int x0 = 10, y0 = 10;
        lock_window("ColorWindow::create_objects");
        change_values();
-       
-       
-       
-//printf("ColorWindow::create_objects 1 %p\n", this);
+
+       int x = x0, y = y0;
        add_tool(wheel = new PaletteWheel(this, x, y));
-//printf("ColorWindow::create_objects 1\n");
        wheel->create_objects();
-//printf("ColorWindow::create_objects 1\n");
 
-       x += 180;
-       add_tool(wheel_value = new PaletteWheelValue(this, x, y));
-//printf("ColorWindow::create_objects 1\n");
+       x += 180;  add_tool(wheel_value = new PaletteWheelValue(this, x, y));
        wheel_value->create_objects();
-
-
-       y += 180;
-       x = init_x; 
-//printf("ColorWindow::create_objects 1\n");
-       add_tool(output = new PaletteOutput(this, x, y));
-//printf("ColorWindow::create_objects 1\n");
+       x = x0;
+       y += 180;  add_tool(output = new PaletteOutput(this, x, y));
        output->create_objects();
-//printf("ColorWindow::create_objects 1\n");
-       
-       x += 240; y = init_y;
-       add_tool(new BC_Title(x, y, _("Hue"), SMALLFONT));
-       y += 15;
-//printf("ColorWindow::create_objects 1 %p\n", this);
-       add_tool(hue = new PaletteHue(this, x, y));
-       y += 30;
-//printf("ColorWindow::create_objects 1\n");
-       add_tool(new BC_Title(x, y, _("Saturation"), SMALLFONT));
-       y += 15;
-//printf("ColorWindow::create_objects 1\n");
-       add_tool(saturation = new PaletteSaturation(this, x, y));
-       y += 30;
-//printf("ColorWindow::create_objects 1\n");
-       add_tool(new BC_Title(x, y, _("Value"), SMALLFONT));
-       y += 15;
-//printf("ColorWindow::create_objects 1\n");
-       add_tool(value = new PaletteValue(this, x, y));
-       y += 30;
-//printf("ColorWindow::create_objects 1\n");
-       add_tool(new BC_Title(x, y, _("Red"), SMALLFONT));
-       y += 15;
-//printf("ColorWindow::create_objects 1\n");
-       add_tool(red = new PaletteRed(this, x, y));
-       y += 30;
-//printf("ColorWindow::create_objects 1\n");
-       add_tool(new BC_Title(x, y, _("Green"), SMALLFONT));
-       y += 15;
-//printf("ColorWindow::create_objects 1\n");
-       add_tool(green = new PaletteGreen(this, x, y));
-       y += 30;
-//printf("ColorWindow::create_objects 1\n");
-       add_tool(new BC_Title(x, y, _("Blue"), SMALLFONT));
-       y += 15;
-//printf("ColorWindow::create_objects 1\n");
-       add_tool(blue = new PaletteBlue(this, x, y));
-
-       if(thread->do_alpha)
-       {
-               y += 30;
-               add_tool(new BC_Title(x, y, _("Alpha"), SMALLFONT));
-               y += 15;
-               add_tool(alpha = new PaletteAlpha(this, x, y));
+       y += output->get_h() + 20;
+
+       load_history();  int x1 = x;
+       add_tool(hex_btn = new PaletteHexButton(this, x1, y));
+       char hex[BCSTRLEN];  sprintf(hex,"%06x",thread->output);
+       x1 += hex_btn->get_w() + 5;
+       add_tool(hex_box = new PaletteHex(this, x1, y, hex));
+       x1 += hex_box->get_w() + 15;
+       add_tool(grab_btn = new PaletteGrabButton(this, x1, y));
+       y += hex_box->get_h() + 15;
+       add_tool(history = new PaletteHistory(this, 10, y));
+
+       x += 240;
+       add_tool(new BC_Title(x, y =y0, C_("H:"), SMALLFONT));
+       add_tool(new BC_Title(x, y+=25, C_("S:"), SMALLFONT));
+       add_tool(new BC_Title(x, y+=25, D_("colorpicker_value#V:"), SMALLFONT));
+       add_tool(new BC_Title(x, y+=40, C_("R:"), SMALLFONT));
+       add_tool(new BC_Title(x, y+=25, C_("G:"), SMALLFONT));
+       add_tool(new BC_Title(x, y+=25, C_("B:"), SMALLFONT));
+       add_tool(new BC_Title(x, y+=40, C_("Y:"), SMALLFONT));
+       add_tool(new BC_Title(x, y+=25, C_("U:"), SMALLFONT));
+       add_tool(new BC_Title(x, y+=25, D_("colorpicker_Cr#V:"), SMALLFONT));
+       if( thread->do_alpha )
+               add_tool(new BC_Title(x, y+=40, C_("A:"), SMALLFONT));
+       x += 24;
+       add_tool(hue = new PaletteHue(this, x, y= y0));
+       add_tool(sat = new PaletteSat(this, x, y+=25));
+       add_tool(val = new PaletteVal(this, x, y+=25));
+       add_tool(red = new PaletteRed(this, x, y+=40));
+       add_tool(grn = new PaletteGrn(this, x, y+=25));
+       add_tool(blu = new PaletteBlu(this, x, y+=25));
+       add_tool(lum = new PaletteLum(this, x, y+=40));
+       add_tool(c_r = new PaletteCr (this, x, y+=25));
+       add_tool(c_b = new PaletteCb (this, x, y+=25));
+       if( thread->do_alpha )
+               add_tool(alpha = new PaletteAlpha(this, x, y+=40));
+
+       x += hue->get_w() + 10;
+       hsv_h = new PaletteHSV(this, x,y= y0, hsv.h, 0, 360);
+       hsv_h->create_objects();  hsv_h->set_tooltip(_("Hue"));
+       hsv_s = new PaletteHSV(this, x,y+=25, hsv.s, 0, 1);
+       hsv_s->create_objects();  hsv_s->set_tooltip(_("Saturation"));
+       hsv_v = new PaletteHSV(this, x,y+=25, hsv.v, 0, 1);
+       hsv_v->create_objects();  hsv_v->set_tooltip(_("Value"));
+       rgb_r = new PaletteRGB(this, x,y+=40, rgb.r, 0, 1);
+       rgb_r->create_objects();  rgb_r->set_tooltip(_("Red"));
+       rgb_g = new PaletteRGB(this, x,y+=25, rgb.g, 0, 1);
+       rgb_g->create_objects();  rgb_g->set_tooltip(_("Green"));
+       rgb_b = new PaletteRGB(this, x,y+=25, rgb.b, 0, 1);
+       rgb_b->create_objects();  rgb_b->set_tooltip(_("Blue"));
+       yuv_y = new PaletteYUV(this, x,y+=40, yuv.y, 0, 1);
+       yuv_y->create_objects();  yuv_y->set_tooltip(_("Luminance"));
+       yuv_u = new PaletteYUV(this, x,y+=25, yuv.u, 0, 1);
+       yuv_u->create_objects();  yuv_u->set_tooltip(_("Blue Luminance Difference"));
+       yuv_v = new PaletteYUV(this, x,y+=25, yuv.v, 0, 1);
+       yuv_v->create_objects();  yuv_v->set_tooltip(_("Red Luminance Difference"));
+       if( thread->do_alpha ) {
+               aph_a = new PaletteAPH(this, x,y+=40, aph, 0, 1);
+               aph_a->create_objects();  aph_a->set_tooltip(_("Alpha"));
+       }
+       if( thread->do_okcancel ) {
+               add_tool(new BC_OKButton(this));
+               add_tool(new BC_CancelButton(this));
        }
 
        update_display();
-       show_window();
-       flush();
+       update_history();
+       show_window(1);
        unlock_window();
-       return;
 }
 
 
 void ColorWindow::change_values()
- {
-       r = (float)((thread->output & 0xff0000) >> 16) / 255;
-       g = (float)((thread->output & 0xff00) >> 8) / 255;
-       b = (float)((thread->output & 0xff)) / 255;
-       HSV::rgb_to_hsv(r, g, b, h, s, v);
-       a = (float)thread->alpha / 255;
+{
+       float r = ((thread->output>>16) & 0xff) / 255.;
+       float g = ((thread->output>>8)  & 0xff) / 255.;
+       float b = ((thread->output>>0)  & 0xff) / 255.;
+       rgb.r = r;  rgb.g = g;  rgb.b = b;
+       aph = (float)thread->alpha / 255;
+       update_rgb(rgb.r, rgb.g, rgb.b);
 }
 
 
 int ColorWindow::close_event()
 {
-       set_done(0);
+       set_done(thread->do_okcancel ? 1 : 0);
        return 1;
 }
 
 
 void ColorWindow::update_rgb()
 {
-       HSV::rgb_to_hsv(red->get_value(), 
-                               green->get_value(), 
-                               blue->get_value(), 
-                               h, 
-                               s, 
-                               v);
+       update_rgb(rgb.r, rgb.g, rgb.b);
+       update_display();
+}
+void ColorWindow::update_hsv()
+{
+       update_hsv(hsv.h, hsv.s, hsv.v);
+       update_display();
+}
+void ColorWindow::update_yuv()
+{
+       update_yuv(yuv.y, yuv.u, yuv.v);
        update_display();
 }
 
 void ColorWindow::update_display()
 {
-       float r, g, b;
-       if(h < 0) h = 0;
-       if(h > 360) h = 360;
-       if(s < 0) s = 0;
-       if(s > 1) s = 1;
-       if(v < 0) v = 0;
-       if(v > 1) v = 1;
-       if(a < 0) a = 0;
-       if(a > 1) a = 1;
-
-       wheel->draw(wheel->oldhue, 
-                               wheel->oldsaturation);
-       wheel->oldhue = h;
-       wheel->oldsaturation = s;
-       wheel->draw(h, s);
+       wheel->draw(wheel->oldhue, wheel->oldsaturation);
+       wheel->oldhue = hsv.h;
+       wheel->oldsaturation = hsv.s;
+       wheel->draw(hsv.h, hsv.s);
        wheel->flash();
-       wheel_value->draw(h, s, v);
+       wheel_value->draw(hsv.h, hsv.s, hsv.v);
        wheel_value->flash();
        output->draw();
        output->flash();
-       hue->update((int)h);
-       saturation->update(s);
-       value->update(v);
 
-       HSV::hsv_to_rgb(r, g, b, h, s, v);
-       red->update(r);
-       green->update(g);
-       blue->update(b);
-       if(thread->do_alpha)
-       {
-               alpha->update(a);
-       }
+       hue->update((int)hsv.h);
+       sat->update(hsv.s);
+       val->update(hsv.v);
+
+       red->update(rgb.r);
+       grn->update(rgb.g);
+       blu->update(rgb.b);
+
+       lum->update(yuv.y);
+       c_r->update(yuv.u);
+       c_b->update(yuv.v);
+
+       hsv_h->update(hsv.h);
+       hsv_s->update(hsv.s);
+       hsv_v->update(hsv.v);
+       rgb_r->update(rgb.r);
+       rgb_g->update(rgb.g);
+       rgb_b->update(rgb.b);
+       yuv_y->update(yuv.y);
+       yuv_u->update(yuv.u);
+       yuv_v->update(yuv.v);
+       hex_box->update();
+
+       if( thread->do_alpha )
+               aph_a->update(aph);
 }
 
 int ColorWindow::handle_event()
 {
-       float r, g, b;
-       HSV::hsv_to_rgb(r, g, b, h, s, v);
-       int result = (((int)(r * 255)) << 16) | (((int)(g * 255)) << 8) | ((int)(b * 255));
-       thread->handle_new_color(result, (int)(a * 255));
+       thread->handle_new_color(rgb888(), (int)(255*aph + 0.5));
        return 1;
 }
 
+void ColorWindow::get_screen_sample()
+{
+       int cx, cy;
+       get_abs_cursor(cx, cy);
+       BC_Capture capture_bitmap(1, 1, 0);
+       VFrame vframe(1,1,BC_RGB888);
+       capture_bitmap.capture_frame(&vframe, cx,cy);
+       unsigned char *data = vframe.get_data();
+       rgb.r = data[0]/255.;  rgb.g = data[1]/255.;  rgb.b = data[2]/255.;
+       update_rgb();
+}
 
+int ColorWindow::cursor_motion_event()
+{
+       if( button_grabbed && get_button_down() ) {
+               get_screen_sample();
+               return 1;
+       }
+       return 0;
+}
 
+int ColorWindow::button_press_event()
+{
+       if( button_grabbed ) {
+               get_screen_sample();
+               return 1;
+       }
+       return 0;
+}
 
+int ColorWindow::button_release_event()
+{
+       if( button_grabbed ) {
+               ungrab_buttons();
+               ungrab_cursor();
+               grab_btn->enable();
+               button_grabbed = 0;
+               update_history();
+               return handle_event();
+       }
+       return 1;
+}
 
+void ColorWindow::update_rgb_hex(const char *hex)
+{
+       int color;
+       if( sscanf(hex,"%x",&color) == 1 ) {
+               float r = ((color>>16) & 0xff) / 255.;
+               float g = ((color>>8)  & 0xff) / 255.;
+               float b = ((color>>0)  & 0xff) / 255.;
+               rgb.r = r;  rgb.g = g;  rgb.b = b;
+               update_rgb();
+               update_history();
+               handle_event();
+       }
+}
 
 
 PaletteWheel::PaletteWheel(ColorWindow *window, int x, int y)
@@ -313,16 +376,16 @@ PaletteWheel::PaletteWheel(ColorWindow *window, int x, int y)
        oldsaturation = 0;
        button_down = 0;
 }
+
 PaletteWheel::~PaletteWheel()
 {
 }
 
 int PaletteWheel::button_press_event()
 {
-       if(get_cursor_x() >= 0 && get_cursor_x() < get_w() &&
-               get_cursor_y() >= 0 && get_cursor_y() < get_h() && 
-               is_event_win())
-       {
+       if( get_cursor_x() >= 0 && get_cursor_x() < get_w() &&
+               get_cursor_y() >= 0 && get_cursor_y() < get_h() &&
+               is_event_win() ) {
                button_down = 1;
                cursor_motion_event();
                return 1;
@@ -333,18 +396,16 @@ int PaletteWheel::button_press_event()
 int PaletteWheel::cursor_motion_event()
 {
        int x1, y1, distance;
-       if(button_down && is_event_win())
-       {
-               window->h = get_angle(get_w() / 2, 
-                       get_h() / 2, 
-                       get_cursor_x(), 
-                       get_cursor_y());
+       if( button_down && is_event_win() ) {
+               float h = get_angle(get_w()/2, get_h()/2, get_cursor_x(), get_cursor_y());
+               bclamp(h, 0, 359.999);  window->hsv.h = h;
                x1 = get_w() / 2 - get_cursor_x();
                y1 = get_h() / 2 - get_cursor_y();
                distance = (int)sqrt(x1 * x1 + y1 * y1);
-               if(distance > get_w() / 2) distance = get_w() / 2;
-               window->s = (float)distance / (get_w() / 2);
-               window->update_display();
+               float s = (float)distance / (get_w() / 2);
+               bclamp(s, 0, 1);  window->hsv.s = s;
+               window->hsv.v = 1;
+               window->update_hsv();
                window->handle_event();
                return 1;
        }
@@ -353,8 +414,7 @@ int PaletteWheel::cursor_motion_event()
 
 int PaletteWheel::button_release_event()
 {
-       if(button_down)
-       {
+       if( button_down ) {
                button_down = 0;
                return 1;
        }
@@ -365,9 +425,7 @@ void PaletteWheel::create_objects()
 {
 // Upper right
 //printf("PaletteWheel::create_objects 1\n");
-       float h;
-       float s;
-       float v = 1;
+       float h, s, v = 1;
        float r, g, b;
        float x1, y1, x2, y2;
        float distance;
@@ -384,31 +442,25 @@ void PaletteWheel::create_objects()
        int highlight_g = (get_resources()->button_light & 0xff00) >> 8;
        int highlight_b = (get_resources()->button_light & 0xff);
 
-       for(y2 = 0; y2 < get_h(); y2++)
-       {
+       for( y2 = 0; y2 < get_h(); y2++ ) {
                unsigned char *row = (unsigned char*)frame.get_rows()[(int)y2];
-               for(x2 = 0; x2 < get_w(); x2++)
-               {
+               for( x2 = 0; x2 < get_w(); x2++ ) {
                        distance = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
-                       if(distance > x1)
-                       {
+                       if( distance > x1 ) {
                                row[(int)x2 * 4] = default_r;
                                row[(int)x2 * 4 + 1] = default_g;
                                row[(int)x2 * 4 + 2] = default_b;
                                row[(int)x2 * 4 + 3] = 0;
                        }
                        else
-                       if(distance > x1 - 1)
-                       {
+                       if( distance > x1 - 1 ) {
                                int r_i, g_i, b_i;
-                               if(get_h() - y2 < x2)
-                               {
+                               if( get_h() - y2 < x2 ) {
                                        r_i = highlight_r;
                                        g_i = highlight_g;
                                        b_i = highlight_b;
                                }
-                               else
-                               {
+                               else {
                                        r_i = 0;
                                        g_i = 0;
                                        b_i = 0;
@@ -419,8 +471,7 @@ void PaletteWheel::create_objects()
                                row[(int)x2 * 4 + 2] = b_i;
                                row[(int)x2 * 4 + 3] = 255;
                        }
-                       else
-                       {
+                       else {
                                h = get_angle(x1, y1, x2, y2);
                                s = distance / x1;
                                HSV::hsv_to_rgb(r, g, b, h, s, v);
@@ -433,20 +484,20 @@ void PaletteWheel::create_objects()
        }
 //printf("PaletteWheel::create_objects 1\n");
 
-       draw_vframe(&frame, 
-               0, 
-               0, 
-               get_w(), 
-               get_h(), 
-               0, 
-               0, 
-               get_w(), 
-               get_h(), 
+       draw_vframe(&frame,
+               0,
+               0,
+               get_w(),
+               get_h(),
+               0,
+               0,
+               get_w(),
+               get_h(),
                0);
 //printf("PaletteWheel::create_objects 1\n");
 
-       oldhue = window->h;
-       oldsaturation = window->s;
+       oldhue = window->hsv.h;
+       oldsaturation = window->hsv.s;
 //printf("PaletteWheel::create_objects 1\n");
        draw(oldhue, oldsaturation);
 //printf("PaletteWheel::create_objects 1\n");
@@ -466,50 +517,35 @@ int PaletteWheel::draw(float hue, float saturation)
        x = w = get_w() / 2;
        y = h = get_h() / 2;
 
-       if(hue > 0 && hue < 90)
-       {
+       if( hue > 0 && hue < 90 ) {
                x = (int)(w - w * cos(torads(90 - hue)) * saturation);
                y = (int)(h - h * sin(torads(90 - hue)) * saturation);
        }
-       else
-       if(hue > 90 && hue < 180)
-       {
+       else if( hue > 90 && hue < 180 ) {
                x = (int)(w - w * cos(torads(hue - 90)) * saturation);
                y = (int)(h + h * sin(torads(hue - 90)) * saturation);
        }
-       else
-       if(hue > 180 && hue < 270)
-       {
+       else if( hue > 180 && hue < 270 ) {
                x = (int)(w + w * cos(torads(270 - hue)) * saturation);
                y = (int)(h + h * sin(torads(270 - hue)) * saturation);
        }
-       else
-       if(hue > 270 && hue < 360)
-       {
+       else if( hue > 270 && hue < 360 ) {
                x = (int)(w + w * cos(torads(hue - 270)) * saturation);
                y = (int)(h - w * sin(torads(hue - 270)) * saturation);
        }
-       else
-       if(hue == 0) 
-       {
+       else if( hue == 0 ) {
                x = w;
                y = (int)(h - h * saturation);
        }
-       else
-       if(hue == 90)
-       {
+       else if( hue == 90 ) {
                x = (int)(w - w * saturation);
                y = h;
        }
-       else
-       if(hue == 180)
-       {
+       else if( hue == 180 ) {
                x = w;
                y = (int)(h + h * saturation);
        }
-       else
-       if(hue == 270)
-       {
+       else if( hue == 270 ) {
                x = (int)(w + w * saturation);
                y = h;
        }
@@ -524,7 +560,7 @@ int PaletteWheel::draw(float hue, float saturation)
 int PaletteWheel::get_angle(float x1, float y1, float x2, float y2)
 {
        float result = -atan2(x2 - x1, y1 - y2) * (360 / M_PI / 2);
-       if (result < 0)
+       if( result < 0 )
                result += 360;
        return (int)result;
 }
@@ -542,18 +578,17 @@ PaletteWheelValue::~PaletteWheelValue()
 
 void PaletteWheelValue::create_objects()
 {
-       frame = new VFrame(0, -1, get_w(), get_h(), BC_RGB888, -1);
-       draw(window->h, window->s, window->v);
+       frame = new VFrame(get_w(), get_h(), BC_RGB888);
+       draw(window->hsv.h, window->hsv.s, window->hsv.v);
        flash();
 }
 
 int PaletteWheelValue::button_press_event()
 {
 //printf("PaletteWheelValue::button_press 1 %d\n", is_event_win());
-       if(get_cursor_x() >= 0 && get_cursor_x() < get_w() &&
-               get_cursor_y() >= 0 && get_cursor_y() < get_h() && 
-               is_event_win())
-       {
+       if( get_cursor_x() >= 0 && get_cursor_x() < get_w() &&
+               get_cursor_y() >= 0 && get_cursor_y() < get_h() &&
+               is_event_win() ) {
 //printf("PaletteWheelValue::button_press 2\n");
                button_down = 1;
                cursor_motion_event();
@@ -564,11 +599,11 @@ int PaletteWheelValue::button_press_event()
 
 int PaletteWheelValue::cursor_motion_event()
 {
-       if(button_down && is_event_win())
-       {
+       if( button_down && is_event_win() ) {
 //printf("PaletteWheelValue::cursor_motion 1\n");
-               window->v = 1.0 - (float)(get_cursor_y() - 2) / (get_h() - 4);
-               window->update_display();
+               float v = 1.0 - (float)(get_cursor_y() - 2) / (get_h() - 4);
+               bclamp(v, 0, 1);  window->hsv.v = v;
+               window->update_hsv();
                window->handle_event();
                return 1;
        }
@@ -577,8 +612,7 @@ int PaletteWheelValue::cursor_motion_event()
 
 int PaletteWheelValue::button_release_event()
 {
-       if(button_down)
-       {
+       if( button_down ) {
 //printf("PaletteWheelValue::button_release 1\n");
                button_down = 0;
                return 1;
@@ -591,46 +625,26 @@ int PaletteWheelValue::draw(float hue, float saturation, float value)
        float r_f, g_f, b_f;
        int i, j, r, g, b;
 
-       for(i = get_h() - 3; i >= 2; i--)
-       {
+       for( i = get_h() - 3; i >= 2; i-- ) {
                unsigned char *row = (unsigned char*)frame->get_rows()[i];
-               HSV::hsv_to_rgb(r_f, 
-                       g_f, 
-                       b_f, 
-                       hue, 
-                       saturation, 
+               HSV::hsv_to_rgb(r_f, g_f, b_f, hue, saturation,
                        1.0 - (float)(i - 2) / (get_h() - 4));
                r = (int)(r_f * 255);
                g = (int)(g_f * 255);
                b = (int)(b_f * 255);
-               for(j = 0; j < get_w(); j++)
-               {
-                       row[j * 3] = r;
-                       row[j * 3 + 1] = g;
-                       row[j * 3 + 2] = b;
+               for( j = 0; j < get_w(); j++ ) {
+                       row[j * 3] = r;
+                       row[j * 3 + 1] = g;
+                       row[j * 3 + 2] = b;
                }
        }
 
-       draw_3d_border(0, 
-               0, 
-               get_w(), 
-               get_h(), 
-               1);
-       draw_vframe(frame, 
-               2, 
-               2, 
-               get_w() - 4, 
-               get_h() - 4, 
-               2, 
-               2, 
-               get_w() - 4, 
-               get_h() - 4, 
-               0);
+       draw_3d_border(0, 0, get_w(), get_h(), 1);
+       draw_vframe(frame, 2, 2, get_w() - 4, get_h() - 4,
+               2, 2, get_w() - 4, get_h() - 4, 0);
        set_color(BLACK);
-       draw_line(2, 
-               get_h() - 3 - (int)(value * (get_h() - 5)), 
-               get_w() - 3, 
-               get_h() - 3 - (int)(value * (get_h() - 5)));
+       draw_line(2, get_h() - 3 - (int)(value * (get_h() - 5)),
+                 get_w() - 3, get_h() - 3 - (int)(value * (get_h() - 5)));
 //printf("PaletteWheelValue::draw %d %f\n", __LINE__, value);
 
        return 0;
@@ -659,23 +673,14 @@ int PaletteOutput::handle_event()
 
 int PaletteOutput::draw()
 {
-       float r_f, g_f, b_f;
-       
-       HSV::hsv_to_rgb(r_f, g_f, b_f, window->h, window->s, window->v);
-       set_color(((int)(r_f * 255) << 16) | ((int)(g_f * 255) << 8) | ((int)(b_f * 255)));
+       set_color(window->rgb888());
        draw_box(2, 2, get_w() - 4, get_h() - 4);
-       draw_3d_border(0, 
-               0, 
-               get_w(), 
-               get_h(), 
-               1);
-
-
+       draw_3d_border(0, 0, get_w(), get_h(), 1);
        return 0;
 }
 
 PaletteHue::PaletteHue(ColorWindow *window, int x, int y)
- : BC_ISlider(x, y, 0, 150, 200, 0, 359, (int)(window->h), 0)
+ : BC_ISlider(x, y, 0, 150, 200, 0, 359, (int)(window->hsv.h), 0)
 {
        this->window = window;
 }
@@ -685,53 +690,52 @@ PaletteHue::~PaletteHue()
 
 int PaletteHue::handle_event()
 {
-       window->h = get_value();
-       window->update_display();
+       window->hsv.h = get_value();
+       window->update_hsv();
        window->handle_event();
        return 1;
 }
 
-PaletteSaturation::PaletteSaturation(ColorWindow *window, int x, int y)
- : BC_FSlider(x, y, 0, 150, 200, 0, 1.0, window->s, 0)
+PaletteSat::PaletteSat(ColorWindow *window, int x, int y)
+ : BC_FSlider(x, y, 0, 150, 200, 0, 1.0, window->hsv.s, 0)
 {
        this->window = window;
        set_precision(0.01);
 }
-PaletteSaturation::~PaletteSaturation()
+PaletteSat::~PaletteSat()
 {
 }
 
-int PaletteSaturation::handle_event()
+int PaletteSat::handle_event()
 {
-//printf("PaletteSaturation::handle_event 1 %f\n", get_value());
-       window->s = get_value();
-       window->update_display();
-//printf("PaletteSaturation::handle_event 2 %f\n", get_value());
+       window->hsv.s = get_value();
+       window->update_hsv();
        window->handle_event();
        return 1;
 }
 
-PaletteValue::PaletteValue(ColorWindow *window, int x, int y)
- : BC_FSlider(x, y, 0, 150, 200, 0, 1.0, window->v, 0)
+
+PaletteVal::PaletteVal(ColorWindow *window, int x, int y)
+ : BC_FSlider(x, y, 0, 150, 200, 0, 1.0, window->hsv.v, 0)
 {
        this->window = window;
        set_precision(0.01);
 }
-PaletteValue::~PaletteValue()
+PaletteVal::~PaletteVal()
 {
 }
 
-int PaletteValue::handle_event()
+int PaletteVal::handle_event()
 {
-       window->v = get_value();
-       window->update_display();
+       window->hsv.v = get_value();
+       window->update_hsv();
        window->handle_event();
        return 1;
 }
 
 
 PaletteRed::PaletteRed(ColorWindow *window, int x, int y)
- : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->r, 0)
+ : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->rgb.r, 0)
 {
        this->window = window;
        set_precision(0.01);
@@ -742,47 +746,50 @@ PaletteRed::~PaletteRed()
 
 int PaletteRed::handle_event()
 {
+       window->rgb.r = get_value();
        window->update_rgb();
        window->handle_event();
        return 1;
 }
 
-PaletteGreen::PaletteGreen(ColorWindow *window, int x, int y)
- : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->g, 0)
+PaletteGrn::PaletteGrn(ColorWindow *window, int x, int y)
+ : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->rgb.g, 0)
 {
        this->window = window;
        set_precision(0.01);
 }
-PaletteGreen::~PaletteGreen()
+PaletteGrn::~PaletteGrn()
 {
 }
 
-int PaletteGreen::handle_event()
+int PaletteGrn::handle_event()
 {
+       window->rgb.g = get_value();
        window->update_rgb();
        window->handle_event();
        return 1;
 }
 
-PaletteBlue::PaletteBlue(ColorWindow *window, int x, int y)
- : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->b, 0)
+PaletteBlu::PaletteBlu(ColorWindow *window, int x, int y)
+ : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->rgb.b, 0)
 {
        this->window = window;
        set_precision(0.01);
 }
-PaletteBlue::~PaletteBlue()
+PaletteBlu::~PaletteBlu()
 {
 }
 
-int PaletteBlue::handle_event()
+int PaletteBlu::handle_event()
 {
+       window->rgb.b = get_value();
        window->update_rgb();
        window->handle_event();
        return 1;
 }
 
 PaletteAlpha::PaletteAlpha(ColorWindow *window, int x, int y)
- : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->a, 0)
+ : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->aph, 0)
 {
        this->window = window;
        set_precision(0.01);
@@ -793,9 +800,358 @@ PaletteAlpha::~PaletteAlpha()
 
 int PaletteAlpha::handle_event()
 {
-       window->a = get_value();
+       window->aph = get_value();
+       window->handle_event();
+       return 1;
+}
+
+PaletteLum::PaletteLum(ColorWindow *window, int x, int y)
+ : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->yuv.y, 0)
+{
+       this->window = window;
+       set_precision(0.01);
+}
+PaletteLum::~PaletteLum()
+{
+}
+
+int PaletteLum::handle_event()
+{
+       window->yuv.y = get_value();
+       window->update_yuv();
+       window->handle_event();
+       return 1;
+}
+
+PaletteCr::PaletteCr(ColorWindow *window, int x, int y)
+ : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->yuv.u, 0)
+{
+       this->window = window;
+       set_precision(0.01);
+}
+PaletteCr::~PaletteCr()
+{
+}
+
+int PaletteCr::handle_event()
+{
+       window->yuv.u = get_value();
+       window->update_yuv();
+       window->handle_event();
+       return 1;
+}
+
+PaletteCb::PaletteCb(ColorWindow *window, int x, int y)
+ : BC_FSlider(x, y, 0, 150, 200, 0, 1, window->yuv.v, 0)
+{
+       this->window = window;
+       set_precision(0.01);
+}
+PaletteCb::~PaletteCb()
+{
+}
+
+int PaletteCb::handle_event()
+{
+       window->yuv.v = get_value();
+       window->update_yuv();
+       window->handle_event();
+       return 1;
+}
+
+void ColorWindow::update_rgb(float r, float g, float b)
+{
+       { float y, u, v;
+       YUV::yuv.rgb_to_yuv_f(r, g, b, y, u, v);
+       u += 0.5;  v += 0.5;
+       bclamp(y, 0, 1);    yuv.y = y;
+       bclamp(u, 0, 1);    yuv.u = u;
+       bclamp(v, 0, 1);    yuv.v = v; }
+       { float h, s, v;
+       HSV::rgb_to_hsv(r,g,b, h,s,v);
+       bclamp(h, 0, 360);  hsv.h = h;
+       bclamp(s, 0, 1);    hsv.s = s;
+       bclamp(v, 0, 1);    hsv.v = v; }
+}
+
+void ColorWindow::update_yuv(float y, float u, float v)
+{
+       u -= 0.5;  v -= 0.5;
+       { float r, g, b;
+       YUV::yuv.yuv_to_rgb_f(r, g, b, y, u, v);
+       bclamp(r, 0, 1);   rgb.r = r;
+       bclamp(g, 0, 1);   rgb.g = g;
+       bclamp(b, 0, 1);   rgb.b = b;
+       float h, s, v;
+       HSV::rgb_to_hsv(r,g,b, h, s, v);
+       bclamp(h, 0, 360); hsv.h = h;
+       bclamp(s, 0, 1);   hsv.s = s;
+       bclamp(v, 0, 1);   hsv.v = v; }
+}
+
+void ColorWindow::update_hsv(float h, float s, float v)
+{
+       { float r, g, b;
+       HSV::hsv_to_rgb(r,g,b, h,s,v);
+       bclamp(r, 0, 1);   rgb.r = r;
+       bclamp(g, 0, 1);   rgb.g = g;
+       bclamp(b, 0, 1);   rgb.b = b;
+       float y, u, v;
+       YUV::yuv.rgb_to_yuv_f(r, g, b, y, u, v);
+       u += 0.5;  v += 0.5;
+       bclamp(y, 0, 1);   yuv.y = y;
+       bclamp(u, 0, 1);   yuv.u = u;
+       bclamp(v, 0, 1);   yuv.v = v; }
+}
+
+void ColorWindow::load_history()
+{
+       char history_path[BCTEXTLEN];
+       MWindow::create_defaults_path(history_path,PALETTE_DATA);
+       FILE *fp = fopen(history_path,"r");
+       int i=0;
+       if( fp ) {
+               while( i < PALLETTE_HISTORY_SIZE ) {
+                       char line[BCSTRLEN];
+                       if( !fgets(line,sizeof(line)-1,fp) ) break;
+                       line[sizeof(line)-1] = 0;
+                       if( sscanf(line, "%x",&palette_history[i]) != 1 ) break;
+                       ++i;
+               }
+               fclose(fp);
+       }
+       int r = 0, g = 0, b = 0;
+       float v0 = 0, v1 = 1;
+       while( i < PALLETTE_HISTORY_SIZE ) {
+               int grey_code = i ^ (i>>1);
+               r = 255 * ((grey_code&4) ? v0 : v1);
+               g = 255 * ((grey_code&2) ? v0 : v1);
+               b = 255 * ((grey_code&1) ? v0 : v1);
+               int color = (r<<16) | (g<<8) | (b<<0);
+               palette_history[i++] = color;
+               if( i & 7 ) continue;
+               v0 = 0.5f * (v0+.5f);
+               v1 = 0.5f * (v1+.5f);
+       }
+}
+void ColorWindow::save_history()
+{
+       char history_path[BCTEXTLEN];
+       MWindow::create_defaults_path(history_path,PALETTE_DATA);
+       FILE *fp = fopen(history_path,"w");
+       if( fp ) {
+               for( int i=0; i<PALLETTE_HISTORY_SIZE; ++i ) {
+                       fprintf(fp, "%06x\n", palette_history[i]);
+               }
+               fclose(fp);
+       }
+}
+void ColorWindow::update_history(int color)
+{
+       int out = palette_history[0];
+       palette_history[0] = color;
+       for( int i=1; out != color && i<PALLETTE_HISTORY_SIZE; ++i ) {
+               int in = out;
+               out = palette_history[i];
+               palette_history[i] = in;
+       }
+}
+void ColorWindow::update_history()
+{
+       update_history(rgb888());
+       history->update(0);
+}
+int ColorWindow::rgb888()
+{
+       int r = 255*rgb.r + 0.5, g = 255*rgb.g + 0.5, b = 255*rgb.b + 0.5;
+       bclamp(r, 0, 255);  bclamp(g, 0, 255);  bclamp(b, 0, 255);
+       return (r<<16) | (g<<8) | (b<<0);
+}
+
+PaletteNum::PaletteNum(ColorWindow *window, int x, int y,
+       float &output, float min, float max)
+ : BC_TumbleTextBox(window, output, min, max, x, y, 64)
+{
+       this->window = window;
+       this->output = &output;
+       set_increment(0.01);
+       set_precision(2);
+}
+
+PaletteNum::~PaletteNum()
+{
+}
+
+
+int PaletteHSV::handle_event()
+{
+       update_output();
+       window->update_hsv();
+       window->handle_event();
+       return 1;
+}
+
+int PaletteRGB::handle_event()
+{
+       update_output();
+       window->update_rgb();
        window->handle_event();
        return 1;
 }
 
+int PaletteYUV::handle_event()
+{
+       update_output();
+       window->update_yuv();
+       window->handle_event();
+       return 1;
+}
+
+int PaletteAPH::handle_event()
+{
+       update_output();
+       window->update_display();
+       window->handle_event();
+       return 1;
+}
+
+PaletteHexButton::PaletteHexButton(ColorWindow *window, int x, int y)
+ : BC_GenericButton(x, y, 50, "#")
+{
+       this->window = window;
+       set_tooltip(_("hex rgb color"));
+}
+PaletteHexButton::~PaletteHexButton()
+{
+}
+int PaletteHexButton::handle_event()
+{
+       const char *hex = window->hex_box->get_text();
+       window->update_rgb_hex(hex);
+       return 1;
+}
+
+PaletteHex::PaletteHex(ColorWindow *window, int x, int y, const char *hex)
+ : BC_TextBox(x, y, 100, 1, hex)
+{
+       this->window = window;
+}
+PaletteHex::~PaletteHex()
+{
+}
+void PaletteHex::update()
+{
+       char hex[BCSTRLEN];  sprintf(hex,"%06x",window->rgb888());
+       BC_TextBox::update(hex);
+}
+
+int PaletteHex::keypress_event()
+{
+       if( get_keypress() != RETURN )
+               return BC_TextBox::keypress_event();
+       window->update_rgb_hex(get_text());
+       return 1;
+}
+
+#include "grabpick_up_png.h"
+#include "grabpick_hi_png.h"
+#include "grabpick_dn_png.h"
+
+PaletteGrabButton::PaletteGrabButton(ColorWindow *window, int x, int y)
+ : BC_Button(x, y, vframes)
+{
+       this->window = window;
+       vframes[0] = new VFramePng(grabpick_up_png);
+       vframes[1] = new VFramePng(grabpick_hi_png);
+       vframes[2] = new VFramePng(grabpick_dn_png);
+       set_tooltip(_("grab from anywhere picker"));
+}
+PaletteGrabButton::~PaletteGrabButton()
+{
+       for( int i=0; i<3; ++i )
+               delete vframes[i];
+}
+int PaletteGrabButton::handle_event()
+{
+       if( window->grab_buttons() ) {
+               grab_cursor();
+               window->button_grabbed = 1;
+               button_press_event(); // redraw face HI
+       }
+       return 1;
+}
+
+PaletteHistory::PaletteHistory(ColorWindow *window, int x, int y)
+ : BC_SubWindow(x,y, 200, 24)
+{
+       this->window = window;
+       button_down = 0;
+       set_tooltip(_("color history"));
+}
+PaletteHistory::~PaletteHistory()
+{
+}
+void PaletteHistory::update(int flush)
+{
+       int x1 = 0, x2 = 0;
+       for( int i=0; i<PALLETTE_HISTORY_SIZE; x1=x2 ) {
+               int rgb = window->palette_history[i];
+               x2 = (++i * get_w())/PALLETTE_HISTORY_SIZE;
+               draw_3d_box(x1,0,x2-x1,get_h(),WHITE,BLACK,rgb,LTBLUE,DKBLUE);
+       }
+       flash(flush);
+}
+
+int PaletteHistory::button_press_event()
+{
+       if( button_down || !is_event_win() ) return 0;
+       button_down =  1;
+       cursor_motion_event();
+       return 1;
+}
+int PaletteHistory::button_release_event()
+{
+       if( !button_down || !is_event_win() ) return 0;
+       cursor_motion_event();
+       if( button_down > 0 ) {
+               window->handle_event();
+               window->update_display();
+               window->update_history();
+       }
+       button_down =  0;
+       return 1;
+}
+int PaletteHistory::cursor_motion_event()
+{
+       if( !button_down || !is_event_win() ) return 0;
+       hide_tooltip();
+       int pick = (PALLETTE_HISTORY_SIZE * get_cursor_x()) / get_w();
+       bclamp(pick, 0, PALLETTE_HISTORY_SIZE-1);
+       int color = window->palette_history[pick];
+       float r = ((color>>16) & 0xff) / 255.;
+       float g = ((color>>8)  & 0xff) / 255.;
+       float b = ((color>>0)  & 0xff) / 255.;
+       if( window->rgb.r != r || window->rgb.g != g || window->rgb.b != b ) {
+               window->rgb.r = r;  window->rgb.g = g;  window->rgb.b = b;
+               window->update_rgb();
+       }
+       return 1;
+}
+
+int PaletteHistory::cursor_leave_event()
+{
+       hide_tooltip();
+       return 0;
+}
+int PaletteHistory::repeat_event(int64_t duration)
+{
+       int result = 0;
+
+       if( duration == get_resources()->tooltip_delay &&
+           get_tooltip() && *get_tooltip() && cursor_above() ) {
+               show_tooltip();
+               result = 1;
+       }
+       return result;
+}