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"
39 Color3WayWindow::Color3WayWindow(Color3WayMain *plugin)
40 : PluginClientWindow(plugin,
47 this->plugin = plugin;
50 Color3WayWindow::~Color3WayWindow()
54 void Color3WayWindow::create_objects()
56 int margin = plugin->get_theme()->widget_border;
57 int x = plugin->get_theme()->widget_border;
58 int y = plugin->get_theme()->widget_border;
60 for(int i = 0; i < SECTIONS; i++)
62 sections[i] = new Color3WaySection(plugin,
66 (get_w() - margin * 4) / 3,
69 sections[i]->create_objects();
70 x += sections[i]->w + margin;
79 int Color3WayWindow::resize_event(int w, int h)
81 int margin = plugin->get_theme()->widget_border;
82 int x = sections[0]->x;
83 int y = sections[0]->y;
85 for(int i = 0; i < SECTIONS; i++)
87 sections[i]->reposition_window(x,
91 x += sections[i]->w + margin;
101 void Color3WayWindow::update()
103 for(int i = 0; i < SECTIONS; i++)
105 sections[i]->update();
111 Color3WaySection::Color3WaySection(Color3WayMain *plugin,
112 Color3WayWindow *gui,
119 this->plugin = plugin;
121 this->section = section;
129 void Color3WaySection::create_objects()
131 int margin = plugin->get_theme()->widget_border;
134 const char *titles[] =
142 gui->add_tool(title = new BC_Title(x + w / 2 -
143 gui->get_text_width(MEDIUMFONT, titles[section]) / 2,
146 y += title->get_h() + margin;
147 gui->add_tool(point = new Color3WayPoint(plugin,
149 &plugin->config.hue_x[section],
150 &plugin->config.hue_y[section],
155 y += point->get_h() + margin;
157 gui->add_tool(value_title = new BC_Title(x, y, _("Value:")));
158 y += value_title->get_h() + margin;
159 gui->add_tool(value = new Color3WaySlider(plugin,
161 &plugin->config.value[section],
166 y += value->get_h() + margin;
168 gui->add_tool(sat_title = new BC_Title(x, y, _("Saturation:")));
169 y += sat_title->get_h() + margin;
170 gui->add_tool(saturation = new Color3WaySlider(plugin,
172 &plugin->config.saturation[section],
177 y += saturation->get_h() + margin;
179 gui->add_tool(reset = new Color3WayResetSection(plugin,
185 y += reset->get_h() + margin;
186 gui->add_tool(balance = new Color3WayBalanceSection(plugin,
192 y += balance->get_h() + margin;
193 gui->add_tool(copy = new Color3WayCopySection(plugin,
200 int Color3WaySection::reposition_window(int x, int y, int w, int h)
202 int margin = plugin->get_theme()->widget_border;
208 title->reposition_window(x + w / 2 -
211 point->reposition_window(x, point->get_y(), w / 2);
212 y = point->get_y() + point->get_h() + margin;
213 value_title->reposition_window(x, y);
214 y += value_title->get_h() + margin;
215 value->reposition_window(x, y, w, value->get_h());
216 value->set_pointer_motion_range(w);
217 y += value->get_h() + margin;
219 sat_title->reposition_window(x, y);
220 y += sat_title->get_h() + margin;
221 saturation->reposition_window(x, y, w, saturation->get_h());
222 saturation->set_pointer_motion_range(w);
223 y += saturation->get_h() + margin;
224 reset->reposition_window(x, y);
225 y += reset->get_h() + margin;
226 balance->reposition_window(x, y);
227 y += balance->get_h() + margin;
228 copy->reposition_window(x, y);
233 void Color3WaySection::update()
236 value->update(plugin->config.value[section]);
237 saturation->update(plugin->config.saturation[section]);
244 Color3WayPoint::Color3WayPoint(Color3WayMain *plugin,
245 Color3WayWindow *gui,
257 this->plugin = plugin;
258 this->x_output = x_output;
259 this->y_output = y_output;
260 this->radius = radius;
264 memset(fg_images, 0, sizeof(BC_Pixmap*) * COLOR_IMAGES);
267 this->section = section;
270 Color3WayPoint::~Color3WayPoint()
272 for(int i = 0; i < COLOR_IMAGES; i++)
273 if(fg_images[i]) delete fg_images[i];
278 int Color3WayPoint::initialize()
280 BC_SubWindow::initialize();
283 VFrame **data = plugin->get_theme()->get_image_set("color3way_point");
284 for(int i = 0; i < COLOR_IMAGES; i++)
286 if(fg_images[i]) delete fg_images[i];
287 fg_images[i] = new BC_Pixmap(gui, data[i], PIXMAP_ALPHA);
296 int Color3WayPoint::reposition_window(int x, int y, int radius)
298 this->radius = radius;
299 BC_SubWindow::reposition_window(x, y, radius * 2, radius * 2);
307 void Color3WayPoint::draw_face(int flash, int flush)
309 // Draw the color wheel
312 VFrame frame(0, -1, radius * 2, radius * 2, BC_RGB888, -1);
313 for(int i = 0; i < radius * 2; i++)
315 unsigned char *row = frame.get_rows()[i];
316 for(int j = 0; j < radius * 2; j++)
318 float point_radius = sqrt(SQR(i - radius) + SQR(j - radius));
321 if(point_radius < radius - 1)
324 float angle = atan2((float)(j - radius) / radius,
325 (float)(i - radius) / radius) *
332 HSV::hsv_to_rgb(r_f, g_f, b_f, angle, point_radius / radius, 1.0);
333 r = (int)(r_f * 0xff);
334 g = (int)(g_f * 0xff);
335 b = (int)(b_f * 0xff);
338 if(point_radius < radius)
340 if(radius * 2 - i < j)
343 g = (MEGREY >> 8) & 0xff;
355 r = (gui->get_bg_color() & 0xff0000) >> 16;
356 g = (gui->get_bg_color() & 0xff00) >> 8;
357 b = (gui->get_bg_color() & 0xff);
366 bg_image = new BC_Pixmap(get_parent(), &frame, PIXMAP_ALPHA);
369 draw_pixmap(bg_image);
372 // draw_arc(0, 0, radius * 2, radius * 2, 45, 180);
374 // set_color(MEGREY);
375 // draw_arc(0, 0, radius * 2, radius * 2, 45, -180);
378 fg_x = (int)(*x_output * (radius - fg_images[0]->get_w() / 2) + radius) -
379 fg_images[0]->get_w() / 2;
380 fg_y = (int)(*y_output * (radius - fg_images[0]->get_h() / 2) + radius) -
381 fg_images[0]->get_h() / 2;
383 draw_pixmap(fg_images[status], fg_x, fg_y);
388 int margin = plugin->get_theme()->widget_border;
390 set_font(MEDIUMFONT);
391 char string[BCTEXTLEN];
393 sprintf(string, "%.3f", *y_output);
394 draw_text(radius - get_text_width(MEDIUMFONT, string) / 2,
395 get_text_ascent(MEDIUMFONT) + margin,
398 sprintf(string, "%.3f", *x_output);
400 radius + get_text_ascent(MEDIUMFONT) / 2,
407 // plugin->calculate_factors(&r_factor, &g_factor, &b_factor, section);
409 // sprintf(string, "%.3f", r_factor);
410 // draw_text(radius - get_text_width(MEDIUMFONT, string) / 2,
411 // get_text_ascent(MEDIUMFONT) + margin,
414 // sprintf(string, "%.3f", g_factor);
415 // draw_text(margin + radius - radius * 1.0 / ROOT_2,
416 // radius + radius * 1.0 / ROOT_2 - margin,
419 // sprintf(string, "%.3f", b_factor);
420 // draw_text(radius + radius * 1.0 / ROOT_2 - margin - get_text_width(MEDIUMFONT, string),
421 // radius + radius * 1.0 / ROOT_2 - margin,
423 set_font(MEDIUMFONT);
426 if(flash) this->flash(0);
427 if(flush) this->flush();
430 int Color3WayPoint::deactivate()
440 int Color3WayPoint::activate()
444 get_top_level()->set_active_subwindow(this);
450 void Color3WayPoint::update()
455 int Color3WayPoint::button_press_event()
460 get_top_level()->deactivate();
466 offset_x = gui->get_relative_cursor_x();
467 offset_y = gui->get_relative_cursor_y();
472 int Color3WayPoint::button_release_event()
474 if(status == COLOR_DN)
483 int Color3WayPoint::cursor_motion_event()
485 int cursor_x = gui->get_relative_cursor_x();
486 int cursor_y = gui->get_relative_cursor_y();
488 //printf("Color3WayPoint::cursor_motion_event %d %d\n", __LINE__, status);
489 if(status == COLOR_DN)
491 //printf("Color3WayPoint::cursor_motion_event %d %d %d\n", __LINE__, starting_x, offset_x);
492 int new_x = starting_x + cursor_x - offset_x;
493 int new_y = starting_y + cursor_y - offset_y;
495 *x_output = (float)(new_x + fg_images[0]->get_w() / 2 - radius) /
496 (radius - fg_images[0]->get_w() / 2);
497 *y_output = (float)(new_y + fg_images[0]->get_h() / 2 - radius) /
498 (radius - fg_images[0]->get_h() / 2);
500 plugin->config.boundaries();
501 if(plugin->copy_to_all[section]) plugin->config.copy_to_all(section);
502 plugin->send_configure_change();
517 int Color3WayPoint::handle_event()
523 int Color3WayPoint::cursor_enter_event()
525 if(is_event_win() && status != COLOR_HI && status != COLOR_DN)
533 int Color3WayPoint::cursor_leave_event()
535 if(status == COLOR_HI)
543 int Color3WayPoint::keypress_event()
546 if(!active) return 0;
547 if(ctrl_down() || shift_down()) return 0;
549 switch(get_keypress())
571 plugin->config.boundaries();
572 if(plugin->copy_to_all[section]) plugin->config.copy_to_all(section);
573 plugin->send_configure_change();
586 Color3WaySlider::Color3WaySlider(Color3WayMain *plugin,
587 Color3WayWindow *gui,
603 this->plugin = plugin;
604 this->output = output;
605 this->section = section;
607 set_precision(0.001);
610 Color3WaySlider::~Color3WaySlider()
614 int Color3WaySlider::handle_event()
616 *output = get_value();
617 if(plugin->copy_to_all[section]) plugin->config.copy_to_all(section);
618 plugin->send_configure_change();
623 char* Color3WaySlider::get_caption()
625 sprintf(string, "%0.3f", *output);
633 Color3WayResetSection::Color3WayResetSection(Color3WayMain *plugin,
634 Color3WayWindow *gui,
638 : BC_GenericButton(x, y, _("Reset"))
640 this->plugin = plugin;
642 this->section = section;
645 int Color3WayResetSection::handle_event()
647 plugin->config.hue_x[section] = 0;
648 plugin->config.hue_y[section] = 0;
649 plugin->config.value[section] = 0;
650 plugin->config.saturation[section] = 0;
651 if(plugin->copy_to_all[section]) plugin->config.copy_to_all(section);
652 plugin->send_configure_change();
662 Color3WayCopySection::Color3WayCopySection(Color3WayMain *plugin,
663 Color3WayWindow *gui,
667 : BC_CheckBox(x, y, plugin->copy_to_all[section], _("Copy to all"))
669 this->plugin = plugin;
671 this->section = section;
674 int Color3WayCopySection::handle_event()
676 if(get_value()) plugin->config.copy_to_all(section);
677 plugin->copy_to_all[section] = get_value();
679 plugin->send_configure_change();
687 Color3WayBalanceSection::Color3WayBalanceSection(Color3WayMain *plugin,
688 Color3WayWindow *gui,
692 : BC_GenericButton(x, y, _("White balance"))
694 this->plugin = plugin;
696 this->section = section;
699 int Color3WayBalanceSection::handle_event()
701 // Get colorpicker value
702 float r = plugin->get_red();
703 float g = plugin->get_green();
704 float b = plugin->get_blue();
706 // Since the transfers aren't linear, use brute force search
711 float best_diff = 255;
717 for(float x = center_x - range; x < center_x + range; x += step)
719 for(float y = center_y - range; y < center_y + range; y += step)
724 plugin->process_pixel(&new_r,
732 float min = MIN(new_r, new_g);
733 min = MIN(min, new_b);
734 float max = MAX(new_r, new_g);
735 max = MAX(max, new_b);
736 float diff = max - min;
753 new_x = Units::quantize(new_x, 0.001);
754 new_y = Units::quantize(new_y, 0.001);
756 plugin->config.hue_x[section] = new_x;
757 plugin->config.hue_y[section] = new_y;
758 plugin->config.boundaries();
759 if(plugin->copy_to_all[section]) plugin->config.copy_to_all(section);
760 plugin->send_configure_change();