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 ======================================================= */
41 using namespace synfig;
45 /* === M A C R O S ========================================================= */
47 #define COLOR_EPSILON (0.000001f)
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 ======================================================= */
58 Color::hex2real(String s)
60 std::istringstream i(s);
64 throw String("bad conversion from hex string \"") + s + String("\"");
69 Color::real2hex(ColorReal c)
76 o << hex << int(c*255.0f);
81 Color::set_hex(String& hex)
88 r = hex2real(hex.substr(0,1)+hex.substr(0,1));
91 else if (hex.size() == 3)
93 r = hex2real(hex.substr(0,1)+hex.substr(0,1));
94 g = hex2real(hex.substr(1,1)+hex.substr(1,1));
95 b = hex2real(hex.substr(2,1)+hex.substr(2,1));
96 r_ = r; g_ = g; b_ = b;
98 else if (hex.size() == 6)
100 r = hex2real(hex.substr(0,2));
101 g = hex2real(hex.substr(2,2));
102 b = hex2real(hex.substr(4,2));
103 r_ = r; g_ = g; b_ = b;
108 printf("caught <%s>\n", s.c_str());
114 Color::get_string(void)const
116 std::ostringstream o;
117 o << std::fixed << std::setprecision(3) << "#" << get_hex() << " : " << std::setw(6) << a_;
118 return String(o.str().c_str());
123 Color::rotate_uv(const Angle& theta)const
127 ret.set_hue(ret.get_hue()+theta);
131 a(angle::sin(theta).get()),
132 b(angle::cos(theta).get());
137 return set_uv(b*u-a*v,a*u+b*v);
138 //return YUV(get_y(),b*u-a*v,a*u+b*v,get_a());
144 Color::clamped_negative()const
173 if(ret.r_>1) ret.r_=1;
174 if(ret.g_>1) ret.g_=1;
175 if(ret.b_>1) ret.b_=1;
176 if(ret.a_>1) ret.a_=1;
178 if(isnan(ret.get_r())) ret.r_=0.5;
179 if(isnan(ret.get_g())) ret.g_=0.5;
180 if(isnan(ret.get_b())) ret.b_=0.5;
181 if(isnan(ret.get_a())) ret.a_=1;
184 if(ret.r_>1) { ret.g_/=ret.r_; ret.b_/=ret.r_; ret.r_=1; }
185 if(ret.g_>1) { ret.r_/=ret.g_; ret.b_/=ret.g_; ret.g_=1; }
186 if(ret.b_>1) { ret.g_/=ret.b_; ret.r_/=ret.b_; ret.b_=1; }
187 if(ret.a_>1) ret.a_=1;
194 Color::clamped()const
206 if(ret.r_>1) ret.r_=1;
207 if(ret.g_>1) ret.g_=1;
208 if(ret.b_>1) ret.b_=1;
209 if(ret.a_>1) ret.a_=1;
211 if(isnan(ret.get_r())) ret.r_=0.5;
212 if(isnan(ret.get_g())) ret.g_=0.5;
213 if(isnan(ret.get_b())) ret.b_=0.5;
214 if(isnan(ret.get_a())) ret.a_=1;
219 typedef Color (*blendfunc)(Color &,Color &,float);
222 blendfunc_COMPOSITE(Color &src,Color &dest,float amount)
224 //c_dest'=c_src+(1.0-a_src)*c_dest
225 //a_dest'=a_src+(1.0-a_src)*a_dest
227 float a_src=src.get_a()*amount;
228 float a_dest=dest.get_a();
231 //if(fabsf(a_src)<COLOR_EPSILON) return dest;
233 // Scale the source and destination by their alpha values
237 dest=src + dest*(1.0f-a_src);
239 a_dest=a_src + a_dest*(1.0f-a_src);
242 if(fabsf(a_dest)>COLOR_EPSILON)
251 assert(dest.is_valid());
256 blendfunc_STRAIGHT(Color &src,Color &bg,float amount)
258 //a_out'=(a_src-a_bg)*amount+a_bg
259 //c_out'=(((c_src*a_src)-(c_bg*a_bg))*amount+(c_bg*a_bg))/a_out'
261 // ie: if(amount==1.0)
262 //if(fabsf(amount-1.0f)<COLOR_EPSILON)return src;
266 float a_out((src.get_a()-bg.get_a())*amount+bg.get_a());
269 if(fabsf(a_out)>COLOR_EPSILON)
270 // if(a_out>COLOR_EPSILON || a_out<-COLOR_EPSILON)
272 out=((src*src.get_a()-bg*bg.get_a())*amount+bg*bg.get_a())/a_out;
278 assert(out.is_valid());
283 blendfunc_ONTO(Color &a,Color &b,float amount)
285 float alpha(b.get_a());
286 return blendfunc_COMPOSITE(a,b.set_a(1.0f),amount).set_a(alpha);
290 blendfunc_STRAIGHT_ONTO(Color &a,Color &b,float amount)
292 a.set_a(a.get_a()*b.get_a());
293 return blendfunc_STRAIGHT(a,b,amount);
297 blendfunc_BRIGHTEN(Color &a,Color &b,float amount)
299 const float alpha(a.get_a()*amount);
301 if(b.get_r()<a.get_r()*alpha)
302 b.set_r(a.get_r()*alpha);
304 if(b.get_g()<a.get_g()*alpha)
305 b.set_g(a.get_g()*alpha);
307 if(b.get_b()<a.get_b()*alpha)
308 b.set_b(a.get_b()*alpha);
314 blendfunc_DARKEN(Color &a,Color &b,float amount)
316 const float alpha(a.get_a()*amount);
318 if(b.get_r()>(a.get_r()-1.0f)*alpha+1.0f)
319 b.set_r((a.get_r()-1.0f)*alpha+1.0f);
321 if(b.get_g()>(a.get_g()-1.0f)*alpha+1.0f)
322 b.set_g((a.get_g()-1.0f)*alpha+1.0f);
324 if(b.get_b()>(a.get_b()-1.0f)*alpha+1.0f)
325 b.set_b((a.get_b()-1.0f)*alpha+1.0f);
332 blendfunc_ADD(Color &a,Color &b,float amount)
334 const float alpha(a.get_a()*amount);
336 b.set_r(b.get_r()+a.get_r()*alpha);
337 b.set_g(b.get_g()+a.get_g()*alpha);
338 b.set_b(b.get_b()+a.get_b()*alpha);
344 blendfunc_SUBTRACT(Color &a,Color &b,float amount)
346 const float alpha(a.get_a()*amount);
348 b.set_r(b.get_r()-a.get_r()*alpha);
349 b.set_g(b.get_g()-a.get_g()*alpha);
350 b.set_b(b.get_b()-a.get_b()*alpha);
356 blendfunc_DIFFERENCE(Color &a,Color &b,float amount)
358 const float alpha(a.get_a()*amount);
360 b.set_r(abs(b.get_r()-a.get_r()*alpha));
361 b.set_g(abs(b.get_g()-a.get_g()*alpha));
362 b.set_b(abs(b.get_b()-a.get_b()*alpha));
368 blendfunc_MULTIPLY(Color &a,Color &b,float amount)
370 if(amount<0) a=~a, amount=-amount;
373 b.set_r(((b.get_r()*a.get_r())-b.get_r())*(amount)+b.get_r());
374 b.set_g(((b.get_g()*a.get_g())-b.get_g())*(amount)+b.get_g());
375 b.set_b(((b.get_b()*a.get_b())-b.get_b())*(amount)+b.get_b());
380 blendfunc_DIVIDE(Color &a,Color &b,float amount)
384 // We add COLOR_EPSILON in order to avoid a divide-by-zero condition.
385 // This causes DIVIDE to bias toward positive values, but the effect is
386 // really negligible. There is a reason why we use COLOR_EPSILON--we
387 // want the change to be imperceptable.
389 b.set_r(((b.get_r()/(a.get_r()+COLOR_EPSILON))-b.get_r())*(amount)+b.get_r());
390 b.set_g(((b.get_g()/(a.get_g()+COLOR_EPSILON))-b.get_g())*(amount)+b.get_g());
391 b.set_b(((b.get_b()/(a.get_b()+COLOR_EPSILON))-b.get_b())*(amount)+b.get_b());
397 blendfunc_COLOR(Color &a,Color &b,float amount)
400 temp.set_uv(a.get_u(),a.get_v());
401 return (temp-b)*amount*a.get_a()+b;
405 blendfunc_HUE(Color &a,Color &b,float amount)
408 temp.set_hue(a.get_hue());
409 return (temp-b)*amount*a.get_a()+b;
413 blendfunc_SATURATION(Color &a,Color &b,float amount)
416 temp.set_s(a.get_s());
417 return (temp-b)*amount*a.get_a()+b;
421 blendfunc_LUMINANCE(Color &a,Color &b,float amount)
424 temp.set_y(a.get_y());
425 return (temp-b)*amount*a.get_a()+b;
429 blendfunc_BEHIND(Color &a,Color &b,float amount)
431 if(a.get_a()==0)a.set_a(COLOR_EPSILON); //!< \hack
432 a.set_a(a.get_a()*amount);
433 return blendfunc_COMPOSITE(b,a,1.0);
437 blendfunc_ALPHA_BRIGHTEN(Color &a,Color &b,float amount)
439 if(a.get_a()<b.get_a()*amount)
440 return a.set_a(a.get_a()*amount);
445 blendfunc_ALPHA_DARKEN(Color &a,Color &b,float amount)
447 if(a.get_a()*amount>b.get_a())
448 return a.set_a(a.get_a()*amount);
453 blendfunc_SCREEN(Color &a,Color &b,float amount)
455 if(amount<0) a=~a, amount=-amount;
457 a.set_r(1.0-(1.0f-a.get_r())*(1.0f-b.get_r()));
458 a.set_g(1.0-(1.0f-a.get_g())*(1.0f-b.get_g()));
459 a.set_b(1.0-(1.0f-a.get_b())*(1.0f-b.get_b()));
461 return blendfunc_ONTO(a,b,amount);
465 blendfunc_OVERLAY(Color &a,Color &b,float amount)
467 if(amount<0) a=~a, amount=-amount;
470 rm.set_r(b.get_r()*a.get_r());
471 rm.set_g(b.get_g()*a.get_g());
472 rm.set_b(b.get_b()*a.get_b());
475 rs.set_r(1.0-(1.0f-a.get_r())*(1.0f-b.get_r()));
476 rs.set_g(1.0-(1.0f-a.get_g())*(1.0f-b.get_g()));
477 rs.set_b(1.0-(1.0f-a.get_b())*(1.0f-b.get_b()));
481 ret.set_r(a.get_r()*rs.get_r() + (1.0-a.get_r())*rm.get_r());
482 ret.set_g(a.get_g()*rs.get_g() + (1.0-a.get_g())*rm.get_g());
483 ret.set_b(a.get_b()*rs.get_b() + (1.0-a.get_b())*rm.get_b());
485 return blendfunc_ONTO(ret,b,amount);
489 blendfunc_HARD_LIGHT(Color &a,Color &b,float amount)
491 if(amount<0) a=~a, amount=-amount;
493 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()));
494 else a.set_r(b.get_r()*(a.get_r()*2.0f));
495 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()));
496 else a.set_g(b.get_g()*(a.get_g()*2.0f));
497 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()));
498 else a.set_b(b.get_b()*(a.get_b()*2.0f));
500 return blendfunc_ONTO(a,b,amount);
504 blendfunc_ALPHA_OVER(Color &a,Color &b,float amount)
508 //multiply the inverse alpha channel with the one below us
509 rm.set_a((1-a.get_a())*b.get_a());
511 return blendfunc_STRAIGHT(rm,b,amount);
516 Color::blend(Color a, Color b,float amount, Color::BlendMethod type)
519 if(isnan(a.get_r()) || isnan(a.get_g()) || isnan(a.get_b()))
522 a=magenta().set_a(a.get_a());
524 a=black().set_a(a.get_a());
528 if(isnan(b.get_r()) || isnan(b.get_g()) || isnan(b.get_b()))
531 b=magenta().set_a(b.get_a());
533 b=black().set_a(b.get_a());
539 if(!a.is_valid()&&b.is_valid())
542 if(a.is_valid()&&!b.is_valid())
545 if(!a.is_valid()||!b.is_valid())
555 // No matter what blend method is being used,
556 // if the amount is equal to zero, then only B
557 // will shine through
558 if(fabsf(amount)<=COLOR_EPSILON)return b;
560 assert(type<BLEND_END);
562 const static blendfunc vtable[BLEND_END]=
574 blendfunc_SATURATION,
578 blendfunc_ALPHA_BRIGHTEN,
579 blendfunc_ALPHA_DARKEN,
581 blendfunc_HARD_LIGHT,
582 blendfunc_DIFFERENCE,
583 blendfunc_ALPHA_OVER,
585 blendfunc_STRAIGHT_ONTO,
588 return vtable[type](a,b,amount);