1 /* === S Y N F I G ========================================================= */
3 ** \brief Color Gradient Class Member Definitions
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 ======================================================= */
35 #include "exception.h"
40 /* === U S I N G =========================================================== */
44 using namespace synfig;
46 /* === M A C R O S ========================================================= */
48 /* === G L O B A L S ======================================================= */
50 /* === P R O C E D U R E S ================================================= */
52 /* === M E T H O D S ======================================================= */
54 synfig::Gradient::Gradient(const Color &c1, const Color &c2)
56 push_back(CPoint(0.0,c1));
57 push_back(CPoint(1.0,c2));
60 synfig::Gradient::Gradient(const Color &c1, const Color &c2, const Color &c3)
62 push_back(CPoint(0.0,c1));
63 push_back(CPoint(0.5,c2));
64 push_back(CPoint(1.0,c3));
67 // This sort algorithm MUST be stable
68 // ie: it must not change the order of items with the same value.
69 // I am using a bubble sort.
70 // This algorithm will sort a nearly-sorted list at ~O(N), and
71 // it will sort an inverse sorted list at ~O(N*N).
73 synfig::Gradient::sort()
75 stable_sort(begin(),end());
80 for(iter=begin();iter!=end();iter++)
82 for(next=iter, iter2=next--;iter2!=begin();iter2=next--)
99 static synfig::ColorAccumulator
100 supersample_helper(const synfig::Gradient::CPoint &color1, const synfig::Gradient::CPoint &color2, float begin, float end, float &weight)
102 if(color1.pos==color2.pos || color1.pos>=end || color2.pos<=begin)
105 return Color::alpha();
107 if(color1.pos>=begin && color2.pos<end)
109 weight=color2.pos-color1.pos;
110 ColorAccumulator ret=Color::blend(color2.color,color1.color, 0.5, Color::BLEND_STRAIGHT).premult_alpha()*weight;
113 if(color1.pos>=begin && color2.pos>=end)
115 weight=end-color1.pos;
116 float pos((end+color1.pos)*0.5);
117 float amount((pos-color1.pos)/(color2.pos-color1.pos));
118 //if(abs(amount)>1)amount=(amount>0)?1:-1;
119 ColorAccumulator ret(Color::blend(color2.color,color1.color, amount, Color::BLEND_STRAIGHT).premult_alpha()*weight);
122 if(color1.pos<begin && color2.pos<end)
124 weight=color2.pos-begin;
125 float pos((begin+color2.pos)*0.5);
126 float amount((pos-color1.pos)/(color2.pos-color1.pos));
127 //if(abs(amount)>1)amount=(amount>0)?1:-1;
128 ColorAccumulator ret(Color::blend(color2.color,color1.color, amount, Color::BLEND_STRAIGHT).premult_alpha()*weight);
131 synfig::error("color1.pos=%f",color1.pos);
132 synfig::error("color2.pos=%f",color2.pos);
133 synfig::error("begin=%f",begin);
134 synfig::error("end=%f",end);
137 return Color::alpha();
143 synfig::Gradient::operator()(const Real &x,float supersample)const
146 return Color(0,0,0,0);
148 supersample=-supersample;
152 float begin_sample(x-supersample*0.5);
153 float end_sample(x+supersample*0.5);
155 if(size()==1 || end_sample<=front().pos || isnan(x))
156 return front().color;
158 if(begin_sample>=back().pos)
162 if(end_sample>=back().pos)
163 end_sample=back().pos;
165 if(begin_sample<=front().pos)
166 begin_sample=front().pos;
169 const_iterator iter,next;
173 Real left = x-supersample/2, right = x+supersample/2;
175 if(left < front().pos) left = front().pos;
176 if(right > back().pos) right = back().pos;
178 //find using binary search...
179 const_iterator iterl,iterr;
181 //the binary search should give us the values BEFORE the point we're looking for...
182 iterl = binary_find(begin(),end(),left);
183 iterr = binary_find(iterl,end(),right);
185 //now integrate over the range of left to right...
189 iterr++; //let's look at the next one shall we :)
191 //interpolate neighboring colors
192 const Real one = iterr->pos - iterl->pos;
193 const Real lambda = (x - iterl->pos)/one;
195 //(1-l)iterl + (l)iterr
196 return iterl->color.premult_alpha()*(1-lambda) + iterr->color.premult_alpha()*lambda;
198 //return Color::blend(iterr->color,iterl->color,lambda,Color::BLEND_STRAIGHT);
202 const_iterator i = iterl, ie = iterr+1;
205 ColorAccumulator clast,cwork;
207 const Real lambda = (x - iterl->pos)/(iterr->pos - iterl->pos);
209 //premultiply because that's the form in which we can combine things...
210 clast = iterl->color.premult_alpha()*(1-lambda) + iterr->color.premult_alpha()*lambda;
211 //Color::blend((i+1)->color,i->color,(left - i->pos)/((i+1)->pos - i->pos),Color::BLEND_STRAIGHT);
214 ColorAccumulator accum = 0;
216 //loop through all the trapezoids and integrate them as we go...
217 // area of trap = (yi + yi1)*(xi1 - xi)
218 // yi = clast, xi = wlast, yi1 = i->color, xi1 = i->pos
220 for(;i<=iterr; wlast=i->pos,clast=i->color.premult_alpha(),++i)
222 const Real diff = i->pos - wlast;
223 if(diff > 0) //only accumulate if there will be area to add
225 cwork = i->color.premult_alpha();
226 accum += (cwork + clast)*diff;
231 const_iterator ibef = i-1;
232 const Real diff = right - ibef->pos;
236 const Real lambda = diff/(i->pos - ibef->pos);
237 cwork = ibef->color.premult_alpha()*(1-lambda) + i->color.premult_alpha()*lambda;
239 accum += (cwork + clast)*diff; //can probably optimize this more... but it's not too bad
243 accum /= supersample; //should be the total area it was sampled over...
244 return accum.demult_alpha();
247 next=begin(),iter=next++;
249 //add for optimization
250 next = binary_find(begin(),end(),(Real)begin_sample);
253 //! As a future optimization, this could be performed faster
254 //! using a binary search.
255 for(;iter<end();iter=next++)
257 if(next==end() || x>=iter->pos && x<next->pos && iter->pos!=next->pos)
259 // If the supersample region falls square in between
260 // two CPoints, then we don't have to do anything special.
261 if(next!=end() && (!supersample || (iter->pos<=begin_sample && next->pos>=end_sample)))
263 const Real dist(next->pos-iter->pos);
264 const Real pos(x-iter->pos);
265 const Real amount(pos/dist);
266 return Color::blend(next->color,iter->color, amount, Color::BLEND_STRAIGHT);
268 // In this case our supersample region extends over one or more
269 // CPoints. So, we need to calculate our coverage amount.
270 ColorAccumulator pool(Color::alpha());
271 float divisor(0.0),weight(0);
273 const_iterator iter2,next2;
275 if(iter==begin() && iter->pos>x)
278 //weight*=iter->color.get_a();
279 pool+=ColorAccumulator(iter->color).premult_alpha()*weight;
284 while(iter2->pos>=begin_sample)
288 weight=iter2->pos-(begin_sample);
289 //weight*=iter2->color.get_a();
290 pool+=ColorAccumulator(iter2->color).premult_alpha()*weight;
295 pool+=supersample_helper(*iter2, *next2, begin_sample, end_sample, weight);
302 while(iter2->pos<=end_sample)
306 weight=(end_sample)-iter2->pos;
307 pool+=ColorAccumulator(iter2->color).premult_alpha()*weight;
311 pool+=supersample_helper(*iter2, *next2, begin_sample, end_sample, weight);
316 if(divisor && pool.get_a() && pool.is_valid())
319 pool.set_r(pool.get_r()/pool.get_a());
320 pool.set_g(pool.get_g()/pool.get_a());
321 pool.set_b(pool.get_b()/pool.get_a());
322 pool.set_a(pool.get_a()/divisor);
325 pool.set_r(pool.get_r()/pool.get_a());
326 pool.set_g(pool.get_g()/pool.get_a());
327 pool.set_b(pool.get_b()/pool.get_a());
331 return Color::alpha();
334 return Color::alpha();
338 // We should never get to this point.
340 synfig::error("synfig::Gradient::operator()(): Logic Error (x=%f)",x);
342 throw std::logic_error(strprintf("synfig::Gradient::operator()(): Logic Error (x=%f)",x));
345 synfig::Gradient::iterator
346 synfig::Gradient::proximity(const Real &x)
349 float dist(100000000);
350 float prev_pos(-0230);
351 // This algorithm requires a sorted list.
352 for(iter=begin();iter<end();iter++)
356 if(prev_pos==iter->pos)
357 new_dist=(abs(x-iter->pos-0.00001));
359 new_dist=(abs(x-iter->pos));
373 synfig::Gradient::const_iterator
374 synfig::Gradient::proximity(const Real &x)const
376 return const_cast<Gradient*>(this)->proximity(x);
379 float dist(100000000);
381 // This algorithm requires a sorted list.
382 for(iter=begin();iter<end();iter++)
384 const float new_dist(abs(x-iter->pos));
397 synfig::Gradient::iterator
398 synfig::Gradient::find(const UniqueID &id)
402 for(iter=begin();iter<end();iter++)
408 throw Exception::NotFound("synfig::Gradient::find(): Unable to find UniqueID in gradient");
411 synfig::Gradient::const_iterator
412 synfig::Gradient::find(const UniqueID &id)const
416 for(iter=begin();iter<end();iter++)
422 throw Exception::NotFound("synfig::Gradient::find()const: Unable to find UniqueID in gradient");