DGtal  1.5.beta
Color.cpp
1 
30 #include "DGtal/io/Color.h"
32 
33 using namespace std;
34 
36 // class Color
38 
39 
41 // Standard services - public :
42 
43 
44 DGtal::Color::Color( const unsigned int rgb, unsigned char aAlphaValue )
45  :myAlpha( aAlphaValue )
46 {
47  myRed = ( rgb & 0xFF0000u ) >> 16;
48  myGreen = ( rgb & 0xFF00u ) >> 8;
49  myBlue = rgb & 0xFF;
50 }
51 
52 
55 {
56  myRed = ( aRGBA & 0xFF000000u ) >> 24;
57  myGreen = ( aRGBA & 0xFF0000u ) >> 16;
58  myBlue = ( aRGBA & 0xFF00u ) >> 8;
59  myAlpha = aRGBA & 0xFF;
60  return *this;
61 }
62 
63 
65 DGtal::Color::setRGBf( float aRedValue,
66  float aGreenValue,
67  float aBlueValue,
68  float aAlphaValue ) {
69  if ( aRedValue > 1.0f ) aRedValue = 1.0f;
70  if ( aRedValue < 0.0f ) aRedValue = 0.0f;
71  myRed = static_cast<unsigned char>( 255 * aRedValue );
72  if ( aGreenValue > 1.0f ) aGreenValue = 1.0f;
73  if ( aGreenValue < 0.0f ) aGreenValue = 0.0f;
74  myGreen = static_cast<unsigned char>( 255 * aGreenValue );
75  if ( aBlueValue > 1.0f ) aBlueValue = 1.0f;
76  if ( aBlueValue < 0.0f ) aBlueValue = 0.0f;
77  myBlue = static_cast<unsigned char>( 255 * aBlueValue );
78  if ( aAlphaValue > 1.0f ) aAlphaValue = 1.0f;
79  if ( aAlphaValue < 0.0f ) aAlphaValue = 0.0f;
80  myAlpha = static_cast<unsigned char>( 255 * aAlphaValue );
81  return *this;
82 }
83 
84 
85 
86 bool
87 DGtal::Color::operator==( const Color & aColor ) const
88 {
89  return myRed == aColor.myRed
90  && myGreen == aColor.myGreen
91  && myBlue == aColor.myBlue
92  && myAlpha == aColor.myAlpha;
93 }
94 
95 bool
96 DGtal::Color::operator!=( const Color & aColor ) const
97 {
98  return myRed != aColor.myRed
99  || myGreen != aColor.myGreen
100  || myBlue != aColor.myBlue
101  || myAlpha != aColor.myAlpha;
102 }
103 
104 bool
105 DGtal::Color::operator<( const Color & aColor ) const
106 {
107  if ( myRed < aColor.myRed )
108  return true;
109  if ( myRed == aColor.myRed ) {
110  if ( myGreen < aColor.myGreen )
111  return true;
112  if ( myGreen == aColor.myGreen ) {
113  if ( myBlue < aColor.myBlue )
114  return true;
115  if ( myBlue == aColor.myBlue )
116  return myAlpha < aColor.myAlpha;
117  }
118  }
119  return false;
120 }
121 
122 bool
123 DGtal::Color::operator>( const Color & aColor ) const
124 {
125  return !this->operator<(aColor);
126 }
127 
128 bool
129 DGtal::Color::operator<=( const Color & aColor ) const
130 {
131  return this->operator<(aColor) || this->operator==(aColor);
132 }
133 
134 bool
135 DGtal::Color::operator>=( const Color & aColor ) const
136 {
137  return this->operator>(aColor) || this->operator==(aColor);
138 }
139 
140 
141 void
142 DGtal::Color::flushPostscript( std::ostream & stream ) const
143 {
144  stream << ((double)myRed/255.0) << " "
145  << ((double)myGreen/255.0) << " "
146  << ((double)myBlue/255.0) << " srgb\n";
147 }
148 
149 string
151 {
152  char buffer[255];
153  secured_sprintf( buffer, 255, "%.4f %.4f %.4f", myRed/255.0, myGreen/255.0, myBlue/255.0 );
154  return buffer;
155 }
156 
157 string
159 {
160  char buffer[255];
161  if ( *this == DGtal::Color::None ) return "none";
162  secured_sprintf( buffer, 255, "rgb(%d,%d,%d)",myRed, myGreen, myBlue );
163  return buffer;
164 }
165 
166 string
167 DGtal::Color::svgAlpha( const char * prefix ) const
168 {
169  char buffer[255];
170  if ( myAlpha == 255 || *this == DGtal::Color::None ) return "";
171  secured_sprintf( buffer, 255, " %s-opacity=\"%f\"", prefix, myAlpha/255.0f );
172  return buffer;
173 }
174 
175 string
177 {
178  // see tex/generic/pgf/utilities/pgfutil-plain.def for color definitions
179  char buffer[255];
180  if ( *this == DGtal::Color::None ) return "none";
181  if ( *this == DGtal::Color::Black ) return "black";
182  if ( *this == DGtal::Color::Gray ) return "gray";
183  if ( *this == DGtal::Color::White ) return "white";
184  if ( *this == DGtal::Color::Red ) return "red";
185  if ( *this == DGtal::Color::Green ) return "green!50!black";
186  if ( *this == DGtal::Color::Lime ) return "green";
187  if ( *this == DGtal::Color::Blue ) return "blue";
188 // if ( *this == DGtal::Color::Cyan ) return "cyan";
189 // if ( *this == DGtal::Color::Magenta ) return "magenta";
190 // if ( *this == DGtal::Color::Yellow ) return "yellow";
191  if ( *this == DGtal::Color::Silver ) return "white!75!black";
192  if ( *this == DGtal::Color::Purple ) return "purple";
193  if ( *this == DGtal::Color::Navy ) return "blue!50!black";
194 // if ( *this == DGtal::Color::Aqua ) return "cyan"; // ???: Is Color::Aqua meant to be equal to Color::Cyan?
195  secured_sprintf( buffer, 255, "{rgb,255:red,%d;green,%d;blue,%d}", myRed, myGreen, myBlue );
196  return buffer;
197 }
198 
199 
201 // Interface - public :
202 
203 const DGtal::Color DGtal::Color::None(0,0,0,0);
204 const DGtal::Color DGtal::Color::Black((unsigned char)0,(unsigned char)0,(unsigned char)0);
205 const DGtal::Color DGtal::Color::Gray((unsigned char)128,(unsigned char)128,(unsigned char)128);
206 const DGtal::Color DGtal::Color::White((unsigned char)255,(unsigned char)255,(unsigned char)255);
207 const DGtal::Color DGtal::Color::Red((unsigned char)255,(unsigned char)0,(unsigned char)0);
208 const DGtal::Color DGtal::Color::Green((unsigned char)0,(unsigned char)255,(unsigned char)0);
209 const DGtal::Color DGtal::Color::Lime((unsigned char)0,(unsigned char)255,(unsigned char)0);
210 const DGtal::Color DGtal::Color::Blue((unsigned char)0,(unsigned char)0,(unsigned char)255);
211 const DGtal::Color DGtal::Color::Cyan((unsigned char)0,(unsigned char)255,(unsigned char)255);
212 const DGtal::Color DGtal::Color::Magenta((unsigned char)255,(unsigned char)0,(unsigned char)255);
213 const DGtal::Color DGtal::Color::Yellow((unsigned char)255,(unsigned char)255,(unsigned char)0);
214 const DGtal::Color DGtal::Color::Silver((unsigned char)190,(unsigned char)190,(unsigned char)190);
215 const DGtal::Color DGtal::Color::Purple((unsigned char)128,(unsigned char)0,(unsigned char)128);
216 const DGtal::Color DGtal::Color::Navy((unsigned char)0,(unsigned char)0,(unsigned char)128);
217 const DGtal::Color DGtal::Color::Aqua((unsigned char)0,(unsigned char)255,(unsigned char)255);
218 
219 
224 void
225 DGtal::Color::selfDisplay ( std::ostream & out ) const
226 {
227  out << "[Color] RGBA("<<(int)myRed<<","<<(int)myGreen<<","<<(int)myBlue<<","<<(int)myAlpha<<")";
228 }
229 
234 bool
236 {
237  return true;
238 }
239 
240 
241 
243 // Internals - private :
244 
245 // //
Structure representing an RGB triple with alpha component.
Definition: Color.h:68
static const Color Purple
Definition: Color.h:424
unsigned char myRed
Definition: Color.h:433
static const Color Aqua
Definition: Color.h:426
std::string postscript() const
Definition: Color.cpp:150
unsigned char myGreen
Definition: Color.h:434
static const Color Yellow
Definition: Color.h:422
bool operator>(const Color &aColor) const
Definition: Color.cpp:123
bool isValid() const
Definition: Color.cpp:235
void flushPostscript(std::ostream &) const
Definition: Color.cpp:142
Color & setRGBA(DGtal::uint32_t aRGBA)
Definition: Color.cpp:54
bool operator<(const Color &aColor) const
Definition: Color.cpp:105
static const Color None
Definition: Color.h:412
static const Color Cyan
Definition: Color.h:420
bool operator==(const Color &aColor) const
Definition: Color.cpp:87
unsigned char myBlue
Definition: Color.h:435
void selfDisplay(std::ostream &out) const
Definition: Color.cpp:225
static const Color Green
Definition: Color.h:417
bool operator!=(const Color &aColor) const
Definition: Color.cpp:96
std::string svg() const
Definition: Color.cpp:158
bool operator<=(const Color &aColor) const
Definition: Color.cpp:129
static const Color Navy
Definition: Color.h:425
static const Color Gray
Definition: Color.h:414
std::string tikz() const
Definition: Color.cpp:176
static const Color Silver
Definition: Color.h:423
static const Color Red
Definition: Color.h:416
static const Color Lime
Definition: Color.h:418
static const Color White
Definition: Color.h:415
static const Color Blue
Definition: Color.h:419
static const Color Black
Definition: Color.h:413
Color & setRGBf(float red, float green, float blue, float alpha=1.0)
Definition: Color.cpp:65
bool operator>=(const Color &aColor) const
Definition: Color.cpp:135
std::string svgAlpha(const char *aPrefix) const
Definition: Color.cpp:167
unsigned char myAlpha
Definition: Color.h:436
static const Color Magenta
Definition: Color.h:421
bool operator==(PointVector< ptDim, LeftEuclideanRing, LeftContainer > const &lhs, PointVector< ptDim, RightEuclideanRing, RightContainer > const &rhs)
Equality operator between two Points/Vectors.
boost::uint32_t uint32_t
unsigned 32-bit integer.
Definition: BasicTypes.h:63
bool operator<(PointVector< ptDim, LeftEuclideanRing, LeftContainer > const &lhs, PointVector< ptDim, RightEuclideanRing, RightContainer > const &rhs)
Comparison operator on Points/Vectors (LesserThan).
bool operator>(PointVector< ptDim, LeftEuclideanRing, LeftContainer > const &lhs, PointVector< ptDim, RightEuclideanRing, RightContainer > const &rhs)
Comparison operator on Points/Vectors (GreaterThan).