1 /* === S Y N F I G ========================================================= */
2 /*! \file widget_coloredit.cpp
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007 Chris Moore
10 ** Copyright (c) 2008 Paul Wise
12 ** This package is free software; you can redistribute it and/or
13 ** modify it under the terms of the GNU General Public License as
14 ** published by the Free Software Foundation; either version 2 of
15 ** the License, or (at your option) any later version.
17 ** This package is distributed in the hope that it will be useful,
18 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ** General Public License for more details.
23 /* ========================================================================= */
25 /* === H E A D E R S ======================================================= */
34 #include "widget_coloredit.h"
37 #include <gtkmm/drawingarea.h>
38 #include <pangomm/attributes.h>
39 #include <pangomm/attrlist.h>
41 #include <gtkmm/notebook.h>
42 #include <gtkmm/box.h>
48 /* === U S I N G =========================================================== */
52 using namespace synfig;
53 using namespace studio;
55 /* === M A C R O S ========================================================= */
57 #define SPINBUTTON_WIDTH 100
59 /* === G L O B A L S ======================================================= */
61 /* === P R O C E D U R E S ================================================= */
63 /* === C L A S S E S ======================================================= */
65 ColorSlider::ColorSlider(const ColorSlider::Type &x):
68 signal_expose_event().connect(sigc::mem_fun(*this, &ColorSlider::redraw));
69 set_size_request(-1,12);
70 add_events(Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK);
71 add_events(Gdk::BUTTON1_MOTION_MASK);
75 ColorSlider::set_type(Type x) { type=x; queue_draw(); }
78 ColorSlider::set_color(synfig::Color x) { color_=x; queue_draw(); }
81 ColorSlider::slider_color_TYPE_R(synfig::Color &color, float amount) { color.set_r(amount); }
83 ColorSlider::slider_color_TYPE_G(synfig::Color &color, float amount) { color.set_g(amount); }
85 ColorSlider::slider_color_TYPE_B(synfig::Color &color, float amount) { color.set_b(amount); }
87 ColorSlider::slider_color_TYPE_Y(synfig::Color &color, float amount) { color.set_y(amount); }
89 ColorSlider::slider_color_TYPE_U(synfig::Color &color, float amount) { color.set_u(amount-0.5f); }
91 ColorSlider::slider_color_TYPE_V(synfig::Color &color, float amount) { color.set_v(amount-0.5f); }
93 ColorSlider::slider_color_TYPE_HUE(synfig::Color &color, float amount) { color.set_uv_angle(Angle::rot(amount)); }
95 ColorSlider::slider_color_TYPE_SAT(synfig::Color &color, float amount) { color.set_s(amount*0.5f); }
97 ColorSlider::slider_color_TYPE_A(synfig::Color &color, float amount) { color.set_a(amount); }
100 ColorSlider::adjust_color(Type type, synfig::Color &color, float amount)
102 static const slider_color_func jump_table[int(TYPE_END)] =
110 slider_color_TYPE_HUE,
111 slider_color_TYPE_SAT,
114 jump_table[int(type)](color,amount);
118 ColorSlider::redraw(GdkEventExpose */*bleh*/)
122 static const slider_color_func jump_table[int(TYPE_END)] =
130 slider_color_TYPE_HUE,
131 slider_color_TYPE_SAT,
135 slider_color_func color_func(jump_table[int(type)]);
140 case TYPE_R: amount=color.get_r(); break;
141 case TYPE_G: amount=color.get_g(); break;
142 case TYPE_B: amount=color.get_b(); break;
143 case TYPE_Y: amount=color.get_y(); break;
144 case TYPE_U: amount=color.get_u()+0.5; break;
145 case TYPE_V: amount=color.get_v()+0.5; break;
146 case TYPE_HUE: amount=Angle::rot(color.get_uv_angle()).get(); amount-=floor(amount); break;
147 case TYPE_SAT: amount=color.get_s()*2.0; break;
148 case TYPE_A: amount=color.get_a(); break;
149 default: amount=0; break;
151 if(use_colorspace_gamma() && (type<TYPE_U))
152 amount=gamma_in(amount);
154 const int height(get_height());
155 const int width(get_width());
157 Gdk::Rectangle ca(0,0,width,height);
159 Glib::RefPtr<Gdk::GC> gc(Gdk::GC::create(get_window()));
160 const Color bg1(0.75, 0.75, 0.75);
161 const Color bg2(0.5, 0.5, 0.5);
163 for(i=width-1;i>=0;i--)
166 (use_colorspace_gamma() && type<TYPE_U)
167 ? gamma_out(float(i)/float(width))
168 : (float(i)/float(width)));
169 const Color c1(Color::blend(color,bg1,1.0).clamped());
170 const Color c2(Color::blend(color,bg2,1.0).clamped());
171 assert(c1.is_valid());
172 assert(c2.is_valid());
176 gc->set_rgb_fg_color(colorconv_synfig2gdk(c1));
177 get_window()->draw_rectangle(gc, true, ca.get_x()+i, ca.get_y(), 1, height/2);
179 gc->set_rgb_fg_color(colorconv_synfig2gdk(c2));
180 get_window()->draw_rectangle(gc, true, ca.get_x()+i, ca.get_y()+height/2, 1, height/2);
184 gc->set_rgb_fg_color(colorconv_synfig2gdk(c2));
185 get_window()->draw_rectangle(gc, true, ca.get_x()+i, ca.get_y(), 1, height/2);
187 gc->set_rgb_fg_color(colorconv_synfig2gdk(c1));
188 get_window()->draw_rectangle(gc, true, ca.get_x()+i, ca.get_y()+height/2, 1, height/2);
192 get_style()->paint_arrow(
201 int(amount*width)-height/2,
207 gc->set_rgb_fg_color(Gdk::Color("#ffffff"));
208 get_window()->draw_rectangle(gc, false, ca.get_x()+1, ca.get_y()+1, width-3, height-3);
209 gc->set_rgb_fg_color(Gdk::Color("#000000"));
210 get_window()->draw_rectangle(gc, false, ca.get_x(), ca.get_y(), width-1, height-1);
215 ColorSlider::on_event(GdkEvent *event)
217 const int width(get_width());
219 if( GDK_SCROLL == event->type ){
224 case TYPE_R: amount=color.get_r(); break;
225 case TYPE_G: amount=color.get_g(); break;
226 case TYPE_B: amount=color.get_b(); break;
227 case TYPE_Y: amount=color.get_y(); break;
228 case TYPE_U: amount=color.get_u()+0.5; break;
229 case TYPE_V: amount=color.get_v()+0.5; break;
230 case TYPE_HUE: amount=Angle::rot(color.get_uv_angle()).get(); amount-=floor(amount); break;
231 case TYPE_SAT: amount=color.get_s()*2.0; break;
232 case TYPE_A: amount=color.get_a(); break;
233 default: amount=0; break;
235 if(use_colorspace_gamma() && (type<TYPE_U))
236 amount=gamma_in(amount);
238 switch(event->scroll.direction){
240 case GDK_SCROLL_RIGHT:
243 case GDK_SCROLL_DOWN:
244 case GDK_SCROLL_LEFT:
249 x = float(event->button.x);
253 if(pos<0 || x<=0)pos=0;
256 if(use_colorspace_gamma() && (type<TYPE_U))
258 if(pos<0 || event->button.x<=0)pos=0;
264 signal_slider_moved_(type,pos);
269 case GDK_BUTTON_RELEASE:
273 case GDK_BUTTON_PRESS:
274 case GDK_MOTION_NOTIFY:
275 // adjust_color(type,color_,pos);
276 signal_slider_moved_(type,pos);
286 /* === M E T H O D S ======================================================= */
288 Widget_ColorEdit::Widget_ColorEdit():
289 R_adjustment(0,-10000000,10000000,1,10,0),
290 G_adjustment(0,-10000000,10000000,1,10,0),
291 B_adjustment(0,-10000000,10000000,1,10,0),
292 A_adjustment(0,-10000000,10000000,1,10,0)
294 notebook=manage(new Gtk::Notebook);
296 Gtk::Table* rgb_table(manage(new Gtk::Table()));
297 Gtk::Table* yuv_table(manage(new Gtk::Table()));
298 Gtk::Table* main_table(this);
301 Gtk::VBox* rgb_box(manage(new Gtk::VBox()));
302 Gtk::VBox* yuv_box(manage(new Gtk::VBox()));
303 rgb_box->pack_start(*rgb_table,false,false);
304 yuv_box->pack_start(*yuv_table,false,false);
305 notebook->append_page(*rgb_box,_("RGB"));
306 notebook->append_page(*yuv_box,_("YUV"));
309 color=Color(0,0,0,0);
311 set_size_request(200,-1);
316 R_adjustment.set_lower(-10000000);
317 G_adjustment.set_lower(-10000000);
318 B_adjustment.set_lower(-10000000);
319 A_adjustment.set_lower(-10000000);
323 Pango::AttrList attr_list;
324 Pango::AttrInt pango_size(Pango::Attribute::create_attr_size(Pango::SCALE*7));
325 pango_size.set_start_index(0);
326 pango_size.set_end_index(64);
327 attr_list.change(pango_size);
329 widget_color.set_size_request(-1,16);
330 attach(widget_color, 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
331 attach(*notebook, 0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
333 #define SLIDER_ROW(i,n,l) \
334 slider_##n=manage(new ColorSlider(ColorSlider::TYPE_##n)); \
335 slider_##n->signal_slider_moved().connect(sigc::mem_fun(*this,&studio::Widget_ColorEdit::on_slider_moved)); \
336 /*slider_##n->signal_activated().connect(sigc::mem_fun(*this,&studio::Widget_ColorEdit::activated));*/ \
337 slider_##n->signal_activated().connect(sigc::mem_fun(*this,&studio::Widget_ColorEdit::on_value_changed)); \
338 label=manage(new class Gtk::Label(l,0.0,0.5)); \
339 label->set_use_markup(false); \
340 label->set_use_underline(false); \
341 label->set_attributes(attr_list); \
342 table->attach(*label, 0, 1, 1+2*i, 2+2*i, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0); \
343 table->attach(*slider_##n, 0, 1, 2+2*i, 3+2*i, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0)
345 #define ATTACH_SPIN_BUTTON(i,n) \
346 spinbutton_##n=manage(new class Gtk::SpinButton(n##_adjustment,1,0)); \
347 spinbutton_##n->set_update_policy(Gtk::UPDATE_ALWAYS); \
348 spinbutton_##n->set_size_request(SPINBUTTON_WIDTH,-1); \
349 spinbutton_##n->show(); \
350 table->attach(*spinbutton_##n, 1, 2, 1+2*i, 3+2*i, Gtk::SHRINK, Gtk::EXPAND, 2, 0)
353 Gtk::Table* table(rgb_table);
354 SLIDER_ROW(0,R,_("Red"));
355 ATTACH_SPIN_BUTTON(0,R);
356 SLIDER_ROW(1,G,_("Green"));
357 ATTACH_SPIN_BUTTON(1,G);
358 SLIDER_ROW(2,B,_("Blue"));
359 ATTACH_SPIN_BUTTON(2,B);
361 hex_color_label = manage(new Gtk::Label(_("HTML code"), 0.0, 0.5));
362 hex_color_label->set_use_markup(false);
363 hex_color_label->set_use_underline(false);
364 hex_color_label->set_attributes(attr_list);
365 table->attach(*hex_color_label, 0, 1, 7, 8, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
367 hex_color = manage(new Gtk::Entry());
368 hex_color->set_width_chars(8);
369 hex_color->signal_activate().connect(sigc::mem_fun(*this,&studio::Widget_ColorEdit::on_hex_edited));
370 table->attach(*hex_color, 0, 1, 8, 9, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
373 Gtk::Table* table(yuv_table);
374 SLIDER_ROW(0,Y,_("Luma"));
375 SLIDER_ROW(1,HUE,_("Hue"));
376 SLIDER_ROW(2,SAT,_("Saturation"));
377 SLIDER_ROW(3,U,_("U"));
378 SLIDER_ROW(4,V,_("V"));
381 Gtk::Table* table(main_table);
382 SLIDER_ROW(1,A,_("Alpha"));
383 ATTACH_SPIN_BUTTON(1,A);
387 #undef ATTACH_SPIN_BUTTON
389 spinbutton_R->signal_activate().connect(sigc::mem_fun(*spinbutton_G,&Gtk::SpinButton::grab_focus));
390 spinbutton_G->signal_activate().connect(sigc::mem_fun(*spinbutton_B,&Gtk::SpinButton::grab_focus));
391 spinbutton_B->signal_activate().connect(sigc::mem_fun(*spinbutton_A,&Gtk::SpinButton::grab_focus));
392 spinbutton_A->signal_activate().connect(sigc::mem_fun(*spinbutton_R,&Gtk::SpinButton::grab_focus));
394 R_adjustment.signal_value_changed().connect(sigc::mem_fun(*this,&studio::Widget_ColorEdit::on_value_changed));
395 G_adjustment.signal_value_changed().connect(sigc::mem_fun(*this,&studio::Widget_ColorEdit::on_value_changed));
396 B_adjustment.signal_value_changed().connect(sigc::mem_fun(*this,&studio::Widget_ColorEdit::on_value_changed));
397 A_adjustment.signal_value_changed().connect(sigc::mem_fun(*this,&studio::Widget_ColorEdit::on_value_changed));
407 Widget_ColorEdit::~Widget_ColorEdit()
412 Widget_ColorEdit::on_slider_moved(ColorSlider::Type type, float amount)
414 Color color(get_value_raw());
416 assert(color.is_valid());
417 ColorSlider::adjust_color(type,color,amount);
418 assert(color.is_valid());
420 // If a non-primary colorslider is adjusted,
421 // we want to make sure that we clamp
422 if(type>ColorSlider::TYPE_B && (color.get_r()<0 ||color.get_g()<0 ||color.get_b()<0))
426 if(type==ColorSlider::TYPE_R && color.get_r()<0)clamp_=false;
427 if(type==ColorSlider::TYPE_G && color.get_g()<0)clamp_=false;
428 if(type==ColorSlider::TYPE_B && color.get_b()<0)clamp_=false;
433 assert(color.is_valid());
437 Widget_ColorEdit::on_hex_edited()
439 Color color(get_value_raw());
440 String s = hex_color->get_text();
443 signal_value_changed_();
447 Widget_ColorEdit::on_value_changed()
452 const Color color(get_value_raw());
453 assert(color.is_valid());
454 slider_R->set_color(color);
455 slider_G->set_color(color);
456 slider_B->set_color(color);
457 slider_Y->set_color(color);
458 slider_U->set_color(color);
459 slider_V->set_color(color);
460 slider_HUE->set_color(color);
461 slider_SAT->set_color(color);
462 slider_A->set_color(color);
463 hex_color->set_text(color.get_hex());
464 widget_color.set_value(color);
467 signal_value_changed_();
471 Widget_ColorEdit::set_has_frame(bool x)
473 spinbutton_R->set_has_frame(x);
474 spinbutton_G->set_has_frame(x);
475 spinbutton_B->set_has_frame(x);
476 spinbutton_A->set_has_frame(x);
477 spinbutton_R->set_size_request(SPINBUTTON_WIDTH,-1);
478 spinbutton_G->set_size_request(SPINBUTTON_WIDTH,-1);
479 spinbutton_B->set_size_request(SPINBUTTON_WIDTH,-1);
480 spinbutton_A->set_size_request(SPINBUTTON_WIDTH,-1);
484 Widget_ColorEdit::set_digits(int x)
486 spinbutton_R->set_digits(x);
487 spinbutton_G->set_digits(x);
488 spinbutton_B->set_digits(x);
489 spinbutton_A->set_digits(x);
490 spinbutton_R->set_size_request(SPINBUTTON_WIDTH,-1);
491 spinbutton_G->set_size_request(SPINBUTTON_WIDTH,-1);
492 spinbutton_B->set_size_request(SPINBUTTON_WIDTH,-1);
493 spinbutton_A->set_size_request(SPINBUTTON_WIDTH,-1);
497 Widget_ColorEdit::set_value(const synfig::Color &data)
499 assert(data.is_valid());
505 if(use_colorspace_gamma())
507 R_adjustment.set_value(gamma_in(color.get_r())*100);
508 G_adjustment.set_value(gamma_in(color.get_g())*100);
509 B_adjustment.set_value(gamma_in(color.get_b())*100);
513 R_adjustment.set_value(color.get_r()*100);
514 G_adjustment.set_value(color.get_g()*100);
515 B_adjustment.set_value(color.get_b()*100);
517 A_adjustment.set_value(color.get_a()*100);
519 slider_R->set_color(color);
520 slider_G->set_color(color);
521 slider_B->set_color(color);
522 slider_Y->set_color(color);
523 slider_U->set_color(color);
524 slider_V->set_color(color);
525 slider_HUE->set_color(color);
526 slider_SAT->set_color(color);
527 slider_A->set_color(color);
528 hex_color->set_text(color.get_hex());
529 widget_color.set_value(color);
535 Widget_ColorEdit::get_value_raw()
538 if(use_colorspace_gamma())
540 color.set_r(gamma_out(R_adjustment.get_value()/100.0f));
541 color.set_g(gamma_out(G_adjustment.get_value()/100.0f));
542 color.set_b(gamma_out(B_adjustment.get_value()/100.0f));
546 color.set_r(R_adjustment.get_value()/100);
547 color.set_g(G_adjustment.get_value()/100);
548 color.set_b(B_adjustment.get_value()/100);
550 color.set_a(A_adjustment.get_value()/100);
551 assert(color.is_valid());
556 const synfig::Color &
557 Widget_ColorEdit::get_value()
559 if(use_colorspace_gamma())
561 color.set_r(gamma_out(R_adjustment.get_value()/100.0f));
562 color.set_g(gamma_out(G_adjustment.get_value()/100.0f));
563 color.set_b(gamma_out(B_adjustment.get_value()/100.0f));
564 assert(color.is_valid());
568 color.set_r(R_adjustment.get_value()/100);
569 color.set_g(G_adjustment.get_value()/100);
570 color.set_b(B_adjustment.get_value()/100);
571 assert(color.is_valid());
573 color.set_a(A_adjustment.get_value()/100);
574 assert(color.is_valid());
576 if(notebook->get_current_page()!=0)
577 color=color.clamped();
580 // Clamp out negative values
581 color.set_r(std::max(0.0f,(float)color.get_r()));
582 color.set_g(std::max(0.0f,(float)color.get_g()));
583 color.set_b(std::max(0.0f,(float)color.get_b()));