absence_thelab

view src/common/color.cpp @ 1:4d5933c261c3

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