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)
216 for (iterator iter = cpoints.begin(); iter!=cpoints.end(); iter++)
217 (*iter).color *= rhs;
222 synfig::Gradient::operator/=(const float &rhs)
224 for (iterator iter = cpoints.begin(); iter!=cpoints.end(); iter++)
225 (*iter).color /= rhs;
230 synfig::Gradient::operator()(const Real &x,float supersample)const
233 return Color(0,0,0,0);
235 supersample=-supersample;
239 float begin_sample(x-supersample*0.5);
240 float end_sample(x+supersample*0.5);
242 if(cpoints.size()==1 || end_sample<=cpoints.front().pos || isnan(x))
243 return cpoints.front().color;
245 if(begin_sample>=cpoints.back().pos)
246 return cpoints.back().color;
249 if(end_sample>=back().pos)
250 end_sample=back().pos;
252 if(begin_sample<=front().pos)
253 begin_sample=front().pos;
256 const_iterator iter,next;
260 Real left = x-supersample/2, right = x+supersample/2;
262 if(left < front().pos) left = front().pos;
263 if(right > back().pos) right = back().pos;
265 //find using binary search...
266 const_iterator iterl,iterr;
268 //the binary search should give us the values BEFORE the point we're looking for...
269 iterl = binary_find(begin(),end(),left);
270 iterr = binary_find(iterl,end(),right);
272 //now integrate over the range of left to right...
276 iterr++; //let's look at the next one shall we :)
278 //interpolate neighboring colors
279 const Real one = iterr->pos - iterl->pos;
280 const Real lambda = (x - iterl->pos)/one;
282 //(1-l)iterl + (l)iterr
283 return iterl->color.premult_alpha()*(1-lambda) + iterr->color.premult_alpha()*lambda;
285 //return Color::blend(iterr->color,iterl->color,lambda,Color::BLEND_STRAIGHT);
289 const_iterator i = iterl, ie = iterr+1;
292 ColorAccumulator clast,cwork;
294 const Real lambda = (x - iterl->pos)/(iterr->pos - iterl->pos);
296 //premultiply because that's the form in which we can combine things...
297 clast = iterl->color.premult_alpha()*(1-lambda) + iterr->color.premult_alpha()*lambda;
298 //Color::blend((i+1)->color,i->color,(left - i->pos)/((i+1)->pos - i->pos),Color::BLEND_STRAIGHT);
301 ColorAccumulator accum = 0;
303 //loop through all the trapezoids and integrate them as we go...
304 // area of trap = (yi + yi1)*(xi1 - xi)
305 // yi = clast, xi = wlast, yi1 = i->color, xi1 = i->pos
307 for(;i<=iterr; wlast=i->pos,clast=i->color.premult_alpha(),++i)
309 const Real diff = i->pos - wlast;
310 if(diff > 0) //only accumulate if there will be area to add
312 cwork = i->color.premult_alpha();
313 accum += (cwork + clast)*diff;
318 const_iterator ibef = i-1;
319 const Real diff = right - ibef->pos;
323 const Real lambda = diff/(i->pos - ibef->pos);
324 cwork = ibef->color.premult_alpha()*(1-lambda) + i->color.premult_alpha()*lambda;
326 accum += (cwork + clast)*diff; //can probably optimize this more... but it's not too bad
330 accum /= supersample; //should be the total area it was sampled over...
331 return accum.demult_alpha();
334 next=begin(),iter=next++;
336 //add for optimization
337 next = binary_find(begin(),end(),(Real)begin_sample);
340 //! As a future optimization, this could be performed faster
341 //! using a binary search.
342 for(;iter<end();iter=next++)
344 if(next==end() || x>=iter->pos && x<next->pos && iter->pos!=next->pos)
346 // If the supersample region falls square in between
347 // two CPoints, then we don't have to do anything special.
348 if(next!=end() && (!supersample || (iter->pos<=begin_sample && next->pos>=end_sample)))
350 const Real dist(next->pos-iter->pos);
351 const Real pos(x-iter->pos);
352 const Real amount(pos/dist);
353 return Color::blend(next->color,iter->color, amount, Color::BLEND_STRAIGHT);
355 // In this case our supersample region extends over one or more
356 // CPoints. So, we need to calculate our coverage amount.
357 ColorAccumulator pool(Color::alpha());
358 float divisor(0.0),weight(0);
360 const_iterator iter2,next2;
362 if(iter==begin() && iter->pos>x)
365 //weight*=iter->color.get_a();
366 pool+=ColorAccumulator(iter->color).premult_alpha()*weight;
371 while(iter2->pos>=begin_sample)
375 weight=iter2->pos-(begin_sample);
376 //weight*=iter2->color.get_a();
377 pool+=ColorAccumulator(iter2->color).premult_alpha()*weight;
382 pool+=supersample_helper(*iter2, *next2, begin_sample, end_sample, weight);
389 while(iter2->pos<=end_sample)
393 weight=(end_sample)-iter2->pos;
394 pool+=ColorAccumulator(iter2->color).premult_alpha()*weight;
398 pool+=supersample_helper(*iter2, *next2, begin_sample, end_sample, weight);
403 if(divisor && pool.get_a() && pool.is_valid())
406 pool.set_r(pool.get_r()/pool.get_a());
407 pool.set_g(pool.get_g()/pool.get_a());
408 pool.set_b(pool.get_b()/pool.get_a());
409 pool.set_a(pool.get_a()/divisor);
412 pool.set_r(pool.get_r()/pool.get_a());
413 pool.set_g(pool.get_g()/pool.get_a());
414 pool.set_b(pool.get_b()/pool.get_a());
418 return Color::alpha();
421 return Color::alpha();
425 // We should never get to this point.
427 synfig::error("synfig::Gradient::operator()(): Logic Error (x=%f)",x);
429 throw std::logic_error(strprintf("synfig::Gradient::operator()(): Logic Error (x=%f)",x));
432 synfig::Gradient::iterator
433 synfig::Gradient::proximity(const Real &x)
436 float dist(100000000);
437 float prev_pos(-0230);
438 // This algorithm requires a sorted list.
439 for(iter=begin();iter<end();iter++)
443 if(prev_pos==iter->pos)
444 new_dist=(abs(x-iter->pos-0.00001));
446 new_dist=(abs(x-iter->pos));
460 synfig::Gradient::const_iterator
461 synfig::Gradient::proximity(const Real &x)const
463 return const_cast<Gradient*>(this)->proximity(x);
466 float dist(100000000);
468 // This algorithm requires a sorted list.
469 for(iter=begin();iter<end();iter++)
471 const float new_dist(abs(x-iter->pos));
484 synfig::Gradient::iterator
485 synfig::Gradient::find(const UniqueID &id)
489 for(iter=begin();iter<end();iter++)
495 throw Exception::NotFound("synfig::Gradient::find(): Unable to find UniqueID in gradient");
498 synfig::Gradient::const_iterator
499 synfig::Gradient::find(const UniqueID &id)const
503 for(iter=begin();iter<end();iter++)
509 throw Exception::NotFound("synfig::Gradient::find()const: Unable to find UniqueID in gradient");