Stable Tag: Copying everything over
[synfig.git] / synfig-core / tags / stable / src / synfig / color.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file color.cpp
3 **      \brief Color Class
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
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.
14 **
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.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
32 #include <ETL/angle>
33 #include "color.h"
34 #include <cstdio>
35 #include <sstream>
36 #include <iostream>
37
38 #endif
39
40 using namespace synfig;
41 using namespace etl;
42 using namespace std;
43
44 /* === M A C R O S ========================================================= */
45
46 #define COLOR_EPSILON   (0.000001f)
47
48 /* === G L O B A L S ======================================================= */
49
50 String Color::hex_;
51
52 /* === P R O C E D U R E S ================================================= */
53
54 /* === M E T H O D S ======================================================= */
55
56
57
58 ColorReal
59 Color::hex2real(String s)
60 {
61         std::istringstream i(s);
62         int n;
63         i.fill('0');
64         if (!(i >> hex >> n))
65                 throw String("bad conversion from hex string \"") + s + String("\"");
66         return n / 255.0f;
67 }
68
69 const String
70 Color::real2hex(ColorReal c)
71 {
72         std::ostringstream o;
73         o.width(2);
74         o.fill('0');
75         if (c<0) c = 0;
76         if (c>1) c = 1;
77         o << hex << int(c*255.0f);
78         return o.str();
79 }
80
81 void
82 Color::set_hex(String& hex)
83 {
84         value_type r, g, b;
85         try
86         {
87                 if (hex.size() == 1)
88                 {
89                         r = hex2real(hex.substr(0,1)+hex.substr(0,1));
90                         r_ = g_ = b_ = r;
91                 }
92                 else if (hex.size() == 3)
93                 {
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;
98                 }
99                 else if (hex.size() == 6)
100                 {
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;
105                 }
106         }
107         catch (string s)
108         {
109                 printf("caught <%s>\n", s.c_str());
110                 return;
111         }
112 }
113
114 #if 0
115 Color&
116 Color::rotate_uv(const Angle& theta)const
117 {
118 /*/
119         Color ret(*this);
120         ret.set_hue(ret.get_hue()+theta);
121         return ret;
122 /*/
123         const float
124                 a(angle::sin(theta).get()),
125                 b(angle::cos(theta).get());
126         const float
127                 u(get_u()),
128                 v(get_v());
129
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());
132 //*/
133 }
134 #endif
135
136 Color
137 Color::clamped_negative()const
138 {
139         Color ret=*this;
140
141         if(ret.a_==0)
142                 return alpha();
143
144         if(ret.a_<0)
145                 ret=-ret;
146
147         if(ret.r_<0)
148         {
149                 ret.g_-=ret.r_;
150                 ret.b_-=ret.r_;
151                 ret.r_=0.0f;
152         }
153         if(ret.g_<0)
154         {
155                 ret.r_-=ret.g_;
156                 ret.b_-=ret.g_;
157                 ret.g_=0.0f;
158         }
159         if(ret.b_<0)
160         {
161                 ret.r_-=ret.b_;
162                 ret.g_-=ret.b_;
163                 ret.b_=0.0f;
164         }
165
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;
170
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;
175
176 /*
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;
181 */
182
183         return ret;
184 }
185
186 Color
187 Color::clamped()const
188 {
189         Color ret(*this);
190         if(ret.get_r()<0)
191                 ret.set_r(0);
192         if(ret.get_g()<0)
193                 ret.set_g(0);
194         if(ret.get_b()<0)
195                 ret.set_b(0);
196         if(ret.get_a()<0)
197                 ret.set_a(0);
198
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;
203
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;
208
209         return(ret);
210 }
211
212 typedef Color (*blendfunc)(Color &,Color &,float);
213
214 static Color
215 blendfunc_COMPOSITE(Color &src,Color &dest,float amount)
216 {
217         //c_dest'=c_src+(1.0-a_src)*c_dest
218         //a_dest'=a_src+(1.0-a_src)*a_dest
219
220         float a_src=src.get_a()*amount;
221         float a_dest=dest.get_a();
222
223         // if a_arc==0.0
224         //if(fabsf(a_src)<COLOR_EPSILON) return dest;
225
226         // Scale the source and destination by their alpha values
227         src*=a_src;
228         dest*=a_dest;
229
230         dest=src + dest*(1.0f-a_src);
231
232         a_dest=a_src + a_dest*(1.0f-a_src);
233
234         // if a_dest!=0.0
235         if(fabsf(a_dest)>COLOR_EPSILON)
236         {
237                 dest/=a_dest;
238                 dest.set_a(a_dest);
239         }
240         else
241         {
242                 dest=Color::alpha();
243         }
244         assert(dest.is_valid());
245         return dest;
246 }
247
248 static Color
249 blendfunc_STRAIGHT(Color &src,Color &bg,float amount)
250 {
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'
253
254         // ie: if(amount==1.0)
255         //if(fabsf(amount-1.0f)<COLOR_EPSILON)return src;
256
257         Color out;
258
259         float a_out((src.get_a()-bg.get_a())*amount+bg.get_a());
260
261         // if a_out!=0.0
262         if(fabsf(a_out)>COLOR_EPSILON)
263 //      if(a_out>COLOR_EPSILON || a_out<-COLOR_EPSILON)
264         {
265                 out=((src*src.get_a()-bg*bg.get_a())*amount+bg*bg.get_a())/a_out;
266                 out.set_a(a_out);
267         }
268         else
269                 out=Color::alpha();
270
271         assert(out.is_valid());
272         return out;
273 }
274
275 static Color
276 blendfunc_ONTO(Color &a,Color &b,float amount)
277 {
278         float alpha(b.get_a());
279         return blendfunc_COMPOSITE(a,b.set_a(1.0f),amount).set_a(alpha);
280 }
281
282 static Color
283 blendfunc_STRAIGHT_ONTO(Color &a,Color &b,float amount)
284 {
285         a.set_a(a.get_a()*b.get_a());
286         return blendfunc_STRAIGHT(a,b,amount);
287 }
288
289 static Color
290 blendfunc_BRIGHTEN(Color &a,Color &b,float amount)
291 {
292         const float alpha(a.get_a()*amount);
293
294         if(b.get_r()<a.get_r()*alpha)
295                 b.set_r(a.get_r()*alpha);
296
297         if(b.get_g()<a.get_g()*alpha)
298                 b.set_g(a.get_g()*alpha);
299
300         if(b.get_b()<a.get_b()*alpha)
301                 b.set_b(a.get_b()*alpha);
302
303         return b;
304 }
305
306 static Color
307 blendfunc_DARKEN(Color &a,Color &b,float amount)
308 {
309         const float alpha(a.get_a()*amount);
310
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);
313
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);
316
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);
319
320
321         return b;
322 }
323
324 static Color
325 blendfunc_ADD(Color &a,Color &b,float amount)
326 {
327         const float alpha(a.get_a()*amount);
328
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);
332
333         return b;
334 }
335
336 static Color
337 blendfunc_SUBTRACT(Color &a,Color &b,float amount)
338 {
339         const float alpha(a.get_a()*amount);
340
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);
344
345         return b;
346 }
347
348 static Color
349 blendfunc_DIFFERENCE(Color &a,Color &b,float amount)
350 {
351         const float alpha(a.get_a()*amount);
352
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));
356
357         return b;
358 }
359
360 static Color
361 blendfunc_MULTIPLY(Color &a,Color &b,float amount)
362 {
363         if(amount<0) a=~a, amount=-amount;
364
365         amount*=a.get_a();
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());
369         return b;
370 }
371
372 static Color
373 blendfunc_DIVIDE(Color &a,Color &b,float amount)
374 {
375         amount*=a.get_a();
376
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.
381
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());
385
386         return b;
387 }
388
389 static Color
390 blendfunc_COLOR(Color &a,Color &b,float amount)
391 {
392         Color temp(b);
393         temp.set_uv(a.get_u(),a.get_v());
394         return (temp-b)*amount*a.get_a()+b;
395 }
396
397 static Color
398 blendfunc_HUE(Color &a,Color &b,float amount)
399 {
400         Color temp(b);
401         temp.set_hue(a.get_hue());
402         return (temp-b)*amount*a.get_a()+b;
403 }
404
405 static Color
406 blendfunc_SATURATION(Color &a,Color &b,float amount)
407 {
408         Color temp(b);
409         temp.set_s(a.get_s());
410         return (temp-b)*amount*a.get_a()+b;
411 }
412
413 static Color
414 blendfunc_LUMINANCE(Color &a,Color &b,float amount)
415 {
416         Color temp(b);
417         temp.set_y(a.get_y());
418         return (temp-b)*amount*a.get_a()+b;
419 }
420
421 static Color
422 blendfunc_BEHIND(Color &a,Color &b,float amount)
423 {
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);
427 }
428
429 static Color
430 blendfunc_ALPHA_BRIGHTEN(Color &a,Color &b,float amount)
431 {
432         if(a.get_a()<b.get_a()*amount)
433                 return a.set_a(a.get_a()*amount);
434         return b;
435 }
436
437 static Color
438 blendfunc_ALPHA_DARKEN(Color &a,Color &b,float amount)
439 {
440         if(a.get_a()*amount>b.get_a())
441                 return a.set_a(a.get_a()*amount);
442         return b;
443 }
444
445 static Color
446 blendfunc_SCREEN(Color &a,Color &b,float amount)
447 {
448         if(amount<0) a=~a, amount=-amount;
449
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()));
453
454         return blendfunc_ONTO(a,b,amount);
455 }
456
457 static Color
458 blendfunc_OVERLAY(Color &a,Color &b,float amount)
459 {
460         if(amount<0) a=~a, amount=-amount;
461
462         Color rm;
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());
466
467         Color rs;
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()));
471
472         Color& ret(a);
473
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());
477
478         return blendfunc_ONTO(ret,b,amount);
479 }
480
481 static Color
482 blendfunc_HARD_LIGHT(Color &a,Color &b,float amount)
483 {
484         if(amount<0) a=~a, amount=-amount;
485
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));
492
493         return blendfunc_ONTO(a,b,amount);
494 }
495
496 static Color
497 blendfunc_ALPHA_OVER(Color &a,Color &b,float amount)
498 {
499         Color rm(b);
500
501         //multiply the inverse alpha channel with the one below us
502         rm.set_a((1-a.get_a())*b.get_a());
503
504         return blendfunc_STRAIGHT(rm,b,amount);
505 }
506
507
508 Color
509 Color::blend(Color a, Color b,float amount, Color::BlendMethod type)
510 {
511 #if 0
512         if(isnan(a.get_r()) || isnan(a.get_g()) || isnan(a.get_b()))
513         {
514 #ifdef _DEBUG
515                 a=magenta().set_a(a.get_a());
516 #else
517                 a=black().set_a(a.get_a());
518 #endif
519         }
520
521         if(isnan(b.get_r()) || isnan(b.get_g()) || isnan(b.get_b()))
522         {
523 #ifdef _DEBUG
524                 b=magenta().set_a(b.get_a());
525 #else
526                 b=black().set_a(b.get_a());
527 #endif
528         }
529 #endif
530
531 /*
532         if(!a.is_valid()&&b.is_valid())
533                 return b;
534
535         if(a.is_valid()&&!b.is_valid())
536                 return a;
537
538         if(!a.is_valid()||!b.is_valid())
539         {
540 #ifdef _DEBUG
541                 return magenta();
542 #else
543                 return black();
544 #endif
545         }
546 */
547
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;
552
553         assert(type<BLEND_END);
554
555         const static blendfunc vtable[BLEND_END]=
556         {
557                 blendfunc_COMPOSITE,
558                 blendfunc_STRAIGHT,
559                 blendfunc_BRIGHTEN,
560                 blendfunc_DARKEN,
561                 blendfunc_ADD,
562                 blendfunc_SUBTRACT,
563                 blendfunc_MULTIPLY,
564                 blendfunc_DIVIDE,
565                 blendfunc_COLOR,
566                 blendfunc_HUE,
567                 blendfunc_SATURATION,
568                 blendfunc_LUMINANCE,
569                 blendfunc_BEHIND,
570                 blendfunc_ONTO,
571                 blendfunc_ALPHA_BRIGHTEN,
572                 blendfunc_ALPHA_DARKEN,
573                 blendfunc_SCREEN,
574                 blendfunc_HARD_LIGHT,
575                 blendfunc_DIFFERENCE,
576                 blendfunc_ALPHA_OVER,
577                 blendfunc_OVERLAY,
578                 blendfunc_STRAIGHT_ONTO,
579         };
580
581         return vtable[type](a,b,amount);
582 }