Browse Source

Add Color class.

--HG--
branch : minor
vrld 14 years ago
parent
commit
aa5eb4380d

+ 103 - 0
src/modules/graphics/Color.h

@@ -0,0 +1,103 @@
+/**
+ * Copyright (c) 2006-2011 LOVE Development Team
+ *
+ * This software is provided 'as-is', without any express or implied
+ * warranty.  In no event will the authors be held liable for any damages
+ * arising from the use of this software.
+ *
+ * Permission is granted to anyone to use this software for any purpose,
+ * including commercial applications, and to alter it and redistribute it
+ * freely, subject to the following restrictions:
+ *
+ * 1. The origin of this software must not be misrepresented; you must not
+ *    claim that you wrote the original software. If you use this software
+ *    in a product, an acknowledgment in the product documentation would be
+ *    appreciated but is not required.
+ * 2. Altered source versions must be plainly marked as such, and must not be
+ *    misrepresented as being the original software.
+ * 3. This notice may not be removed or altered from any source distribution.
+ **/
+
+#ifndef LOVE_GRAPHICS_COLOR_H
+#define LOVE_GRAPHICS_COLOR_H
+
+namespace love
+{
+namespace graphics
+{
+
+	template <typename T>
+	struct ColorT
+	{
+		T r;
+		T g;
+		T b;
+		T a;
+
+		ColorT() : r(0), g(0), b(0), a(0) {}
+		ColorT(T r_, T g_, T b_, T a_) : r(r_), g(g_), b(b_), a(a_) {}
+		void set(T r_, T g_, T b_, T a_) { r = r_; g = g_; b = b_; a = a_; }
+
+		ColorT<T> operator+=(const ColorT<T>& other);
+		ColorT<T> operator*=(T s);
+		ColorT<T> operator/=(T s);
+	};
+
+	template <typename T>
+	ColorT<T> ColorT<T>::operator+=(const ColorT<T>& other)
+	{
+		r += other.r;
+		g += other.g;
+		b += other.b;
+		a += other.a;
+		return *this;
+	}
+
+	template <typename T>
+	ColorT<T> ColorT<T>::operator*=(T s)
+	{
+		r *= s;
+		g *= s;
+		b *= s;
+		a *= s;
+		return *this;
+	}
+
+	template <typename T>
+	ColorT<T> ColorT<T>::operator/=(T s)
+	{
+		r /= s;
+		g /= s;
+		b /= s;
+		a /= s;
+		return *this;
+	}
+
+	template <typename T>
+	ColorT<T> operator+(const ColorT<T>& a, const ColorT<T>& b)
+	{
+		ColorT<T> tmp(a);
+		return tmp += b;
+	}
+
+	template <typename T>
+	ColorT<T> operator*(const ColorT<T>& a, T s)
+	{
+		ColorT<T> tmp(a);
+		return tmp *= s;
+	}
+
+	template <typename T>
+	ColorT<T> operator/(const ColorT<T>& a, T s)
+	{
+		ColorT<T> tmp(a);
+		return tmp /= s;
+	}
+
+	typedef ColorT<unsigned char> Color;
+	typedef ColorT<float> Colorf;
+
+} // graphics
+} // love
+
+#endif // LOVE_GRAPHICS_COLOR_H

+ 1 - 2
src/modules/graphics/opengl/Framebuffer.h

@@ -4,6 +4,7 @@
 #include <graphics/Drawable.h>
 #include <graphics/Volatile.h>
 #include <graphics/Image.h>
+#include <graphics/Color.h>
 #include <image/Image.h>
 #include <image/ImageData.h>
 #include <common/math.h>
@@ -15,8 +16,6 @@ namespace graphics
 {
 namespace opengl
 {
-	struct Color; // forward declaration for clear
-
 	class Framebuffer : public Drawable, public Volatile
 	{
 	public:

+ 4 - 10
src/modules/graphics/opengl/Graphics.cpp

@@ -82,16 +82,10 @@ namespace opengl
 		float color[4];
 		//get the color
 		glGetFloatv(GL_CURRENT_COLOR, color);
-		s.color.r = (GLubyte)(color[0]*255.0f);
-		s.color.g = (GLubyte)(color[1]*255.0f);
-		s.color.b = (GLubyte)(color[2]*255.0f);
-		s.color.a = (GLubyte)(color[3]*255.0f);
+		s.color.set( (color[0]*255.0f), (color[1]*255.0f), (color[2]*255.0f), (color[3]*255.0f) );
 		//get the background color
 		glGetFloatv(GL_COLOR_CLEAR_VALUE, color);
-		s.backgroundColor.r = (GLubyte)(color[0]*255.0f);
-		s.backgroundColor.g = (GLubyte)(color[1]*255.0f);
-		s.backgroundColor.b = (GLubyte)(color[2]*255.0f);
-		s.backgroundColor.a = (GLubyte)(color[3]*255.0f);
+		s.backgroundColor.set( color[0]*255.0f, color[1]*255.0f, color[2]*255.0f, color[3]*255.0f );
 		//store modes here
 		GLint mode;
 		//get blend mode
@@ -489,7 +483,7 @@ namespace opengl
 		return new Framebuffer(width, height);
 	}
 
-	void Graphics::setColor(Color c)
+	void Graphics::setColor(const Color& c)
 	{
 		glColor4ubv(&c.r);
 	}
@@ -508,7 +502,7 @@ namespace opengl
 		return t;
 	}
 
-	void Graphics::setBackgroundColor(Color c)
+	void Graphics::setBackgroundColor(const Color& c)
 	{
 		glClearColor((float)c.r/255.0f, (float)c.g/255.0f, (float)c.b/255.0f, (float)c.a/255.0f);
 	}

+ 4 - 10
src/modules/graphics/opengl/Graphics.h

@@ -31,6 +31,7 @@
 
 // LOVE
 #include <graphics/Graphics.h>
+#include <graphics/Color.h>
 
 #include <image/Image.h>
 #include <image/ImageData.h>
@@ -48,10 +49,6 @@ namespace graphics
 {
 namespace opengl
 {
-	struct Color
-	{
-		unsigned char r, g, b, a;
-	};
 
 	struct DisplayMode
 	{
@@ -93,10 +90,7 @@ namespace opengl
 		// Default values.
 		DisplayState()
 		{
-			color.r = 255;
-			color.g = 255;
-			color.b = 255;
-			color.a = 255;
+			color.set(255,255,255,255);
 			backgroundColor.r = 0;
 			backgroundColor.g = 0;
 			backgroundColor.b = 0;
@@ -265,7 +259,7 @@ namespace opengl
 		/**
 		* Sets the foreground color.
 		**/
-		void setColor(Color c);
+		void setColor(const Color& c);
 
 		/**
 		* Gets current color.
@@ -275,7 +269,7 @@ namespace opengl
 		/**
 		* Sets the background Color.
 		**/
-		void setBackgroundColor(Color c);
+		void setBackgroundColor(const Color& c);
 
 		/**
 		* Gets the current background color.