From f2e0f1b5865e4927097214bd29d09c58844511bc Mon Sep 17 00:00:00 2001
From: Stijn Buys <ingar@osirion.org>
Date: Sun, 3 Jul 2011 18:51:58 +0000
Subject: Convert RGB colors in the 0-255 range to 0.0-1.0 when reading clor
 values from inputstreams.

---
 src/math/color.cc | 11 +++++++++++
 src/math/color.h  |  3 +++
 2 files changed, 14 insertions(+)

(limited to 'src/math')

diff --git a/src/math/color.cc b/src/math/color.cc
index b807b96..70a48e5 100644
--- a/src/math/color.cc
+++ b/src/math/color.cc
@@ -131,6 +131,12 @@ Color & Color::operator*=(const float scalar)
 	return (*this);
 }
 
+Color & Color::operator/=(const float scalar)
+{
+	for (int i = 0; i < 3; i++)
+		rgba_data[i] /= scalar;
+	return (*this);
+}
 
 std::ostream &operator<<(std::ostream &os, Color const & color)
 {
@@ -143,6 +149,11 @@ std::istream &operator>>(std::istream & is, Color & color)
 	is >> color.r;
 	is >> color.g;
 	is >> color.b;
+	
+	if ((color.r > 1.0f) || (color.g > 1.0f) || (color.b > 1.0f)) {
+		color /= 255.0f;
+	}
+	
 	//is >> color.a;
 	color.a = 1.0;
 	return (is);
diff --git a/src/math/color.h b/src/math/color.h
index 8996af2..507402d 100644
--- a/src/math/color.h
+++ b/src/math/color.h
@@ -48,6 +48,9 @@ public:
 
 	/// multiply rgb values with scalar value.
 	Color & operator*=(const float scalar);
+	
+	/// divide rgb values with scalar value.
+	Color & operator/=(const float scalar);
 
 	/// assign a value to an element of this color
 	/*! WARNING: range is not checked
-- 
cgit v1.2.3