1 /* === S Y N F I G ========================================================= */
2 /*! \file conicalgradient.cpp
3 ** \brief Implementation of the "Conical Gradient" layer
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
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.
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.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
32 #include <synfig/string.h>
33 #include <synfig/time.h>
34 #include <synfig/context.h>
35 #include <synfig/paramdesc.h>
36 #include <synfig/renddesc.h>
37 #include <synfig/surface.h>
38 #include <synfig/value.h>
39 #include <synfig/valuenode.h>
40 #include <synfig/angle.h>
42 #include "conicalgradient.h"
46 /* === U S I N G =========================================================== */
50 using namespace synfig;
52 /* === G L O B A L S ======================================================= */
54 SYNFIG_LAYER_INIT(ConicalGradient);
55 SYNFIG_LAYER_SET_NAME(ConicalGradient,"conical_gradient");
56 SYNFIG_LAYER_SET_LOCAL_NAME(ConicalGradient,N_("Conical Gradient"));
57 SYNFIG_LAYER_SET_CATEGORY(ConicalGradient,N_("Gradients"));
58 SYNFIG_LAYER_SET_VERSION(ConicalGradient,"0.1");
59 SYNFIG_LAYER_SET_CVS_ID(ConicalGradient,"$Id$");
61 /* === P R O C E D U R E S ================================================= */
63 /* === M E T H O D S ======================================================= */
65 /* === E N T R Y P O I N T ================================================= */
67 ConicalGradient::ConicalGradient():
68 Layer_Composite(1.0,Color::BLEND_COMPOSITE),
69 gradient(Color::black(),Color::white()),
74 Layer::Vocab voc(get_param_vocab());
75 Layer::fill_static(voc);
79 ConicalGradient::set_param(const String & param, const ValueBase &value)
85 return Layer_Composite::set_param(param,value);
89 ConicalGradient::get_param(const String ¶m)const
99 return Layer_Composite::get_param(param);
103 ConicalGradient::get_param_vocab()const
105 Layer::Vocab ret(Layer_Composite::get_param_vocab());
107 ret.push_back(ParamDesc("gradient")
108 .set_local_name(_("Gradient"))
111 ret.push_back(ParamDesc("center")
112 .set_local_name(_("Center"))
115 ret.push_back(ParamDesc("angle")
116 .set_local_name(_("Angle"))
117 .set_origin("center")
120 ret.push_back(ParamDesc("symmetric")
121 .set_local_name(_("Symmetric"))
128 ConicalGradient::color_func(const Point &pos, float supersample)const
130 const Point centered(pos-center);
131 Angle::rot a=Angle::tan(-centered[1],centered[0]).mod();
133 Real dist(a.mod().get());
141 if(dist>1)dist=2.0-dist;
143 /* if(dist+supersample*0.5>1.0)
145 Color pool(gradient(dist,supersample*0.5)*(1.0-(dist-supersample*0.5)));
146 pool+=gradient((dist+supersample*0.5)-1.0,supersample*0.5)*((dist+supersample*0.5)-1.0);
147 if(pool.get_a() && pool.is_valid())
149 pool.set_r(pool.get_r()/pool.get_a());
150 pool.set_g(pool.get_g()/pool.get_a());
151 pool.set_b(pool.get_b()/pool.get_a());
152 pool.set_a(pool.get_a()/supersample);
157 if(dist-supersample*0.5<0.0)
159 Color pool(gradient(dist,supersample*0.5)*(dist+supersample*0.5));
160 pool+=gradient(1.0-(dist-supersample*0.5),supersample*0.5)*(-(dist-supersample*0.5));
161 if(pool.get_a() && pool.is_valid())
163 pool.set_r(pool.get_r()/pool.get_a());
164 pool.set_g(pool.get_g()/pool.get_a());
165 pool.set_b(pool.get_b()/pool.get_a());
166 pool.set_a(pool.get_a()/supersample);
171 if(dist+supersample*0.5>1.0)
173 float left(supersample*0.5-(dist-1.0));
174 float right(supersample*0.5+(dist-1.0));
175 Color pool(gradient(1.0-(left*0.5),left).premult_alpha()*left/supersample);
176 pool+=gradient(right*0.5,right).premult_alpha()*right/supersample;
177 return pool.demult_alpha();
179 if(dist-supersample*0.5<0.0)
181 float left(supersample*0.5-dist);
182 float right(supersample*0.5+dist);
183 Color pool(gradient(right*0.5,right).premult_alpha()*right/supersample);
184 pool+=gradient(1.0-left*0.5,left).premult_alpha()*left/supersample;
185 return pool.demult_alpha();
188 return gradient(dist,supersample);
192 ConicalGradient::calc_supersample(const synfig::Point &x, float pw,float ph)const
195 if(abs(adj[0])<abs(pw*0.5) && abs(adj[1])<abs(ph*0.5))
197 return (pw/Point(x-center).mag())/(PI*2);
200 synfig::Layer::Handle
201 ConicalGradient::hit_check(synfig::Context context, const synfig::Point &point)const
203 if(get_blend_method()==Color::BLEND_STRAIGHT && get_amount()>=0.5)
204 return const_cast<ConicalGradient*>(this);
205 if(get_amount()==0.0)
206 return context.hit_check(point);
207 if((get_blend_method()==Color::BLEND_STRAIGHT || get_blend_method()==Color::BLEND_COMPOSITE) && color_func(point).get_a()>0.5)
208 return const_cast<ConicalGradient*>(this);
209 return context.hit_check(point);
213 ConicalGradient::get_color(Context context, const Point &pos)const
215 const Color color(color_func(pos));
217 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
220 return Color::blend(color,context.get_color(pos),get_amount(),get_blend_method());
224 ConicalGradient::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
226 SuperCallback supercb(cb,0,9500,10000);
228 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
230 surface->set_wh(renddesc.get_w(),renddesc.get_h());
234 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
243 Surface::pen pen(surface->begin());
244 const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
246 Point tl(renddesc.get_tl());
247 const int w(surface->get_w());
248 const int h(surface->get_h());
250 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
254 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
255 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
256 pen.put_value(color_func(pos,calc_supersample(pos,pw,ph)));
260 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
261 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
262 pen.put_value(color_func(pos,0));
269 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
270 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
271 pen.put_value(Color::blend(color_func(pos,calc_supersample(pos,pw,ph)),pen.get_value(),get_amount(),get_blend_method()));
275 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
276 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
277 pen.put_value(Color::blend(color_func(pos,0),pen.get_value(),get_amount(),get_blend_method()));
281 // Mark our progress as finished
282 if(cb && !cb->amount_complete(10000,10000))