DGtal  0.9.2
Color.cpp
1 
29 #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 
123 void
124 DGtal::Color::flushPostscript( std::ostream & stream ) const
125 {
126  stream << ((double)myRed/255.0) << " "
127  << ((double)myGreen/255.0) << " "
128  << ((double)myBlue/255.0) << " srgb\n";
129 }
130 
131 string
133 {
134  char buffer[255];
135  secured_sprintf( buffer, 255, "%.4f %.4f %.4f", myRed/255.0, myGreen/255.0, myBlue/255.0 );
136  return buffer;
137 }
138 
139 string
141 {
142  char buffer[255];
143  if ( *this == DGtal::Color::None ) return "none";
144  secured_sprintf( buffer, 255, "rgb(%d,%d,%d)",myRed, myGreen, myBlue );
145  return buffer;
146 }
147 
148 string
149 DGtal::Color::svgAlpha( const char * prefix ) const
150 {
151  char buffer[255];
152  if ( myAlpha == 255 || *this == DGtal::Color::None ) return "";
153  secured_sprintf( buffer, 255, " %s-opacity=\"%f\"", prefix, myAlpha/255.0f );
154  return buffer;
155 }
156 
157 string
159 {
160  // see tex/generic/pgf/utilities/pgfutil-plain.def for color definitions
161  char buffer[255];
162  if ( *this == DGtal::Color::None ) return "none";
163  if ( *this == DGtal::Color::Black ) return "black";
164  if ( *this == DGtal::Color::Gray ) return "gray";
165  if ( *this == DGtal::Color::White ) return "white";
166  if ( *this == DGtal::Color::Red ) return "red";
167  if ( *this == DGtal::Color::Green ) return "green!50!black";
168  if ( *this == DGtal::Color::Lime ) return "green";
169  if ( *this == DGtal::Color::Blue ) return "blue";
170 // if ( *this == DGtal::Color::Cyan ) return "cyan";
171 // if ( *this == DGtal::Color::Magenta ) return "magenta";
172 // if ( *this == DGtal::Color::Yellow ) return "yellow";
173  if ( *this == DGtal::Color::Silver ) return "white!75!black";
174  if ( *this == DGtal::Color::Purple ) return "gray"; // ???: Is Color::Purple meant to be equal to Color::Gray?
175  if ( *this == DGtal::Color::Navy ) return "blue!50!black";
176 // if ( *this == DGtal::Color::Aqua ) return "cyan"; // ???: Is Color::Aqua meant to be equal to Color::Cyan?
177  secured_sprintf( buffer, 255, "{rgb,255:red,%d;green,%d;blue,%d}", myRed, myGreen, myBlue );
178  return buffer;
179 }
180 
181 
182 
187 {
188 }
189 
190 
191 
193 // Interface - public :
194 
195 const DGtal::Color DGtal::Color::None(0,0,0,0);
196 const DGtal::Color DGtal::Color::Black((unsigned char)0,(unsigned char)0,(unsigned char)0);
197 const DGtal::Color DGtal::Color::Gray((unsigned char)128,(unsigned char)128,(unsigned char)128);
198 const DGtal::Color DGtal::Color::White((unsigned char)255,(unsigned char)255,(unsigned char)255);
199 const DGtal::Color DGtal::Color::Red((unsigned char)255,(unsigned char)0,(unsigned char)0);
200 const DGtal::Color DGtal::Color::Green((unsigned char)0,(unsigned char)128,(unsigned char)0);
201 const DGtal::Color DGtal::Color::Lime((unsigned char)0,(unsigned char)255,(unsigned char)0);
202 const DGtal::Color DGtal::Color::Blue((unsigned char)0,(unsigned char)0,(unsigned char)255);
203 const DGtal::Color DGtal::Color::Cyan((unsigned char)0,(unsigned char)255,(unsigned char)255);
204 const DGtal::Color DGtal::Color::Magenta((unsigned char)255,(unsigned char)0,(unsigned char)255);
205 const DGtal::Color DGtal::Color::Yellow((unsigned char)255,(unsigned char)255,(unsigned char)0);
206 const DGtal::Color DGtal::Color::Silver((unsigned char)190,(unsigned char)190,(unsigned char)190);
207 const DGtal::Color DGtal::Color::Purple((unsigned char)128,(unsigned char)128,(unsigned char)128);
208 const DGtal::Color DGtal::Color::Navy((unsigned char)0,(unsigned char)0,(unsigned char)128);
209 const DGtal::Color DGtal::Color::Aqua((unsigned char)0,(unsigned char)255,(unsigned char)255);
210 
211 
216 void
217 DGtal::Color::selfDisplay ( std::ostream & out ) const
218 {
219  out << "[Color] RGBA("<<(int)myRed<<","<<(int)myGreen<<","<<(int)myBlue<<","<<(int)myAlpha<<")";
220 }
221 
226 bool
228 {
229  return true;
230 }
231 
232 
233 
235 // Internals - private :
236 
237 // //
static const Color Black
Definition: Color.h:388
boost::uint32_t uint32_t
unsigned 32-bit integer.
Definition: BasicTypes.h:63
bool operator!=(const Color &aColor) const
Definition: Color.cpp:96
static const Color Magenta
Definition: Color.h:396
unsigned char myGreen
Definition: Color.h:408
unsigned char myRed
Definition: Color.h:407
static const Color Navy
Definition: Color.h:400
STL namespace.
bool operator<(const Color &aColor) const
Definition: Color.cpp:105
static const Color Cyan
Definition: Color.h:395
Color & setRGBA(DGtal::uint32_t aRGBA)
Definition: Color.cpp:54
static const Color Yellow
Definition: Color.h:397
void selfDisplay(std::ostream &out) const
Definition: Color.cpp:217
unsigned char myAlpha
Definition: Color.h:410
static const Color None
Definition: Color.h:387
static const Color Lime
Definition: Color.h:393
void flushPostscript(std::ostream &) const
Definition: Color.cpp:124
static const Color Blue
Definition: Color.h:394
static const Color Gray
Definition: Color.h:389
unsigned char myBlue
Definition: Color.h:409
std::string svg() const
Definition: Color.cpp:140
static const Color Silver
Definition: Color.h:398
bool isValid() const
Definition: Color.cpp:227
std::string tikz() const
Definition: Color.cpp:158
std::string postscript() const
Definition: Color.cpp:132
static const Color Aqua
Definition: Color.h:401
bool operator==(const Color &aColor) const
Definition: Color.cpp:87
Structure representing an RGB triple with alpha component.
Definition: Color.h:66
static const Color White
Definition: Color.h:390
static const Color Red
Definition: Color.h:391
Color & setRGBf(float red, float green, float blue, float alpha=1.0)
Definition: Color.cpp:65
static const Color Green
Definition: Color.h:392
static const Color Purple
Definition: Color.h:399
std::string svgAlpha(const char *aPrefix) const
Definition: Color.cpp:149