1// This may look like C code, but it is really -*- C++ -*-
2//
3// Copyright Bob Friesenhahn, 1999, 2000, 2001, 2002, 2003, 2008
4// Copyright Dirk Lemstra 2013-2014
5//
6// Color Implementation
7//
8#if !defined (Magick_Color_header)
9#define Magick_Color_header
10
11#include "Magick++/Include.h"
12#include <string>
13
14namespace Magick
15{
16  class MagickPPExport Color;
17
18  // Compare two Color objects regardless of LHS/RHS
19  MagickPPExport int operator ==
20    (const Magick::Color& left_,const Magick::Color& right_);
21  MagickPPExport int operator !=
22    (const Magick::Color& left_,const Magick::Color& right_);
23  MagickPPExport int operator >
24    (const Magick::Color& left_,const Magick::Color& right_);
25  MagickPPExport int operator <
26    (const Magick::Color& left_,const Magick::Color& right_);
27  MagickPPExport int operator >=
28    (const Magick::Color& left_,const Magick::Color& right_);
29  MagickPPExport int operator <=
30    (const Magick::Color& left_,const Magick::Color& right_);
31
32  // Base color class stores RGBA components scaled to fit Quantum
33  // All double arguments have a valid range of 0.0 - 1.0.
34  class MagickPPExport Color
35  {
36  public:
37
38    // PixelType specifies the interpretation of PixelInfo members
39    // CYMKPixel:
40    //   Cyan     = red
41    //   Magenta  = green
42    //   Yellow   = blue
43    //   Black(K) = black
44    // CYMKPixel:
45    //   Cyan     = red
46    //   Magenta  = green
47    //   Yellow   = blue
48    //   Black(K) = black
49    //   Alpha    = alpha
50    // RGBPixel:
51    //   Red      = red;
52    //   Green    = green;
53    //   Blue     = blue;
54    // RGBAPixel:
55    //   Red      = red;
56    //   Green    = green;
57    //   Blue     = blue;
58    //   Alpha    = alpha;
59    enum PixelType
60    {
61      CMYKPixel,
62      CMYKAPixel,
63      RGBPixel,
64      RGBAPixel
65    };
66
67    // Default constructor
68    Color(void);
69
70    // Construct Color using the specified RGB values
71    Color(const Quantum red_,const Quantum green_,const Quantum blue_);
72
73    // Construct Color using the specified RGBA values
74    Color(const Quantum red_,const Quantum green_,const Quantum blue_,
75      const Quantum alpha_);
76
77    // Construct Color using the specified CMYKA values
78    Color(const Quantum cyan_,const Quantum magenta_,const Quantum yellow_,
79      const Quantum black_,const Quantum alpha_);
80
81    // Construct Color using the specified color string
82    Color(const char *color_);
83
84    // Copy constructor
85    Color(const Color &color_);
86
87    // Construct color via ImageMagick PixelInfo
88    Color(const PixelInfo &color_);
89
90    // Constructor Color using the specified color string
91    Color(const std::string &color_);
92
93    // Destructor
94    virtual ~Color(void);
95
96    // Assignment operator
97    Color& operator=(const Color &color_);
98
99    // Set color via X11 color specification string
100    const Color& operator=(const char *color);
101
102    // Set color via ImageMagick PixelInfo
103    const Color& operator=(const PixelInfo &color_);
104
105    // Set color via color specification string
106    const Color& operator=(const std::string &color);
107
108    // Return ImageMagick PixelInfo
109    operator PixelInfo() const;
110
111    // Return color specification string
112    operator std::string() const;
113
114    // Returns true if the distance between the other color is less than the
115    // specified distance in a linear three(or four) % dimensional color space.
116    bool isFuzzyEquivalent(const Color &color_,const double fuzz_) const;
117
118    // Does object contain valid color?
119    void isValid(const bool valid_);
120    bool isValid(void) const;
121
122    // Returns pixel type of the color
123    Magick::Color::PixelType pixelType(void) const;
124
125    // Alpha level (range OpaqueAlpha=0 to TransparentAlpha=QuantumRange)
126    void quantumAlpha(const Quantum alpha_);
127    Quantum quantumAlpha(void) const;
128
129    // Black color (range 0 to QuantumRange)
130    void quantumBlack(const Quantum black_);
131    Quantum quantumBlack(void) const;
132
133    // Blue/Yellow color (range 0 to QuantumRange)
134    void quantumBlue(const Quantum blue_);
135    Quantum quantumBlue(void) const;
136
137    // Green/Magenta color (range 0 to QuantumRange)
138    void quantumGreen(const Quantum green_);
139    Quantum quantumGreen(void) const;
140
141    // Red/Cyan color (range 0 to QuantumRange)
142    void quantumRed(const Quantum red_);
143    Quantum quantumRed(void) const;
144
145  protected:
146
147    // Constructor to construct with PixelInfo*
148    // Used to point Color at a pixel in an image
149    Color(PixelInfo *rep_,PixelType pixelType_);
150
151    // Constructor to construct with PixelType
152    Color(PixelType pixelType_);
153
154    // Set pixel
155    // Used to point Color at a pixel in an image
156    void pixel(PixelInfo *rep_,PixelType pixelType_);
157
158    // Scale a value expressed as a double (0-1) to Quantum range (0-QuantumRange)
159    static Quantum scaleDoubleToQuantum(const double double_);
160
161    // Scale a value expressed as a Quantum (0-QuantumRange) to double range (0-1)
162    static double scaleQuantumToDouble(const Quantum quantum_);
163
164    // PixelInfo 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    //  alpha   = alpha (range OpaqueAlpha=0 to TransparentAlpha=QuantumRange)
169    //  index   = PseudoColor colormap index
170    PixelInfo *_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 PixelInfo representation
179    void initPixel();
180
181    // Sets the pixel type using the specified PixelInfo.
182    void setPixelType(const PixelInfo &color_);
183  };
184
185  class MagickPPExport ColorCMYK: public Color
186  {
187  public:
188
189    // Default constructor
190    ColorCMYK(void);
191
192    // Copy constructor
193    ColorCMYK(const Color &color_);
194
195    // Construct ColorCMYK using the specified CMYK values
196    ColorCMYK(const double cyan_,const double magenta_,const double yellow_,
197      const double black_);
198
199    // Construct ColorCMYK using the specified CMYKA values
200    ColorCMYK(const double cyan_,const double magenta_,const double yellow_,
201      const double black_,const double alpha_);
202
203    // Destructor
204    ~ColorCMYK(void);
205
206    // Assignment operator from base class
207    ColorCMYK& operator=(const Color& color_);
208
209    // Alpha level (range 0 to 1.0)
210    void alpha(const double alpha_);
211    double alpha(void) const;
212
213    // Black/Key color (range 0 to 1.0)
214    void black(const double black_);
215    double black(void) const;
216
217    // Black/Key color (range 0.0 to 1.0)
218    void cyan(const double cyan_);
219    double cyan(void) const;
220
221    // Magenta color (range 0 to 1.0)
222    void magenta(const double magenta_);
223    double magenta(void) const;
224
225    // Yellow color (range 0 to 1.0)
226    void yellow(const double yellow_);
227    double yellow(void) const;
228
229  protected:
230
231    // Constructor to construct with PixelInfo*
232    ColorCMYK(PixelInfo *rep_,PixelType pixelType_);
233  };
234
235  //
236  // Grayscale RGB color
237  //
238  // Grayscale is simply RGB with equal parts of red, green, and blue
239  // All double arguments have a valid range of 0.0 - 1.0.
240  class MagickPPExport ColorGray: public Color
241  {
242  public:
243
244    // Default constructor
245    ColorGray(void);
246
247    // Copy constructor
248    ColorGray(const Color &color_);
249
250    // Construct ColorGray using the specified shade
251    ColorGray(const double shade_);
252
253    // Destructor
254    ~ColorGray();
255
256    // Shade
257    void shade(const double shade_);
258    double shade(void) const;
259
260    // Assignment operator from base class
261    ColorGray& operator=(const Color& color_);
262
263  protected:
264
265    // Constructor to construct with PixelInfo*
266    ColorGray(PixelInfo *rep_,PixelType pixelType_);
267  };
268
269  //
270  // HSL Colorspace colors
271  //
272  // All double arguments have a valid range of 0.0 - 1.0.
273  class MagickPPExport ColorHSL: public Color
274  {
275  public:
276
277    // Default constructor
278    ColorHSL(void);
279
280    // Copy constructor
281    ColorHSL(const Color &color_);
282
283    // Construct ColorHSL using the specified HSL values
284    ColorHSL(const double hue_,const double saturation_,
285      const double lightness_);
286
287    // Destructor
288    ~ColorHSL();
289
290    // Assignment operator from base class
291    ColorHSL& operator=(const Color& color_);
292
293    // Hue color
294    void hue(const double hue_);
295    double hue(void) const;
296
297    // Lightness color
298    void lightness(const double lightness_);
299    double lightness(void) const;
300
301    // Saturation color
302    void saturation(const double saturation_);
303    double saturation(void) const;
304
305  protected:
306
307    // Constructor to construct with PixelInfo*
308    ColorHSL(PixelInfo *rep_,PixelType pixelType_);
309  };
310
311  //
312  // Monochrome color
313  //
314  // Color arguments are constrained to 'false' (black pixel) and 'true'
315  // (white pixel)
316  class MagickPPExport ColorMono: public Color
317  {
318  public:
319
320    // Default constructor
321    ColorMono(void);
322
323    // Construct ColorMono (false=black, true=white)
324    ColorMono(const bool mono_);
325
326    // Copy constructor
327    ColorMono(const Color &color_);
328
329    // Destructor
330    ~ColorMono();
331
332    // Assignment operator from base class
333    ColorMono& operator=(const Color& color_);
334
335    // Mono color
336    void mono(const bool mono_);
337    bool mono(void) const;
338
339  protected:
340
341    // Constructor to construct with PixelInfo*
342    ColorMono(PixelInfo* rep_,PixelType pixelType_);
343  };
344
345  class MagickPPExport ColorRGB: public Color
346  {
347  public:
348
349    // Default constructor
350    ColorRGB(void);
351
352    // Copy constructor
353    ColorRGB(const Color &color_);
354
355    // Construct ColorRGB using the specified RGB values
356    ColorRGB(const double red_,const double green_,const double blue_);
357
358    // Construct ColorRGB using the specified RGBA values
359    ColorRGB(const double red_,const double green_,const double blue_,
360      const double alpha_);
361
362    // Destructor
363    ~ColorRGB(void);
364
365    // Assignment operator from base class
366    ColorRGB& operator=(const Color& color_);
367
368    // Alpha level (range 0 to 1.0)
369    void alpha(const double alpha_);
370    double alpha(void) const;
371
372    // Blue color (range 0.0 to 1.0)
373    void blue(const double blue_);
374    double blue(void) const;
375
376    // Green color (range 0 to 1.0)
377    void green(const double green_);
378    double green(void) const;
379
380    // Red color (range 0 to 1.0)
381    void red(const double red_);
382    double red(void) const;
383
384  protected:
385
386    // Constructor to construct with PixelInfo*
387    ColorRGB(PixelInfo *rep_,PixelType pixelType_);
388  };
389
390  //
391  // YUV Colorspace color
392  //
393  // Argument ranges:
394  //        Y:  0.0 through 1.0
395  //        U: -0.5 through 0.5
396  //        V: -0.5 through 0.5
397  class MagickPPExport ColorYUV: public Color
398  {
399  public:
400
401    // Default constructor
402    ColorYUV(void);
403
404    // Copy constructor
405    ColorYUV(const Color &color_);
406
407    // Construct ColorYUV using the specified YUV values
408    ColorYUV(const double y_,const double u_,const double v_);
409
410    // Destructor
411    ~ColorYUV(void);
412
413    // Assignment operator from base class
414    ColorYUV& operator=(const Color& color_);
415
416    // Color U (0.0 through 1.0)
417    void u(const double u_);
418    double u(void) const;
419
420    // Color V (-0.5 through 0.5)
421    void v(const double v_);
422    double v(void) const;
423
424    // Color Y (-0.5 through 0.5)
425    void y(const double y_);
426    double y(void) const;
427
428  protected:
429
430    // Constructor to construct with PixelInfo*
431    ColorYUV(PixelInfo *rep_,PixelType pixelType_);
432
433  private:
434
435    void convert(const double y_,const double u_,const double v_);
436
437  };
438} // namespace Magick
439
440#endif // Magick_Color_header
441