1 /* === S Y N F I G ========================================================= */
2 /*! \file spiralgradient.cpp
3 ** \brief Template Header
5 ** $Id: spiralgradient.cpp,v 1.1.1.1 2005/01/04 01:23:11 darco Exp $
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>
41 #include "spiralgradient.h"
45 /* === U S I N G =========================================================== */
49 using namespace synfig;
51 /* === G L O B A L S ======================================================= */
53 SYNFIG_LAYER_INIT(SpiralGradient);
54 SYNFIG_LAYER_SET_NAME(SpiralGradient,"spiral_gradient");
55 SYNFIG_LAYER_SET_LOCAL_NAME(SpiralGradient,_("Spiral Gradient"));
56 SYNFIG_LAYER_SET_CATEGORY(SpiralGradient,_("Gradients"));
57 SYNFIG_LAYER_SET_VERSION(SpiralGradient,"0.1");
58 SYNFIG_LAYER_SET_CVS_ID(SpiralGradient,"$Id: spiralgradient.cpp,v 1.1.1.1 2005/01/04 01:23:11 darco Exp $");
60 /* === P R O C E D U R E S ================================================= */
62 /* === M E T H O D S ======================================================= */
64 /* === E N T R Y P O I N T ================================================= */
66 SpiralGradient::SpiralGradient():
67 Layer_Composite(1.0,Color::BLEND_STRAIGHT),
68 gradient(Color::black(),Color::white()),
77 SpiralGradient::set_param(const String & param, const ValueBase &value)
85 return Layer_Composite::set_param(param,value);
89 SpiralGradient::get_param(const String ¶m)const
100 return Layer_Composite::get_param(param);
104 SpiralGradient::get_param_vocab()const
106 Layer::Vocab ret(Layer_Composite::get_param_vocab());
108 ret.push_back(ParamDesc("gradient")
109 .set_local_name(_("Gradient"))
112 ret.push_back(ParamDesc("center")
113 .set_local_name(_("Center"))
116 ret.push_back(ParamDesc("radius")
117 .set_local_name(_("Radius"))
118 .set_description(_("This is the radius of the circle"))
120 .set_origin("center")
123 ret.push_back(ParamDesc("angle")
124 .set_local_name(_("Angle"))
125 .set_origin("center")
128 ret.push_back(ParamDesc("clockwise")
129 .set_local_name(_("Clockwise"))
136 SpiralGradient::color_func(const Point &pos, float supersample)const
138 const Point centered(pos-center);
140 a=Angle::tan(-centered[1],centered[0]).mod();
143 if(supersample<0.00001)supersample=0.00001;
145 Real dist((pos-center).mag()/radius);
147 dist+=Angle::rot(a.mod()).get();
149 dist-=Angle::rot(a.mod()).get();
152 if(dist+supersample*0.5>1.0)
154 Color pool(gradient(dist,supersample*0.5).premult_alpha()*(1.0-(dist-supersample*0.5)));
155 pool+=gradient((dist+supersample*0.5)-1.0,supersample*0.5).premult_alpha()*((dist+supersample*0.5)-1.0);
156 return pool.demult_alpha();
158 if(dist-supersample*0.5<0.0)
160 Color pool(gradient(dist,supersample*0.5).premult_alpha()*(dist+supersample*0.5));
161 pool+=gradient(1.0-(dist-supersample*0.5),supersample*0.5).premult_alpha()*(-(dist-supersample*0.5));
162 return pool.demult_alpha();
165 return gradient(dist,supersample);
169 SpiralGradient::calc_supersample(const synfig::Point &x, float pw,float ph)const
171 return (1.41421*pw/radius+(1.41421*pw/Point(x-center).mag())/(PI*2))*0.5;
174 synfig::Layer::Handle
175 SpiralGradient::hit_check(synfig::Context context, const synfig::Point &point)const
177 if(get_blend_method()==Color::BLEND_STRAIGHT && get_amount()>=0.5)
178 return const_cast<SpiralGradient*>(this);
179 if(get_amount()==0.0)
180 return context.hit_check(point);
181 if((get_blend_method()==Color::BLEND_STRAIGHT || get_blend_method()==Color::BLEND_COMPOSITE) && color_func(point).get_a()>0.5)
182 return const_cast<SpiralGradient*>(this);
183 return context.hit_check(point);
187 SpiralGradient::get_color(Context context, const Point &pos)const
189 const Color color(color_func(pos));
191 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
194 return Color::blend(color,context.get_color(pos),get_amount(),get_blend_method());
198 SpiralGradient::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
200 SuperCallback supercb(cb,0,9500,10000);
202 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
204 surface->set_wh(renddesc.get_w(),renddesc.get_h());
208 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
217 Surface::pen pen(surface->begin());
218 const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
220 Point tl(renddesc.get_tl());
221 const int w(surface->get_w());
222 const int h(surface->get_h());
224 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
226 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
227 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
228 pen.put_value(color_func(pos,calc_supersample(pos,pw,ph)));
232 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
233 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
234 pen.put_value(Color::blend(color_func(pos,calc_supersample(pos,pw,ph)),pen.get_value(),get_amount(),get_blend_method()));
237 // Mark our progress as finished
238 if(cb && !cb->amount_complete(10000,10000))