1 /* === S Y N F I G ========================================================= */
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 ======================================================= */
40 using namespace synfig;
44 /* === M A C R O S ========================================================= */
46 #define COLOR_EPSILON (0.000001f)
48 /* === G L O B A L S ======================================================= */
52 /* === P R O C E D U R E S ================================================= */
54 /* === M E T H O D S ======================================================= */
59 Color::hex2real(String s)
61 std::istringstream i(s);
65 throw String("bad conversion from hex string \"") + s + String("\"");
70 Color::real2hex(ColorReal c)
77 o << hex << int(c*255.0f);
82 Color::set_hex(String& hex)
89 r = hex2real(hex.substr(0,1)+hex.substr(0,1));
92 else if (hex.size() == 3)
94 r = hex2real(hex.substr(0,1)+hex.substr(0,1));
95 g = hex2real(hex.substr(1,1)+hex.substr(1,1));
96 b = hex2real(hex.substr(2,1)+hex.substr(2,1));
97 r_ = r; g_ = g; b_ = b;
99 else if (hex.size() == 6)
101 r = hex2real(hex.substr(0,2));
102 g = hex2real(hex.substr(2,2));
103 b = hex2real(hex.substr(4,2));
104 r_ = r; g_ = g; b_ = b;
109 printf("caught <%s>\n", s.c_str());
116 Color::rotate_uv(const Angle& theta)const
120 ret.set_hue(ret.get_hue()+theta);
124 a(angle::sin(theta).get()),
125 b(angle::cos(theta).get());
130 return set_uv(b*u-a*v,a*u+b*v);
131 //return YUV(get_y(),b*u-a*v,a*u+b*v,get_a());
137 Color::clamped_negative()const
166 if(ret.r_>1) ret.r_=1;
167 if(ret.g_>1) ret.g_=1;
168 if(ret.b_>1) ret.b_=1;
169 if(ret.a_>1) ret.a_=1;
171 if(isnan(ret.get_r())) ret.r_=0.5;
172 if(isnan(ret.get_g())) ret.g_=0.5;
173 if(isnan(ret.get_b())) ret.b_=0.5;
174 if(isnan(ret.get_a())) ret.a_=1;
177 if(ret.r_>1) { ret.g_/=ret.r_; ret.b_/=ret.r_; ret.r_=1; }
178 if(ret.g_>1) { ret.r_/=ret.g_; ret.b_/=ret.g_; ret.g_=1; }
179 if(ret.b_>1) { ret.g_/=ret.b_; ret.r_/=ret.b_; ret.b_=1; }
180 if(ret.a_>1) ret.a_=1;
187 Color::clamped()const
199 if(ret.r_>1) ret.r_=1;
200 if(ret.g_>1) ret.g_=1;
201 if(ret.b_>1) ret.b_=1;
202 if(ret.a_>1) ret.a_=1;
204 if(isnan(ret.get_r())) ret.r_=0.5;
205 if(isnan(ret.get_g())) ret.g_=0.5;
206 if(isnan(ret.get_b())) ret.b_=0.5;
207 if(isnan(ret.get_a())) ret.a_=1;
212 typedef Color (*blendfunc)(Color &,Color &,float);
215 blendfunc_COMPOSITE(Color &src,Color &dest,float amount)
217 //c_dest'=c_src+(1.0-a_src)*c_dest
218 //a_dest'=a_src+(1.0-a_src)*a_dest
220 float a_src=src.get_a()*amount;
221 float a_dest=dest.get_a();
224 //if(fabsf(a_src)<COLOR_EPSILON) return dest;
226 // Scale the source and destination by their alpha values
230 dest=src + dest*(1.0f-a_src);
232 a_dest=a_src + a_dest*(1.0f-a_src);
235 if(fabsf(a_dest)>COLOR_EPSILON)
244 assert(dest.is_valid());
249 blendfunc_STRAIGHT(Color &src,Color &bg,float amount)
251 //a_out'=(a_src-a_bg)*amount+a_bg
252 //c_out'=(((c_src*a_src)-(c_bg*a_bg))*amount+(c_bg*a_bg))/a_out'
254 // ie: if(amount==1.0)
255 //if(fabsf(amount-1.0f)<COLOR_EPSILON)return src;
259 float a_out((src.get_a()-bg.get_a())*amount+bg.get_a());
262 if(fabsf(a_out)>COLOR_EPSILON)
263 // if(a_out>COLOR_EPSILON || a_out<-COLOR_EPSILON)
265 out=((src*src.get_a()-bg*bg.get_a())*amount+bg*bg.get_a())/a_out;
271 assert(out.is_valid());
276 blendfunc_ONTO(Color &a,Color &b,float amount)
278 float alpha(b.get_a());
279 return blendfunc_COMPOSITE(a,b.set_a(1.0f),amount).set_a(alpha);
283 blendfunc_STRAIGHT_ONTO(Color &a,Color &b,float amount)
285 a.set_a(a.get_a()*b.get_a());
286 return blendfunc_STRAIGHT(a,b,amount);
290 blendfunc_BRIGHTEN(Color &a,Color &b,float amount)
292 const float alpha(a.get_a()*amount);
294 if(b.get_r()<a.get_r()*alpha)
295 b.set_r(a.get_r()*alpha);
297 if(b.get_g()<a.get_g()*alpha)
298 b.set_g(a.get_g()*alpha);
300 if(b.get_b()<a.get_b()*alpha)
301 b.set_b(a.get_b()*alpha);
307 blendfunc_DARKEN(Color &a,Color &b,float amount)
309 const float alpha(a.get_a()*amount);
311 if(b.get_r()>(a.get_r()-1.0f)*alpha+1.0f)
312 b.set_r((a.get_r()-1.0f)*alpha+1.0f);
314 if(b.get_g()>(a.get_g()-1.0f)*alpha+1.0f)
315 b.set_g((a.get_g()-1.0f)*alpha+1.0f);
317 if(b.get_b()>(a.get_b()-1.0f)*alpha+1.0f)
318 b.set_b((a.get_b()-1.0f)*alpha+1.0f);
325 blendfunc_ADD(Color &a,Color &b,float amount)
327 const float alpha(a.get_a()*amount);
329 b.set_r(b.get_r()+a.get_r()*alpha);
330 b.set_g(b.get_g()+a.get_g()*alpha);
331 b.set_b(b.get_b()+a.get_b()*alpha);
337 blendfunc_SUBTRACT(Color &a,Color &b,float amount)
339 const float alpha(a.get_a()*amount);
341 b.set_r(b.get_r()-a.get_r()*alpha);
342 b.set_g(b.get_g()-a.get_g()*alpha);
343 b.set_b(b.get_b()-a.get_b()*alpha);
349 blendfunc_DIFFERENCE(Color &a,Color &b,float amount)
351 const float alpha(a.get_a()*amount);
353 b.set_r(abs(b.get_r()-a.get_r()*alpha));
354 b.set_g(abs(b.get_g()-a.get_g()*alpha));
355 b.set_b(abs(b.get_b()-a.get_b()*alpha));
361 blendfunc_MULTIPLY(Color &a,Color &b,float amount)
363 if(amount<0) a=~a, amount=-amount;
366 b.set_r(((b.get_r()*a.get_r())-b.get_r())*(amount)+b.get_r());
367 b.set_g(((b.get_g()*a.get_g())-b.get_g())*(amount)+b.get_g());
368 b.set_b(((b.get_b()*a.get_b())-b.get_b())*(amount)+b.get_b());
373 blendfunc_DIVIDE(Color &a,Color &b,float amount)
377 // We add COLOR_EPSILON in order to avoid a divide-by-zero condition.
378 // This causes DIVIDE to bias toward positive values, but the effect is
379 // really neglegable. There is a reason why we use COLOR_EPSILON--we
380 // want the change to be imperceptable.
382 b.set_r(((b.get_r()/(a.get_r()+COLOR_EPSILON))-b.get_r())*(amount)+b.get_r());
383 b.set_g(((b.get_g()/(a.get_g()+COLOR_EPSILON))-b.get_g())*(amount)+b.get_g());
384 b.set_b(((b.get_b()/(a.get_b()+COLOR_EPSILON))-b.get_b())*(amount)+b.get_b());
390 blendfunc_COLOR(Color &a,Color &b,float amount)
393 temp.set_uv(a.get_u(),a.get_v());
394 return (temp-b)*amount*a.get_a()+b;
398 blendfunc_HUE(Color &a,Color &b,float amount)
401 temp.set_hue(a.get_hue());
402 return (temp-b)*amount*a.get_a()+b;
406 blendfunc_SATURATION(Color &a,Color &b,float amount)
409 temp.set_s(a.get_s());
410 return (temp-b)*amount*a.get_a()+b;
414 blendfunc_LUMINANCE(Color &a,Color &b,float amount)
417 temp.set_y(a.get_y());
418 return (temp-b)*amount*a.get_a()+b;
422 blendfunc_BEHIND(Color &a,Color &b,float amount)
424 if(a.get_a()==0)a.set_a(COLOR_EPSILON); //!< \hack
425 a.set_a(a.get_a()*amount);
426 return blendfunc_COMPOSITE(b,a,1.0);
430 blendfunc_ALPHA_BRIGHTEN(Color &a,Color &b,float amount)
432 if(a.get_a()<b.get_a()*amount)
433 return a.set_a(a.get_a()*amount);
438 blendfunc_ALPHA_DARKEN(Color &a,Color &b,float amount)
440 if(a.get_a()*amount>b.get_a())
441 return a.set_a(a.get_a()*amount);
446 blendfunc_SCREEN(Color &a,Color &b,float amount)
448 if(amount<0) a=~a, amount=-amount;
450 a.set_r(1.0-(1.0f-a.get_r())*(1.0f-b.get_r()));
451 a.set_g(1.0-(1.0f-a.get_g())*(1.0f-b.get_g()));
452 a.set_b(1.0-(1.0f-a.get_b())*(1.0f-b.get_b()));
454 return blendfunc_ONTO(a,b,amount);
458 blendfunc_OVERLAY(Color &a,Color &b,float amount)
460 if(amount<0) a=~a, amount=-amount;
463 rm.set_r(b.get_r()*a.get_r());
464 rm.set_g(b.get_g()*a.get_g());
465 rm.set_b(b.get_b()*a.get_b());
468 rs.set_r(1.0-(1.0f-a.get_r())*(1.0f-b.get_r()));
469 rs.set_g(1.0-(1.0f-a.get_g())*(1.0f-b.get_g()));
470 rs.set_b(1.0-(1.0f-a.get_b())*(1.0f-b.get_b()));
474 ret.set_r(a.get_r()*rs.get_r() + (1.0-a.get_r())*rm.get_r());
475 ret.set_g(a.get_g()*rs.get_g() + (1.0-a.get_g())*rm.get_g());
476 ret.set_b(a.get_b()*rs.get_b() + (1.0-a.get_b())*rm.get_b());
478 return blendfunc_ONTO(ret,b,amount);
482 blendfunc_HARD_LIGHT(Color &a,Color &b,float amount)
484 if(amount<0) a=~a, amount=-amount;
486 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()));
487 else a.set_r(b.get_r()*(a.get_r()*2.0f));
488 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()));
489 else a.set_g(b.get_g()*(a.get_g()*2.0f));
490 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()));
491 else a.set_b(b.get_b()*(a.get_b()*2.0f));
493 return blendfunc_ONTO(a,b,amount);
497 blendfunc_ALPHA_OVER(Color &a,Color &b,float amount)
501 //multiply the inverse alpha channel with the one below us
502 rm.set_a((1-a.get_a())*b.get_a());
504 return blendfunc_STRAIGHT(rm,b,amount);
509 Color::blend(Color a, Color b,float amount, Color::BlendMethod type)
512 if(isnan(a.get_r()) || isnan(a.get_g()) || isnan(a.get_b()))
515 a=magenta().set_a(a.get_a());
517 a=black().set_a(a.get_a());
521 if(isnan(b.get_r()) || isnan(b.get_g()) || isnan(b.get_b()))
524 b=magenta().set_a(b.get_a());
526 b=black().set_a(b.get_a());
532 if(!a.is_valid()&&b.is_valid())
535 if(a.is_valid()&&!b.is_valid())
538 if(!a.is_valid()||!b.is_valid())
548 // No matter what blend method is being used,
549 // if the amount is equal to zero, then only B
550 // will shine through
551 if(fabsf(amount)<=COLOR_EPSILON)return b;
553 assert(type<BLEND_END);
555 const static blendfunc vtable[BLEND_END]=
567 blendfunc_SATURATION,
571 blendfunc_ALPHA_BRIGHTEN,
572 blendfunc_ALPHA_DARKEN,
574 blendfunc_HARD_LIGHT,
575 blendfunc_DIFFERENCE,
576 blendfunc_ALPHA_OVER,
578 blendfunc_STRAIGHT_ONTO,
581 return vtable[type](a,b,amount);