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"
41 /* === U S I N G =========================================================== */
45 using namespace synfig;
47 /* === M A C R O S ========================================================= */
49 /* === G L O B A L S ======================================================= */
51 /* === P R O C E D U R E S ================================================= */
53 /* === M E T H O D S ======================================================= */
55 synfig::Gradient::Gradient(const Color &c1, const Color &c2)
57 push_back(CPoint(0.0,c1));
58 push_back(CPoint(1.0,c2));
61 synfig::Gradient::Gradient(const Color &c1, const Color &c2, const Color &c3)
63 push_back(CPoint(0.0,c1));
64 push_back(CPoint(0.5,c2));
65 push_back(CPoint(1.0,c3));
68 // This sort algorithm MUST be stable
69 // ie: it must not change the order of items with the same value.
70 // I am using a bubble sort.
71 // This algorithm will sort a nearly-sorted list at ~O(N), and
72 // it will sort an inverse sorted list at ~O(N*N).
74 synfig::Gradient::sort()
76 stable_sort(begin(),end());
81 for(iter=begin();iter!=end();iter++)
83 for(next=iter, iter2=next--;iter2!=begin();iter2=next--)
100 static synfig::ColorAccumulator
101 supersample_helper(const synfig::Gradient::CPoint &color1, const synfig::Gradient::CPoint &color2, float begin, float end, float &weight)
103 if(color1.pos==color2.pos || color1.pos>=end || color2.pos<=begin)
106 return Color::alpha();
108 if(color1.pos>=begin && color2.pos<end)
110 weight=color2.pos-color1.pos;
111 ColorAccumulator ret=Color::blend(color2.color,color1.color, 0.5, Color::BLEND_STRAIGHT).premult_alpha()*weight;
114 if(color1.pos>=begin && color2.pos>=end)
116 weight=end-color1.pos;
117 float pos((end+color1.pos)*0.5);
118 float amount((pos-color1.pos)/(color2.pos-color1.pos));
119 //if(abs(amount)>1)amount=(amount>0)?1:-1;
120 ColorAccumulator ret(Color::blend(color2.color,color1.color, amount, Color::BLEND_STRAIGHT).premult_alpha()*weight);
123 if(color1.pos<begin && color2.pos<end)
125 weight=color2.pos-begin;
126 float pos((begin+color2.pos)*0.5);
127 float amount((pos-color1.pos)/(color2.pos-color1.pos));
128 //if(abs(amount)>1)amount=(amount>0)?1:-1;
129 ColorAccumulator ret(Color::blend(color2.color,color1.color, amount, Color::BLEND_STRAIGHT).premult_alpha()*weight);
132 synfig::error("color1.pos=%f",color1.pos);
133 synfig::error("color2.pos=%f",color2.pos);
134 synfig::error("begin=%f",begin);
135 synfig::error("end=%f",end);
138 return Color::alpha();
144 synfig::Gradient::operator+=(const Gradient &rhs)
147 const_iterator iter1 = begin(), iter2 = rhs.begin(), left_same, right_same;
149 if (iter1 != end()) left = *iter1;
150 if (iter2 != rhs.end()) right = *iter2;
151 if (iter1 != end() && iter2 != rhs.end())
154 if (left.pos < right.pos)
156 ret.push_back(CPoint(left.pos, left.color + rhs(left.pos)));
157 if(++iter1 == end()) break;
160 else if (left.pos > right.pos)
162 ret.push_back(CPoint(right.pos, right.color + (*this)(right.pos)));
163 if(++iter2 == rhs.end()) break;
168 for(left_same = iter1++; (*iter1).pos == left.pos; iter1++);
169 for(right_same = iter2++; (*iter2).pos == right.pos; iter2++);
170 if (iter1 == left_same+1 && iter2 == right_same+1)
171 ret.push_back(CPoint(left.pos, left.color + right.color));
174 ret.push_back(CPoint(left.pos, left.color + right.color));
175 ret.push_back(CPoint(left.pos, (*(iter1-1)).color + (*(iter2-1)).color));
177 if (iter1 != end()) left=*iter1;
178 if (iter2 == rhs.end()) break;
180 if (iter1 == end()) break;
187 ret.push_back(CPoint(left.pos, left.color + rhs(left.pos)));
188 if(++iter1 == end()) break;
192 if (iter2 != rhs.end())
195 ret.push_back(CPoint(right.pos, right.color + (*this)(right.pos)));
196 if(++iter2 == rhs.end()) break;
205 synfig::Gradient::operator-=(const Gradient &rhs)
207 return (*this)+=(rhs*-1);
211 synfig::Gradient::operator*=(const float &rhs)
213 for (iterator iter = cpoints.begin(); iter!=cpoints.end(); iter++)
214 (*iter).color *= rhs;
219 synfig::Gradient::operator/=(const float &rhs)
221 for (iterator iter = cpoints.begin(); iter!=cpoints.end(); iter++)
222 (*iter).color /= rhs;
227 synfig::Gradient::operator()(const Real &x,float supersample)const
230 return Color(0,0,0,0);
232 supersample=-supersample;
236 float begin_sample(x-supersample*0.5);
237 float end_sample(x+supersample*0.5);
239 if(cpoints.size()==1 || end_sample<=cpoints.front().pos || isnan(x))
240 return cpoints.front().color;
242 if(begin_sample>=cpoints.back().pos)
243 return cpoints.back().color;
246 if(end_sample>=back().pos)
247 end_sample=back().pos;
249 if(begin_sample<=front().pos)
250 begin_sample=front().pos;
253 const_iterator iter,next;
257 Real left = x-supersample/2, right = x+supersample/2;
259 if(left < front().pos) left = front().pos;
260 if(right > back().pos) right = back().pos;
262 //find using binary search...
263 const_iterator iterl,iterr;
265 //the binary search should give us the values BEFORE the point we're looking for...
266 iterl = binary_find(begin(),end(),left);
267 iterr = binary_find(iterl,end(),right);
269 //now integrate over the range of left to right...
273 iterr++; //let's look at the next one shall we :)
275 //interpolate neighboring colors
276 const Real one = iterr->pos - iterl->pos;
277 const Real lambda = (x - iterl->pos)/one;
279 //(1-l)iterl + (l)iterr
280 return iterl->color.premult_alpha()*(1-lambda) + iterr->color.premult_alpha()*lambda;
282 //return Color::blend(iterr->color,iterl->color,lambda,Color::BLEND_STRAIGHT);
286 const_iterator i = iterl, ie = iterr+1;
289 ColorAccumulator clast,cwork;
291 const Real lambda = (x - iterl->pos)/(iterr->pos - iterl->pos);
293 //premultiply because that's the form in which we can combine things...
294 clast = iterl->color.premult_alpha()*(1-lambda) + iterr->color.premult_alpha()*lambda;
295 //Color::blend((i+1)->color,i->color,(left - i->pos)/((i+1)->pos - i->pos),Color::BLEND_STRAIGHT);
298 ColorAccumulator accum = 0;
300 //loop through all the trapezoids and integrate them as we go...
301 // area of trap = (yi + yi1)*(xi1 - xi)
302 // yi = clast, xi = wlast, yi1 = i->color, xi1 = i->pos
304 for(;i<=iterr; wlast=i->pos,clast=i->color.premult_alpha(),++i)
306 const Real diff = i->pos - wlast;
307 if(diff > 0) //only accumulate if there will be area to add
309 cwork = i->color.premult_alpha();
310 accum += (cwork + clast)*diff;
315 const_iterator ibef = i-1;
316 const Real diff = right - ibef->pos;
320 const Real lambda = diff/(i->pos - ibef->pos);
321 cwork = ibef->color.premult_alpha()*(1-lambda) + i->color.premult_alpha()*lambda;
323 accum += (cwork + clast)*diff; //can probably optimize this more... but it's not too bad
327 accum /= supersample; //should be the total area it was sampled over...
328 return accum.demult_alpha();
331 next=begin(),iter=next++;
333 //add for optimization
334 next = binary_find(begin(),end(),(Real)begin_sample);
337 //! As a future optimization, this could be performed faster
338 //! using a binary search.
339 for(;iter<end();iter=next++)
341 if(next==end() || x>=iter->pos && x<next->pos && iter->pos!=next->pos)
343 // If the supersample region falls square in between
344 // two CPoints, then we don't have to do anything special.
345 if(next!=end() && (!supersample || (iter->pos<=begin_sample && next->pos>=end_sample)))
347 const Real dist(next->pos-iter->pos);
348 const Real pos(x-iter->pos);
349 const Real amount(pos/dist);
350 return Color::blend(next->color,iter->color, amount, Color::BLEND_STRAIGHT);
352 // In this case our supersample region extends over one or more
353 // CPoints. So, we need to calculate our coverage amount.
354 ColorAccumulator pool(Color::alpha());
355 float divisor(0.0),weight(0);
357 const_iterator iter2,next2;
359 if(iter==begin() && iter->pos>x)
362 //weight*=iter->color.get_a();
363 pool+=ColorAccumulator(iter->color).premult_alpha()*weight;
368 while(iter2->pos>=begin_sample)
372 weight=iter2->pos-(begin_sample);
373 //weight*=iter2->color.get_a();
374 pool+=ColorAccumulator(iter2->color).premult_alpha()*weight;
379 pool+=supersample_helper(*iter2, *next2, begin_sample, end_sample, weight);
386 while(iter2->pos<=end_sample)
390 weight=(end_sample)-iter2->pos;
391 pool+=ColorAccumulator(iter2->color).premult_alpha()*weight;
395 pool+=supersample_helper(*iter2, *next2, begin_sample, end_sample, weight);
400 if(divisor && pool.get_a() && pool.is_valid())
403 pool.set_r(pool.get_r()/pool.get_a());
404 pool.set_g(pool.get_g()/pool.get_a());
405 pool.set_b(pool.get_b()/pool.get_a());
406 pool.set_a(pool.get_a()/divisor);
409 pool.set_r(pool.get_r()/pool.get_a());
410 pool.set_g(pool.get_g()/pool.get_a());
411 pool.set_b(pool.get_b()/pool.get_a());
415 return Color::alpha();
418 return Color::alpha();
422 // We should never get to this point.
424 synfig::error("synfig::Gradient::operator()(): Logic Error (x=%f)",x);
426 throw std::logic_error(strprintf("synfig::Gradient::operator()(): Logic Error (x=%f)",x));
429 synfig::Gradient::iterator
430 synfig::Gradient::proximity(const Real &x)
433 float dist(100000000);
434 float prev_pos(-0230);
435 // This algorithm requires a sorted list.
436 for(iter=begin();iter<end();iter++)
440 if(prev_pos==iter->pos)
441 new_dist=(abs(x-iter->pos-0.00001));
443 new_dist=(abs(x-iter->pos));
457 synfig::Gradient::const_iterator
458 synfig::Gradient::proximity(const Real &x)const
460 return const_cast<Gradient*>(this)->proximity(x);
463 float dist(100000000);
465 // This algorithm requires a sorted list.
466 for(iter=begin();iter<end();iter++)
468 const float new_dist(abs(x-iter->pos));
481 synfig::Gradient::iterator
482 synfig::Gradient::find(const UniqueID &id)
486 for(iter=begin();iter<end();iter++)
492 throw Exception::NotFound("synfig::Gradient::find(): Unable to find UniqueID in gradient");
495 synfig::Gradient::const_iterator
496 synfig::Gradient::find(const UniqueID &id)const
500 for(iter=begin();iter<end();iter++)
506 throw Exception::NotFound("synfig::Gradient::find()const: Unable to find UniqueID in gradient");