Add method to convert a synfig::Color to a Gdk::Color.
[synfig.git] / synfig-studio / src / gtkmm / widget_color.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file widget_color.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
10 **      This package is free software; you can redistribute it and/or
11 **      modify it under the terms of the GNU General Public License as
12 **      published by the Free Software Foundation; either version 2 of
13 **      the License, or (at your option) any later version.
14 **
15 **      This package is distributed in the hope that it will be useful,
16 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 **      General Public License for more details.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
32 #include "widget_color.h"
33 #include <cmath>
34 #include "app.h"
35 #include <gtkmm/drawingarea.h>
36
37 #include "general.h"
38
39 #endif
40
41 /* === U S I N G =========================================================== */
42
43 using namespace std;
44 using namespace etl;
45 using namespace synfig;
46 using namespace studio;
47
48 /* === M A C R O S ========================================================= */
49
50 /* === G L O B A L S ======================================================= */
51
52 /* === P R O C E D U R E S ================================================= */
53
54 Gdk::Color
55 studio::colorconv_synfig2gdk(const synfig::Color &c_)
56 {
57         const synfig::Color c(c_.clamped());
58         Gdk::Color ret;
59         ret.set_rgb(
60                         256*App::gamma.r_F32_to_U8(c.get_r()),
61                         256*App::gamma.g_F32_to_U8(c.get_g()),
62                         256*App::gamma.b_F32_to_U8(c.get_b())
63                 );
64         return ret;
65 }
66
67 synfig::Color
68 studio::colorconv_gdk2synfig(const Gdk::Color& color, const synfig::ColorReal& alpha)
69 {
70         synfig::Color *synfigColor;
71
72         synfigColor = new synfig::Color(color.get_red_p(), color.get_green_p(), color.get_blue_p(), alpha);
73         return *synfigColor;
74 }
75
76 void
77 studio::render_color_to_window(const Glib::RefPtr<Gdk::Drawable>& window,const Gdk::Rectangle& ca,const synfig::Color &color)
78 {
79         const int height(ca.get_height());
80         const int width(ca.get_width());
81
82         const int square_size(height/2);
83
84         Glib::RefPtr<Gdk::GC> gc(Gdk::GC::create(window));
85
86         if(color.get_alpha()!=1.0)
87         {
88                 // In this case we need to render the alpha squares
89
90                 const Color bg1(Color::blend(color,Color(0.75, 0.75, 0.75),1.0).clamped());
91                 const Color bg2(Color::blend(color,Color(0.5, 0.5, 0.5),1.0).clamped());
92
93                 Gdk::Color gdk_c1(colorconv_synfig2gdk(bg1));
94                 Gdk::Color gdk_c2(colorconv_synfig2gdk(bg2));
95
96                 bool toggle(false);
97                 for(int i=0;i<width;i+=square_size)
98                 {
99                         const int square_width(min(square_size,width-i));
100
101                         if(toggle)
102                         {
103                                 gc->set_rgb_fg_color(gdk_c1);
104                                 window->draw_rectangle(gc, true, ca.get_x()+i, ca.get_y(), square_width, square_size);
105
106                                 gc->set_rgb_fg_color(gdk_c2);
107                                 window->draw_rectangle(gc, true, ca.get_x()+i, ca.get_y()+square_size, square_width, square_size);
108                                 toggle=false;
109                         }
110                         else
111                         {
112                                 gc->set_rgb_fg_color(gdk_c2);
113                                 window->draw_rectangle(gc, true, ca.get_x()+i, ca.get_y(), square_width, square_size);
114
115                                 gc->set_rgb_fg_color(gdk_c1);
116                                 window->draw_rectangle(gc, true, ca.get_x()+i, ca.get_y()+square_size, square_width, square_size);
117                                 toggle=true;
118                         }
119                 }
120         }
121         else
122         {
123                 // In this case we have a solid color to use
124                 Gdk::Color gdk_c1(colorconv_synfig2gdk(color));
125
126                 gc->set_rgb_fg_color(gdk_c1);
127                 window->draw_rectangle(gc, true, ca.get_x(), ca.get_y(), width-1, height-1);
128         }
129         gc->set_rgb_fg_color(Gdk::Color("#ffffff"));
130         window->draw_rectangle(gc, false, ca.get_x()+1, ca.get_y()+1, width-3, height-3);
131         gc->set_rgb_fg_color(Gdk::Color("#000000"));
132         window->draw_rectangle(gc, false, ca.get_x(), ca.get_y(), width-1, height-1);
133 }
134
135 /* === C L A S S E S ======================================================= */
136
137
138 /* === M E T H O D S ======================================================= */
139
140 Widget_Color::Widget_Color()
141 {
142         color=Color(0,0,0,0);
143         set_size_request(-1,16);
144
145         signal_expose_event().connect(sigc::mem_fun(*this, &studio::Widget_Color::redraw));
146         add_events(Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK);
147
148 }
149
150 Widget_Color::~Widget_Color()
151 {
152 }
153
154 void
155 Widget_Color::set_value(const synfig::Color &data)
156 {
157         assert(data.is_valid());
158         color=data;
159         queue_draw();
160 }
161
162 const synfig::Color &
163 Widget_Color::get_value()
164 {
165         assert(color.is_valid());
166         return color;
167 }
168
169 bool
170 Widget_Color::on_event(GdkEvent *event)
171 {
172         switch(event->type)
173         {
174         case GDK_BUTTON_PRESS:
175                 if(event->button.button==1)
176                 {
177                         signal_activate_();
178                         return true;
179                 }
180                 if(event->button.button==3)
181                 {
182                         signal_secondary_();
183                         return true;
184                 }
185                 break;
186
187         default:
188                 break;
189         }
190         return false;
191 }
192
193 bool
194 Widget_Color::redraw(GdkEventExpose */*bleh*/)
195 {
196         //Glib::RefPtr<Gdk::GC> gc(Gdk::GC::create(get_window()));
197
198         const int h(get_height());
199         const int w(get_width());
200
201         render_color_to_window(get_window(),Gdk::Rectangle(0,0,w,h),color);
202
203         return true;
204 }