1 /* === S Y N F I G ========================================================= */
3 ** \brief Color Gradient Class Member Definitions
5 ** $Id: gradient.cpp,v 1.2 2005/01/21 19:29:10 darco Exp $
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);
111 ret.set_r(ret.get_r()*ret.get_a());
112 ret.set_g(ret.get_g()*ret.get_a());
113 ret.set_b(ret.get_b()*ret.get_a());
116 if(color1.pos>=begin && color2.pos>=end)
118 weight=end-color1.pos;
119 float pos((end+color1.pos)*0.5);
120 float amount((pos-color1.pos)/(color2.pos-color1.pos));
121 //if(abs(amount)>1)amount=(amount>0)?1:-1;
122 ColorAccumulator ret(Color::blend(color2.color,color1.color, amount, Color::BLEND_STRAIGHT));
123 ret.set_r(ret.get_r()*ret.get_a());
124 ret.set_g(ret.get_g()*ret.get_a());
125 ret.set_b(ret.get_b()*ret.get_a());
128 if(color1.pos<begin && color2.pos<end)
130 weight=color2.pos-begin;
131 float pos((begin+color2.pos)*0.5);
132 float amount((pos-color1.pos)/(color2.pos-color1.pos));
133 //if(abs(amount)>1)amount=(amount>0)?1:-1;
134 ColorAccumulator ret(Color::blend(color2.color,color1.color, amount, Color::BLEND_STRAIGHT));
135 ret.set_r(ret.get_r()*ret.get_a());
136 ret.set_g(ret.get_g()*ret.get_a());
137 ret.set_b(ret.get_b()*ret.get_a());
140 synfig::error("color1.pos=%f",color1.pos);
141 synfig::error("color2.pos=%f",color2.pos);
142 synfig::error("begin=%f",begin);
143 synfig::error("end=%f",end);
146 return Color::alpha();
152 synfig::Gradient::operator()(const Real &x,float supersample)const
155 return Color(0,0,0,0);
157 supersample=-supersample;
161 float begin_sample(x-supersample*0.5);
162 float end_sample(x+supersample*0.5);
164 if(size()==1 || end_sample<=front().pos || isnan(x))
165 return front().color;
167 if(begin_sample>=back().pos)
171 if(end_sample>=back().pos)
172 end_sample=back().pos;
174 if(begin_sample<=front().pos)
175 begin_sample=front().pos;
178 const_iterator iter,next;
182 Real left = x-supersample/2, right = x+supersample/2;
184 if(left < front().pos) left = front().pos;
185 if(right > back().pos) right = back().pos;
187 //find using binary search...
188 const_iterator iterl,iterr;
190 //the binary search should give us the values BEFORE the point we're looking for...
191 iterl = binary_find(begin(),end(),left);
192 iterr = binary_find(iterl,end(),right);
194 //now integrate over the range of left to right...
198 iterr++; //let's look at the next one shall we :)
200 //interpolate neighboring colors
201 const Real one = iterr->pos - iterl->pos;
202 const Real lambda = (x - iterl->pos)/one;
204 //(1-l)iterl + (l)iterr
205 return iterl->color.premult_alpha()*(1-lambda) + iterr->color.premult_alpha()*lambda;
207 //return Color::blend(iterr->color,iterl->color,lambda,Color::BLEND_STRAIGHT);
211 const_iterator i = iterl, ie = iterr+1;
214 ColorAccumulator clast,cwork;
216 const Real lambda = (x - iterl->pos)/(iterr->pos - iterl->pos);
218 //premultiply because that's the form in which we can combine things...
219 clast = iterl->color.premult_alpha()*(1-lambda) + iterr->color.premult_alpha()*lambda;
220 //Color::blend((i+1)->color,i->color,(left - i->pos)/((i+1)->pos - i->pos),Color::BLEND_STRAIGHT);
223 ColorAccumulator accum = 0;
225 //loop through all the trapezoids and integrate them as we go...
226 // area of trap = (yi + yi1)*(xi1 - xi)
227 // yi = clast, xi = wlast, yi1 = i->color, xi1 = i->pos
229 for(;i<=iterr; wlast=i->pos,clast=i->color.premult_alpha(),++i)
231 const Real diff = i->pos - wlast;
232 if(diff > 0) //only accumulate if there will be area to add
234 cwork = i->color.premult_alpha();
235 accum += (cwork + clast)*diff;
240 const_iterator ibef = i-1;
241 const Real diff = right - ibef->pos;
245 const Real lambda = diff/(i->pos - ibef->pos);
246 cwork = ibef->color.premult_alpha()*(1-lambda) + i->color.premult_alpha()*lambda;
248 accum += (cwork + clast)*diff; //can probably optimize this more... but it's not too bad
252 accum /= supersample; //should be the total area it was sampled over...
253 return accum.demult_alpha();
256 next=begin(),iter=next++;
258 //add for optimization
259 next = binary_find(begin(),end(),(Real)begin_sample);
262 //! As a future optimization, this could be performed faster
263 //! using a binary search.
264 for(;iter<end();iter=next++)
266 if(next==end() || x>=iter->pos && x<next->pos && iter->pos!=next->pos)
268 // If the supersample region falls square in between
269 // two CPoints, then we don't have to do anything special.
270 if(next!=end() && (!supersample || (iter->pos<=begin_sample && next->pos>=end_sample)))
272 const Real dist(next->pos-iter->pos);
273 const Real pos(x-iter->pos);
274 const Real amount(pos/dist);
275 return Color::blend(next->color,iter->color, amount, Color::BLEND_STRAIGHT);
277 // In this case our supersample region extends over one or more
278 // CPoints. So, we need to calculate our coverage amount.
279 ColorAccumulator pool(Color::alpha());
280 float divisor(0.0),weight(0);
282 const_iterator iter2,next2;
284 if(iter==begin() && iter->pos>x)
287 //weight*=iter->color.get_a();
288 pool+=ColorAccumulator(iter->color)*(float)iter->color.get_a()*weight;
293 while(iter2->pos>=begin_sample)
297 weight=iter2->pos-(begin_sample);
298 //weight*=iter2->color.get_a();
299 pool+=ColorAccumulator(iter2->color)*(float)iter2->color.get_a()*weight;
304 pool+=supersample_helper(*iter2, *next2, begin_sample, end_sample, weight);
311 while(iter2->pos<=end_sample)
315 weight=(end_sample)-iter2->pos;
316 pool+=ColorAccumulator(iter2->color)*(float)iter2->color.get_a()*weight;
320 pool+=supersample_helper(*iter2, *next2, begin_sample, end_sample, weight);
325 if(divisor && pool.get_a() && pool.is_valid())
328 pool.set_r(pool.get_r()/pool.get_a());
329 pool.set_g(pool.get_g()/pool.get_a());
330 pool.set_b(pool.get_b()/pool.get_a());
331 pool.set_a(pool.get_a()/divisor);
334 pool.set_r(pool.get_r()/pool.get_a());
335 pool.set_g(pool.get_g()/pool.get_a());
336 pool.set_b(pool.get_b()/pool.get_a());
340 return Color::alpha();
343 return Color::alpha();
347 // We should never get to this point.
349 synfig::error("synfig::Gradient::operator()(): Logic Error (x=%f)",x);
351 throw std::logic_error(strprintf("synfig::Gradient::operator()(): Logic Error (x=%f)",x));
354 synfig::Gradient::iterator
355 synfig::Gradient::proximity(const Real &x)
358 float dist(100000000);
359 float prev_pos(-0230);
360 // This algorithm requires a sorted list.
361 for(iter=begin();iter<end();iter++)
365 if(prev_pos==iter->pos)
366 new_dist=(abs(x-iter->pos-0.00001));
368 new_dist=(abs(x-iter->pos));
382 synfig::Gradient::const_iterator
383 synfig::Gradient::proximity(const Real &x)const
385 return const_cast<Gradient*>(this)->proximity(x);
388 float dist(100000000);
390 // This algorithm requires a sorted list.
391 for(iter=begin();iter<end();iter++)
393 const float new_dist(abs(x-iter->pos));
406 synfig::Gradient::iterator
407 synfig::Gradient::find(const UniqueID &id)
411 for(iter=begin();iter<end();iter++)
417 throw Exception::NotFound("synfig::Gradient::find(): Unable to find UniqueID in gradient");
420 synfig::Gradient::const_iterator
421 synfig::Gradient::find(const UniqueID &id)const
425 for(iter=begin();iter<end();iter++)
431 throw Exception::NotFound("synfig::Gradient::find()const: Unable to find UniqueID in gradient");