Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStijn Buys <ingar@osirion.org>2008-03-24 20:40:57 +0000
committerStijn Buys <ingar@osirion.org>2008-03-24 20:40:57 +0000
commit78572764acf68b94b9992f488a725bc4be96a5b3 (patch)
tree03d4c6b55b413b8f147b2b8d62028bca9bf7c6cb /src/math/color.cc
parentbd647ce0f411c1a78407762e0a01511694e76e6f (diff)
libmath optimizations and cleanups
Diffstat (limited to 'src/math/color.cc')
-rw-r--r--src/math/color.cc115
1 files changed, 71 insertions, 44 deletions
diff --git a/src/math/color.cc b/src/math/color.cc
index ecdfd2f..7784338 100644
--- a/src/math/color.cc
+++ b/src/math/color.cc
@@ -11,77 +11,106 @@ namespace math
{
Color::Color() :
- r(_r), g(_g), b(_b), a(_a)
+ r(rgba_data[0]),
+ g(rgba_data[1]),
+ b(rgba_data[2]),
+ a(rgba_data[3])
{
- _r = _g = _b = 0.0f;
- _a = 1.0f;
+ assign(1.0f, 1.0f);
}
-Color::Color(const float red, const float green , const float blue , const float alpha) :
- r(_r), g(_g), b(_b), a(_a)
+Color::Color(float red, float green, float blue, float alpha) :
+ r(rgba_data[0]),
+ g(rgba_data[1]),
+ b(rgba_data[2]),
+ a(rgba_data[3])
{
- _r = red;
- _g = green;
- _b = blue;
- _a = alpha;
+ assign(red, green, blue, alpha);
}
Color::Color(const float grey, const float alpha) :
- r(_r), g(_g), b(_b), a(_a)
+ r(rgba_data[0]),
+ g(rgba_data[1]),
+ b(rgba_data[2]),
+ a(rgba_data[3])
{
- _r = _g = _b = grey;
- _a = alpha;
+ assign(grey, alpha);
}
-Color::Color(const Color &other) :
- r(_r), g(_g), b(_b), a(_a)
+Color::Color(Color const &other) :
+ r(rgba_data[0]),
+ g(rgba_data[1]),
+ b(rgba_data[2]),
+ a(rgba_data[3])
{
- this->operator=(other);
+ assign(other);
}
-const Color & Color::operator=(const Color &other)
+
+void Color::assign(float red, float green, float blue, float alpha)
{
- this->_r = other._r;
- this->_g = other._g;
- this->_b = other._b;
- this->_a = other._a;
- return (*this);
+ rgba_data[0] = red;
+ rgba_data[1] = green;
+ rgba_data[2] = blue;
+ rgba_data[3] = alpha;
}
-void Color::normalize()
+void Color::assign(Color const & other)
{
- float tmp = _r;
+ memcpy(rgba_data, other.rgba_data, sizeof(rgba_data));
+}
- if (_g > tmp)
- tmp = _g;
- if (_b > tmp)
- tmp = _b;
+void Color::assign(float grey, float alpha)
+{
+ rgba_data[0] = rgba_data[1] = rgba_data[2] = grey;
+ rgba_data[3] = alpha;
+}
+
+const Color & Color::operator=(Color const & other)
+{
+ assign(other);
+ return *this;
+}
+
+void Color::clamp()
+{
+ for (int i =0; i < 4; i++)
+ if (rgba_data[i] < 0)
+ rgba_data[i] = 0;
+
+ float tmp = rgba_data[0];
+ if (rgba_data[1] > tmp)
+ tmp = rgba_data[1];
+ if (rgba_data[2] > tmp)
+ tmp = rgba_data[2];
if (tmp > 1) {
- _r /= tmp;
- _g /= tmp;
- _b /= tmp;
+ for (int i =0; i < 3; i++)
+ rgba_data[i] /= tmp;
}
+
+ if (rgba_data[3] > 1)
+ rgba_data[3] = 1;
}
float Color::red() const
{
- return _r;
+ return rgba_data[0];
}
float Color::green() const
{
- return _g;
+ return rgba_data[1];
}
float Color::blue() const
{
- return _b;
+ return rgba_data[2];
}
float Color::alpha() const
{
- return _a;
+ return rgba_data[3];
}
Color Color::operator*(float scalar) const
@@ -89,25 +118,23 @@ Color Color::operator*(float scalar) const
return Color(red()*scalar, green()*scalar, blue()*scalar, alpha());
}
-Color operator*(float scalar, const Color& color)
+Color const operator*(float scalar, Color const & color)
{
return color * scalar;
}
-std::ostream &operator<<(std::ostream &os, const Color &c)
+std::ostream &operator<<(std::ostream &os, Color const & color)
{
- os << c.red() << " " << c.green() << " " << c.blue(); // << " " << c.alpha();
+ os << color.red() << " " << color.green() << " " << color.blue(); // << " " << c.alpha();
return os;
}
std::istream &operator>>(std::istream & is, Color & color)
{
- float r, g, b, a;
- is >> r;
- is >> g;
- is >> b;
- //is >> a;
- a = 1.0;
- color = Color(r,g,b,a);
+ is >> color.r;
+ is >> color.g;
+ is >> color.b;
+ //is >> color.a;
+ color.a = 1.0;
return (is);
}