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
22 #include "bcsignals.h"
23 #include "color3waywindow.h"
38 Color3WayWindow::Color3WayWindow(Color3WayMain *plugin)
39 : PluginClientWindow(plugin,
46 this->plugin = plugin;
49 Color3WayWindow::~Color3WayWindow()
53 void Color3WayWindow::create_objects()
55 int margin = plugin->get_theme()->widget_border;
56 int x = plugin->get_theme()->widget_border;
57 int y = plugin->get_theme()->widget_border;
59 for(int i = 0; i < SECTIONS; i++)
61 sections[i] = new Color3WaySection(plugin,
65 (get_w() - margin * 4) / 3,
68 sections[i]->create_objects();
69 x += sections[i]->w + margin;
78 int Color3WayWindow::resize_event(int w, int h)
80 int margin = plugin->get_theme()->widget_border;
81 int x = sections[0]->x;
82 int y = sections[0]->y;
84 for(int i = 0; i < SECTIONS; i++)
86 sections[i]->reposition_window(x,
90 x += sections[i]->w + margin;
100 void Color3WayWindow::update()
102 for(int i = 0; i < SECTIONS; i++)
104 sections[i]->update();
110 Color3WaySection::Color3WaySection(Color3WayMain *plugin,
111 Color3WayWindow *gui,
118 this->plugin = plugin;
120 this->section = section;
128 void Color3WaySection::create_objects()
130 int margin = plugin->get_theme()->widget_border;
133 const char *titles[] =
139 int w1 = w - (CLEAR_BTN_WIDTH + margin * 2);
141 gui->add_tool(title = new BC_Title(x + w1 / 2 -
142 gui->get_text_width(MEDIUMFONT, titles[section]) / 2,
145 y += title->get_h() + margin;
146 gui->add_tool(point = new Color3WayPoint(plugin,
148 &plugin->config.hue_x[section],
149 &plugin->config.hue_y[section],
154 gui->add_tool(pointClr = new Color3WaySliderClrSection(plugin, gui,
155 x + w1 + margin, y, CLEAR_BTN_WIDTH, RESET_POINT, section));
156 y += point->get_h() + margin;
158 gui->add_tool(value_title = new BC_Title(x, y, _("Value:")));
159 y += value_title->get_h() + margin;
160 gui->add_tool(value = new Color3WaySlider(plugin,
162 &plugin->config.value[section],
167 gui->add_tool(valueClr = new Color3WaySliderClrSection(plugin, gui,
168 x + w1 + margin, y, CLEAR_BTN_WIDTH, RESET_VALUE, section));
169 y += value->get_h() + margin;
171 gui->add_tool(sat_title = new BC_Title(x, y, _("Saturation:")));
172 y += sat_title->get_h() + margin;
173 gui->add_tool(saturation = new Color3WaySlider(plugin,
175 &plugin->config.saturation[section],
180 gui->add_tool(saturationClr = new Color3WaySliderClrSection(plugin, gui,
181 x + w1 + margin, y, CLEAR_BTN_WIDTH, RESET_SATURATION, section));
182 y += saturation->get_h() + margin;
184 gui->add_tool(reset = new Color3WayResetSection(plugin,
190 y += reset->get_h() + margin;
191 gui->add_tool(balance = new Color3WayBalanceSection(plugin,
197 y += balance->get_h() + margin;
198 gui->add_tool(copy = new Color3WayCopySection(plugin,
205 int Color3WaySection::reposition_window(int x, int y, int w, int h)
207 int margin = plugin->get_theme()->widget_border;
212 int w1 = w - (CLEAR_BTN_WIDTH + margin * 2);
214 title->reposition_window(x + w1 / 2 -
217 point->reposition_window(x, point->get_y(), w1 / 2);
218 pointClr->reposition_window(x + w1 + margin, point->get_y());
219 y = point->get_y() + point->get_h() + margin;
221 value_title->reposition_window(x, y);
222 y += value_title->get_h() + margin;
223 value->reposition_window(x, y, w1, value->get_h());
224 value->set_pointer_motion_range(w1);
225 valueClr->reposition_window(x + w1 + margin, y);
226 y += value->get_h() + margin;
228 sat_title->reposition_window(x, y);
229 y += sat_title->get_h() + margin;
230 saturation->reposition_window(x, y, w1, saturation->get_h());
231 saturation->set_pointer_motion_range(w1);
232 saturationClr->reposition_window(x + w1 + margin, y);
233 y += saturation->get_h() + margin;
235 reset->reposition_window(x, y);
236 y += reset->get_h() + margin;
237 balance->reposition_window(x, y);
238 y += balance->get_h() + margin;
239 copy->reposition_window(x, y);
244 void Color3WaySection::update()
247 value->update(plugin->config.value[section]);
248 saturation->update(plugin->config.saturation[section]);
255 Color3WayPoint::Color3WayPoint(Color3WayMain *plugin,
256 Color3WayWindow *gui,
268 this->plugin = plugin;
269 this->x_output = x_output;
270 this->y_output = y_output;
271 this->radius = radius;
275 memset(fg_images, 0, sizeof(BC_Pixmap*) * COLOR_IMAGES);
278 this->section = section;
281 Color3WayPoint::~Color3WayPoint()
283 for(int i = 0; i < COLOR_IMAGES; i++)
284 if(fg_images[i]) delete fg_images[i];
289 int Color3WayPoint::initialize()
291 BC_SubWindow::initialize();
294 VFrame **data = plugin->get_theme()->get_image_set("color3way_point");
295 for(int i = 0; i < COLOR_IMAGES; i++)
297 if(fg_images[i]) delete fg_images[i];
298 fg_images[i] = new BC_Pixmap(gui, data[i], PIXMAP_ALPHA);
307 int Color3WayPoint::reposition_window(int x, int y, int radius)
309 this->radius = radius;
310 BC_SubWindow::reposition_window(x, y, radius * 2, radius * 2);
318 void Color3WayPoint::draw_face(int flash, int flush)
320 // Draw the color wheel
323 VFrame frame(0, -1, radius * 2, radius * 2, BC_RGB888, -1);
324 for(int i = 0; i < radius * 2; i++)
326 unsigned char *row = frame.get_rows()[i];
327 for(int j = 0; j < radius * 2; j++)
329 float point_radius = sqrt(SQR(i - radius) + SQR(j - radius));
332 if(point_radius < radius - 1)
335 float angle = atan2((float)(j - radius) / radius,
336 (float)(i - radius) / radius) *
343 HSV::hsv_to_rgb(r_f, g_f, b_f, angle, point_radius / radius, 1.0);
344 r = (int)(r_f * 0xff);
345 g = (int)(g_f * 0xff);
346 b = (int)(b_f * 0xff);
349 if(point_radius < radius)
351 if(radius * 2 - i < j)
354 g = (MEGREY >> 8) & 0xff;
366 r = (gui->get_bg_color() & 0xff0000) >> 16;
367 g = (gui->get_bg_color() & 0xff00) >> 8;
368 b = (gui->get_bg_color() & 0xff);
377 bg_image = new BC_Pixmap(get_parent(), &frame, PIXMAP_ALPHA);
380 draw_pixmap(bg_image);
383 // draw_arc(0, 0, radius * 2, radius * 2, 45, 180);
385 // set_color(MEGREY);
386 // draw_arc(0, 0, radius * 2, radius * 2, 45, -180);
389 fg_x = (int)(*x_output * (radius - fg_images[0]->get_w() / 2) + radius) -
390 fg_images[0]->get_w() / 2;
391 fg_y = (int)(*y_output * (radius - fg_images[0]->get_h() / 2) + radius) -
392 fg_images[0]->get_h() / 2;
394 draw_pixmap(fg_images[status], fg_x, fg_y);
399 int margin = plugin->get_theme()->widget_border;
401 set_font(MEDIUMFONT);
402 char string[BCTEXTLEN];
404 sprintf(string, "%.3f", *y_output);
405 draw_text(radius - get_text_width(MEDIUMFONT, string) / 2,
406 get_text_ascent(MEDIUMFONT) + margin,
409 sprintf(string, "%.3f", *x_output);
411 radius + get_text_ascent(MEDIUMFONT) / 2,
418 // plugin->calculate_factors(&r_factor, &g_factor, &b_factor, section);
420 // sprintf(string, "%.3f", r_factor);
421 // draw_text(radius - get_text_width(MEDIUMFONT, string) / 2,
422 // get_text_ascent(MEDIUMFONT) + margin,
425 // sprintf(string, "%.3f", g_factor);
426 // draw_text(margin + radius - radius * 1.0 / ROOT_2,
427 // radius + radius * 1.0 / ROOT_2 - margin,
430 // sprintf(string, "%.3f", b_factor);
431 // draw_text(radius + radius * 1.0 / ROOT_2 - margin - get_text_width(MEDIUMFONT, string),
432 // radius + radius * 1.0 / ROOT_2 - margin,
434 set_font(MEDIUMFONT);
437 if(flash) this->flash(0);
438 if(flush) this->flush();
441 int Color3WayPoint::deactivate()
451 int Color3WayPoint::activate()
455 get_top_level()->set_active_subwindow(this);
461 void Color3WayPoint::update()
466 int Color3WayPoint::button_press_event()
471 get_top_level()->deactivate();
477 offset_x = gui->get_relative_cursor_x();
478 offset_y = gui->get_relative_cursor_y();
483 int Color3WayPoint::button_release_event()
485 if(status == COLOR_DN)
494 int Color3WayPoint::cursor_motion_event()
496 int cursor_x = gui->get_relative_cursor_x();
497 int cursor_y = gui->get_relative_cursor_y();
499 //printf("Color3WayPoint::cursor_motion_event %d %d\n", __LINE__, status);
500 if(status == COLOR_DN)
502 //printf("Color3WayPoint::cursor_motion_event %d %d %d\n", __LINE__, starting_x, offset_x);
503 int new_x = starting_x + cursor_x - offset_x;
504 int new_y = starting_y + cursor_y - offset_y;
506 *x_output = (float)(new_x + fg_images[0]->get_w() / 2 - radius) /
507 (radius - fg_images[0]->get_w() / 2);
508 *y_output = (float)(new_y + fg_images[0]->get_h() / 2 - radius) /
509 (radius - fg_images[0]->get_h() / 2);
511 plugin->config.boundaries();
512 if(plugin->copy_to_all[section]) plugin->config.copy_to_all(section);
513 plugin->send_configure_change();
528 int Color3WayPoint::handle_event()
534 int Color3WayPoint::cursor_enter_event()
536 if(is_event_win() && status != COLOR_HI && status != COLOR_DN)
544 int Color3WayPoint::cursor_leave_event()
546 if(status == COLOR_HI)
554 int Color3WayPoint::keypress_event()
557 if(!active) return 0;
558 if(ctrl_down() || shift_down()) return 0;
560 switch(get_keypress())
582 plugin->config.boundaries();
583 if(plugin->copy_to_all[section]) plugin->config.copy_to_all(section);
584 plugin->send_configure_change();
597 Color3WaySlider::Color3WaySlider(Color3WayMain *plugin,
598 Color3WayWindow *gui,
614 this->plugin = plugin;
615 this->output = output;
616 this->section = section;
618 set_precision(0.001);
621 Color3WaySlider::~Color3WaySlider()
625 int Color3WaySlider::handle_event()
627 *output = get_value();
628 if(plugin->copy_to_all[section]) plugin->config.copy_to_all(section);
629 plugin->send_configure_change();
634 char* Color3WaySlider::get_caption()
636 sprintf(string, "%0.3f", *output);
644 Color3WayResetSection::Color3WayResetSection(Color3WayMain *plugin,
645 Color3WayWindow *gui,
649 : BC_GenericButton(x, y, _("Reset"))
651 this->plugin = plugin;
653 this->section = section;
656 int Color3WayResetSection::handle_event()
658 plugin->config.hue_x[section] = 0;
659 plugin->config.hue_y[section] = 0;
660 plugin->config.value[section] = 0;
661 plugin->config.saturation[section] = 0;
662 if(plugin->copy_to_all[section]) plugin->config.copy_to_all(section);
663 plugin->send_configure_change();
673 Color3WaySliderClrSection::Color3WaySliderClrSection(Color3WayMain *plugin,
674 Color3WayWindow *gui,
680 : BC_Button(x, y, w, plugin->get_theme()->get_image_set("reset_button"))
682 this->plugin = plugin;
685 this->section = section;
687 Color3WaySliderClrSection::~Color3WaySliderClrSection()
690 int Color3WaySliderClrSection::handle_event()
694 plugin->config.hue_x[section] = 0;
695 plugin->config.hue_y[section] = 0;
697 case RESET_VALUE : plugin->config.value[section] = 0;
699 case RESET_SATURATION : plugin->config.saturation[section] = 0;
702 if(plugin->copy_to_all[section]) plugin->config.copy_to_all(section);
703 plugin->send_configure_change();
712 Color3WayCopySection::Color3WayCopySection(Color3WayMain *plugin,
713 Color3WayWindow *gui,
717 : BC_CheckBox(x, y, plugin->copy_to_all[section], _("Copy to all"))
719 this->plugin = plugin;
721 this->section = section;
724 int Color3WayCopySection::handle_event()
726 if(get_value()) plugin->config.copy_to_all(section);
727 plugin->copy_to_all[section] = get_value();
729 plugin->send_configure_change();
737 Color3WayBalanceSection::Color3WayBalanceSection(Color3WayMain *plugin,
738 Color3WayWindow *gui,
742 : BC_GenericButton(x, y, _("White balance"))
744 this->plugin = plugin;
746 this->section = section;
749 int Color3WayBalanceSection::handle_event()
751 // Get colorpicker value
752 float r = plugin->get_red();
753 float g = plugin->get_green();
754 float b = plugin->get_blue();
756 // Since the transfers aren't linear, use brute force search
761 float best_diff = 255;
767 for(float x = center_x - range; x < center_x + range; x += step)
769 for(float y = center_y - range; y < center_y + range; y += step)
774 plugin->process_pixel(&new_r,
782 float min = MIN(new_r, new_g);
783 min = MIN(min, new_b);
784 float max = MAX(new_r, new_g);
785 max = MAX(max, new_b);
786 float diff = max - min;
803 new_x = Units::quantize(new_x, 0.001);
804 new_y = Units::quantize(new_y, 0.001);
806 plugin->config.hue_x[section] = new_x;
807 plugin->config.hue_y[section] = new_y;
808 plugin->config.boundaries();
809 if(plugin->copy_to_all[section]) plugin->config.copy_to_all(section);
810 plugin->send_configure_change();