DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Color.cpp
1 
29 
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 
53 Color &
54 DGtal::Color::setRGBf( float aRedValue,
55  float aGreenValue,
56  float aBlueValue,
57  float aAlphaValue ) {
58  if ( aRedValue > 1.0f ) aRedValue = 1.0f;
59  if ( aRedValue < 0.0f ) aRedValue = 0.0f;
60  myRed = static_cast<unsigned char>( 255 * aRedValue );
61  if ( aGreenValue > 1.0f ) aGreenValue = 1.0f;
62  if ( aGreenValue < 0.0f ) aGreenValue = 0.0f;
63  myGreen = static_cast<unsigned char>( 255 * aGreenValue );
64  if ( aBlueValue > 1.0f ) aBlueValue = 1.0f;
65  if ( aBlueValue < 0.0f ) aBlueValue = 0.0f;
66  myBlue = static_cast<unsigned char>( 255 * aBlueValue );
67  if ( aAlphaValue > 1.0f ) aAlphaValue = 1.0f;
68  if ( aAlphaValue < 0.0f ) aAlphaValue = 0.0f;
69  myAlpha = static_cast<unsigned char>( 255 * aAlphaValue );
70  return *this;
71 }
72 
73 
74 
75 bool
76 DGtal::Color::operator==( const Color & aColor ) const
77 {
78  return myRed == aColor.myRed
79  && myGreen == aColor.myGreen
80  && myBlue == aColor.myBlue
81  && myAlpha == aColor.myAlpha;
82 }
83 
84 bool
85 DGtal::Color::operator!=( const Color & aColor ) const
86 {
87  return myRed != aColor.myRed
88  || myGreen != aColor.myGreen
89  || myBlue != aColor.myBlue
90  || myAlpha != aColor.myAlpha;
91 }
92 
93 bool
94 DGtal::Color::operator<( const Color & aColor ) const
95 {
96  if ( myRed < aColor.myRed )
97  return true;
98  if ( myRed == aColor.myRed ) {
99  if ( myGreen < aColor.myGreen )
100  return true;
101  if ( myGreen == aColor.myGreen ) {
102  if ( myBlue < aColor.myBlue )
103  return true;
104  if ( myBlue == aColor.myBlue )
105  return myAlpha < aColor.myAlpha;
106  }
107  }
108  return false;
109 }
110 
111 
112 void
113 DGtal::Color::flushPostscript( std::ostream & stream ) const
114 {
115  stream << ((double)myRed/255.0) << " "
116  << ((double)myGreen/255.0) << " "
117  << ((double)myBlue/255.0) << " srgb\n";
118 }
119 
120 string
122 {
123  char buffer[255];
124  secured_sprintf( buffer, 255, "%.4f %.4f %.4f", myRed/255.0, myGreen/255.0, myBlue/255.0 );
125  return buffer;
126 }
127 
128 string
130 {
131  char buffer[255];
132  if ( *this == DGtal::Color::None ) return "none";
133  secured_sprintf( buffer, 255, "rgb(%d,%d,%d)",myRed, myGreen, myBlue );
134  return buffer;
135 }
136 
137 string
138 DGtal::Color::svgAlpha( const char * prefix ) const
139 {
140  char buffer[255];
141  if ( myAlpha == 255 || *this == DGtal::Color::None ) return "";
142  secured_sprintf( buffer, 255, " %s-opacity=\"%f\"", prefix, myAlpha/255.0f );
143  return buffer;
144 }
145 
146 string
148 {
149  // see tex/generic/pgf/utilities/pgfutil-plain.def for color definitions
150  char buffer[255];
151  if ( *this == DGtal::Color::None ) return "none";
152  if ( *this == DGtal::Color::Black ) return "black";
153  if ( *this == DGtal::Color::Gray ) return "gray";
154  if ( *this == DGtal::Color::White ) return "white";
155  if ( *this == DGtal::Color::Red ) return "red";
156  if ( *this == DGtal::Color::Green ) return "green!50!black";
157  if ( *this == DGtal::Color::Lime ) return "green";
158  if ( *this == DGtal::Color::Blue ) return "blue";
159 // if ( *this == DGtal::Color::Cyan ) return "cyan";
160 // if ( *this == DGtal::Color::Magenta ) return "magenta";
161 // if ( *this == DGtal::Color::Yellow ) return "yellow";
162  if ( *this == DGtal::Color::Silver ) return "white!75!black";
163  if ( *this == DGtal::Color::Purple ) return "gray"; // ???: Is Color::Purple meant to be equal to Color::Gray?
164  if ( *this == DGtal::Color::Navy ) return "blue!50!black";
165 // if ( *this == DGtal::Color::Aqua ) return "cyan"; // ???: Is Color::Aqua meant to be equal to Color::Cyan?
166  secured_sprintf( buffer, 255, "{rgb,255:red,%d;green,%d;blue,%d}", myRed, myGreen, myBlue );
167  return buffer;
168 }
169 
170 
171 
176 {
177 }
178 
179 
180 
182 // Interface - public :
183 
184 const Color DGtal::Color::None(false);
185 const Color DGtal::Color::Black((unsigned char)0,(unsigned char)0,(unsigned char)0);
186 const Color DGtal::Color::Gray((unsigned char)128,(unsigned char)128,(unsigned char)128);
187 const Color DGtal::Color::White((unsigned char)255,(unsigned char)255,(unsigned char)255);
188 const Color DGtal::Color::Red((unsigned char)255,(unsigned char)0,(unsigned char)0);
189 const Color DGtal::Color::Green((unsigned char)0,(unsigned char)128,(unsigned char)0);
190 const Color DGtal::Color::Lime((unsigned char)0,(unsigned char)255,(unsigned char)0);
191 const Color DGtal::Color::Blue((unsigned char)0,(unsigned char)0,(unsigned char)255);
192 const Color DGtal::Color::Cyan((unsigned char)0,(unsigned char)255,(unsigned char)255);
193 const Color DGtal::Color::Magenta((unsigned char)255,(unsigned char)0,(unsigned char)255);
194 const Color DGtal::Color::Yellow((unsigned char)255,(unsigned char)255,(unsigned char)0);
195 const Color DGtal::Color::Silver((unsigned char)190,(unsigned char)190,(unsigned char)190);
196 const Color DGtal::Color::Purple((unsigned char)128,(unsigned char)128,(unsigned char)128);
197 const Color DGtal::Color::Navy((unsigned char)0,(unsigned char)0,(unsigned char)128);
198 const Color DGtal::Color::Aqua((unsigned char)0,(unsigned char)255,(unsigned char)255);
199 
200 
205 void
206 DGtal::Color::selfDisplay ( std::ostream & out ) const
207 {
208  out << "[Color]";
209 }
210 
215 bool
217 {
218  return true;
219 }
220 
221 
222 
224 // Internals - private :
225 
226 // //