incorporate drawable and annotations
[mussa.git] / alg / color.cpp
1 #include "alg/color.hpp"
2 #include <math.h>
3
4 Color::Color() 
5 {
6   set(0.0, 0.0, 0.0, 0.0);
7 }
8
9 Color::Color(const Color &c)
10 {
11   set(c.colors[RedChannel], 
12       c.colors[GreenChannel], 
13       c.colors[BlueChannel], 
14       c.colors[AlphaChannel]);
15 }
16
17 Color::Color(float red, float green, float blue, float alpha)
18 {
19   set(red, green, blue, alpha);
20 }
21
22 Color::Color(const ColorRef c)
23 {
24   set(c->colors[RedChannel], 
25       c->colors[GreenChannel], 
26       c->colors[BlueChannel], 
27       c->colors[AlphaChannel]);
28 }
29
30 void Color::set(float r, float g, float b, float a)
31 {
32   colors[RedChannel]   = r;
33   colors[GreenChannel] = g;
34   colors[BlueChannel]  = b;
35   colors[AlphaChannel] = a;
36 }
37
38 const float *const Color::get() const
39 {
40   return colors;
41 }
42
43 void Color::setR(float r)
44 {
45   colors[RedChannel]   = r;
46 }
47
48 float Color::r() const
49 {
50   return colors[RedChannel];
51 }
52
53 void Color::setG(float g)
54 {
55   colors[GreenChannel]   = g;
56 }
57
58 float Color::g() const
59 {
60   return colors[GreenChannel];
61 }
62
63 void Color::setB(float b)
64 {
65   colors[BlueChannel]   = b;
66 }
67
68 float Color::b() const
69 {
70   return colors[BlueChannel];
71 }
72
73 void Color::setA(float a)
74 {
75   colors[AlphaChannel]   = a;
76 }
77
78 float Color::a() const
79 {
80   return colors[AlphaChannel];
81 }
82
83 static bool close(float l, float r, float t=1e-6)
84 {
85   float difference = fabsf(l-r);
86   l = fabsf(l);
87   r = fabsf(r);
88    
89   return (difference <= (t*l) or difference <= (t*r));
90 }
91 bool operator==(const Color &x, const Color &y)
92 {
93   return (close(x.colors[Color::RedChannel]  ,y.colors[Color::RedChannel]) and
94          close(x.colors[Color::GreenChannel],y.colors[Color::GreenChannel]) and 
95          close(x.colors[Color::BlueChannel] ,y.colors[Color::BlueChannel]) and
96          close(x.colors[Color::AlphaChannel],y.colors[Color::AlphaChannel]));
97 }
98
99 std::ostream &operator<<(std::ostream &out, const Color &c)
100 {
101   out << c.r() << " " << c.g() << " " << c.b() << " " << c.a();
102   return out;
103 }