1 /* === S I N F 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 Robert B. Quattlebaum Jr.
10 ** This software and associated documentation
11 ** are CONFIDENTIAL and PROPRIETARY property of
12 ** the above-mentioned copyright holder.
14 ** You may not copy, print, publish, or in any
15 ** other way distribute this software without
16 ** a prior written agreement with
17 ** the copyright holder.
20 /* ========================================================================= */
22 /* === H E A D E R S ======================================================= */
34 #include "exception.h"
39 /* === U S I N G =========================================================== */
43 using namespace sinfg;
45 /* === M A C R O S ========================================================= */
47 /* === G L O B A L S ======================================================= */
49 /* === P R O C E D U R E S ================================================= */
51 /* === M E T H O D S ======================================================= */
53 sinfg::Gradient::Gradient(const Color &c1, const Color &c2)
55 push_back(CPoint(0.0,c1));
56 push_back(CPoint(1.0,c2));
59 sinfg::Gradient::Gradient(const Color &c1, const Color &c2, const Color &c3)
61 push_back(CPoint(0.0,c1));
62 push_back(CPoint(0.5,c2));
63 push_back(CPoint(1.0,c3));
66 // This sort algorithm MUST be stable
67 // ie: it must not change the order of items with the same value.
68 // I am using a bubble sort.
69 // This algorithm will sort a nearly-sorted list at ~O(N), and
70 // it will sort an inverse sorted list at ~O(N*N).
72 sinfg::Gradient::sort()
74 stable_sort(begin(),end());
79 for(iter=begin();iter!=end();iter++)
81 for(next=iter, iter2=next--;iter2!=begin();iter2=next--)
98 static sinfg::ColorAccumulator
99 supersample_helper(const sinfg::Gradient::CPoint &color1, const sinfg::Gradient::CPoint &color2, float begin, float end, float &weight)
101 if(color1.pos==color2.pos || color1.pos>=end || color2.pos<=begin)
104 return Color::alpha();
106 if(color1.pos>=begin && color2.pos<end)
108 weight=color2.pos-color1.pos;
109 ColorAccumulator ret=Color::blend(color2.color,color1.color, 0.5, Color::BLEND_STRAIGHT);
110 ret.set_r(ret.get_r()*ret.get_a());
111 ret.set_g(ret.get_g()*ret.get_a());
112 ret.set_b(ret.get_b()*ret.get_a());
115 if(color1.pos>=begin && color2.pos>=end)
117 weight=end-color1.pos;
118 float pos((end+color1.pos)*0.5);
119 float amount((pos-color1.pos)/(color2.pos-color1.pos));
120 //if(abs(amount)>1)amount=(amount>0)?1:-1;
121 ColorAccumulator ret(Color::blend(color2.color,color1.color, amount, Color::BLEND_STRAIGHT));
122 ret.set_r(ret.get_r()*ret.get_a());
123 ret.set_g(ret.get_g()*ret.get_a());
124 ret.set_b(ret.get_b()*ret.get_a());
127 if(color1.pos<begin && color2.pos<end)
129 weight=color2.pos-begin;
130 float pos((begin+color2.pos)*0.5);
131 float amount((pos-color1.pos)/(color2.pos-color1.pos));
132 //if(abs(amount)>1)amount=(amount>0)?1:-1;
133 ColorAccumulator ret(Color::blend(color2.color,color1.color, amount, Color::BLEND_STRAIGHT));
134 ret.set_r(ret.get_r()*ret.get_a());
135 ret.set_g(ret.get_g()*ret.get_a());
136 ret.set_b(ret.get_b()*ret.get_a());
139 sinfg::error("color1.pos=%f",color1.pos);
140 sinfg::error("color2.pos=%f",color2.pos);
141 sinfg::error("begin=%f",begin);
142 sinfg::error("end=%f",end);
145 return Color::alpha();
151 sinfg::Gradient::operator()(const Real &x,float supersample)const
154 return Color(0,0,0,0);
156 supersample=-supersample;
160 float begin_sample(x-supersample*0.5);
161 float end_sample(x+supersample*0.5);
163 if(size()==1 || end_sample<=front().pos || isnan(x))
164 return front().color;
166 if(begin_sample>=back().pos)
170 if(end_sample>=back().pos)
171 end_sample=back().pos;
173 if(begin_sample<=front().pos)
174 begin_sample=front().pos;
177 const_iterator iter,next;
181 Real left = x-supersample/2, right = x+supersample/2;
183 if(left < front().pos) left = front().pos;
184 if(right > back().pos) right = back().pos;
186 //find using binary search...
187 const_iterator iterl,iterr;
189 //the binary search should give us the values BEFORE the point we're looking for...
190 iterl = binary_find(begin(),end(),left);
191 iterr = binary_find(iterl,end(),right);
193 //now integrate over the range of left to right...
197 iterr++; //let's look at the next one shall we :)
199 //interpolate neighboring colors
200 const Real one = iterr->pos - iterl->pos;
201 const Real lambda = (x - iterl->pos)/one;
203 //(1-l)iterl + (l)iterr
204 return iterl->color.premult_alpha()*(1-lambda) + iterr->color.premult_alpha()*lambda;
206 //return Color::blend(iterr->color,iterl->color,lambda,Color::BLEND_STRAIGHT);
210 const_iterator i = iterl, ie = iterr+1;
213 ColorAccumulator clast,cwork;
215 const Real lambda = (x - iterl->pos)/(iterr->pos - iterl->pos);
217 //premultiply because that's the form in which we can combine things...
218 clast = iterl->color.premult_alpha()*(1-lambda) + iterr->color.premult_alpha()*lambda;
219 //Color::blend((i+1)->color,i->color,(left - i->pos)/((i+1)->pos - i->pos),Color::BLEND_STRAIGHT);
222 ColorAccumulator accum = 0;
224 //loop through all the trapezoids and integrate them as we go...
225 // area of trap = (yi + yi1)*(xi1 - xi)
226 // yi = clast, xi = wlast, yi1 = i->color, xi1 = i->pos
228 for(;i<=iterr; wlast=i->pos,clast=i->color.premult_alpha(),++i)
230 const Real diff = i->pos - wlast;
231 if(diff > 0) //only accumulate if there will be area to add
233 cwork = i->color.premult_alpha();
234 accum += (cwork + clast)*diff;
239 const_iterator ibef = i-1;
240 const Real diff = right - ibef->pos;
244 const Real lambda = diff/(i->pos - ibef->pos);
245 cwork = ibef->color.premult_alpha()*(1-lambda) + i->color.premult_alpha()*lambda;
247 accum += (cwork + clast)*diff; //can probably optimize this more... but it's not too bad
251 accum /= supersample; //should be the total area it was sampled over...
252 return accum.demult_alpha();
255 next=begin(),iter=next++;
257 //add for optimization
258 next = binary_find(begin(),end(),(Real)begin_sample);
261 //! As a future optimization, this could be performed faster
262 //! using a binary search.
263 for(;iter<end();iter=next++)
265 if(next==end() || x>=iter->pos && x<next->pos && iter->pos!=next->pos)
267 // If the supersample region falls square in between
268 // two CPoints, then we don't have to do anything special.
269 if(next!=end() && (!supersample || (iter->pos<=begin_sample && next->pos>=end_sample)))
271 const Real dist(next->pos-iter->pos);
272 const Real pos(x-iter->pos);
273 const Real amount(pos/dist);
274 return Color::blend(next->color,iter->color, amount, Color::BLEND_STRAIGHT);
276 // In this case our supersample region extends over one or more
277 // CPoints. So, we need to calculate our coverage amount.
278 ColorAccumulator pool(Color::alpha());
279 float divisor(0.0),weight(0);
281 const_iterator iter2,next2;
283 if(iter==begin() && iter->pos>x)
286 //weight*=iter->color.get_a();
287 pool+=ColorAccumulator(iter->color)*(float)iter->color.get_a()*weight;
292 while(iter2->pos>=begin_sample)
296 weight=iter2->pos-(begin_sample);
297 //weight*=iter2->color.get_a();
298 pool+=ColorAccumulator(iter2->color)*(float)iter2->color.get_a()*weight;
303 pool+=supersample_helper(*iter2, *next2, begin_sample, end_sample, weight);
310 while(iter2->pos<=end_sample)
314 weight=(end_sample)-iter2->pos;
315 pool+=ColorAccumulator(iter2->color)*(float)iter2->color.get_a()*weight;
319 pool+=supersample_helper(*iter2, *next2, begin_sample, end_sample, weight);
324 if(divisor && pool.get_a() && pool.is_valid())
327 pool.set_r(pool.get_r()/pool.get_a());
328 pool.set_g(pool.get_g()/pool.get_a());
329 pool.set_b(pool.get_b()/pool.get_a());
330 pool.set_a(pool.get_a()/divisor);
333 pool.set_r(pool.get_r()/pool.get_a());
334 pool.set_g(pool.get_g()/pool.get_a());
335 pool.set_b(pool.get_b()/pool.get_a());
339 return Color::alpha();
342 return Color::alpha();
346 // We should never get to this point.
348 sinfg::error("sinfg::Gradient::operator()(): Logic Error (x=%f)",x);
350 throw std::logic_error(strprintf("sinfg::Gradient::operator()(): Logic Error (x=%f)",x));
353 sinfg::Gradient::iterator
354 sinfg::Gradient::proximity(const Real &x)
357 float dist(100000000);
358 float prev_pos(-0230);
359 // This algorithm requires a sorted list.
360 for(iter=begin();iter<end();iter++)
364 if(prev_pos==iter->pos)
365 new_dist=(abs(x-iter->pos-0.00001));
367 new_dist=(abs(x-iter->pos));
381 sinfg::Gradient::const_iterator
382 sinfg::Gradient::proximity(const Real &x)const
384 return const_cast<Gradient*>(this)->proximity(x);
387 float dist(100000000);
389 // This algorithm requires a sorted list.
390 for(iter=begin();iter<end();iter++)
392 const float new_dist(abs(x-iter->pos));
405 sinfg::Gradient::iterator
406 sinfg::Gradient::find(const UniqueID &id)
410 for(iter=begin();iter<end();iter++)
416 throw Exception::NotFound("sinfg::Gradient::find(): Unable to find UniqueID in gradient");
419 sinfg::Gradient::const_iterator
420 sinfg::Gradient::find(const UniqueID &id)const
424 for(iter=begin();iter<end();iter++)
430 throw Exception::NotFound("sinfg::Gradient::find()const: Unable to find UniqueID in gradient");