Magick++  6.9.3
Color.h
Go to the documentation of this file.
1 // This may look like C code, but it is really -*- C++ -*-
2 //
3 // Copyright Bob Friesenhahn, 1999, 2000, 2001, 2002, 2003, 2008
4 //
5 // Color Implementation
6 //
7 #if !defined (Magick_Color_header)
8 #define Magick_Color_header
9 
10 #include "Magick++/Include.h"
11 #include <string>
12 
13 namespace Magick
14 {
16 
17  // Compare two Color objects regardless of LHS/RHS
18  MagickPPExport int operator ==
19  (const Magick::Color &left_,const Magick::Color &right_);
20  MagickPPExport int operator !=
21  (const Magick::Color &left_,const Magick::Color &right_);
22  MagickPPExport int operator >
23  (const Magick::Color &left_,const Magick::Color &right_);
24  MagickPPExport int operator <
25  (const Magick::Color &left_,const Magick::Color &right_);
26  MagickPPExport int operator >=
27  (const Magick::Color &left_,const Magick::Color &right_);
28  MagickPPExport int operator <=
29  (const Magick::Color &left_,const Magick::Color &right_);
30 
31  // Base color class stores RGB components scaled to fit Quantum
33  {
34  public:
35 
36  // Default constructor
37  Color(void);
38 
39  // Construct Color using the specified RGB values
40  Color(Quantum red_,Quantum green_,Quantum blue_);
41 
42  // Construct Color using the specified RGBA values
43  Color(Quantum red_,Quantum green_,Quantum blue_,Quantum alpha_);
44 
45  // Construct Color using the specified color string
46  Color(const char *x11color_);
47 
48  // Copy constructor
49  Color(const Color &color_);
50 
51  // Construct color via ImageMagick PixelPacket
52  Color(const PixelPacket &color_);
53 
54  // Constructor Color using the specified color string
55  Color(const std::string &x11color_);
56 
57  // Destructor
58  virtual ~Color(void);
59 
60  // Assignment operator
61  Color& operator=(const Color& color_);
62 
63  // Set color via X11 color specification string
64  const Color& operator=(const char *x11color);
65 
66  // Set color via X11 color specification string
67  const Color& operator=(const std::string &x11color_);
68 
69  // Set color via ImageMagick PixelPacket
70  const Color& operator=(const PixelPacket &color_);
71 
72  // Return ImageMagick PixelPacket
73  operator PixelPacket() const;
74 
75  // Return X11 color specification string
76  operator std::string() const;
77 
78  // Scaled (to 1.0) version of alpha for use in sub-classes
79  // (range opaque=0 to transparent=1.0)
80  void alpha(double alpha_);
81  double alpha(void) const;
82 
83  // Alpha level (range OpaqueOpacity=0 to TransparentOpacity=QuantumRange)
84  void alphaQuantum(Quantum alpha_);
85  Quantum alphaQuantum(void) const;
86 
87  // Blue color (range 0 to QuantumRange)
88  void blueQuantum(Quantum blue_);
89  Quantum blueQuantum (void) const;
90 
91  // Green color (range 0 to QuantumRange)
92  void greenQuantum(Quantum green_);
93  Quantum greenQuantum(void) const;
94 
95  // Does object contain valid color?
96  void isValid(bool valid_);
97  bool isValid(void) const;
98 
99  // Red color (range 0 to QuantumRange)
100  void redQuantum(Quantum red_);
101  Quantum redQuantum (void) const;
102 
103  //
104  // Public methods beyond this point are for Magick++ use only.
105  //
106 
107  // Obtain pixel intensity as a double
108  double intensity(void) const
109  {
110  return (0.299*(_pixel->red)+0.587*(_pixel->green)+0.114*(_pixel->blue));
111  }
112 
113  // Scale a value expressed as a double (0-1) to Quantum range (0-QuantumRange)
114  static Quantum scaleDoubleToQuantum(const double double_)
115  {
116  return (static_cast<Magick::Quantum>(double_*QuantumRange));
117  }
118 
119  // Scale a value expressed as a Quantum (0-QuantumRange) to double range (0-1)
120 #if (MAGICKCORE_QUANTUM_DEPTH < 32)
121  static double scaleQuantumToDouble(const Quantum quantum_)
122  {
123  return (static_cast<double>(quantum_)/QuantumRange);
124  }
125 #endif
126  static double scaleQuantumToDouble(const double quantum_)
127  {
128  return (quantum_/QuantumRange);
129  }
130 
131  protected:
132 
133  // PixelType specifies the interpretation of PixelPacket members
134  // RGBPixel:
135  // Red = red;
136  // Green = green;
137  // Blue = blue;
138  // RGBAPixel:
139  // Red = red;
140  // Green = green;
141  // Blue = blue;
142  // Alpha = opacity;
143  // CYMKPixel:
144  // Cyan = red
145  // Yellow = green
146  // Magenta = blue
147  // Black(K) = opacity
149  {
152  CYMKPixel
153  };
154 
155  // Constructor to construct with PixelPacket*
156  // Used to point Color at a pixel in an image
157  Color(PixelPacket *rep_,PixelType pixelType_);
158 
159  // Set pixel
160  // Used to point Color at a pixel in an image
161  void pixel(PixelPacket *rep_,PixelType pixelType_);
162 
163  // PixelPacket represents a color pixel:
164  // red = red (range 0 to QuantumRange)
165  // green = green (range 0 to QuantumRange)
166  // blue = blue (range 0 to QuantumRange)
167  // opacity = alpha (range OpaqueOpacity=0 to TransparentOpacity=QuantumRange)
168  // index = PseudoColor colormap index
169  PixelPacket *_pixel;
170 
171  private:
172 
173  bool _isValid; // Set true if pixel is "valid"
174  bool _pixelOwn; // Set true if we allocated pixel
175  PixelType _pixelType; // Color type supported by _pixel
176 
177  // Common initializer for PixelPacket representation
178  void initPixel();
179  };
180 
181  //
182  // Grayscale RGB color
183  //
184  // Grayscale is simply RGB with equal parts of red, green, and blue
185  // All double arguments have a valid range of 0.0 - 1.0.
187  {
188  public:
189 
190  // Default constructor
191  ColorGray(void);
192 
193  // Copy constructor
194  ColorGray(const Color & color_);
195 
196  // Construct ColorGray using the specified shade
197  ColorGray(double shade_);
198 
199  // Destructor
200  ~ColorGray();
201 
202  void shade(double shade_);
203  double shade(void) const;
204 
205  // Assignment operator from base class
206  ColorGray& operator=(const Color& color_);
207 
208  protected:
209 
210  // Constructor to construct with PixelPacket*
211  ColorGray(PixelPacket *rep_,PixelType pixelType_);
212  };
213 
214  //
215  // HSL Colorspace colors
216  //
218  {
219  public:
220 
221  // Default constructor
222  ColorHSL(void);
223 
224  // Copy constructor
225  ColorHSL(const Color &color_);
226 
227  // Construct ColorHSL using the specified HSL values
228  ColorHSL(double hue_,double saturation_,double luminosity_);
229 
230  // Destructor
231  ~ColorHSL();
232 
233  // Assignment operator from base class
234  ColorHSL& operator=(const Color& color_);
235 
236  // Hue color
237  void hue(double hue_);
238  double hue(void) const;
239 
240  // Luminosity color
241  void luminosity(double luminosity_);
242  double luminosity(void) const;
243 
244  // Saturation color
245  void saturation(double saturation_);
246  double saturation(void) const;
247 
248  protected:
249 
250  // Constructor to construct with PixelPacket*
251  ColorHSL(PixelPacket *rep_,PixelType pixelType_);
252  };
253 
254  //
255  // Monochrome color
256  //
257  // Color arguments are constrained to 'false' (black pixel) and 'true'
258  // (white pixel)
260  {
261  public:
262 
263  // Default constructor
264  ColorMono(void);
265 
266  // Construct ColorMono (false=black, true=white)
267  ColorMono(bool mono_);
268 
269  // Copy constructor
270  ColorMono(const Color & color_);
271 
272  // Destructor
273  ~ColorMono();
274 
275  // Assignment operator from base class
276  ColorMono& operator=(const Color& color_);
277 
278  // Mono color
279  void mono(bool mono_);
280  bool mono(void) const;
281 
282  protected:
283  // Constructor to construct with PixelPacket*
284  ColorMono(PixelPacket *rep_,PixelType pixelType_);
285  };
286 
287  //
288  // RGB color
289  //
290  // All color arguments have a valid range of 0.0 - 1.0.
292  {
293  public:
294 
295  // Default constructor
296  ColorRGB(void);
297 
298  // Copy constructor
299  ColorRGB(const Color &color_);
300 
301  // Construct ColorRGB using the specified RGB values
302  ColorRGB(double red_,double green_,double blue_);
303 
304  // Destructor
305  ~ColorRGB(void);
306 
307  // Assignment operator from base class
308  ColorRGB& operator=(const Color& color_);
309 
310  // Blue color
311  void blue(double blue_);
312  double blue(void) const;
313 
314  // Green color
315  void green(double green_);
316  double green(void) const;
317 
318  // Red color
319  void red(double red_);
320  double red(void) const;
321 
322  protected:
323 
324  // Constructor to construct with PixelPacket*
325  ColorRGB(PixelPacket *rep_,PixelType pixelType_);
326  };
327 
328  //
329  // YUV Colorspace color
330  //
331  // Argument ranges:
332  // Y: 0.0 through 1.0
333  // U: -0.5 through 0.5
334  // V: -0.5 through 0.5
336  {
337  public:
338 
339  // Default constructor
340  ColorYUV(void);
341 
342  // Copy constructor
343  ColorYUV(const Color &color_);
344 
345  // Construct ColorYUV using the specified YUV values
346  ColorYUV(double y_,double u_,double v_);
347 
348  // Destructor
349  ~ColorYUV(void);
350 
351  // Assignment operator from base class
352  ColorYUV& operator=(const Color& color_);
353 
354  // Color U (0.0 through 1.0)
355  void u(double u_);
356  double u(void) const;
357 
358  // Color V (-0.5 through 0.5)
359  void v(double v_);
360  double v(void) const;
361 
362  // Color Y (-0.5 through 0.5)
363  void y(double y_);
364  double y(void) const;
365 
366  protected:
367 
368  // Constructor to construct with PixelInfo*
369  ColorYUV(PixelPacket *rep_,PixelType pixelType_);
370  };
371 } // namespace Magick
372 
373 //
374 // Inlines
375 //
376 
377 //
378 // Color
379 //
380 
381 inline void Magick::Color::alpha(double alpha_)
382 {
384 }
385 inline double Magick::Color::alpha(void) const
386 {
387  return scaleQuantumToDouble(alphaQuantum());
388 }
389 
390 inline void Magick::Color::alphaQuantum(Magick::Quantum alpha_)
391 {
392  _pixel->opacity=alpha_;
393  _isValid=true ;
394 }
395 
396 inline Magick::Quantum Magick::Color::alphaQuantum(void) const
397 {
398  return _pixel->opacity;
399 }
400 
401 inline void Magick::Color::blueQuantum(Magick::Quantum blue_)
402 {
403  _pixel->blue=blue_;
404  _isValid=true;
405 }
406 
407 inline Magick::Quantum Magick::Color::blueQuantum(void) const
408 {
409  return _pixel->blue;
410 }
411 
412 inline void Magick::Color::greenQuantum(Magick::Quantum green_)
413 {
414  _pixel->green=green_;
415  _isValid=true;
416 }
417 
418 inline Magick::Quantum Magick::Color::greenQuantum(void) const
419 {
420  return _pixel->green;
421 }
422 
423 inline void Magick::Color::redQuantum(Magick::Quantum red_)
424 {
425  _pixel->red=red_;
426  _isValid=true;
427 }
428 
429 inline Magick::Quantum Magick::Color::redQuantum(void) const
430 {
431  return _pixel->red;
432 }
433 
434 inline void Magick::Color::initPixel()
435 {
436  _pixel->red=0;
437  _pixel->green=0;
438  _pixel->blue=0;
439  _pixel->opacity=TransparentOpacity;
440 }
441 
442 inline Magick::Color::operator MagickCore::PixelPacket() const
443 {
444  return *_pixel;
445 }
446 
447 //
448 // ColorGray
449 //
450 inline Magick::ColorGray::ColorGray(Magick::PixelPacket *rep_,
451  Magick::Color::PixelType pixelType_)
452 : Color(rep_,pixelType_)
453 {
454 }
455 
456 //
457 // ColorHSL
458 //
459 inline Magick::ColorHSL::ColorHSL(Magick::PixelPacket *rep_,
460  Magick::Color::PixelType pixelType_)
461 : Color(rep_,pixelType_)
462 {
463 }
464 
465 //
466 // ColorMono
467 //
468 inline Magick::ColorMono::ColorMono(Magick::PixelPacket *rep_,
469  Magick::Color::PixelType pixelType_)
470  : Color(rep_,pixelType_)
471 {
472 }
473 
474 //
475 // ColorRGB
476 //
477 inline Magick::ColorRGB::ColorRGB(Magick::PixelPacket *rep_,
478  Magick::Color::PixelType pixelType_)
479  : Color(rep_,pixelType_)
480 {
481 }
482 
483 inline void Magick::ColorRGB::blue(double blue_)
484 {
485  blueQuantum(scaleDoubleToQuantum(blue_));
486 }
487 
488 inline double Magick::ColorRGB::blue(void) const
489 {
490  return scaleQuantumToDouble(blueQuantum());
491 }
492 
493 inline void Magick::ColorRGB::green(double green_)
494 {
495  greenQuantum(scaleDoubleToQuantum(green_));
496 }
497 
498 inline double Magick::ColorRGB::green(void) const
499 {
500  return scaleQuantumToDouble(greenQuantum());
501 }
502 
503 inline void Magick::ColorRGB::red(double red_)
504 {
505  redQuantum(scaleDoubleToQuantum(red_));
506 }
507 
508 inline double Magick::ColorRGB::red(void) const
509 {
510  return scaleQuantumToDouble(redQuantum());
511 }
512 
513 //
514 // ColorYUV
515 //
516 
517 inline Magick::ColorYUV::ColorYUV(Magick::PixelPacket *rep_,
518  Magick::Color::PixelType pixelType_)
519  : Color(rep_,pixelType_)
520 {
521 }
522 
523 #endif // Magick_Color_header
class MagickPPExport Color
Definition: Color.h:15
Quantum greenQuantum(void) const
Definition: Color.h:418
Quantum alphaQuantum(void) const
Definition: Color.h:396
ColorYUV(void)
Definition: Color.cpp:534
double alpha(void) const
Definition: Color.h:385
Quantum blueQuantum(void) const
Definition: Color.h:407
ColorRGB(void)
Definition: Color.cpp:507
double red(void) const
Definition: Color.h:508
#define MagickPPExport
Definition: Include.h:255
PixelPacket * _pixel
Definition: Color.h:169
double blue(void) const
Definition: Color.h:488
double intensity(void) const
Definition: Color.h:108
ColorHSL(void)
Definition: Color.cpp:320
static double scaleQuantumToDouble(const Quantum quantum_)
Definition: Color.h:121
static double scaleQuantumToDouble(const double quantum_)
Definition: Color.h:126
Definition: Blob.h:15
Quantum redQuantum(void) const
Definition: Color.h:429
static Quantum scaleDoubleToQuantum(const double double_)
Definition: Color.h:114
double green(void) const
Definition: Color.h:498