11 template <
typename Type>
class RGBA;
12 template <
typename Type>
class CMYK;
23 template <
typename Type>
28 RGB() {
for (
int i=0; i<3; i++ ) data[i] = 0; }
29 RGB(
const RGB& c) {
for (
int i=0; i<3; i++ ) data[i] = c.data[i]; }
30 RGB(
const double d[3]) {
for (
int i=0; i<3; i++ ) data[i] = d[i]; }
31 RGB(
const double rr,
const double gg,
const double bb) {
32 data[0] = (Type) rr; data[1] = (Type) gg; data[2] = (Type) bb; }
34 for (
int i=0; i<3; i++ ) data[i] = c.data[i];
38 for (
int i=0; i<3; i++ ) data[i] -= c.data[i];
42 if ( i < 3 )
return data[i];
45 Type
r() {
return data[0]; }
46 Type
g() {
return data[1]; }
47 Type
b() {
return data[2]; }
48 void red() { data[0] = 1; data[1] = data[2] = 0; }
50 for (
int i=0; i<3; i++ ) data[i] = c[i];
53 template <
typename T2>
operator RGB<T2>()
const {
54 return RGB<T2>(data[0], data[1], data[2]);
58 double r,
g,
b, k, k1;
60 if (
sizeof(Type) > 1 ) max = 1;
66 if (
r > max )
r = max;
67 if (
g > max )
g = max;
68 if (
b > max )
b = max;
69 data[0] = (Type) (max -
r);
70 data[1] = (Type) (max -
g);
71 data[2] = (Type) (max -
b);
75 for ( i=0; i<3; i++ ) data[i] = 255;
77 sscanf(
hex.
c_str(),
"#%2x%2x%2x", &data[0], &data[1], &data[2]);
78 if (
sizeof(Type) > 1 )
for ( i=0; i<3; i++ ) data[i] /= 255.0;
83 if (
sizeof(Type) > 1 ) s = 255;
84 for ( i=0; i<3; i++ )
hex +=
Bstring(s*data[i],
"%02x");
87 double average() {
return ((
double)data[0] + (
double)data[1] + (
double)data[2])/3; }
88 double rms() {
return sqrt(((
double)data[0]*data[0] + (
double)data[1]*data[1] + (
double)data[2]*data[2])/3); }
89 void spectrum(
double value,
double cmin,
double cmax ) {
90 if ( fabs(cmin - cmax) < 1e-6 ) {
95 double type_max = (
sizeof(Type) > 1 )? 1: 255;
97 double mid = 0.5*(cmax + cmin);
98 double scale = 2*type_max/(cmax - cmin);
100 red = scale*(value - mid);
102 if (
red > type_max )
red = type_max;
103 blu = scale*(mid - value);
104 if ( blu < 0 ) blu = 0;
105 if ( blu > type_max ) blu = type_max;
106 grn = type_max -
red - blu;
107 if ( grn < 0 ) grn = 0;
108 if ( grn > type_max ) grn = type_max;
110 data[0] = (Type)
red;
111 data[1] = (Type) grn;
112 data[2] = (Type) blu;
114 void phase(
double phi,
double amp_ratio) {
115 if ( amp_ratio > 255 ) amp_ratio = 255;
116 if (
sizeof(Type) > 1 )
if ( amp_ratio > 1 ) amp_ratio = 1;
117 double red, grn, blu;
130 if ( grn < 0 ) grn = 0;
131 if ( blu < 0 ) blu = 0;
133 if ( grn > 1 ) grn = 1;
134 if ( blu > 1 ) blu = 1;
136 data[0] = (Type) (amp_ratio*
red);
137 data[1] = (Type) (amp_ratio*grn);
138 data[2] = (Type) (amp_ratio*blu);
141 data[0] = (Type) (random()*255/RAND_MAX);
142 data[1] = (Type) (random()*255/RAND_MAX);
143 data[2] = (Type) (random()*255/RAND_MAX);
147 template <
typename Type>
149 output.setf(ios::fixed, ios::floatfield);
150 output << rgb.
r() <<
tab << rgb.
g() <<
tab << rgb.
b();
161 template <
typename Type>
166 RGBA() {
for (
int i=0; i<4; i++ ) data[i] = 0; }
167 RGBA(
const RGBA& c) {
for (
int i=0; i<4; i++ ) data[i] = c.data[i]; }
168 RGBA(
const double d[4]) {
for (
int i=0; i<4; i++ ) data[i] = d[i]; }
169 RGBA(
const double rr,
const double gg,
const double bb) {
170 data[0] = (Type) rr; data[1] = (Type) gg; data[2] = (Type) bb; data[3] = 0; }
171 RGBA(
const double rr,
const double gg,
const double bb,
const double aa) {
172 data[0] = (Type) rr; data[1] = (Type) gg; data[2] = (Type) bb; data[3] = (Type) aa; }
174 for (
int i=0; i<4; i++ ) data[i] = c.data[i];
178 for (
int i=0; i<3; i++ ) data[i] = c[i];
182 return RGBA<T2>(data[0], data[1], data[2], data[3]);
185 return RGBA(data[0] + c.data[0], data[1] + c.data[1], data[2] + c.data[2], data[3] + c.data[3]);
188 return RGBA(data[0]/d, data[1] /d, data[2]/d, data[3]/d);
190 RGBA(
RGB<Type>& c) {
for (
int i=0; i<3; i++ ) data[i] = c[i]; data[3] = 0; }
193 for (
int i=0; i<3; i++ ) data[i] = rgb[i];
197 if ( i < 4 )
return data[i];
200 Type
r() {
return data[0]; }
201 Type
g() {
return data[1]; }
202 Type
b() {
return data[2]; }
203 Type
a() {
return data[3]; }
204 void red() { data[0] = data[3] = 1; data[1] = data[2] = 0; }
207 unsigned int urgb[3];
208 for ( i=0; i<4; i++ ) data[i] = 255;
209 if (
hex !=
"white" )
210 sscanf(
hex.
c_str(),
"#%2x%2x%2x", &urgb[0], &urgb[1], &urgb[2]);
211 for ( i=0; i<3; i++ ) data[i] = urgb[i];
212 if (
sizeof(Type) > 1 )
for ( i=0; i<4; i++ ) data[i] /= 255.0;
217 if (
sizeof(Type) > 1 ) s = 255;
218 for ( i=0; i<3; i++ )
hex +=
Bstring((
int)(s*data[i]),
"%02x");
221 void spectrum(
double value,
double cmin,
double cmax ) {
222 if ( fabs(cmin - cmax) < 1e-6 ) {
227 double type_max = (
sizeof(Type) > 1 )? 1: 255;
228 double red, blu, grn;
229 double mid = 0.5*(cmax + cmin);
230 double scale = 2*type_max/(cmax - cmin);
232 red = scale*(value - mid);
234 if (
red > type_max )
red = type_max;
235 blu = scale*(mid - value);
236 if ( blu < 0 ) blu = 0;
237 if ( blu > type_max ) blu = type_max;
238 grn = type_max -
red - blu;
239 if ( grn < 0 ) grn = 0;
240 if ( grn > type_max ) grn = type_max;
242 data[0] = (Type)
red;
243 data[1] = (Type) grn;
244 data[2] = (Type) blu;
245 data[3] = (Type) type_max;
249 template <
typename Type>
251 output.setf(ios::fixed, ios::floatfield);
252 output << rgba.
r() <<
tab << rgba.
g() <<
tab << rgba.
b() <<
tab << rgba.
a();
263 template <
typename Type>
268 CMYK() {
for (
int i=0; i<4; i++ ) data[i] = 0; }
269 CMYK(
const CMYK& col) {
for (
int i=0; i<4; i++ ) data[i] = col.data[i]; }
270 CMYK(
const double d[4]) {
for (
int i=0; i<4; i++ ) data[i] = d[i]; }
271 CMYK(
const double cc,
const double mm,
const double yy,
const double kk) {
272 data[0] = (Type) cc; data[1] = (Type) mm; data[2] = (Type) yy; data[3] = (Type) kk; }
274 for (
int i=0; i<4; i++ ) data[i] = col.data[i];
278 double r1, g1, b1,
k, k1,
c,
m,
y;
280 if (
sizeof(Type) > 1 ) max = 1;
285 if (
k > g1 )
k = g1;
286 if (
k > b1 )
k = b1;
291 if (
c > max )
c = max;
292 if (
m > max )
m = max;
293 if (
y > max )
y = max;
294 data[0] = (Type)
c; data[1] = (Type)
m; data[2] = (Type)
y; data[3] = (Type)
k;
297 if ( i < 4 )
return data[i];
300 Type
c() {
return data[0]; }
301 Type
m() {
return data[1]; }
302 Type
y() {
return data[2]; }
303 Type
k() {
return data[3]; }
306 template <
typename Type>
308 output.setf(ios::fixed, ios::floatfield);
309 output << cmyk.
c() <<
tab << cmyk.
m() <<
tab << cmyk[1] <<
tab << cmyk.
k();