nuclear@0: #include nuclear@0: #include "color.h" nuclear@0: nuclear@0: template nuclear@0: const T &clamp(const T &val, const T &low, const T &high) { nuclear@0: if(val > high) return high; nuclear@0: if(val < low) return low; nuclear@0: return val; nuclear@0: } nuclear@0: nuclear@0: Color::Color(float_t intensity) { nuclear@0: r = g = b = clamp(intensity, 0.0f, 1.0f); nuclear@0: a = 1.0f; nuclear@0: } nuclear@0: nuclear@0: Color::Color(float_t r, float_t g, float_t b, float_t a) { nuclear@0: this->r = clamp(r, 0.0f, 1.0f); nuclear@0: this->g = clamp(g, 0.0f, 1.0f); nuclear@0: this->b = clamp(b, 0.0f, 1.0f); nuclear@0: this->a = clamp(a, 0.0f, 1.0f); nuclear@0: } nuclear@0: nuclear@0: Color::Color(unsigned int r, unsigned int g, unsigned int b, unsigned int a) { nuclear@0: this->r = (float_t)clamp(r, 0, 255) / 255.0f; nuclear@0: this->g = (float_t)clamp(g, 0, 255) / 255.0f; nuclear@0: this->b = (float_t)clamp(b, 0, 255) / 255.0f; nuclear@0: this->a = (float_t)clamp(a, 0, 255) / 255.0f; nuclear@0: } nuclear@0: nuclear@0: Color::Color(int r, int g, int b, int a) { nuclear@0: this->r = (float_t)clamp(r, 0, 255) / 255.0f; nuclear@0: this->g = (float_t)clamp(g, 0, 255) / 255.0f; nuclear@0: this->b = (float_t)clamp(b, 0, 255) / 255.0f; nuclear@0: this->a = (float_t)clamp(a, 0, 255) / 255.0f; nuclear@0: } nuclear@0: nuclear@0: Color::Color(long pcol) { nuclear@0: a = (float_t)(((dword)pcol >> 24) & 0xff) / 255.0f; nuclear@0: r = (float_t)(((dword)pcol >> 16) & 0xff) / 255.0f; nuclear@0: g = (float_t)(((dword)pcol >> 8) & 0xff) / 255.0f; nuclear@0: b = (float_t)((dword)pcol & 0xff) / 255.0f; nuclear@0: } nuclear@0: nuclear@0: Color::Color(dword pcol) { nuclear@0: a = (float_t)((pcol >> 24) & 0xff) / 255.0f; nuclear@0: r = (float_t)((pcol >> 16) & 0xff) / 255.0f; nuclear@0: g = (float_t)((pcol >> 8) & 0xff) / 255.0f; nuclear@0: b = (float_t)(pcol & 0xff) / 255.0f; nuclear@0: } nuclear@0: nuclear@0: dword Color::GetPacked32() const { nuclear@0: return (((dword)(a * 255.0f) << 24) & 0xff000000) | nuclear@0: (((dword)(r * 255.0f) << 16) & 0x00ff0000) | nuclear@0: (((dword)(g * 255.0f) << 8) & 0x0000ff00) | nuclear@0: ((dword)(b * 255.0f) & 0x000000ff); nuclear@0: } nuclear@0: nuclear@0: word Color::GetPacked16() const { nuclear@0: return (word)(r * 32.0f) << 11 | (word)(g * 64.0f) << 5 | (word)(b * 32.0f); nuclear@0: } nuclear@0: nuclear@0: word Color::GetPacked15() const { nuclear@0: return (word)a << 15 | (word)(r * 32.0f) << 10 | (word)(g * 32.0f) << 5 | (word)(b * 32.0f); nuclear@0: } nuclear@0: nuclear@0: #ifndef HalfPi nuclear@0: const float_t HalfPi = 1.5707963268f; nuclear@0: #endif // HalfPi nuclear@0: nuclear@0: byte Color::GetNearest8(const byte **pal) const { nuclear@0: float_t Score[256]; nuclear@0: for(int i=0; i<256; i++) { nuclear@0: Color palcol = Color(pal[i][0], pal[i][1], pal[i][2]); nuclear@0: float_t NearR = (float_t)cos(fabs(r - palcol.r) * HalfPi); nuclear@0: float_t NearG = (float_t)cos(fabs(g - palcol.g) * HalfPi); nuclear@0: float_t NearB = (float_t)cos(fabs(b - palcol.b) * HalfPi); nuclear@0: Score[i] = NearR + NearG + NearB; nuclear@0: } nuclear@0: nuclear@0: int nearest = 0; nuclear@0: for(int i=0; i<256; i++) { nuclear@0: if(Score[i] > Score[nearest]) nearest = i; nuclear@0: } nuclear@0: return nearest; nuclear@0: } nuclear@0: nuclear@0: Color Color::operator +(const Color &col) const { nuclear@0: return Color(r + col.r, g + col.g, b + col.b, a + col.a); nuclear@0: } nuclear@0: nuclear@0: Color Color::operator -(const Color &col) const { nuclear@0: return Color(r - col.r, g - col.g, b - col.b, a - col.a); nuclear@0: } nuclear@0: nuclear@0: Color Color::operator *(const Color &col) const { nuclear@0: return Color(r * col.r, g * col.g, b * col.b, a * col.a); nuclear@0: } nuclear@0: nuclear@0: Color Color::operator *(float_t scalar) const { nuclear@0: return Color(r * scalar, g * scalar, b * scalar, a); nuclear@0: } nuclear@0: nuclear@0: nuclear@0: void Color::operator +=(const Color &col) { nuclear@0: *this = Color(r + col.r, g + col.g, b + col.b, a + col.a); nuclear@0: } nuclear@0: nuclear@0: void Color::operator -=(const Color &col) { nuclear@0: *this = Color(r - col.r, g - col.g, b - col.b, a - col.a); nuclear@0: } nuclear@0: nuclear@0: void Color::operator *=(const Color &col) { nuclear@0: *this = Color(r * col.r, g * col.g, b * col.b, a * col.a); nuclear@0: } nuclear@0: nuclear@0: void Color::operator *=(float_t scalar) { nuclear@0: *this = Color(r * scalar, g * scalar, b * scalar, a); nuclear@0: } nuclear@0: nuclear@0: Color BlendColors(const Color &c1, const Color &c2, float_t t) { nuclear@0: float_t r = c1.r + (c2.r - c1.r) * t; nuclear@0: float_t g = c1.g + (c2.g - c1.g) * t; nuclear@0: float_t b = c1.b + (c2.b - c1.b) * t; nuclear@0: return Color(r, g, b); nuclear@0: }