Color formatting fix

This commit is contained in:
Alex Zolotarev 2014-07-07 23:03:36 +02:00 committed by Alex Zolotarev
parent b4e256b642
commit 59bf4fa529
2 changed files with 132 additions and 117 deletions

View file

@ -3,96 +3,105 @@
namespace graphics
{
Color::Color(uint8_t r1, uint8_t g1, uint8_t b1, uint8_t a1)
: r(r1), g(g1), b(b1), a(a1)
{}
Color::Color() : r(0), g(0), b(0), a(0)
{}
Color::Color(uint8_t r1, uint8_t g1, uint8_t b1, uint8_t a1)
: r(r1), g(g1), b(b1), a(a1)
{}
Color::Color(Color const & c)
Color::Color() : r(0), g(0), b(0), a(0)
{}
Color::Color(Color const & c)
{
r = c.r;
g = c.g;
b = c.b;
a = c.a;
}
Color const & Color::operator=(Color const & c)
{
if (&c != this)
{
r = c.r;
g = c.g;
b = c.b;
a = c.a;
}
Color const & Color::operator=(Color const & c)
{
if (&c != this)
{
r = c.r;
g = c.g;
b = c.b;
a = c.a;
}
return *this;
}
Color const Color::fromXRGB(uint32_t _c, uint8_t _a)
{
return Color(
redFromARGB(_c),
greenFromARGB(_c),
blueFromARGB(_c),
_a);
}
Color const Color::fromARGB(uint32_t _c)
{
return Color(
redFromARGB(_c),
greenFromARGB(_c),
blueFromARGB(_c),
alphaFromARGB(_c));
}
Color const & Color::operator /= (unsigned k)
{
r /= k;
g /= k;
b /= k;
a /= k;
return *this;
}
bool operator < (Color const & l, Color const & r)
{
if (l.r != r.r) return l.r < r.r;
if (l.g != r.g) return l.g < r.g;
if (l.b != r.b) return l.b < r.b;
if (l.a != r.a) return l.a < r.a;
return false;
}
bool operator > (Color const & l, Color const & r)
{
if (l.r != r.r) return l.r > r.r;
if (l.g != r.g) return l.g > r.g;
if (l.b != r.b) return l.b > r.b;
if (l.a != r.a) return l.a > r.a;
return false;
}
bool operator != (Color const & l, Color const & r)
{
return (l.r != r.r) || (l.g != r.g) || (l.b != r.b) || (l.a != r.a);
}
bool operator == (Color const & l, Color const & r)
{
return !(l != r);
}
Color Color::Black()
{
return Color(0, 0, 0, 255);
}
Color Color::White()
{
return Color(255, 255, 255, 255);
}
return *this;
}
Color const Color::fromXRGB(uint32_t _c, uint8_t _a)
{
return Color(redFromARGB(_c), greenFromARGB(_c), blueFromARGB(_c), _a);
}
Color const Color::fromARGB(uint32_t _c)
{
return Color(redFromARGB(_c), greenFromARGB(_c), blueFromARGB(_c), alphaFromARGB(_c));
}
Color const & Color::operator /= (unsigned k)
{
r /= k;
g /= k;
b /= k;
a /= k;
return *this;
}
bool operator < (Color const & l, Color const & r)
{
if (l.r != r.r) return l.r < r.r;
if (l.g != r.g) return l.g < r.g;
if (l.b != r.b) return l.b < r.b;
if (l.a != r.a) return l.a < r.a;
return false;
}
bool operator > (Color const & l, Color const & r)
{
if (l.r != r.r) return l.r > r.r;
if (l.g != r.g) return l.g > r.g;
if (l.b != r.b) return l.b > r.b;
if (l.a != r.a) return l.a > r.a;
return false;
}
bool operator != (Color const & l, Color const & r)
{
return (l.r != r.r) || (l.g != r.g) || (l.b != r.b) || (l.a != r.a);
}
bool operator == (Color const & l, Color const & r)
{
return !(l != r);
}
Color Color::Black()
{
return Color(0, 0, 0, 255);
}
Color Color::White()
{
return Color(255, 255, 255, 255);
}
Color Color::Red()
{
return Color(255, 0, 0, 255);
}
Color Color::Green()
{
return Color(0, 255, 0, 255);
}
Color Color::Blue()
{
return Color(0, 0, 255, 255);
}
}

View file

@ -6,44 +6,50 @@
namespace graphics
{
struct Color
{
uint8_t r;
uint8_t g;
uint8_t b;
uint8_t a;
Color(uint8_t r1, uint8_t g1, uint8_t b1, uint8_t a1);
Color();
Color(Color const & p);
Color const & operator=(Color const & p);
struct Color
{
uint8_t r;
uint8_t g;
uint8_t b;
uint8_t a;
static Color const fromARGB(uint32_t _c);
static Color const fromXRGB(uint32_t _c, uint8_t _a = 0);
/// alpha 255 means non-transparent and 0 means fully transparent
Color(uint8_t r1, uint8_t g1, uint8_t b1, uint8_t a1);
Color();
Color(Color const & p);
Color const & operator=(Color const & p);
Color const & operator /= (unsigned k);
static Color const fromARGB(uint32_t _c);
static Color const fromXRGB(uint32_t _c, uint8_t _a = 0);
static Color Black();
static Color White();
};
Color const & operator /= (unsigned k);
bool operator < (Color const & l, Color const & r);
bool operator > (Color const & l, Color const & r);
bool operator !=(Color const & l, Color const & r);
bool operator ==(Color const & l, Color const & r);
static Color Black();
static Color White();
static Color Red();
static Color Green();
static Color Blue();
};
inline int redFromARGB(uint32_t c) {return (c >> 16) & 0xFF;}
inline int greenFromARGB(uint32_t c) {return (c >> 8) & 0xFF; }
inline int blueFromARGB(uint32_t c) {return (c & 0xFF); }
inline int alphaFromARGB(uint32_t c) {return (c >> 24) & 0xFF;}
bool operator < (Color const & l, Color const & r);
bool operator > (Color const & l, Color const & r);
bool operator !=(Color const & l, Color const & r);
bool operator ==(Color const & l, Color const & r);
inline int redFromARGB(uint32_t c) {return (c >> 16) & 0xFF;}
inline int greenFromARGB(uint32_t c) {return (c >> 8) & 0xFF; }
inline int blueFromARGB(uint32_t c) {return (c & 0xFF); }
inline int alphaFromARGB(uint32_t c) {return (c >> 24) & 0xFF;}
inline string DebugPrint(Color const & c)
{
ostringstream os;
os << "r: " << (int)c.r << " ";
os << "g: " << (int)c.g << " ";
os << "b: " << (int)c.b << " ";
os << "alpha: " << (int)c.a;
return os.str();
}
inline string DebugPrint(Color const & c)
{
ostringstream os;
os << "r: " << (int)c.r << " ";
os << "g: " << (int)c.g << " ";
os << "b: " << (int)c.b << " ";
os << "alpha: " << (int)c.a;
return os.str();
}
}