1 /* === S Y N F I G ========================================================= */
2 /*! \file lineargradient.cpp
3 ** \brief Template Header
6 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
8 ** This package is free software; you can redistribute it and/or
9 ** modify it under the terms of the GNU General Public License as
10 ** published by the Free Software Foundation; either version 2 of
11 ** the License, or (at your option) any later version.
13 ** This package is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ** General Public License for more details.
19 ** === N O T E S ===========================================================
21 ** ========================================================================= */
23 /* === H E A D E R S ======================================================= */
32 #include "lineargradient.h"
34 #include <synfig/string.h>
35 #include <synfig/time.h>
36 #include <synfig/context.h>
37 #include <synfig/paramdesc.h>
38 #include <synfig/renddesc.h>
39 #include <synfig/surface.h>
40 #include <synfig/value.h>
41 #include <synfig/valuenode.h>
45 /* === M A C R O S ========================================================= */
47 /* === G L O B A L S ======================================================= */
49 SYNFIG_LAYER_INIT(LinearGradient);
50 SYNFIG_LAYER_SET_NAME(LinearGradient,"linear_gradient");
51 SYNFIG_LAYER_SET_LOCAL_NAME(LinearGradient,_("Linear Gradient"));
52 SYNFIG_LAYER_SET_CATEGORY(LinearGradient,_("Gradients"));
53 SYNFIG_LAYER_SET_VERSION(LinearGradient,"0.0");
54 SYNFIG_LAYER_SET_CVS_ID(LinearGradient,"$Id: lineargradient.cpp,v 1.1.1.1 2005/01/04 01:23:10 darco Exp $");
56 /* === P R O C E D U R E S ================================================= */
58 /* === M E T H O D S ======================================================= */
61 LinearGradient::sync()
64 const Real mag(diff.inv_mag());
69 LinearGradient::LinearGradient():
72 gradient(Color::black(), Color::white()),
80 LinearGradient::color_func(const Point &point, float supersample)const
82 Real dist(point*diff-p1*diff);
91 if(dist>1)dist=2.0-dist;
96 if(dist+supersample*0.5>1.0)
98 Color pool(gradient(dist,supersample*0.5).premult_alpha()*(1.0-(dist-supersample*0.5)));
99 pool+=gradient((dist+supersample*0.5)-1.0,supersample*0.5).premult_alpha()*((dist+supersample*0.5)-1.0);
100 return pool.demult_alpha();
102 if(dist-supersample*0.5<0.0)
104 Color pool(gradient(dist,supersample*0.5).premult_alpha()*(dist+supersample*0.5));
105 pool+=gradient(1.0-(dist-supersample*0.5),supersample*0.5).premult_alpha()*(-(dist-supersample*0.5));
106 return pool.demult_alpha();
109 return gradient(dist,supersample);
113 LinearGradient::calc_supersample(const synfig::Point &x, float pw,float ph)const
115 return pw/(p2-p1).mag();
118 synfig::Layer::Handle
119 LinearGradient::hit_check(synfig::Context context, const synfig::Point &point)const
121 if(get_blend_method()==Color::BLEND_STRAIGHT && get_amount()>=0.5)
122 return const_cast<LinearGradient*>(this);
123 if(get_amount()==0.0)
124 return context.hit_check(point);
125 if((get_blend_method()==Color::BLEND_STRAIGHT || get_blend_method()==Color::BLEND_COMPOSITE) && color_func(point).get_a()>0.5)
126 return const_cast<LinearGradient*>(this);
127 return context.hit_check(point);
131 LinearGradient::set_param(const String & param, const ValueBase &value)
133 if(param=="p1" && value.same_as(p1))
139 if(param=="p2" && value.same_as(p2))
152 return Layer_Composite::set_param(param,value);
156 LinearGradient::get_param(const String & param)const
167 return Layer_Composite::get_param(param);
171 LinearGradient::get_param_vocab()const
173 Layer::Vocab ret(Layer_Composite::get_param_vocab());
175 ret.push_back(ParamDesc("p1")
176 .set_local_name(_("Point 1"))
179 ret.push_back(ParamDesc("p2")
180 .set_local_name(_("Point 2"))
182 ret.push_back(ParamDesc("gradient")
183 .set_local_name(_("Gradient"))
185 ret.push_back(ParamDesc("loop")
186 .set_local_name(_("Loop"))
188 ret.push_back(ParamDesc("zigzag")
189 .set_local_name(_("ZigZag"))
196 LinearGradient::get_color(Context context, const Point &point)const
198 const Color color(color_func(point));
200 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
203 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
207 LinearGradient::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
209 SuperCallback supercb(cb,0,9500,10000);
211 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
213 surface->set_wh(renddesc.get_w(),renddesc.get_h());
217 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
226 Surface::pen pen(surface->begin());
227 const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
229 Point tl(renddesc.get_tl());
230 const int w(surface->get_w());
231 const int h(surface->get_h());
233 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
235 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
236 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
237 pen.put_value(color_func(pos,calc_supersample(pos,pw,ph)));
241 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
242 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
243 pen.put_value(Color::blend(color_func(pos,calc_supersample(pos,pw,ph)),pen.get_value(),get_amount(),get_blend_method()));
246 // Mark our progress as finished
247 if(cb && !cb->amount_complete(10000,10000))