Colors are now compare- and explicit convertible

This commit is contained in:
Michael Contento 2013-11-07 18:13:33 +01:00
parent 237f7a7839
commit 0160bec3ad
2 changed files with 253 additions and 86 deletions

View File

@ -26,13 +26,192 @@
NS_CC_BEGIN
Color4B::Color4B(const Color4F &color4F)
: r((GLubyte)(color4F.r * 255.0f)),
g((GLubyte)(color4F.g * 255.0f)),
b((GLubyte)(color4F.b * 255.0f)),
a((GLubyte)(color4F.a * 255.0f))
/**
* Color3B
*/
Color3B::Color3B()
: r(0)
, g(0)
, b(0)
{}
Color3B::Color3B(GLubyte _r, GLubyte _g, GLubyte _b)
: r(_r)
, g(_g)
, b(_b)
{}
Color3B::Color3B(const Color4B& color)
: r(color.r)
, g(color.g)
, b(color.b)
{}
Color3B::Color3B(const Color4F& color)
: r(color.r * 255.0f)
, g(color.g * 255.0f)
, b(color.b * 255.0f)
{}
bool Color3B::operator==(const Color3B& right) const
{
return (r == right.r && g == right.g && b == right.b);
}
bool Color3B::operator==(const Color4B& right) const
{
return (r == right.r && g == right.g && b == right.b && 255 == right.a);
}
bool Color3B::operator==(const Color4F& right) const
{
return (right.a == 1.0f && Color4F(*this) == right);
}
bool Color3B::operator!=(const Color3B& right) const
{
return !(*this == right);
}
bool Color3B::operator!=(const Color4B& right) const
{
return !(*this == right);
}
bool Color3B::operator!=(const Color4F& right) const
{
return !(*this == right);
}
/**
* Color4B
*/
Color4B::Color4B()
: r(0)
, g(0)
, b(0)
, a(0)
{}
Color4B::Color4B(GLubyte _r, GLubyte _g, GLubyte _b, GLubyte _a)
: r(_r)
, g(_g)
, b(_b)
, a(_a)
{}
Color4B::Color4B(const Color3B& color)
: r(color.r * 255)
, g(color.g * 255)
, b(color.b * 255)
, a(255)
{}
Color4B::Color4B(const Color4F& color)
: r(color.r * 255)
, g(color.g * 255)
, b(color.b * 255)
, a(color.a * 255)
{}
bool Color4B::operator==(const Color4B& right) const
{
return (r == right.r && g == right.g && b == right.b && a == right.a);
}
bool Color4B::operator==(const Color3B& right) const
{
return (r == right.r && g == right.g && b == right.b && a == 255);
}
bool Color4B::operator==(const Color4F& right) const
{
return (*this == Color4B(right));
}
bool Color4B::operator!=(const Color4B& right) const
{
return !(*this == right);
}
bool Color4B::operator!=(const Color3B& right) const
{
return !(*this == right);
}
bool Color4B::operator!=(const Color4F& right) const
{
return !(*this == right);
}
/**
* Color4F
*/
Color4F::Color4F()
: r(0.0f)
, g(0.0f)
, b(0.0f)
, a(0.0f)
{}
Color4F::Color4F(float _r, float _g, float _b, float _a)
: r(_r)
, g(_g)
, b(_b)
, a(_a)
{}
Color4F::Color4F(const Color3B& color)
: r(color.r / 255.0f)
, g(color.g / 255.0f)
, b(color.b / 255.0f)
, a(1.0f)
{}
Color4F::Color4F(const Color4B& color)
: r(color.r / 255.0f)
, g(color.g / 255.0f)
, b(color.b / 255.0f)
, a(color.a / 255.0f)
{}
bool Color4F::operator==(const Color4F& right) const
{
return (r == right.r && g == right.g && b == right.b && a == right.a);
}
bool Color4F::operator==(const Color3B& right) const
{
return (a == 1.0f && Color3B(*this) == right);
}
bool Color4F::operator==(const Color4B& right) const
{
return (*this == Color4F(right));
}
bool Color4F::operator!=(const Color4F& right) const
{
return !(*this == right);
}
bool Color4F::operator!=(const Color3B& right) const
{
return !(*this == right);
}
bool Color4F::operator!=(const Color4B& right) const
{
return !(*this == right);
}
/**
* Color constants
*/
const Color3B Color3B::WHITE (255, 255, 255);
const Color3B Color3B::YELLOW (255, 255, 0);
const Color3B Color3B::GREEN ( 0, 255, 0);

