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(Magick::Quantum red_,Magick::Quantum green_,Magick::Quantum blue_);
41 
42  // Construct Color using the specified RGBA values
43  Color(Magick::Quantum red_,Magick::Quantum green_,Magick::Quantum blue_,Magick::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) && (MAGICKCORE_SIZEOF_FLOAT_T != MAGICKCORE_SIZEOF_DOUBLE || !defined(MAGICKCORE_HDRI_SUPPORT))
121  static double scaleQuantumToDouble(const Quantum quantum_)
122  {
123  return (static_cast<double>(quantum_)/QuantumRange);
124  }
125 #else
126  static double scaleQuantumToDouble(const double quantum_)
127  {
128  return (quantum_/QuantumRange);
129  }
130 #endif
131 
132  protected:
133 
134  // PixelType specifies the interpretation of PixelPacket members
135  // RGBPixel:
136  // Red = red;
137  // Green = green;
138  // Blue = blue;
139  // RGBAPixel:
140  // Red = red;
141  // Green = green;
142  // Blue = blue;
143  // Alpha = opacity;
144  // CYMKPixel:
145  // Cyan = red
146  // Yellow = green
147  // Magenta = blue
148  // Black(K) = opacity
150  {
153  CYMKPixel
154  };
155 
156  // Constructor to construct with PixelPacket*
157  // Used to point Color at a pixel in an image
158  Color(PixelPacket *rep_,PixelType pixelType_);
159 
160  // Set pixel
161  // Used to point Color at a pixel in an image
162  void pixel(PixelPacket *rep_,PixelType pixelType_);
163 
164  // PixelPacket represents a color pixel:
165  // red = red (range 0 to QuantumRange)
166  // green = green (range 0 to QuantumRange)
167  // blue = blue (range 0 to QuantumRange)
168  // opacity = alpha (range OpaqueOpacity=0 to TransparentOpacity=QuantumRange)
169  // index = PseudoColor colormap index
170  PixelPacket *_pixel;
171 
172  private:
173 
174  bool _isValid; // Set true if pixel is "valid"
175  bool _pixelOwn; // Set true if we allocated pixel
176  PixelType _pixelType; // Color type supported by _pixel
177 
178  // Common initializer for PixelPacket representation
179  void initPixel();
180  };
181 
182  //
183  // Grayscale RGB color
184  //
185  // Grayscale is simply RGB with equal parts of red, green, and blue
186  // All double arguments have a valid range of 0.0 - 1.0.
188  {
189  public:
190 
191  // Default constructor
192  ColorGray(void);
193 
194  // Copy constructor
195  ColorGray(const Color & color_);
196 
197  // Construct ColorGray using the specified shade
198  ColorGray(double shade_);
199 
200  // Destructor
201  ~ColorGray();
202 
203  void shade(double shade_);
204  double shade(void) const;
205 
206  // Assignment operator from base class
207  ColorGray& operator=(const Color& color_);
208 
209  protected:
210 
211  // Constructor to construct with PixelPacket*
212  ColorGray(PixelPacket *rep_,PixelType pixelType_);
213  };
214 
215  //
216  // HSL Colorspace colors
217  //
219  {
220  public:
221 
222  // Default constructor
223  ColorHSL(void);
224 
225  // Copy constructor
226  ColorHSL(const Color &color_);
227 
228  // Construct ColorHSL using the specified HSL values
229  ColorHSL(double hue_,double saturation_,double luminosity_);
230 
231  // Destructor
232  ~ColorHSL();
233 
234  // Assignment operator from base class
235  ColorHSL& operator=(const Color& color_);
236 
237  // Hue color
238  void hue(double hue_);
239  double hue(void) const;
240 
241  // Luminosity color
242  void luminosity(double luminosity_);
243  double luminosity(void) const;
244 
245  // Saturation color
246  void saturation(double saturation_);
247  double saturation(void) const;
248 
249  protected:
250 
251  // Constructor to construct with PixelPacket*
252  ColorHSL(PixelPacket *rep_,PixelType pixelType_);
253  };
254 
255  //
256  // Monochrome color
257  //
258  // Color arguments are constrained to 'false' (black pixel) and 'true'
259  // (white pixel)
261  {
262  public:
263 
264  // Default constructor
265  ColorMono(void);
266 
267  // Construct ColorMono (false=black, true=white)
268  ColorMono(bool mono_);
269 
270  // Copy constructor
271  ColorMono(const Color & color_);
272 
273  // Destructor
274  ~ColorMono();
275 
276  // Assignment operator from base class
277  ColorMono& operator=(const Color& color_);
278 
279  // Mono color
280  void mono(bool mono_);
281  bool mono(void) const;
282 
283  protected:
284  // Constructor to construct with PixelPacket*
285  ColorMono(PixelPacket *rep_,PixelType pixelType_);
286  };
287 
288  //
289  // RGB color
290  //
291  // All color arguments have a valid range of 0.0 - 1.0.
293  {
294  public:
295 
296  // Default constructor
297  ColorRGB(void);
298 
299  // Copy constructor
300  ColorRGB(const Color &color_);
301 
302  // Construct ColorRGB using the specified RGB values
303  ColorRGB(double red_,double green_,double blue_);
304 
305  // Destructor
306  ~ColorRGB(void);
307 
308  // Assignment operator from base class
309  ColorRGB& operator=(const Color& color_);
310 
311  // Blue color
312  void blue(double blue_);
313  double blue(void) const;
314 
315  // Green color
316  void green(double green_);
317  double green(void) const;
318 
319  // Red color
320  void red(double red_);
321  double red(void) const;
322 
323  protected:
324 
325  // Constructor to construct with PixelPacket*
326  ColorRGB(PixelPacket *rep_,PixelType pixelType_);
327  };
328 
329  //
330  // YUV Colorspace color
331  //
332  // Argument ranges:
333  // Y: 0.0 through 1.0
334  // U: -0.5 through 0.5
335  // V: -0.5 through 0.5
337  {
338  public:
339 
340  // Default constructor
341  ColorYUV(void);
342 
343  // Copy constructor
344  ColorYUV(const Color &color_);
345 
346  // Construct ColorYUV using the specified YUV values
347  ColorYUV(double y_,double u_,double v_);
348 
349  // Destructor
350  ~ColorYUV(void);
351 
352  // Assignment operator from base class
353  ColorYUV& operator=(const Color& color_);
354 
355  // Color U (0.0 through 1.0)
356  void u(double u_);
357  double u(void) const;
358 
359  // Color V (-0.5 through 0.5)
360  void v(double v_);
361  double v(void) const;
362 
363  // Color Y (-0.5 through 0.5)
364  void y(double y_);
365  double y(void) const;
366 
367  protected:
368 
369  // Constructor to construct with PixelInfo*
370  ColorYUV(PixelPacket *rep_,PixelType pixelType_);
371  };
372 } // namespace Magick
373 
374 //
375 // Inlines
376 //
377 
378 //
379 // Color
380 //
381 
382 inline void Magick::Color::alpha(double alpha_)
383 {
385 }
386 inline double Magick::Color::alpha(void) const
387 {
388  return scaleQuantumToDouble(alphaQuantum());
389 }
390 
391 inline void Magick::Color::alphaQuantum(Magick::Quantum alpha_)
392 {
393  _pixel->opacity=alpha_;
394  _isValid=true ;
395 }
396 
397 inline Magick::Quantum Magick::Color::alphaQuantum(void) const
398 {
399  return _pixel->opacity;
400 }
401 
402 inline void Magick::Color::blueQuantum(Magick::Quantum blue_)
403 {
404  _pixel->blue=blue_;
405  _isValid=true;
406 }
407 
408 inline Magick::Quantum Magick::Color::blueQuantum(void) const
409 {
410  return _pixel->blue;
411 }
412 
413 inline void Magick::Color::greenQuantum(Magick::Quantum green_)
414 {
415  _pixel->green=green_;
416  _isValid=true;
417 }
418 
419 inline Magick::Quantum Magick::Color::greenQuantum(void) const
420 {
421  return _pixel->green;
422 }
423 
424 inline void Magick::Color::redQuantum(Magick::Quantum red_)
425 {
426  _pixel->red=red_;
427  _isValid=true;
428 }
429 
430 inline Magick::Quantum Magick::Color::redQuantum(void) const
431 {
432  return _pixel->red;
433 }
434 
435 inline void Magick::Color::initPixel()
436 {
437  _pixel->red=0;
438  _pixel->green=0;
439  _pixel->blue=0;
440  _pixel->opacity=TransparentOpacity;
441 }
442 
443 inline Magick::Color::operator MagickCore::PixelPacket() const
444 {
445  return *_pixel;
446 }
447 
448 //
449 // ColorGray
450 //
451 inline Magick::ColorGray::ColorGray(Magick::PixelPacket *rep_,
452  Magick::Color::PixelType pixelType_)
453 : Color(rep_,pixelType_)
454 {
455 }
456 
457 //
458 // ColorHSL
459 //
460 inline Magick::ColorHSL::ColorHSL(Magick::PixelPacket *rep_,
461  Magick::Color::PixelType pixelType_)
462 : Color(rep_,pixelType_)
463 {
464 }
465 
466 //
467 // ColorMono
468 //
469 inline Magick::ColorMono::ColorMono(Magick::PixelPacket *rep_,
470  Magick::Color::PixelType pixelType_)
471  : Color(rep_,pixelType_)
472 {
473 }
474 
475 //
476 // ColorRGB
477 //
478 inline Magick::ColorRGB::ColorRGB(Magick::PixelPacket *rep_,
479  Magick::Color::PixelType pixelType_)
480  : Color(rep_,pixelType_)
481 {
482 }
483 
484 inline void Magick::ColorRGB::blue(double blue_)
485 {
486  blueQuantum(scaleDoubleToQuantum(blue_));
487 }
488 
489 inline double Magick::ColorRGB::blue(void) const
490 {
491  return scaleQuantumToDouble(blueQuantum());
492 }
493 
494 inline void Magick::ColorRGB::green(double green_)
495 {
496  greenQuantum(scaleDoubleToQuantum(green_));
497 }
498 
499 inline double Magick::ColorRGB::green(void) const
500 {
501  return scaleQuantumToDouble(greenQuantum());
502 }
503 
504 inline void Magick::ColorRGB::red(double red_)
505 {
506  redQuantum(scaleDoubleToQuantum(red_));
507 }
508 
509 inline double Magick::ColorRGB::red(void) const
510 {
511  return scaleQuantumToDouble(redQuantum());
512 }
513 
514 //
515 // ColorYUV
516 //
517 
518 inline Magick::ColorYUV::ColorYUV(Magick::PixelPacket *rep_,
519  Magick::Color::PixelType pixelType_)
520  : Color(rep_,pixelType_)
521 {
522 }
523 
524 #endif // Magick_Color_header
class MagickPPExport Color
Definition: Color.h:15
Quantum greenQuantum(void) const
Definition: Color.h:419
Quantum alphaQuantum(void) const
Definition: Color.h:397
ColorYUV(void)
Definition: Color.cpp:545
double alpha(void) const
Definition: Color.h:386
Quantum blueQuantum(void) const
Definition: Color.h:408
ColorRGB(void)
Definition: Color.cpp:518
double red(void) const
Definition: Color.h:509
#define MagickPPExport
Definition: Include.h:261
PixelPacket * _pixel
Definition: Color.h:170
double blue(void) const
Definition: Color.h:489
double intensity(void) const
Definition: Color.h:108
ColorHSL(void)
Definition: Color.cpp:331
static double scaleQuantumToDouble(const Quantum quantum_)
Definition: Color.h:121
Quantum redQuantum(void) const
Definition: Color.h:430
static Quantum scaleDoubleToQuantum(const double double_)
Definition: Color.h:114
double green(void) const
Definition: Color.h:499