V-ART
color.cpp
Go to the documentation of this file.
1 
5 #include "vart/color.h"
6 #include <cassert>
7 #include <cstdlib>
8 
9 #include <cmath>
10 #include <iostream>
11 using namespace std;
12 
13 typedef unsigned char uchar;
14 
16  rgba[0] = 0;
17  rgba[1] = 0;
18  rgba[2] = 0;
19  rgba[3] = 255;
20 }
21 
23 // a defaults to 255 (see header)
24  rgba[0] = r;
25  rgba[1] = g;
26  rgba[2] = b;
27  rgba[3] = a;
28 }
29 
31  rgba[0] = color.rgba[0];
32  rgba[1] = color.rgba[1];
33  rgba[2] = color.rgba[2];
34  rgba[3] = color.rgba[3];
35 }
36 
38 // Conversion from http://www.cs.rit.edu/~ncs/color/t_convert.html
39 {
40  if (s_ == 0) // achromatic (grey)
41  return VART::Color(v_, v_, v_, 255);
42 
43  float h = h_ / 42.5; // sector 0 to 5
44  float s = s_ / 255.0;
45  float v = v_ / 255.0;
46 
47  int i = floor(h);
48  float f = h - i; // fractional part of h
49  uchar p = static_cast<uchar>(v * (1 - s) * 255);
50  uchar q = static_cast<uchar>(v * (1 - s * f) * 255);
51  uchar t = static_cast<uchar>(v * (1 - s * (1 - f)) * 255);
52 
53  switch(i) {
54  case 0:
55  return VART::Color(v_, t, p, 255);
56  case 1:
57  return VART::Color(q, v_, p, 255);
58  case 2:
59  return VART::Color(p, v_, t, 255);
60  case 3:
61  return VART::Color(p, q, v_, 255);
62  case 4:
63  return VART::Color(t, p, v_, 255);
64  default:
65  return VART::Color(v_, p, q, 255);
66  }
67 }
68 
69 
71  static const VART::Color black(0,0,0,255);
72  return black;
73 }
74 
76  static const VART::Color red(255,0,0,255);
77  return red;
78 }
79 
81  static const VART::Color green(0,255,0,255);
82  return green;
83 }
84 
86  static const VART::Color blue(0,0,255,255);
87  return blue;
88 }
89 
91  static const VART::Color magenta(255,0,255,255);
92  return magenta;
93 }
94 
96  static const VART::Color cyan(0,255,255,255);
97  return cyan;
98 }
99 
101  static const VART::Color yellow(255,255,0,255);
102  return yellow;
103 }
104 
106  static const VART::Color white(255,255,255,255);
107  return white;
108 }
109 
111  VART::Color color;
112  unsigned char byte;
113  byte = static_cast<unsigned char>(rand() % 256);
114  color.SetR(byte);
115  byte = static_cast<unsigned char>(rand() % 256);
116  color.SetG(byte);
117  byte = static_cast<unsigned char>(rand() % 256);
118  color.SetB(byte);
119  color.SetA(255);
120  return color;
121 }
122 
123 void VART::Color::SetRGBA(unsigned char r, unsigned char g, unsigned char b, unsigned char a) {
124  rgba[0] = r;
125  rgba[1] = g;
126  rgba[2] = b;
127  rgba[3] = a;
128 }
129 
130 void VART::Color::GetRGBA(unsigned char* r, unsigned char* g,
131  unsigned char* b, unsigned char* a) const {
132  *r = rgba[0];
133  *g = rgba[1];
134  *b = rgba[2];
135  *a = rgba[3];
136 }
137 
138 void VART::Color::Get(float rgbaVec[4]) const {
139  rgbaVec[0] = rgba[0]/255.0f;
140  rgbaVec[1] = rgba[1]/255.0f;
141  rgbaVec[2] = rgba[2]/255.0f;
142  rgbaVec[3] = rgba[3]/255.0f;
143 }
145  rgba[0] = color.rgba[0];
146  rgba[1] = color.rgba[1];
147  rgba[2] = color.rgba[2];
148  rgba[3] = color.rgba[3];
149  return *this;
150 }
151 
153  VART::Color c(rgba[0]+color.rgba[0], rgba[1]+color.rgba[1],
154  rgba[2]+color.rgba[2], rgba[3]+color.rgba[3]);
155  return c;
156 }
157 
159  VART::Color c(rgba[0]-color.rgba[0], rgba[1]-color.rgba[1],
160  rgba[2]-color.rgba[2], rgba[3]-color.rgba[3]);
161  return c;
162 }
163 
164 bool VART::Color::operator==(const VART::Color& color) const {
165  return ( rgba[0] == color.rgba[0] &&
166  rgba[1] == color.rgba[1] &&
167  rgba[2] == color.rgba[2] &&
168  rgba[3] == color.rgba[3] );
169 }
170 
171 bool VART::Color::operator!=(const VART::Color& color) const {
172  return ( rgba[0] != color.rgba[0] ||
173  rgba[1] != color.rgba[1] ||
174  rgba[2] != color.rgba[2] ||
175  rgba[3] != color.rgba[3] );
176 }
177 
178 void VART::Color::GetScaled(float escalar, float result[4]) const {
179  assert(escalar >= 0);
180  assert(escalar <= 1);
181  result[0] = rgba[0]/255.0f * escalar;
182  result[1] = rgba[1]/255.0f * escalar;
183  result[2] = rgba[2]/255.0f * escalar;
184  result[3] = rgba[3]/255.0f;
185 }
186 
187 void VART::Color::GetScaled(float escalar, VART::Color* result) const {
188  assert(escalar >= 0);
189  assert(escalar <= 1);
190  result->rgba[0] = static_cast<unsigned char>(rgba[0] * escalar);
191  result->rgba[1] = static_cast<unsigned char>(rgba[1] * escalar);
192  result->rgba[2] = static_cast<unsigned char>(rgba[2] * escalar);
193  result->rgba[3] = rgba[3];
194 }
195 
196 namespace VART {
197  ostream& operator<<(ostream& output, const Color& c)
198  {
199  output << "{" << static_cast<int>(c.rgba[0]) << " "
200  << static_cast<int>(c.rgba[1]) << " " << static_cast<int>(c.rgba[2])
201  << " " << static_cast<int>(c.rgba[3]) << "}";
202  return output;
203  }
204 }
static const Color & YELLOW()
Yellow opaque color.
Definition: color.cpp:100
static const Color & RED()
Red opaque color.
Definition: color.cpp:75
static const Color & BLUE()
Blue opaque color.
Definition: color.cpp:85
Color operator-(const Color &color) const
Definition: color.cpp:158
void GetRGBA(unsigned char *r, unsigned char *g, unsigned char *b, unsigned char *a) const
Definition: color.cpp:130
ostream & operator<<(ostream &output, const Color &c)
Definition: color.cpp:197
Color()
Creates a black, opaque color.
Definition: color.cpp:15
void SetR(unsigned char r)
Definition: color.h:31
void SetG(unsigned char g)
Definition: color.h:32
static const Color & GREEN()
Green opaque color.
Definition: color.cpp:80
bool operator==(const Color &color) const
Definition: color.cpp:164
RGBA color representation.
Definition: color.h:24
unsigned char uchar
Definition: color.cpp:13
static const Color & MAGENTA()
Magenta opaque color.
Definition: color.cpp:90
static Color HSV(unsigned char h_, unsigned char s_, unsigned char v_)
HSV named Constructor.
Definition: color.cpp:37
void SetRGBA(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
Definition: color.cpp:123
void SetB(unsigned char b)
Definition: color.h:33
static const Color & CYAN()
Cyan opaque color.
Definition: color.cpp:95
void GetScaled(float escalar, float result[4]) const
Multiplies RGB components, keeps A component.
Definition: color.cpp:178
bool operator!=(const Color &color) const
Definition: color.cpp:171
static const Color & BLACK()
Black opaque color.
Definition: color.cpp:70
void Get(float rgbaVec[4]) const
Returns RGBA componts as a vector of floats.
Definition: color.cpp:138
Header file for V-ART class "Color".
static Color RANDOM()
Returns a randomly chosen color.
Definition: color.cpp:110
Color operator+(const Color &color) const
Definition: color.cpp:152
Color & operator=(const Color &color)
Definition: color.cpp:144
static const Color & WHITE()
White opaque color.
Definition: color.cpp:105
void SetA(unsigned char a)
Definition: color.h:34