absence_thelab
diff src/common/color.cpp @ 0:1cffe3409164
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 23 Oct 2014 01:46:07 +0300 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/common/color.cpp Thu Oct 23 01:46:07 2014 +0300 1.3 @@ -0,0 +1,125 @@ 1.4 +#include <cmath> 1.5 +#include "color.h" 1.6 + 1.7 +template <class T> 1.8 +const T &clamp(const T &val, const T &low, const T &high) { 1.9 + if(val > high) return high; 1.10 + if(val < low) return low; 1.11 + return val; 1.12 +} 1.13 + 1.14 +Color::Color(float_t intensity) { 1.15 + r = g = b = clamp<float_t>(intensity, 0.0f, 1.0f); 1.16 + a = 1.0f; 1.17 +} 1.18 + 1.19 +Color::Color(float_t r, float_t g, float_t b, float_t a) { 1.20 + this->r = clamp<float_t>(r, 0.0f, 1.0f); 1.21 + this->g = clamp<float_t>(g, 0.0f, 1.0f); 1.22 + this->b = clamp<float_t>(b, 0.0f, 1.0f); 1.23 + this->a = clamp<float_t>(a, 0.0f, 1.0f); 1.24 +} 1.25 + 1.26 +Color::Color(unsigned int r, unsigned int g, unsigned int b, unsigned int a) { 1.27 + this->r = (float_t)clamp<unsigned int>(r, 0, 255) / 255.0f; 1.28 + this->g = (float_t)clamp<unsigned int>(g, 0, 255) / 255.0f; 1.29 + this->b = (float_t)clamp<unsigned int>(b, 0, 255) / 255.0f; 1.30 + this->a = (float_t)clamp<unsigned int>(a, 0, 255) / 255.0f; 1.31 +} 1.32 + 1.33 +Color::Color(int r, int g, int b, int a) { 1.34 + this->r = (float_t)clamp<int>(r, 0, 255) / 255.0f; 1.35 + this->g = (float_t)clamp<int>(g, 0, 255) / 255.0f; 1.36 + this->b = (float_t)clamp<int>(b, 0, 255) / 255.0f; 1.37 + this->a = (float_t)clamp<int>(a, 0, 255) / 255.0f; 1.38 +} 1.39 + 1.40 +Color::Color(long pcol) { 1.41 + a = (float_t)(((dword)pcol >> 24) & 0xff) / 255.0f; 1.42 + r = (float_t)(((dword)pcol >> 16) & 0xff) / 255.0f; 1.43 + g = (float_t)(((dword)pcol >> 8) & 0xff) / 255.0f; 1.44 + b = (float_t)((dword)pcol & 0xff) / 255.0f; 1.45 +} 1.46 + 1.47 +Color::Color(dword pcol) { 1.48 + a = (float_t)((pcol >> 24) & 0xff) / 255.0f; 1.49 + r = (float_t)((pcol >> 16) & 0xff) / 255.0f; 1.50 + g = (float_t)((pcol >> 8) & 0xff) / 255.0f; 1.51 + b = (float_t)(pcol & 0xff) / 255.0f; 1.52 +} 1.53 + 1.54 +dword Color::GetPacked32() const { 1.55 + return (((dword)(a * 255.0f) << 24) & 0xff000000) | 1.56 + (((dword)(r * 255.0f) << 16) & 0x00ff0000) | 1.57 + (((dword)(g * 255.0f) << 8) & 0x0000ff00) | 1.58 + ((dword)(b * 255.0f) & 0x000000ff); 1.59 +} 1.60 + 1.61 +word Color::GetPacked16() const { 1.62 + return (word)(r * 32.0f) << 11 | (word)(g * 64.0f) << 5 | (word)(b * 32.0f); 1.63 +} 1.64 + 1.65 +word Color::GetPacked15() const { 1.66 + return (word)a << 15 | (word)(r * 32.0f) << 10 | (word)(g * 32.0f) << 5 | (word)(b * 32.0f); 1.67 +} 1.68 + 1.69 +#ifndef HalfPi 1.70 +const float_t HalfPi = 1.5707963268f; 1.71 +#endif // HalfPi 1.72 + 1.73 +byte Color::GetNearest8(const byte **pal) const { 1.74 + float_t Score[256]; 1.75 + for(int i=0; i<256; i++) { 1.76 + Color palcol = Color(pal[i][0], pal[i][1], pal[i][2]); 1.77 + float_t NearR = (float_t)cos(fabs(r - palcol.r) * HalfPi); 1.78 + float_t NearG = (float_t)cos(fabs(g - palcol.g) * HalfPi); 1.79 + float_t NearB = (float_t)cos(fabs(b - palcol.b) * HalfPi); 1.80 + Score[i] = NearR + NearG + NearB; 1.81 + } 1.82 + 1.83 + int nearest = 0; 1.84 + for(int i=0; i<256; i++) { 1.85 + if(Score[i] > Score[nearest]) nearest = i; 1.86 + } 1.87 + return nearest; 1.88 +} 1.89 + 1.90 +Color Color::operator +(const Color &col) const { 1.91 + return Color(r + col.r, g + col.g, b + col.b, a + col.a); 1.92 +} 1.93 + 1.94 +Color Color::operator -(const Color &col) const { 1.95 + return Color(r - col.r, g - col.g, b - col.b, a - col.a); 1.96 +} 1.97 + 1.98 +Color Color::operator *(const Color &col) const { 1.99 + return Color(r * col.r, g * col.g, b * col.b, a * col.a); 1.100 +} 1.101 + 1.102 +Color Color::operator *(float_t scalar) const { 1.103 + return Color(r * scalar, g * scalar, b * scalar, a); 1.104 +} 1.105 + 1.106 + 1.107 +void Color::operator +=(const Color &col) { 1.108 + *this = Color(r + col.r, g + col.g, b + col.b, a + col.a); 1.109 +} 1.110 + 1.111 +void Color::operator -=(const Color &col) { 1.112 + *this = Color(r - col.r, g - col.g, b - col.b, a - col.a); 1.113 +} 1.114 + 1.115 +void Color::operator *=(const Color &col) { 1.116 + *this = Color(r * col.r, g * col.g, b * col.b, a * col.a); 1.117 +} 1.118 + 1.119 +void Color::operator *=(float_t scalar) { 1.120 + *this = Color(r * scalar, g * scalar, b * scalar, a); 1.121 +} 1.122 + 1.123 +Color BlendColors(const Color &c1, const Color &c2, float_t t) { 1.124 + float_t r = c1.r + (c2.r - c1.r) * t; 1.125 + float_t g = c1.g + (c2.g - c1.g) * t; 1.126 + float_t b = c1.b + (c2.b - c1.b) * t; 1.127 + return Color(r, g, b); 1.128 +}