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 Real &x,float supersample)const
147 return Color(0,0,0,0);
149 supersample=-supersample;
153 float begin_sample(x-supersample*0.5);
154 float end_sample(x+supersample*0.5);
156 if(size()==1 || end_sample<=front().pos || isnan(x))
157 return front().color;
159 if(begin_sample>=back().pos)
163 if(end_sample>=back().pos)
164 end_sample=back().pos;
166 if(begin_sample<=front().pos)
167 begin_sample=front().pos;
170 const_iterator iter,next;
174 Real left = x-supersample/2, right = x+supersample/2;
176 if(left < front().pos) left = front().pos;
177 if(right > back().pos) right = back().pos;
179 //find using binary search...
180 const_iterator iterl,iterr;
182 //the binary search should give us the values BEFORE the point we're looking for...
183 iterl = binary_find(begin(),end(),left);
184 iterr = binary_find(iterl,end(),right);
186 //now integrate over the range of left to right...
190 iterr++; //let's look at the next one shall we :)
192 //interpolate neighboring colors
193 const Real one = iterr->pos - iterl->pos;
194 const Real lambda = (x - iterl->pos)/one;
196 //(1-l)iterl + (l)iterr
197 return iterl->color.premult_alpha()*(1-lambda) + iterr->color.premult_alpha()*lambda;
199 //return Color::blend(iterr->color,iterl->color,lambda,Color::BLEND_STRAIGHT);
203 const_iterator i = iterl, ie = iterr+1;
206 ColorAccumulator clast,cwork;
208 const Real lambda = (x - iterl->pos)/(iterr->pos - iterl->pos);
210 //premultiply because that's the form in which we can combine things...
211 clast = iterl->color.premult_alpha()*(1-lambda) + iterr->color.premult_alpha()*lambda;
212 //Color::blend((i+1)->color,i->color,(left - i->pos)/((i+1)->pos - i->pos),Color::BLEND_STRAIGHT);
215 ColorAccumulator accum = 0;
217 //loop through all the trapezoids and integrate them as we go...
218 // area of trap = (yi + yi1)*(xi1 - xi)
219 // yi = clast, xi = wlast, yi1 = i->color, xi1 = i->pos
221 for(;i<=iterr; wlast=i->pos,clast=i->color.premult_alpha(),++i)
223 const Real diff = i->pos - wlast;
224 if(diff > 0) //only accumulate if there will be area to add
226 cwork = i->color.premult_alpha();
227 accum += (cwork + clast)*diff;
232 const_iterator ibef = i-1;
233 const Real diff = right - ibef->pos;
237 const Real lambda = diff/(i->pos - ibef->pos);
238 cwork = ibef->color.premult_alpha()*(1-lambda) + i->color.premult_alpha()*lambda;
240 accum += (cwork + clast)*diff; //can probably optimize this more... but it's not too bad
244 accum /= supersample; //should be the total area it was sampled over...
245 return accum.demult_alpha();
248 next=begin(),iter=next++;
250 //add for optimization
251 next = binary_find(begin(),end(),(Real)begin_sample);
254 //! As a future optimization, this could be performed faster
255 //! using a binary search.
256 for(;iter<end();iter=next++)
258 if(next==end() || x>=iter->pos && x<next->pos && iter->pos!=next->pos)
260 // If the supersample region falls square in between
261 // two CPoints, then we don't have to do anything special.
262 if(next!=end() && (!supersample || (iter->pos<=begin_sample && next->pos>=end_sample)))
264 const Real dist(next->pos-iter->pos);
265 const Real pos(x-iter->pos);
266 const Real amount(pos/dist);
267 return Color::blend(next->color,iter->color, amount, Color::BLEND_STRAIGHT);
269 // In this case our supersample region extends over one or more
270 // CPoints. So, we need to calculate our coverage amount.
271 ColorAccumulator pool(Color::alpha());
272 float divisor(0.0),weight(0);
274 const_iterator iter2,next2;
276 if(iter==begin() && iter->pos>x)
279 //weight*=iter->color.get_a();
280 pool+=ColorAccumulator(iter->color).premult_alpha()*weight;
285 while(iter2->pos>=begin_sample)
289 weight=iter2->pos-(begin_sample);
290 //weight*=iter2->color.get_a();
291 pool+=ColorAccumulator(iter2->color).premult_alpha()*weight;
296 pool+=supersample_helper(*iter2, *next2, begin_sample, end_sample, weight);
303 while(iter2->pos<=end_sample)
307 weight=(end_sample)-iter2->pos;
308 pool+=ColorAccumulator(iter2->color).premult_alpha()*weight;
312 pool+=supersample_helper(*iter2, *next2, begin_sample, end_sample, weight);
317 if(divisor && pool.get_a() && pool.is_valid())
320 pool.set_r(pool.get_r()/pool.get_a());
321 pool.set_g(pool.get_g()/pool.get_a());
322 pool.set_b(pool.get_b()/pool.get_a());
323 pool.set_a(pool.get_a()/divisor);
326 pool.set_r(pool.get_r()/pool.get_a());
327 pool.set_g(pool.get_g()/pool.get_a());
328 pool.set_b(pool.get_b()/pool.get_a());
332 return Color::alpha();
335 return Color::alpha();
339 // We should never get to this point.
341 synfig::error("synfig::Gradient::operator()(): Logic Error (x=%f)",x);
343 throw std::logic_error(strprintf("synfig::Gradient::operator()(): Logic Error (x=%f)",x));
346 synfig::Gradient::iterator
347 synfig::Gradient::proximity(const Real &x)
350 float dist(100000000);
351 float prev_pos(-0230);
352 // This algorithm requires a sorted list.
353 for(iter=begin();iter<end();iter++)
357 if(prev_pos==iter->pos)
358 new_dist=(abs(x-iter->pos-0.00001));
360 new_dist=(abs(x-iter->pos));
374 synfig::Gradient::const_iterator
375 synfig::Gradient::proximity(const Real &x)const
377 return const_cast<Gradient*>(this)->proximity(x);
380 float dist(100000000);
382 // This algorithm requires a sorted list.
383 for(iter=begin();iter<end();iter++)
385 const float new_dist(abs(x-iter->pos));
398 synfig::Gradient::iterator
399 synfig::Gradient::find(const UniqueID &id)
403 for(iter=begin();iter<end();iter++)
409 throw Exception::NotFound("synfig::Gradient::find(): Unable to find UniqueID in gradient");
412 synfig::Gradient::const_iterator
413 synfig::Gradient::find(const UniqueID &id)const
417 for(iter=begin();iter<end();iter++)
423 throw Exception::NotFound("synfig::Gradient::find()const: Unable to find UniqueID in gradient");