View File

@ -31,27 +31,31 @@ THE SOFTWARE.
#include "CCGeometry.h"
#include "CCGL.h"
NS_CC_BEGIN
struct Color4B;
struct Color4F;
/** RGB color composed of bytes 3 bytes
@since v3.0
*/
struct Color3B
{
Color3B(): r(0), g(0), b(0) {}
Color3B();
Color3B(GLubyte _r, GLubyte _g, GLubyte _b);
explicit Color3B(const Color4B& color);
explicit Color3B(const Color4F& color);
Color3B(GLubyte _r, GLubyte _g, GLubyte _b)
: r(_r)
, g(_g)
, b(_b)
{}
bool operator==(const Color3B& right) const;
bool operator==(const Color4B& right) const;
bool operator==(const Color4F& right) const;
bool operator!=(const Color3B& right) const;
bool operator!=(const Color4B& right) const;
bool operator!=(const Color4F& right) const;
bool equals(const Color3B& other)
{
return (this->r == other.r &&
this->g == other.g &&
this->b == other.b);
return (*this == other);
}
GLubyte r;
@ -69,24 +73,22 @@ struct Color3B
const static Color3B GRAY;
};
struct Color4F;
/** RGBA color composed of 4 bytes
@since v3.0
*/
struct Color4B
{
Color4B(GLubyte _r, GLubyte _g, GLubyte _b, GLubyte _a)
: r(_r)
, g(_g)
, b(_b)
, a(_a)
{}
Color4B();
Color4B(GLubyte _r, GLubyte _g, GLubyte _b, GLubyte _a);
explicit Color4B(const Color3B& color);
explicit Color4B(const Color4F& color);
Color4B(): r(0), g(0), b(0), a(0) {}
// This function should use Color4F, so implement it in .cpp file.
explicit Color4B(const Color4F &color4F);
bool operator==(const Color4B& right) const;
bool operator==(const Color3B& right) const;
bool operator==(const Color4F& right) const;
bool operator!=(const Color4B& right) const;
bool operator!=(const Color3B& right) const;
bool operator!=(const Color4F& right) const;
GLubyte r;
GLubyte g;
@ -110,35 +112,21 @@ struct Color4B
*/
struct Color4F
{
Color4F(float _r, float _g, float _b, float _a)
: r(_r)
, g(_g)
, b(_b)
, a(_a)
{}
Color4F();
Color4F(float _r, float _g, float _b, float _a);
explicit Color4F(const Color3B& color);
explicit Color4F(const Color4B& color);
explicit Color4F(const Color3B &color3B)
: r(color3B.r / 255.0f)
, g(color3B.g / 255.0f)
, b(color3B.b / 255.0f)
, a(1.f)
{}
explicit Color4F(const Color4B &color4B)
: r(color4B.r / 255.0f)
, g(color4B.g / 255.0f)
, b(color4B.b / 255.0f)
, a(color4B.a / 255.0f)
{}
Color4F(): r(0.f), g(0.f), b(0.f), a(0.f) {}
bool operator==(const Color4F& right) const;
bool operator==(const Color3B& right) const;
bool operator==(const Color4B& right) const;
bool operator!=(const Color4F& right) const;
bool operator!=(const Color3B& right) const;
bool operator!=(const Color4B& right) const;
bool equals(const Color4F &other)
{
return (this->r == other.r &&
this->g == other.g &&
this->b == other.b &&
this->a == other.a);
return (*this == other);
}
GLfloat r;