Sfoglia il codice sorgente

Refactor: Cleaned up Color class and updated its code style

BearishSun 8 anni fa
parent
commit
e1fffb303d
2 ha cambiato i file con 302 aggiunte e 289 eliminazioni
  1. 120 120
      Source/BansheeUtility/Image/BsColor.cpp
  2. 182 169
      Source/BansheeUtility/Image/BsColor.h

+ 120 - 120
Source/BansheeUtility/Image/BsColor.cpp

@@ -5,66 +5,66 @@
 
 namespace bs 
 {
-    const Color Color::ZERO = Color(0.0,0.0,0.0,0.0);
-    const Color Color::Black = Color(0.0,0.0,0.0);
-    const Color Color::White = Color(1.0,1.0,1.0);
-    const Color Color::Red = Color(1.0,0.0,0.0);
-    const Color Color::Green = Color(0.0,1.0,0.0);
-    const Color Color::Blue = Color(0.0,0.0,1.0);
-
-    ABGR Color::getAsABGR() const
-    {
-        UINT8 val8;
-        UINT32 val32 = 0;
+	const Color Color::ZERO = Color(0.0,0.0,0.0,0.0);
+	const Color Color::Black = Color(0.0,0.0,0.0);
+	const Color Color::White = Color(1.0,1.0,1.0);
+	const Color Color::Red = Color(1.0,0.0,0.0);
+	const Color Color::Green = Color(0.0,1.0,0.0);
+	const Color Color::Blue = Color(0.0,0.0,1.0);
+
+	ABGR Color::getAsABGR() const
+	{
+		UINT8 val8;
+		UINT32 val32 = 0;
 
-        // Convert to 32bit pattern
-        // (RGBA = 8888)
+		// Convert to 32bit pattern
+		// (RGBA = 8888)
 
-        // Red
-        val8 = static_cast<UINT8>(r * 255);
-        val32 = val8 << 24;
+		// Red
+		val8 = static_cast<UINT8>(r * 255);
+		val32 = val8 << 24;
 
-        // Green
-        val8 = static_cast<UINT8>(g * 255);
-        val32 += val8 << 16;
+		// Green
+		val8 = static_cast<UINT8>(g * 255);
+		val32 += val8 << 16;
 
-        // Blue
-        val8 = static_cast<UINT8>(b * 255);
-        val32 += val8 << 8;
+		// Blue
+		val8 = static_cast<UINT8>(b * 255);
+		val32 += val8 << 8;
 
-        // Alpha
-        val8 = static_cast<UINT8>(a * 255);
-        val32 += val8;
+		// Alpha
+		val8 = static_cast<UINT8>(a * 255);
+		val32 += val8;
 
-        return val32;
-    }
+		return val32;
+	}
 
-    BGRA Color::getAsBGRA() const
-    {
-        UINT8 val8;
-        UINT32 val32 = 0;
+	BGRA Color::getAsBGRA() const
+	{
+		UINT8 val8;
+		UINT32 val32 = 0;
 
-        // Convert to 32bit pattern
-        // (ARGB = 8888)
+		// Convert to 32bit pattern
+		// (ARGB = 8888)
 
-        // Alpha
-        val8 = static_cast<UINT8>(a * 255);
-        val32 = val8 << 24;
+		// Alpha
+		val8 = static_cast<UINT8>(a * 255);
+		val32 = val8 << 24;
 
-        // Red
-        val8 = static_cast<UINT8>(r * 255);
-        val32 += val8 << 16;
+		// Red
+		val8 = static_cast<UINT8>(r * 255);
+		val32 += val8 << 16;
 
-        // Green
-        val8 = static_cast<UINT8>(g * 255);
-        val32 += val8 << 8;
+		// Green
+		val8 = static_cast<UINT8>(g * 255);
+		val32 += val8 << 8;
 
-        // Blue
-        val8 = static_cast<UINT8>(b * 255);
-        val32 += val8;
+		// Blue
+		val8 = static_cast<UINT8>(b * 255);
+		val32 += val8;
 
-        return val32;
-    }
+		return val32;
+	}
 
 	ARGB Color::getAsARGB() const
 	{
@@ -94,73 +94,73 @@ namespace bs
 		return val32;
 	}
 
-    RGBA Color::getAsRGBA() const
-    {
-        UINT8 val8;
-        UINT32 val32 = 0;
+	RGBA Color::getAsRGBA() const
+	{
+		UINT8 val8;
+		UINT32 val32 = 0;
 
-        // Convert to 32bit pattern
-        // (ABRG = 8888)
+		// Convert to 32bit pattern
+		// (ABRG = 8888)
 
-        // Alpha
-        val8 = static_cast<UINT8>(a * 255);
-        val32 = val8 << 24;
+		// Alpha
+		val8 = static_cast<UINT8>(a * 255);
+		val32 = val8 << 24;
 
-        // Blue
-        val8 = static_cast<UINT8>(b * 255);
-        val32 += val8 << 16;
+		// Blue
+		val8 = static_cast<UINT8>(b * 255);
+		val32 += val8 << 16;
 
-        // Green
-        val8 = static_cast<UINT8>(g * 255);
-        val32 += val8 << 8;
+		// Green
+		val8 = static_cast<UINT8>(g * 255);
+		val32 += val8 << 8;
 
-        // Red
-        val8 = static_cast<UINT8>(r * 255);
-        val32 += val8;
+		// Red
+		val8 = static_cast<UINT8>(r * 255);
+		val32 += val8;
 
 
-        return val32;
-    }
+		return val32;
+	}
 
-    void Color::setAsABGR(const ABGR val)
-    {
-        UINT32 val32 = val;
+	void Color::setAsABGR(const ABGR val)
+	{
+		UINT32 val32 = val;
 
-        // Convert from 32bit pattern
-        // (RGBA = 8888)
+		// Convert from 32bit pattern
+		// (RGBA = 8888)
 
-        // Red
-        r = ((val32 >> 24) & 0xFF) / 255.0f;
+		// Red
+		r = ((val32 >> 24) & 0xFF) / 255.0f;
 
-        // Green
-        g = ((val32 >> 16) & 0xFF) / 255.0f;
+		// Green
+		g = ((val32 >> 16) & 0xFF) / 255.0f;
 
-        // Blue
-        b = ((val32 >> 8) & 0xFF) / 255.0f;
+		// Blue
+		b = ((val32 >> 8) & 0xFF) / 255.0f;
 
-        // Alpha
-        a = (val32 & 0xFF) / 255.0f;
-    }
+		// Alpha
+		a = (val32 & 0xFF) / 255.0f;
+	}
 
-    void Color::setAsBGRA(const BGRA val)
-    {
-        UINT32 val32 = val;
+	void Color::setAsBGRA(const BGRA val)
+	{
+		UINT32 val32 = val;
 
-        // Convert from 32bit pattern
-        // (ARGB = 8888)
+		// Convert from 32bit pattern
+		// (ARGB = 8888)
 
-        // Alpha
-        a = ((val32 >> 24) & 0xFF) / 255.0f;
+		// Alpha
+		a = ((val32 >> 24) & 0xFF) / 255.0f;
 
-        // Red
-        r = ((val32 >> 16) & 0xFF) / 255.0f;
+		// Red
+		r = ((val32 >> 16) & 0xFF) / 255.0f;
 
-        // Green
-        g = ((val32 >> 8) & 0xFF) / 255.0f;
+		// Green
+		g = ((val32 >> 8) & 0xFF) / 255.0f;
 
-        // Blue
-        b = (val32 & 0xFF) / 255.0f;
-    }
+		// Blue
+		b = (val32 & 0xFF) / 255.0f;
+	}
 
 	void Color::setAsARGB(const ARGB val)
 	{
@@ -182,38 +182,38 @@ namespace bs
 		a = (val32 & 0xFF) / 255.0f;
 	}
 
-    void Color::setAsRGBA(const RGBA val)
-    {
-        UINT32 val32 = val;
+	void Color::setAsRGBA(const RGBA val)
+	{
+		UINT32 val32 = val;
 
-        // Convert from 32bit pattern
-        // (ABGR = 8888)
+		// Convert from 32bit pattern
+		// (ABGR = 8888)
 
-        // Alpha
-        a = ((val32 >> 24) & 0xFF) / 255.0f;
+		// Alpha
+		a = ((val32 >> 24) & 0xFF) / 255.0f;
 
-        // Blue
-        b = ((val32 >> 16) & 0xFF) / 255.0f;
+		// Blue
+		b = ((val32 >> 16) & 0xFF) / 255.0f;
 
-        // Green
-        g = ((val32 >> 8) & 0xFF) / 255.0f;
+		// Green
+		g = ((val32 >> 8) & 0xFF) / 255.0f;
 
-        // Red
-        r = (val32 & 0xFF) / 255.0f;
-    }
+		// Red
+		r = (val32 & 0xFF) / 255.0f;
+	}
 
-    bool Color::operator==(const Color& rhs) const
-    {
-        return (r == rhs.r &&
-            g == rhs.g &&
-            b == rhs.b &&
-            a == rhs.a);
-    }
+	bool Color::operator==(const Color& rhs) const
+	{
+		return (r == rhs.r &&
+			g == rhs.g &&
+			b == rhs.b &&
+			a == rhs.a);
+	}
 
-    bool Color::operator!=(const Color& rhs) const
-    {
-        return !(*this == rhs);
-    }
+	bool Color::operator!=(const Color& rhs) const
+	{
+		return !(*this == rhs);
+	}
 
 	void Color::setHSB(float hue, float saturation, float brightness)
 	{

+ 182 - 169
Source/BansheeUtility/Image/BsColor.h

@@ -10,74 +10,87 @@ namespace bs
 	 *  @{
 	 */
 
-    typedef UINT32 RGBA;
-    typedef UINT32 ARGB;
-    typedef UINT32 ABGR;
+	typedef UINT32 RGBA;
+	typedef UINT32 ARGB;
+	typedef UINT32 ABGR;
 	typedef UINT32 BGRA;
 
 	/** 
 	 * Color represented as 4 components, each being a floating point value ranging from 0 to 1. Color components are 
 	 * red, green, blue and alpha.
 	 */
-    class BS_UTILITY_EXPORT Color
-    {
-    public:
-        static const Color ZERO;
-        static const Color Black;
-        static const Color White;
-        static const Color Red;
-        static const Color Green;
-        static const Color Blue;
-
-	    explicit Color(float red = 1.0f, float green = 1.0f,
-				    float blue = 1.0f, float alpha = 1.0f ) 
-					:r(red), g(green), b(blue), a(alpha)
-        { }
-
-	    bool operator==(const Color& rhs) const;
-	    bool operator!=(const Color& rhs) const;
-
-	    RGBA getAsRGBA(void) const;
-	    ARGB getAsARGB(void) const;
-		BGRA getAsBGRA(void) const;
-	    ABGR getAsABGR(void) const;
-
-        void setAsRGBA(const RGBA val);
-        void setAsARGB(const ARGB val);
-		void setAsBGRA(const BGRA val);
-        void setAsABGR(const ABGR val);
-
-		/** Clamps colour value to the range [0, 1]. */
-        void saturate()
-        {
-            if (r < 0)
-                r = 0;
-            else if (r > 1)
-                r = 1;
-
-            if (g < 0)
-                g = 0;
-            else if (g > 1)
-                g = 1;
-
-            if (b < 0)
-                b = 0;
-            else if (b > 1)
-                b = 1;
-
-            if (a < 0)
-                a = 0;
-            else if (a > 1)
-                a = 1;
-        }
+	class BS_UTILITY_EXPORT Color
+	{
+	public:
+		static const Color ZERO;
+		static const Color Black;
+		static const Color White;
+		static const Color Red;
+		static const Color Green;
+		static const Color Blue;
+
+		explicit Color(float red = 1.0f, float green = 1.0f, float blue = 1.0f, float alpha = 1.0f ) 
+			:r(red), g(green), b(blue), a(alpha)
+		{ }
+
+		bool operator==(const Color& rhs) const;
+		bool operator!=(const Color& rhs) const;
+
+		/** Returns the color as a 32-bit value in RGBA order. */
+		RGBA getAsRGBA() const;
+
+		/** Returns the color as a 32-bit value in ARGB order. */
+		ARGB getAsARGB() const;
+
+		/** Returns the color as a 32-bit value in BGRA order. */
+		BGRA getAsBGRA() const;
+
+		/** Returns the color as a 32-bit value in ABGR order. */
+		ABGR getAsABGR() const;
+
+		/** Assigns the color from a 32-bit value that encodes a RGBA color. */
+		void setAsRGBA(RGBA val);
+
+		/** Assigns the color from a 32-bit value that encodes a ARGB color. */
+		void setAsARGB(ARGB val);
+
+		/** Assigns the color from a 32-bit value that encodes a BGRA color. */
+		void setAsBGRA(BGRA val);
+
+		/** Assigns the color from a 32-bit value that encodes a ABGR color. */
+		void setAsABGR(ABGR val);
+
+		/** Clamps color value to the range [0, 1]. */
+		void saturate()
+		{
+			if (r < 0)
+				r = 0;
+			else if (r > 1)
+				r = 1;
+
+			if (g < 0)
+				g = 0;
+			else if (g > 1)
+				g = 1;
+
+			if (b < 0)
+				b = 0;
+			else if (b > 1)
+				b = 1;
+
+			if (a < 0)
+				a = 0;
+			else if (a > 1)
+				a = 1;
+		}
 
 		/** Clamps colour value to the range [0, 1]. Returned saturated color as a copy. */
-        Color saturateCopy() const
-        {
-            Color ret = *this;
-            ret.saturate();
-            return ret;
-        }
+		Color saturateCopy() const
+		{
+			Color ret = *this;
+			ret.saturate();
+			return ret;
+		}
 
 		float operator[] (const UINT32 i) const
 		{
@@ -105,139 +118,139 @@ namespace bs
 			return &r;
 		}
 
-        Color operator+ (const Color& rhs) const
-        {
-            Color kSum;
+		Color operator+ (const Color& rhs) const
+		{
+			Color sum;
 
-            kSum.r = r + rhs.r;
-            kSum.g = g + rhs.g;
-            kSum.b = b + rhs.b;
-            kSum.a = a + rhs.a;
+			sum.r = r + rhs.r;
+			sum.g = g + rhs.g;
+			sum.b = b + rhs.b;
+			sum.a = a + rhs.a;
 
-            return kSum;
-        }
-
-        Color operator- ( const Color& rhs) const
-        {
-            Color kDiff;
-
-            kDiff.r = r - rhs.r;
-            kDiff.g = g - rhs.g;
-            kDiff.b = b - rhs.b;
-            kDiff.a = a - rhs.a;
+			return sum;
+		}
 
-            return kDiff;
-        }
+		Color operator- (const Color& rhs) const
+		{
+			Color diff;
 
-        Color operator* (const float rhs) const
-        {
-            Color kProd;
+			diff.r = r - rhs.r;
+			diff.g = g - rhs.g;
+			diff.b = b - rhs.b;
+			diff.a = a - rhs.a;
 
-            kProd.r = rhs*r;
-            kProd.g = rhs*g;
-            kProd.b = rhs*b;
-            kProd.a = rhs*a;
+			return diff;
+		}
 
-            return kProd;
-        }
+		Color operator* (float rhs) const
+		{
+			Color prod;
 
-        Color operator* (const Color& rhs) const
-        {
-            Color kProd;
+			prod.r = rhs*r;
+			prod.g = rhs*g;
+			prod.b = rhs*b;
+			prod.a = rhs*a;
 
-            kProd.r = rhs.r * r;
-            kProd.g = rhs.g * g;
-            kProd.b = rhs.b * b;
-            kProd.a = rhs.a * a;
+			return prod;
+		}
+
+		Color operator* (const Color& rhs) const
+		{
+			Color prod;
 
-            return kProd;
-        }
+			prod.r = rhs.r * r;
+			prod.g = rhs.g * g;
+			prod.b = rhs.b * b;
+			prod.a = rhs.a * a;
+
+			return prod;
+		}
 
-        Color operator/ (const Color& rhs) const
-        {
-            Color kProd;
+		Color operator/ (const Color& rhs) const
+		{
+			Color prod;
 
-            kProd.r = rhs.r / r;
-            kProd.g = rhs.g / g;
-            kProd.b = rhs.b / b;
-            kProd.a = rhs.a / a;
+			prod.r = rhs.r / r;
+			prod.g = rhs.g / g;
+			prod.b = rhs.b / b;
+			prod.a = rhs.a / a;
 
-            return kProd;
-        }
+			return prod;
+		}
 
-        Color operator/ (const float rhs) const
-        {
-            assert(rhs != 0.0f);
+		Color operator/ (float rhs) const
+		{
+			assert(rhs != 0.0f);
 
-            Color kDiv;
+			Color div;
 
-            float fInv = 1.0f / rhs;
-            kDiv.r = r * fInv;
-            kDiv.g = g * fInv;
-            kDiv.b = b * fInv;
-            kDiv.a = a * fInv;
+			float invRhs = 1.0f / rhs;
+			div.r = r * invRhs;
+			div.g = g * invRhs;
+			div.b = b * invRhs;
+			div.a = a * invRhs;
 
-            return kDiv;
-        }
+			return div;
+		}
 
-        friend Color operator* (const float lhs, const Color& rhs)
-        {
-            Color result;
+		friend Color operator* (float lhs, const Color& rhs)
+		{
+			Color result;
 
-            result.r = lhs * rhs.r;
-            result.g = lhs * rhs.g;
-            result.b = lhs * rhs.b;
-            result.a = lhs * rhs.a;
+			result.r = lhs * rhs.r;
+			result.g = lhs * rhs.g;
+			result.b = lhs * rhs.b;
+			result.a = lhs * rhs.a;
 
-            return result;
-        }
+			return result;
+		}
 
-        Color& operator+= (const Color& rhs)
-        {
-            r += rhs.r;
-            g += rhs.g;
-            b += rhs.b;
-            a += rhs.a;
+		Color& operator+= (const Color& rhs)
+		{
+			r += rhs.r;
+			g += rhs.g;
+			b += rhs.b;
+			a += rhs.a;
 
-            return *this;
-        }
+			return *this;
+		}
 
-        Color& operator-= (const Color& rhs)
-        {
-            r -= rhs.r;
-            g -= rhs.g;
-            b -= rhs.b;
-            a -= rhs.a;
+		Color& operator-= (const Color& rhs)
+		{
+			r -= rhs.r;
+			g -= rhs.g;
+			b -= rhs.b;
+			a -= rhs.a;
 
-            return *this;
-        }
+			return *this;
+		}
 
-        Color& operator*= (const float rhs)
-        {
-            r *= rhs;
-            g *= rhs;
-            b *= rhs;
-            a *= rhs;
+		Color& operator*= (float rhs)
+		{
+			r *= rhs;
+			g *= rhs;
+			b *= rhs;
+			a *= rhs;
 
-            return *this;
-        }
+			return *this;
+		}
 
-        Color& operator/= (const float rhs)
-        {
-            assert(rhs != 0.0f);
+		Color& operator/= (float rhs)
+		{
+			assert(rhs != 0.0f);
 
-            float fInv = 1.0f / rhs;
+			float invRhs = 1.0f / rhs;
 
-            r *= fInv;
-            g *= fInv;
-            b *= fInv;
-            a *= fInv;
+			r *= invRhs;
+			g *= invRhs;
+			b *= invRhs;
+			a *= invRhs;
 
-            return *this;
-        }
+			return *this;
+		}
 
 		/** 
-		 * Set a colour value from Hue, Saturation and Brightness.
+		 * Set a color value from hue, saturation and brightness.
 		 *
 		 * @param[in] hue			Hue value, scaled to the [0,1] range.
 		 * @param[in] saturation	Saturation level, [0,1].
@@ -246,7 +259,7 @@ namespace bs
 		void setHSB(float hue, float saturation, float brightness);
 
 		/** 
-		 * Convert the current color to Hue, Saturation and Brightness values. 
+		 * Convert the current color to hue, saturation and brightness values. 
 		 * 
 		 * @param[in] hue			Output hue value, scaled to the [0,1] range.
 		 * @param[in] saturation	Output saturation level, [0,1].
@@ -254,14 +267,14 @@ namespace bs
 		 */
 		void getHSB(float* hue, float* saturation, float* brightness) const;
 
-        /**
-        * Linearly interpolates between the two colors using @p t. t should be in [0, 1] range, where t = 0 corresponds
-        * to the left color, while t = 1 corresponds to the right color.
-        */
-        static Color lerp(float t, const Color& a, const Color& b);
+		/**
+		 * Linearly interpolates between the two colors using @p t. t should be in [0, 1] range, where t = 0 corresponds
+		 * to the left color, while t = 1 corresponds to the right color.
+		 */
+		static Color lerp(float t, const Color& a, const Color& b);
 
 		float r, g, b, a;
-    };
+	};
 
 	/** @cond SPECIALIZATIONS */
 	BS_ALLOW_MEMCPY_SERIALIZATION(Color);