1 /*! ========================================================================
4 ** $Id: lineargradient.cpp,v 1.1.1.1 2005/01/04 01:23:10 darco Exp $
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.
18 ** === N O T E S ===========================================================
20 ** ========================================================================= */
22 /* === H E A D E R S ======================================================= */
31 #include "lineargradient.h"
33 #include <synfig/string.h>
34 #include <synfig/time.h>
35 #include <synfig/context.h>
36 #include <synfig/paramdesc.h>
37 #include <synfig/renddesc.h>
38 #include <synfig/surface.h>
39 #include <synfig/value.h>
40 #include <synfig/valuenode.h>
44 /* === M A C R O S ========================================================= */
46 /* === G L O B A L S ======================================================= */
48 SYNFIG_LAYER_INIT(LinearGradient);
49 SYNFIG_LAYER_SET_NAME(LinearGradient,"linear_gradient");
50 SYNFIG_LAYER_SET_LOCAL_NAME(LinearGradient,_("Linear Gradient"));
51 SYNFIG_LAYER_SET_CATEGORY(LinearGradient,_("Gradients"));
52 SYNFIG_LAYER_SET_VERSION(LinearGradient,"0.0");
53 SYNFIG_LAYER_SET_CVS_ID(LinearGradient,"$Id: lineargradient.cpp,v 1.1.1.1 2005/01/04 01:23:10 darco Exp $");
55 /* === P R O C E D U R E S ================================================= */
57 /* === M E T H O D S ======================================================= */
60 LinearGradient::sync()
63 const Real mag(diff.inv_mag());
68 LinearGradient::LinearGradient():
71 gradient(Color::black(), Color::white()),
79 LinearGradient::color_func(const Point &point, float supersample)const
81 Real dist(point*diff-p1*diff);
90 if(dist>1)dist=2.0-dist;
95 if(dist+supersample*0.5>1.0)
97 Color pool(gradient(dist,supersample*0.5).premult_alpha()*(1.0-(dist-supersample*0.5)));
98 pool+=gradient((dist+supersample*0.5)-1.0,supersample*0.5).premult_alpha()*((dist+supersample*0.5)-1.0);
99 return pool.demult_alpha();
101 if(dist-supersample*0.5<0.0)
103 Color pool(gradient(dist,supersample*0.5).premult_alpha()*(dist+supersample*0.5));
104 pool+=gradient(1.0-(dist-supersample*0.5),supersample*0.5).premult_alpha()*(-(dist-supersample*0.5));
105 return pool.demult_alpha();
108 return gradient(dist,supersample);
112 LinearGradient::calc_supersample(const synfig::Point &x, float pw,float ph)const
114 return pw/(p2-p1).mag();
117 synfig::Layer::Handle
118 LinearGradient::hit_check(synfig::Context context, const synfig::Point &point)const
120 if(get_blend_method()==Color::BLEND_STRAIGHT && get_amount()>=0.5)
121 return const_cast<LinearGradient*>(this);
122 if(get_amount()==0.0)
123 return context.hit_check(point);
124 if((get_blend_method()==Color::BLEND_STRAIGHT || get_blend_method()==Color::BLEND_COMPOSITE) && color_func(point).get_a()>0.5)
125 return const_cast<LinearGradient*>(this);
126 return context.hit_check(point);
130 LinearGradient::set_param(const String & param, const ValueBase &value)
132 if(param=="p1" && value.same_as(p1))
138 if(param=="p2" && value.same_as(p2))
151 return Layer_Composite::set_param(param,value);
155 LinearGradient::get_param(const String & param)const
166 return Layer_Composite::get_param(param);
170 LinearGradient::get_param_vocab()const
172 Layer::Vocab ret(Layer_Composite::get_param_vocab());
174 ret.push_back(ParamDesc("p1")
175 .set_local_name(_("Point 1"))
178 ret.push_back(ParamDesc("p2")
179 .set_local_name(_("Point 2"))
181 ret.push_back(ParamDesc("gradient")
182 .set_local_name(_("Gradient"))
184 ret.push_back(ParamDesc("loop")
185 .set_local_name(_("Loop"))
187 ret.push_back(ParamDesc("zigzag")
188 .set_local_name(_("ZigZag"))
195 LinearGradient::get_color(Context context, const Point &point)const
197 const Color color(color_func(point));
199 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
202 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
206 LinearGradient::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
208 SuperCallback supercb(cb,0,9500,10000);
210 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
212 surface->set_wh(renddesc.get_w(),renddesc.get_h());
216 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
225 Surface::pen pen(surface->begin());
226 const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
228 Point tl(renddesc.get_tl());
229 const int w(surface->get_w());
230 const int h(surface->get_h());
232 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
234 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
235 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
236 pen.put_value(color_func(pos,calc_supersample(pos,pw,ph)));
240 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
241 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
242 pen.put_value(Color::blend(color_func(pos,calc_supersample(pos,pw,ph)),pen.get_value(),get_amount(),get_blend_method()));
245 // Mark our progress as finished
246 if(cb && !cb->amount_complete(10000,10000))