absence_thelab

annotate 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
rev   line source
nuclear@0 1 #include <cmath>
nuclear@0 2 #include "color.h"
nuclear@0 3
nuclear@0 4 template <class T>
nuclear@0 5 const T &clamp(const T &val, const T &low, const T &high) {
nuclear@0 6 if(val > high) return high;
nuclear@0 7 if(val < low) return low;
nuclear@0 8 return val;
nuclear@0 9 }
nuclear@0 10
nuclear@0 11 Color::Color(float_t intensity) {
nuclear@0 12 r = g = b = clamp<float_t>(intensity, 0.0f, 1.0f);
nuclear@0 13 a = 1.0f;
nuclear@0 14 }
nuclear@0 15
nuclear@0 16 Color::Color(float_t r, float_t g, float_t b, float_t a) {
nuclear@0 17 this->r = clamp<float_t>(r, 0.0f, 1.0f);
nuclear@0 18 this->g = clamp<float_t>(g, 0.0f, 1.0f);
nuclear@0 19 this->b = clamp<float_t>(b, 0.0f, 1.0f);
nuclear@0 20 this->a = clamp<float_t>(a, 0.0f, 1.0f);
nuclear@0 21 }
nuclear@0 22
nuclear@0 23 Color::Color(unsigned int r, unsigned int g, unsigned int b, unsigned int a) {
nuclear@0 24 this->r = (float_t)clamp<unsigned int>(r, 0, 255) / 255.0f;
nuclear@0 25 this->g = (float_t)clamp<unsigned int>(g, 0, 255) / 255.0f;
nuclear@0 26 this->b = (float_t)clamp<unsigned int>(b, 0, 255) / 255.0f;
nuclear@0 27 this->a = (float_t)clamp<unsigned int>(a, 0, 255) / 255.0f;
nuclear@0 28 }
nuclear@0 29
nuclear@0 30 Color::Color(int r, int g, int b, int a) {
nuclear@0 31 this->r = (float_t)clamp<int>(r, 0, 255) / 255.0f;
nuclear@0 32 this->g = (float_t)clamp<int>(g, 0, 255) / 255.0f;
nuclear@0 33 this->b = (float_t)clamp<int>(b, 0, 255) / 255.0f;
nuclear@0 34 this->a = (float_t)clamp<int>(a, 0, 255) / 255.0f;
nuclear@0 35 }
nuclear@0 36
nuclear@0 37 Color::Color(long pcol) {
nuclear@0 38 a = (float_t)(((dword)pcol >> 24) & 0xff) / 255.0f;
nuclear@0 39 r = (float_t)(((dword)pcol >> 16) & 0xff) / 255.0f;
nuclear@0 40 g = (float_t)(((dword)pcol >> 8) & 0xff) / 255.0f;
nuclear@0 41 b = (float_t)((dword)pcol & 0xff) / 255.0f;
nuclear@0 42 }
nuclear@0 43
nuclear@0 44 Color::Color(dword pcol) {
nuclear@0 45 a = (float_t)((pcol >> 24) & 0xff) / 255.0f;
nuclear@0 46 r = (float_t)((pcol >> 16) & 0xff) / 255.0f;
nuclear@0 47 g = (float_t)((pcol >> 8) & 0xff) / 255.0f;
nuclear@0 48 b = (float_t)(pcol & 0xff) / 255.0f;
nuclear@0 49 }
nuclear@0 50
nuclear@0 51 dword Color::GetPacked32() const {
nuclear@0 52 return (((dword)(a * 255.0f) << 24) & 0xff000000) |
nuclear@0 53 (((dword)(r * 255.0f) << 16) & 0x00ff0000) |
nuclear@0 54 (((dword)(g * 255.0f) << 8) & 0x0000ff00) |
nuclear@0 55 ((dword)(b * 255.0f) & 0x000000ff);
nuclear@0 56 }
nuclear@0 57
nuclear@0 58 word Color::GetPacked16() const {
nuclear@0 59 return (word)(r * 32.0f) << 11 | (word)(g * 64.0f) << 5 | (word)(b * 32.0f);
nuclear@0 60 }
nuclear@0 61
nuclear@0 62 word Color::GetPacked15() const {
nuclear@0 63 return (word)a << 15 | (word)(r * 32.0f) << 10 | (word)(g * 32.0f) << 5 | (word)(b * 32.0f);
nuclear@0 64 }
nuclear@0 65
nuclear@0 66 #ifndef HalfPi
nuclear@0 67 const float_t HalfPi = 1.5707963268f;
nuclear@0 68 #endif // HalfPi
nuclear@0 69
nuclear@0 70 byte Color::GetNearest8(const byte **pal) const {
nuclear@0 71 float_t Score[256];
nuclear@0 72 for(int i=0; i<256; i++) {
nuclear@0 73 Color palcol = Color(pal[i][0], pal[i][1], pal[i][2]);
nuclear@0 74 float_t NearR = (float_t)cos(fabs(r - palcol.r) * HalfPi);
nuclear@0 75 float_t NearG = (float_t)cos(fabs(g - palcol.g) * HalfPi);
nuclear@0 76 float_t NearB = (float_t)cos(fabs(b - palcol.b) * HalfPi);
nuclear@0 77 Score[i] = NearR + NearG + NearB;
nuclear@0 78 }
nuclear@0 79
nuclear@0 80 int nearest = 0;
nuclear@0 81 for(int i=0; i<256; i++) {
nuclear@0 82 if(Score[i] > Score[nearest]) nearest = i;
nuclear@0 83 }
nuclear@0 84 return nearest;
nuclear@0 85 }
nuclear@0 86
nuclear@0 87 Color Color::operator +(const Color &col) const {
nuclear@0 88 return Color(r + col.r, g + col.g, b + col.b, a + col.a);
nuclear@0 89 }
nuclear@0 90
nuclear@0 91 Color Color::operator -(const Color &col) const {
nuclear@0 92 return Color(r - col.r, g - col.g, b - col.b, a - col.a);
nuclear@0 93 }
nuclear@0 94
nuclear@0 95 Color Color::operator *(const Color &col) const {
nuclear@0 96 return Color(r * col.r, g * col.g, b * col.b, a * col.a);
nuclear@0 97 }
nuclear@0 98
nuclear@0 99 Color Color::operator *(float_t scalar) const {
nuclear@0 100 return Color(r * scalar, g * scalar, b * scalar, a);
nuclear@0 101 }
nuclear@0 102
nuclear@0 103
nuclear@0 104 void Color::operator +=(const Color &col) {
nuclear@0 105 *this = Color(r + col.r, g + col.g, b + col.b, a + col.a);
nuclear@0 106 }
nuclear@0 107
nuclear@0 108 void Color::operator -=(const Color &col) {
nuclear@0 109 *this = Color(r - col.r, g - col.g, b - col.b, a - col.a);
nuclear@0 110 }
nuclear@0 111
nuclear@0 112 void Color::operator *=(const Color &col) {
nuclear@0 113 *this = Color(r * col.r, g * col.g, b * col.b, a * col.a);
nuclear@0 114 }
nuclear@0 115
nuclear@0 116 void Color::operator *=(float_t scalar) {
nuclear@0 117 *this = Color(r * scalar, g * scalar, b * scalar, a);
nuclear@0 118 }
nuclear@0 119
nuclear@0 120 Color BlendColors(const Color &c1, const Color &c2, float_t t) {
nuclear@0 121 float_t r = c1.r + (c2.r - c1.r) * t;
nuclear@0 122 float_t g = c1.g + (c2.g - c1.g) * t;
nuclear@0 123 float_t b = c1.b + (c2.b - c1.b) * t;
nuclear@0 124 return Color(r, g, b);
nuclear@0 125 }