1 #include "alg/color.hpp"
6 set(0.0, 0.0, 0.0, 0.0);
9 Color::Color(const Color &c)
11 set(c.colors[RedChannel],
12 c.colors[GreenChannel],
13 c.colors[BlueChannel],
14 c.colors[AlphaChannel]);
17 Color::Color(float red, float green, float blue, float alpha)
19 set(red, green, blue, alpha);
22 Color::Color(const ColorRef c)
24 set(c->colors[RedChannel],
25 c->colors[GreenChannel],
26 c->colors[BlueChannel],
27 c->colors[AlphaChannel]);
30 void Color::set(float r, float g, float b, float a)
32 colors[RedChannel] = r;
33 colors[GreenChannel] = g;
34 colors[BlueChannel] = b;
35 colors[AlphaChannel] = a;
38 const float *const Color::get() const
43 void Color::setR(float r)
45 colors[RedChannel] = r;
48 float Color::r() const
50 return colors[RedChannel];
53 void Color::setG(float g)
55 colors[GreenChannel] = g;
58 float Color::g() const
60 return colors[GreenChannel];
63 void Color::setB(float b)
65 colors[BlueChannel] = b;
68 float Color::b() const
70 return colors[BlueChannel];
73 void Color::setA(float a)
75 colors[AlphaChannel] = a;
78 float Color::a() const
80 return colors[AlphaChannel];
83 static bool close(float l, float r, float t=1e-6)
85 float difference = fabsf(l-r);
89 return (difference <= (t*l) or difference <= (t*r));
91 bool operator==(const Color &x, const Color &y)
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]));
99 std::ostream &operator<<(std::ostream &out, const Color &c)
101 out << c.r() << " " << c.g() << " " << c.b() << " " << c.a();