1 /* === S Y N F I G ========================================================= */
5 ** $Id: color.cpp,v 1.2 2005/01/23 04:03:21 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 ======================================================= */
38 using namespace synfig;
42 /* === M A C R O S ========================================================= */
44 #define COLOR_EPSILON (0.000001f)
46 /* === G L O B A L S ======================================================= */
48 /* === P R O C E D U R E S ================================================= */
50 /* === M E T H O D S ======================================================= */
56 Color::rotate_uv(const Angle& theta)const
60 ret.set_hue(ret.get_hue()+theta);
64 a(angle::sin(theta).get()),
65 b(angle::cos(theta).get());
70 return set_uv(b*u-a*v,a*u+b*v);
71 //return YUV(get_y(),b*u-a*v,a*u+b*v,get_a());
77 Color::clamped_negative()const
106 if(ret.r_>1) ret.r_=1;
107 if(ret.g_>1) ret.g_=1;
108 if(ret.b_>1) ret.b_=1;
109 if(ret.a_>1) ret.a_=1;
111 if(isnan(ret.get_r())) ret.r_=0.5;
112 if(isnan(ret.get_g())) ret.g_=0.5;
113 if(isnan(ret.get_b())) ret.b_=0.5;
114 if(isnan(ret.get_a())) ret.a_=1;
117 if(ret.r_>1) { ret.g_/=ret.r_; ret.b_/=ret.r_; ret.r_=1; }
118 if(ret.g_>1) { ret.r_/=ret.g_; ret.b_/=ret.g_; ret.g_=1; }
119 if(ret.b_>1) { ret.g_/=ret.b_; ret.r_/=ret.b_; ret.b_=1; }
120 if(ret.a_>1) ret.a_=1;
127 Color::clamped()const
139 if(ret.r_>1) ret.r_=1;
140 if(ret.g_>1) ret.g_=1;
141 if(ret.b_>1) ret.b_=1;
142 if(ret.a_>1) ret.a_=1;
144 if(isnan(ret.get_r())) ret.r_=0.5;
145 if(isnan(ret.get_g())) ret.g_=0.5;
146 if(isnan(ret.get_b())) ret.b_=0.5;
147 if(isnan(ret.get_a())) ret.a_=1;
152 typedef Color (*blendfunc)(Color &,Color &,float);
155 blendfunc_COMPOSITE(Color &src,Color &dest,float amount)
157 //c_dest'=c_src+(1.0-a_src)*c_dest
158 //a_dest'=a_src+(1.0-a_src)*a_dest
160 float a_src=src.get_a()*amount;
161 float a_dest=dest.get_a();
164 //if(fabsf(a_src)<COLOR_EPSILON) return dest;
166 // Scale the source and destination by their alpha values
170 dest=src + dest*(1.0f-a_src);
172 a_dest=a_src + a_dest*(1.0f-a_src);
175 if(fabsf(a_dest)>COLOR_EPSILON)
184 assert(dest.is_valid());
189 blendfunc_STRAIGHT(Color &src,Color &bg,float amount)
191 //a_out'=(a_src-a_bg)*amount+a_bg
192 //c_out'=(((c_src*a_src)-(c_bg*a_bg))*amount+(c_bg*a_bg))/a_out'
194 // ie: if(amount==1.0)
195 //if(fabsf(amount-1.0f)<COLOR_EPSILON)return src;
199 float a_out((src.get_a()-bg.get_a())*amount+bg.get_a());
202 if(fabsf(a_out)>COLOR_EPSILON)
203 // if(a_out>COLOR_EPSILON || a_out<-COLOR_EPSILON)
205 out=((src*src.get_a()-bg*bg.get_a())*amount+bg*bg.get_a())/a_out;
211 assert(out.is_valid());
216 blendfunc_ONTO(Color &a,Color &b,float amount)
218 float alpha(b.get_a());
219 return blendfunc_COMPOSITE(a,b.set_a(1.0f),amount).set_a(alpha);
223 blendfunc_STRAIGHT_ONTO(Color &a,Color &b,float amount)
225 a.set_a(a.get_a()*b.get_a());
226 return blendfunc_STRAIGHT(a,b,amount);
230 blendfunc_BRIGHTEN(Color &a,Color &b,float amount)
232 const float alpha(a.get_a()*amount);
234 if(b.get_r()<a.get_r()*alpha)
235 b.set_r(a.get_r()*alpha);
237 if(b.get_g()<a.get_g()*alpha)
238 b.set_g(a.get_g()*alpha);
240 if(b.get_b()<a.get_b()*alpha)
241 b.set_b(a.get_b()*alpha);
247 blendfunc_DARKEN(Color &a,Color &b,float amount)
249 const float alpha(a.get_a()*amount);
251 if(b.get_r()>(a.get_r()-1.0f)*alpha+1.0f)
252 b.set_r((a.get_r()-1.0f)*alpha+1.0f);
254 if(b.get_g()>(a.get_g()-1.0f)*alpha+1.0f)
255 b.set_g((a.get_g()-1.0f)*alpha+1.0f);
257 if(b.get_b()>(a.get_b()-1.0f)*alpha+1.0f)
258 b.set_b((a.get_b()-1.0f)*alpha+1.0f);
265 blendfunc_ADD(Color &a,Color &b,float amount)
267 const float alpha(a.get_a()*amount);
269 b.set_r(b.get_r()+a.get_r()*alpha);
270 b.set_g(b.get_g()+a.get_g()*alpha);
271 b.set_b(b.get_b()+a.get_b()*alpha);
277 blendfunc_SUBTRACT(Color &a,Color &b,float amount)
279 const float alpha(a.get_a()*amount);
281 b.set_r(b.get_r()-a.get_r()*alpha);
282 b.set_g(b.get_g()-a.get_g()*alpha);
283 b.set_b(b.get_b()-a.get_b()*alpha);
289 blendfunc_DIFFERENCE(Color &a,Color &b,float amount)
291 const float alpha(a.get_a()*amount);
293 b.set_r(abs(b.get_r()-a.get_r()*alpha));
294 b.set_g(abs(b.get_g()-a.get_g()*alpha));
295 b.set_b(abs(b.get_b()-a.get_b()*alpha));
301 blendfunc_MULTIPLY(Color &a,Color &b,float amount)
303 if(amount<0) a=~a, amount=-amount;
306 b.set_r(((b.get_r()*a.get_r())-b.get_r())*(amount)+b.get_r());
307 b.set_g(((b.get_g()*a.get_g())-b.get_g())*(amount)+b.get_g());
308 b.set_b(((b.get_b()*a.get_b())-b.get_b())*(amount)+b.get_b());
313 blendfunc_DIVIDE(Color &a,Color &b,float amount)
317 // We add COLOR_EPSILON in order to avoid a divide-by-zero condition.
318 // This causes DIVIDE to bias toward positive values, but the effect is
319 // really neglegable. There is a reason why we use COLOR_EPSILON--we
320 // want the change to be imperceptable.
322 b.set_r(((b.get_r()/(a.get_r()+COLOR_EPSILON))-b.get_r())*(amount)+b.get_r());
323 b.set_g(((b.get_g()/(a.get_g()+COLOR_EPSILON))-b.get_g())*(amount)+b.get_g());
324 b.set_b(((b.get_b()/(a.get_b()+COLOR_EPSILON))-b.get_b())*(amount)+b.get_b());
330 blendfunc_COLOR(Color &a,Color &b,float amount)
333 temp.set_uv(a.get_u(),a.get_v());
334 return (temp-b)*amount*a.get_a()+b;
338 blendfunc_HUE(Color &a,Color &b,float amount)
341 temp.set_hue(a.get_hue());
342 return (temp-b)*amount*a.get_a()+b;
346 blendfunc_SATURATION(Color &a,Color &b,float amount)
349 temp.set_s(a.get_s());
350 return (temp-b)*amount*a.get_a()+b;
354 blendfunc_LUMINANCE(Color &a,Color &b,float amount)
357 temp.set_y(a.get_y());
358 return (temp-b)*amount*a.get_a()+b;
362 blendfunc_BEHIND(Color &a,Color &b,float amount)
364 if(a.get_a()==0)a.set_a(COLOR_EPSILON); //!< \hack
365 a.set_a(a.get_a()*amount);
366 return blendfunc_COMPOSITE(b,a,1.0);
370 blendfunc_ALPHA_BRIGHTEN(Color &a,Color &b,float amount)
372 if(a.get_a()<b.get_a()*amount)
373 return a.set_a(a.get_a()*amount);
378 blendfunc_ALPHA_DARKEN(Color &a,Color &b,float amount)
380 if(a.get_a()*amount>b.get_a())
381 return a.set_a(a.get_a()*amount);
386 blendfunc_SCREEN(Color &a,Color &b,float amount)
388 if(amount<0) a=~a, amount=-amount;
390 a.set_r(1.0-(1.0f-a.get_r())*(1.0f-b.get_r()));
391 a.set_g(1.0-(1.0f-a.get_g())*(1.0f-b.get_g()));
392 a.set_b(1.0-(1.0f-a.get_b())*(1.0f-b.get_b()));
394 return blendfunc_ONTO(a,b,amount);
398 blendfunc_OVERLAY(Color &a,Color &b,float amount)
400 if(amount<0) a=~a, amount=-amount;
403 rm.set_r(b.get_r()*a.get_r());
404 rm.set_g(b.get_g()*a.get_g());
405 rm.set_b(b.get_b()*a.get_b());
408 rs.set_r(1.0-(1.0f-a.get_r())*(1.0f-b.get_r()));
409 rs.set_g(1.0-(1.0f-a.get_g())*(1.0f-b.get_g()));
410 rs.set_b(1.0-(1.0f-a.get_b())*(1.0f-b.get_b()));
414 ret.set_r(a.get_r()*rs.get_r() + (1.0-a.get_r())*rm.get_r());
415 ret.set_g(a.get_g()*rs.get_g() + (1.0-a.get_g())*rm.get_g());
416 ret.set_b(a.get_b()*rs.get_b() + (1.0-a.get_b())*rm.get_b());
418 return blendfunc_ONTO(ret,b,amount);
422 blendfunc_HARD_LIGHT(Color &a,Color &b,float amount)
424 if(amount<0) a=~a, amount=-amount;
426 if(a.get_r()>0.5f) a.set_r(1.0-(1.0f-(a.get_r()*2.0f-1.0f))*(1.0f-b.get_r()));
427 else a.set_r(b.get_r()*(a.get_r()*2.0f));
428 if(a.get_g()>0.5f) a.set_g(1.0-(1.0f-(a.get_g()*2.0f-1.0f))*(1.0f-b.get_g()));
429 else a.set_g(b.get_g()*(a.get_g()*2.0f));
430 if(a.get_b()>0.5f) a.set_b(1.0-(1.0f-(a.get_b()*2.0f-1.0f))*(1.0f-b.get_b()));
431 else a.set_b(b.get_b()*(a.get_b()*2.0f));
433 return blendfunc_ONTO(a,b,amount);
437 blendfunc_ALPHA_OVER(Color &a,Color &b,float amount)
441 //multiply the inverse alpha channel with the one below us
442 rm.set_a((1-a.get_a())*b.get_a());
444 return blendfunc_STRAIGHT(rm,b,amount);
449 Color::blend(Color a, Color b,float amount, Color::BlendMethod type)
452 if(isnan(a.get_r()) || isnan(a.get_g()) || isnan(a.get_b()))
455 a=magenta().set_a(a.get_a());
457 a=black().set_a(a.get_a());
461 if(isnan(b.get_r()) || isnan(b.get_g()) || isnan(b.get_b()))
464 b=magenta().set_a(b.get_a());
466 b=black().set_a(b.get_a());
472 if(!a.is_valid()&&b.is_valid())
475 if(a.is_valid()&&!b.is_valid())
478 if(!a.is_valid()||!b.is_valid())
488 // No matter what blend method is being used,
489 // if the amount is equal to zero, then only B
490 // will shine through
491 if(fabsf(amount)<=COLOR_EPSILON)return b;
493 assert(type<BLEND_END);
495 const static blendfunc vtable[BLEND_END]=
507 blendfunc_SATURATION,
511 blendfunc_ALPHA_BRIGHTEN,
512 blendfunc_ALPHA_DARKEN,
514 blendfunc_HARD_LIGHT,
515 blendfunc_DIFFERENCE,
516 blendfunc_ALPHA_OVER,
518 blendfunc_STRAIGHT_ONTO,
521 return vtable[type](a,b,amount);