1 /*! ========================================================================
4 ** $Id: lineargradient.cpp,v 1.1.1.1 2005/01/04 01:23:10 darco Exp $
6 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
8 ** This software and associated documentation
9 ** are CONFIDENTIAL and PROPRIETARY property of
10 ** the above-mentioned copyright holder.
12 ** You may not copy, print, publish, or in any
13 ** other way distribute this software without
14 ** a prior written agreement with
15 ** the copyright holder.
17 ** === N O T E S ===========================================================
19 ** ========================================================================= */
21 /* === H E A D E R S ======================================================= */
30 #include "lineargradient.h"
32 #include <sinfg/string.h>
33 #include <sinfg/time.h>
34 #include <sinfg/context.h>
35 #include <sinfg/paramdesc.h>
36 #include <sinfg/renddesc.h>
37 #include <sinfg/surface.h>
38 #include <sinfg/value.h>
39 #include <sinfg/valuenode.h>
43 /* === M A C R O S ========================================================= */
45 /* === G L O B A L S ======================================================= */
47 SINFG_LAYER_INIT(LinearGradient);
48 SINFG_LAYER_SET_NAME(LinearGradient,"linear_gradient");
49 SINFG_LAYER_SET_LOCAL_NAME(LinearGradient,_("Linear Gradient"));
50 SINFG_LAYER_SET_CATEGORY(LinearGradient,_("Gradients"));
51 SINFG_LAYER_SET_VERSION(LinearGradient,"0.0");
52 SINFG_LAYER_SET_CVS_ID(LinearGradient,"$Id: lineargradient.cpp,v 1.1.1.1 2005/01/04 01:23:10 darco Exp $");
54 /* === P R O C E D U R E S ================================================= */
56 /* === M E T H O D S ======================================================= */
59 LinearGradient::sync()
62 const Real mag(diff.inv_mag());
67 LinearGradient::LinearGradient():
70 gradient(Color::black(), Color::white()),
78 LinearGradient::color_func(const Point &point, float supersample)const
80 Real dist(point*diff-p1*diff);
89 if(dist>1)dist=2.0-dist;
94 if(dist+supersample*0.5>1.0)
96 Color pool(gradient(dist,supersample*0.5).premult_alpha()*(1.0-(dist-supersample*0.5)));
97 pool+=gradient((dist+supersample*0.5)-1.0,supersample*0.5).premult_alpha()*((dist+supersample*0.5)-1.0);
98 return pool.demult_alpha();
100 if(dist-supersample*0.5<0.0)
102 Color pool(gradient(dist,supersample*0.5).premult_alpha()*(dist+supersample*0.5));
103 pool+=gradient(1.0-(dist-supersample*0.5),supersample*0.5).premult_alpha()*(-(dist-supersample*0.5));
104 return pool.demult_alpha();
107 return gradient(dist,supersample);
111 LinearGradient::calc_supersample(const sinfg::Point &x, float pw,float ph)const
113 return pw/(p2-p1).mag();
117 LinearGradient::hit_check(sinfg::Context context, const sinfg::Point &point)const
119 if(get_blend_method()==Color::BLEND_STRAIGHT && get_amount()>=0.5)
120 return const_cast<LinearGradient*>(this);
121 if(get_amount()==0.0)
122 return context.hit_check(point);
123 if((get_blend_method()==Color::BLEND_STRAIGHT || get_blend_method()==Color::BLEND_COMPOSITE) && color_func(point).get_a()>0.5)
124 return const_cast<LinearGradient*>(this);
125 return context.hit_check(point);
129 LinearGradient::set_param(const String & param, const ValueBase &value)
131 if(param=="p1" && value.same_as(p1))
137 if(param=="p2" && value.same_as(p2))
150 return Layer_Composite::set_param(param,value);
154 LinearGradient::get_param(const String & param)const
165 return Layer_Composite::get_param(param);
169 LinearGradient::get_param_vocab()const
171 Layer::Vocab ret(Layer_Composite::get_param_vocab());
173 ret.push_back(ParamDesc("p1")
174 .set_local_name(_("Point 1"))
177 ret.push_back(ParamDesc("p2")
178 .set_local_name(_("Point 2"))
180 ret.push_back(ParamDesc("gradient")
181 .set_local_name(_("Gradient"))
183 ret.push_back(ParamDesc("loop")
184 .set_local_name(_("Loop"))
186 ret.push_back(ParamDesc("zigzag")
187 .set_local_name(_("ZigZag"))
194 LinearGradient::get_color(Context context, const Point &point)const
196 const Color color(color_func(point));
198 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
201 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
205 LinearGradient::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
207 SuperCallback supercb(cb,0,9500,10000);
209 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
211 surface->set_wh(renddesc.get_w(),renddesc.get_h());
215 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
224 Surface::pen pen(surface->begin());
225 const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
227 Point tl(renddesc.get_tl());
228 const int w(surface->get_w());
229 const int h(surface->get_h());
231 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
233 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
234 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
235 pen.put_value(color_func(pos,calc_supersample(pos,pw,ph)));
239 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
240 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
241 pen.put_value(Color::blend(color_func(pos,calc_supersample(pos,pw,ph)),pen.get_value(),get_amount(),get_blend_method()));
244 // Mark our progress as finished
245 if(cb && !cb->amount_complete(10000,10000))