2015-05-01 06:31:59 +00:00
|
|
|
#ifndef CCOLOR_HPP
|
|
|
|
#define CCOLOR_HPP
|
|
|
|
|
2015-11-26 02:38:43 +00:00
|
|
|
#include "Global.hpp"
|
2015-11-26 01:42:24 +00:00
|
|
|
#include "Math.hpp"
|
|
|
|
#include "TVectorUnion.hpp"
|
2015-11-26 02:38:43 +00:00
|
|
|
#if ZE_ATHENA_TYPES
|
|
|
|
#include <Athena/FileReader.hpp>
|
|
|
|
#endif
|
2015-11-26 01:26:23 +00:00
|
|
|
#include <iostream>
|
2015-05-01 06:31:59 +00:00
|
|
|
|
2015-08-12 01:41:28 +00:00
|
|
|
#if BYTE_ORDER == __ORDER_LITTLE_ENDIAN__
|
2015-11-26 02:38:43 +00:00
|
|
|
#define COLOR(rgba) (unsigned)( ( (rgba) & 0x000000FF ) << 24 | ( (rgba) & 0x0000FF00 ) << 8 \
|
2015-08-12 01:41:28 +00:00
|
|
|
| ( (rgba) & 0x00FF0000 ) >> 8 | ( (rgba) & 0xFF000000 ) >> 24 )
|
|
|
|
#else
|
2015-11-26 01:26:23 +00:00
|
|
|
#define COLOR(rgba) rgba
|
2015-08-12 01:41:28 +00:00
|
|
|
#endif
|
|
|
|
|
2015-10-08 00:21:38 +00:00
|
|
|
namespace Zeus
|
|
|
|
{
|
2015-11-26 01:26:23 +00:00
|
|
|
typedef union
|
|
|
|
{
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
unsigned char r, g, b, a;
|
|
|
|
};
|
|
|
|
unsigned int rgba;
|
|
|
|
} RGBA32;
|
|
|
|
|
|
|
|
typedef uint8_t Comp8;
|
|
|
|
typedef uint32_t Comp32;
|
2015-05-04 10:00:15 +00:00
|
|
|
|
2015-11-26 01:42:24 +00:00
|
|
|
class CVector4f;
|
|
|
|
|
2015-10-25 19:31:41 +00:00
|
|
|
class alignas(16) CColor
|
2015-05-01 06:31:59 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
ZE_DECLARE_ALIGNED_ALLOCATOR();
|
|
|
|
|
2015-08-12 01:41:28 +00:00
|
|
|
static const CColor skRed;
|
|
|
|
static const CColor skBlack;
|
|
|
|
static const CColor skBlue;
|
|
|
|
static const CColor skGreen;
|
|
|
|
static const CColor skGrey;
|
|
|
|
static const CColor skOrange;
|
|
|
|
static const CColor skPurple;
|
|
|
|
static const CColor skYellow;
|
|
|
|
static const CColor skWhite;
|
2015-11-29 02:55:05 +00:00
|
|
|
static const CColor skClear;
|
2015-08-12 01:41:28 +00:00
|
|
|
|
2015-11-26 01:26:23 +00:00
|
|
|
#if __SSE__
|
|
|
|
CColor(const __m128& mVec128) : mVec128(mVec128) {}
|
|
|
|
#endif
|
2015-05-04 10:00:15 +00:00
|
|
|
|
2015-11-26 01:26:23 +00:00
|
|
|
CColor() : r(1.0f), g(1.0f), b(1.0f), a(1.0f) {}
|
2015-05-04 10:00:15 +00:00
|
|
|
CColor(float rgb, float a = 1.0) { splat(rgb, a); }
|
2015-11-26 01:26:23 +00:00
|
|
|
CColor(float r, float g, float b, float a = 1.0f) {v[0] = r; v[1] = g; v[2] = b; v[3] = a; }
|
2015-11-01 12:04:29 +00:00
|
|
|
#if ZE_ATHENA_TYPES
|
2015-05-01 06:31:59 +00:00
|
|
|
CColor(Athena::io::IStreamReader& reader) {readRGBA(reader);}
|
2015-11-01 12:04:29 +00:00
|
|
|
#endif
|
2015-11-26 01:26:23 +00:00
|
|
|
|
2015-11-01 12:04:29 +00:00
|
|
|
CColor(Comp32 rgba) { fromRGBA32(rgba); }
|
2015-11-26 01:26:23 +00:00
|
|
|
CColor(const Comp8* rgba) { fromRGBA8(rgba[0], rgba[1], rgba[2], rgba[3]); }
|
2015-05-04 10:00:15 +00:00
|
|
|
|
2015-11-26 01:42:24 +00:00
|
|
|
CColor(const CVector4f& other);
|
|
|
|
CColor& operator=(const CVector4f& other);
|
|
|
|
|
2015-11-01 12:04:29 +00:00
|
|
|
#if ZE_ATHENA_TYPES
|
2015-05-01 06:31:59 +00:00
|
|
|
inline void readRGBA(Athena::io::IStreamReader& reader)
|
|
|
|
{
|
2015-11-26 01:26:23 +00:00
|
|
|
r = reader.readFloat();
|
|
|
|
g = reader.readFloat();
|
|
|
|
b = reader.readFloat();
|
|
|
|
a = reader.readFloat();
|
2015-05-01 06:31:59 +00:00
|
|
|
}
|
|
|
|
inline void readBGRA(Athena::io::IStreamReader& reader)
|
|
|
|
{
|
2015-11-26 01:26:23 +00:00
|
|
|
b = reader.readFloat();
|
|
|
|
g = reader.readFloat();
|
|
|
|
r = reader.readFloat();
|
|
|
|
a = reader.readFloat();
|
2015-05-01 06:31:59 +00:00
|
|
|
}
|
2015-11-01 12:04:29 +00:00
|
|
|
#endif
|
|
|
|
|
2015-05-04 10:00:15 +00:00
|
|
|
inline bool operator==(const CColor& rhs) const
|
2015-11-26 01:26:23 +00:00
|
|
|
{ return (r == rhs.r && g == rhs.g && b == rhs.b && a == rhs.a); }
|
2015-05-04 10:00:15 +00:00
|
|
|
inline bool operator!=(const CColor& rhs) const
|
|
|
|
{ return !(*this == rhs); }
|
|
|
|
inline CColor operator+(const CColor& rhs) const
|
|
|
|
{
|
2015-11-26 01:26:23 +00:00
|
|
|
#if __SSE__
|
|
|
|
return CColor(_mm_add_ps(mVec128, rhs.mVec128));
|
|
|
|
#else
|
|
|
|
return CColor(r + rhs.r, g + rhs.g, b + rhs.b, a + rhs.a);
|
|
|
|
#endif
|
2015-05-04 10:00:15 +00:00
|
|
|
}
|
|
|
|
inline CColor operator-(const CColor& rhs) const
|
|
|
|
{
|
2015-11-26 01:26:23 +00:00
|
|
|
#if __SSE__
|
|
|
|
return CColor(_mm_sub_ps(mVec128, rhs.mVec128));
|
|
|
|
#else
|
|
|
|
return CColor(r - rhs.r, g - rhs.g, b - rhs.b, a - rhs.a);
|
|
|
|
#endif
|
2015-05-04 10:00:15 +00:00
|
|
|
}
|
|
|
|
inline CColor operator*(const CColor& rhs) const
|
|
|
|
{
|
2015-11-26 01:26:23 +00:00
|
|
|
#if __SSE__
|
|
|
|
return CColor(_mm_mul_ps(mVec128, rhs.mVec128));
|
|
|
|
#else
|
|
|
|
return CColor(r * rhs.r, g * rhs.g, b * rhs.b, a * rhs.a);
|
|
|
|
#endif
|
2015-05-04 10:00:15 +00:00
|
|
|
}
|
|
|
|
inline CColor operator/(const CColor& rhs) const
|
|
|
|
{
|
2015-11-26 01:26:23 +00:00
|
|
|
#if __SSE__
|
|
|
|
return CColor(_mm_div_ps(mVec128, rhs.mVec128));
|
|
|
|
#else
|
|
|
|
return CColor(r / rhs.r, g / rhs.g, b / rhs.b, a / rhs.a);
|
|
|
|
#endif
|
2015-05-04 10:00:15 +00:00
|
|
|
}
|
|
|
|
inline CColor operator+(float val) const
|
|
|
|
{
|
2015-11-26 01:26:23 +00:00
|
|
|
#if __SSE__
|
|
|
|
TVectorUnion splat = {{val, val, val, val}};
|
|
|
|
return CColor(_mm_add_ps(mVec128, splat.mVec128));
|
|
|
|
#else
|
|
|
|
return CColor(r + val, g + val, b + val, a + val);
|
|
|
|
#endif
|
2015-05-04 10:00:15 +00:00
|
|
|
}
|
|
|
|
inline CColor operator-(float val) const
|
|
|
|
{
|
2015-11-26 01:26:23 +00:00
|
|
|
#if __SSE__
|
|
|
|
TVectorUnion splat = {{val, val, val, val}};
|
|
|
|
return CColor(_mm_sub_ps(mVec128, splat.mVec128));
|
|
|
|
#else
|
|
|
|
return CColor(r - val, g - val, b - val, a - val);
|
|
|
|
#endif
|
2015-05-04 10:00:15 +00:00
|
|
|
}
|
|
|
|
inline CColor operator*(float val) const
|
|
|
|
{
|
2015-11-26 01:26:23 +00:00
|
|
|
#if __SSE__
|
|
|
|
TVectorUnion splat = {{val, val, val, val}};
|
|
|
|
return CColor(_mm_mul_ps(mVec128, splat.mVec128));
|
|
|
|
#else
|
|
|
|
return CColor(r * val, g * val, b * val, a * val);
|
|
|
|
#endif
|
2015-05-04 10:00:15 +00:00
|
|
|
}
|
|
|
|
inline CColor operator/(float val) const
|
|
|
|
{
|
2015-11-26 01:26:23 +00:00
|
|
|
#if __SSE__
|
|
|
|
TVectorUnion splat = {{val, val, val, val}};
|
|
|
|
return CColor(_mm_div_ps(mVec128, splat.mVec128));
|
|
|
|
#else
|
|
|
|
return CColor(r / val, g / val, b / val, a / val);
|
|
|
|
#endif
|
2015-05-04 10:00:15 +00:00
|
|
|
}
|
|
|
|
inline const CColor& operator+=(const CColor& rhs)
|
|
|
|
{
|
2015-11-26 01:26:23 +00:00
|
|
|
#if __SSE__
|
|
|
|
mVec128 = _mm_add_ps(mVec128, rhs.mVec128);
|
|
|
|
#else
|
|
|
|
r += rhs.r; g += rhs.g; b += rhs.b; a += rhs.a;
|
|
|
|
#endif
|
|
|
|
return *this;
|
2015-05-04 10:00:15 +00:00
|
|
|
}
|
|
|
|
inline const CColor& operator-=(const CColor& rhs)
|
|
|
|
{
|
2015-11-26 01:26:23 +00:00
|
|
|
#if __SSE__
|
|
|
|
mVec128 = _mm_sub_ps(mVec128, rhs.mVec128);
|
|
|
|
#else
|
2015-05-04 10:00:15 +00:00
|
|
|
r -= rhs.r; g -= rhs.g; b -= rhs.b; a -= rhs.a;
|
2015-11-26 01:26:23 +00:00
|
|
|
#endif
|
2015-05-04 10:00:15 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
inline const CColor& operator *=(const CColor& rhs)
|
|
|
|
{
|
2015-11-26 01:26:23 +00:00
|
|
|
#if __SSE__
|
|
|
|
mVec128 = _mm_mul_ps(mVec128, rhs.mVec128);
|
|
|
|
#else
|
2015-05-04 10:00:15 +00:00
|
|
|
r *= rhs.r; g *= rhs.g; b *= rhs.b; a *= rhs.a;
|
2015-11-26 01:26:23 +00:00
|
|
|
#endif
|
2015-05-04 10:00:15 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
inline const CColor& operator /=(const CColor& rhs)
|
|
|
|
{
|
2015-11-26 01:26:23 +00:00
|
|
|
#if __SSE__
|
|
|
|
mVec128 = _mm_div_ps(mVec128, rhs.mVec128);
|
|
|
|
#else
|
2015-05-04 10:00:15 +00:00
|
|
|
r /= rhs.r; g /= rhs.g; b /= rhs.b; a /= rhs.a;
|
2015-11-26 01:26:23 +00:00
|
|
|
#endif
|
2015-05-04 10:00:15 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
inline void normalize()
|
|
|
|
{
|
2015-10-08 00:21:38 +00:00
|
|
|
float mag = magnitude();
|
2015-11-26 01:26:23 +00:00
|
|
|
mag = 1.0 / mag;
|
2015-05-04 10:00:15 +00:00
|
|
|
*this *= mag;
|
|
|
|
}
|
2015-11-26 02:38:43 +00:00
|
|
|
inline CColor normalized() const
|
2015-05-04 10:00:15 +00:00
|
|
|
{
|
2015-10-08 00:21:38 +00:00
|
|
|
float mag = magnitude();
|
2015-11-26 01:26:23 +00:00
|
|
|
mag = 1.0 / mag;
|
2015-05-04 10:00:15 +00:00
|
|
|
return *this * mag;
|
|
|
|
}
|
2015-11-26 02:38:43 +00:00
|
|
|
|
2015-10-08 00:21:38 +00:00
|
|
|
inline float magSquared() const
|
2015-11-26 01:26:23 +00:00
|
|
|
{
|
2015-11-26 01:42:24 +00:00
|
|
|
#if __SSE__
|
2015-11-26 01:26:23 +00:00
|
|
|
TVectorUnion result;
|
2015-11-26 01:42:24 +00:00
|
|
|
#if __SSE4_1__ || __SSE4_2__
|
|
|
|
if (cpuFeatures().SSE41 || cpuFeatures().SSE42)
|
|
|
|
{
|
|
|
|
result.mVec128 = _mm_dp_ps(mVec128, mVec128, 0xF1);
|
|
|
|
return result.v[0];
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-11-26 01:26:23 +00:00
|
|
|
result.mVec128 = _mm_mul_ps(mVec128, mVec128);
|
|
|
|
return result.v[0] + result.v[1] + result.v[2] + result.v[3];
|
|
|
|
#else
|
|
|
|
return r * r + g * g + b * b + a * a;
|
|
|
|
#endif
|
|
|
|
}
|
2015-10-08 00:21:38 +00:00
|
|
|
inline float magnitude() const
|
2015-05-04 10:00:15 +00:00
|
|
|
{
|
2015-11-26 01:26:23 +00:00
|
|
|
return sqrtf(magSquared());
|
2015-05-04 10:00:15 +00:00
|
|
|
}
|
|
|
|
static inline CColor lerp(const CColor& a, const CColor& b, float t)
|
|
|
|
{
|
|
|
|
return (a + (b - a) * t);
|
|
|
|
}
|
|
|
|
static inline CColor nlerp(const CColor& a, const CColor& b, float t)
|
|
|
|
{
|
|
|
|
return lerp(a, b, t).normalized();
|
|
|
|
}
|
2015-11-26 01:26:23 +00:00
|
|
|
inline float& operator[](const size_t& idx) {return (&r)[idx];}
|
|
|
|
inline const float& operator[](const size_t& idx) const { return (&r)[idx]; }
|
2015-05-04 10:00:15 +00:00
|
|
|
inline void splat(float rgb, float a)
|
|
|
|
{
|
2015-11-26 01:26:23 +00:00
|
|
|
#if __SSE__
|
|
|
|
TVectorUnion splat = {{rgb, rgb, rgb, a}};
|
|
|
|
mVec128 = splat.mVec128;
|
|
|
|
#else
|
|
|
|
v[0] = rgb; v[1] = rgb; v[2] = rgb; v[3] = a;
|
|
|
|
#endif
|
2015-05-04 10:00:15 +00:00
|
|
|
}
|
2015-05-01 06:31:59 +00:00
|
|
|
|
|
|
|
union
|
|
|
|
{
|
2015-11-26 01:26:23 +00:00
|
|
|
struct
|
|
|
|
{
|
|
|
|
float r, g, b, a;
|
|
|
|
};
|
|
|
|
float v[4];
|
|
|
|
#if __SSE__
|
|
|
|
__m128 mVec128;
|
|
|
|
#endif
|
2015-05-01 06:31:59 +00:00
|
|
|
};
|
|
|
|
|
2015-05-04 10:00:15 +00:00
|
|
|
void fromRGBA8(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
|
2015-05-01 06:31:59 +00:00
|
|
|
{
|
2015-11-26 01:26:23 +00:00
|
|
|
this->r = r / 255.f;
|
|
|
|
this->g = g / 255.f;
|
|
|
|
this->b = b / 255.f;
|
|
|
|
this->a = a / 255.f;
|
|
|
|
}
|
|
|
|
|
|
|
|
void fromRGBA32(unsigned int rgba)
|
|
|
|
{
|
|
|
|
static RGBA32 tmp;
|
|
|
|
tmp.rgba = COLOR(rgba);
|
|
|
|
fromRGBA8(tmp.r, tmp.g, tmp.b, tmp.a);
|
2015-05-04 10:00:15 +00:00
|
|
|
}
|
2015-05-01 06:31:59 +00:00
|
|
|
|
2015-11-01 12:04:29 +00:00
|
|
|
/**
|
|
|
|
* @brief Assigns rgba from hsv
|
|
|
|
* @param h[0-1] The hue percentagee of the color.
|
|
|
|
* @param s[0-1] The saturation percentage of the color.
|
|
|
|
* @param v[0-1] The value percentage of the color.
|
|
|
|
* @param a[0-1] The alpha percentage of the color.
|
|
|
|
*/
|
2015-11-06 04:09:33 +00:00
|
|
|
void fromHSV(float h, float s, float v, float _a = 1.0);
|
2015-11-01 12:04:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Converts rgba to hsv
|
|
|
|
* @param h[0-1] The hue percentagee of the color.
|
|
|
|
* @param s[0-1] The saturation percentage of the color.
|
|
|
|
* @param v[0-1] The value percentage of the color.
|
|
|
|
* @param a[0-1] The alpha percentage of the color.
|
|
|
|
*/
|
2015-11-06 04:09:33 +00:00
|
|
|
void toHSV(float& h, float& s, float& v) const;
|
2015-11-06 03:52:47 +00:00
|
|
|
|
|
|
|
|
2015-11-06 04:09:33 +00:00
|
|
|
void fromHSL(float h, float s, float l, float _a = 1.0);
|
2015-11-06 03:52:47 +00:00
|
|
|
|
2015-11-06 04:09:33 +00:00
|
|
|
void toHSL(float& h, float& s, float& l);
|
2016-01-05 07:49:12 +00:00
|
|
|
|
|
|
|
CColor toGrayscale()
|
2016-01-06 01:45:06 +00:00
|
|
|
{ return {Math::sqrtF((r * r + g * g + b * b) / 3), a}; }
|
2015-05-01 06:31:59 +00:00
|
|
|
};
|
|
|
|
|
2015-05-04 10:00:15 +00:00
|
|
|
static inline CColor operator+(float lhs, const CColor& rhs)
|
|
|
|
{
|
2015-11-26 01:26:23 +00:00
|
|
|
#if __SSE__
|
|
|
|
TVectorUnion splat = {{lhs, lhs, lhs, lhs}};
|
|
|
|
return CColor(_mm_add_ps(splat.mVec128, rhs.mVec128));
|
|
|
|
#else
|
|
|
|
return CColor(lhs + rhs.r, lhs + rhs.g, lhs + rhs.b, lhs + rhs.a);
|
|
|
|
#endif
|
2015-05-04 10:00:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline CColor operator-(float lhs, const CColor& rhs)
|
|
|
|
{
|
2015-11-26 01:26:23 +00:00
|
|
|
#if __SSE__
|
|
|
|
TVectorUnion splat = {{lhs, lhs, lhs, lhs}};
|
|
|
|
return CColor(_mm_sub_ps(splat.mVec128, rhs.mVec128));
|
|
|
|
#else
|
|
|
|
return CColor(lhs - rhs.r, lhs - rhs.g, lhs - rhs.b, lhs - rhs.a);
|
|
|
|
#endif
|
2015-05-04 10:00:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline CColor operator*(float lhs, const CColor& rhs)
|
|
|
|
{
|
2015-11-26 01:26:23 +00:00
|
|
|
#if __SSE__
|
|
|
|
TVectorUnion splat = {{lhs, lhs, lhs, lhs}};
|
|
|
|
return CColor(_mm_mul_ps(splat.mVec128, rhs.mVec128));
|
|
|
|
#else
|
|
|
|
return CColor(lhs * rhs.r, lhs * rhs.g, lhs * rhs.b, lhs * rhs.a);
|
|
|
|
#endif
|
2015-05-04 10:00:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline CColor operator/(float lhs, const CColor& rhs)
|
|
|
|
{
|
2015-11-26 01:26:23 +00:00
|
|
|
#if __SSE__
|
|
|
|
TVectorUnion splat = {{lhs, lhs, lhs, lhs}};
|
|
|
|
return CColor(_mm_div_ps(splat.mVec128, rhs.mVec128));
|
|
|
|
#else
|
|
|
|
return CColor(lhs / rhs.r, lhs / rhs.g, lhs / rhs.b, lhs / rhs.a);
|
|
|
|
#endif
|
2015-05-04 10:00:15 +00:00
|
|
|
}
|
|
|
|
|
2015-10-08 00:21:38 +00:00
|
|
|
}
|
2015-05-01 06:31:59 +00:00
|
|
|
#endif // CCOLOR_HPP
|