Magick++  6.9.3
Color.cpp
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
4 // Copyright Dirk Lemstra 2014-2018
5 //
6 // Color Implementation
7 //
8 
9 #define MAGICKCORE_IMPLEMENTATION
10 #define MAGICK_PLUSPLUS_IMPLEMENTATION 1
11 
12 #include "Magick++/Include.h"
13 #include <string>
14 
15 using namespace std;
16 
17 #include "Magick++/Color.h"
18 #include "Magick++/Exception.h"
19 
21  const Magick::Color &right_)
22 {
23 #if defined(MAGICKCORE_HDRI_SUPPORT)
24  return((left_.isValid() == right_.isValid()) &&
25  (fabs(left_.redQuantum()-right_.redQuantum()) < MagickEpsilon) &&
26  (fabs(left_.greenQuantum()-right_.greenQuantum()) < MagickEpsilon) &&
27  (fabs(left_.blueQuantum()-right_.blueQuantum()) < MagickEpsilon));
28 #else
29  return((left_.isValid() == right_.isValid()) &&
30  (left_.redQuantum() == right_.redQuantum()) &&
31  (left_.greenQuantum() == right_.greenQuantum()) &&
32  (left_.blueQuantum() == right_.blueQuantum()));
33 #endif
34 }
35 
37  const Magick::Color &right_)
38 {
39  return(!(left_ == right_));
40 }
41 
43  const Magick::Color &right_)
44 {
45  return(!(left_ < right_ ) && (left_ != right_ ));
46 }
47 
49  const Magick::Color &right_)
50 {
51  if(left_.redQuantum() < right_.redQuantum())
52  return(true);
53  if(left_.redQuantum() > right_.redQuantum())
54  return(false);
55  if(left_.greenQuantum() < right_.greenQuantum())
56  return(true);
57  if(left_.greenQuantum() > right_.greenQuantum())
58  return(false);
59  if(left_.blueQuantum() < right_.blueQuantum())
60  return(true);
61  return(false);
62 }
63 
65  const Magick::Color &right_)
66 {
67  return((left_ > right_) || (left_ == right_));
68 }
69 
71  const Magick::Color &right_)
72 {
73  return((left_ < right_) || (left_ == right_));
74 }
75 
77  : _pixel(new PixelPacket),
78  _isValid(false),
79  _pixelOwn(true),
80  _pixelType(RGBAPixel)
81 {
82  initPixel();
83 
84  _pixel->opacity=TransparentOpacity;
85 }
86 
87 Magick::Color::Color(Magick::Quantum red_,Magick::Quantum green_,
88  Magick::Quantum blue_)
89  : _pixel(new PixelPacket),
90  _isValid(true),
91  _pixelOwn(true),
92  _pixelType(RGBPixel)
93 {
94  redQuantum(red_);
95  greenQuantum(green_);
96  blueQuantum(blue_);
97  alphaQuantum(OpaqueOpacity);
98 }
99 
100 Magick::Color::Color(Magick::Quantum red_,Magick::Quantum green_,
101  Magick::Quantum blue_,Magick::Quantum alpha_)
102  : _pixel(new PixelPacket),
103  _isValid(true),
104  _pixelOwn(true),
105  _pixelType(RGBAPixel)
106 {
107  redQuantum(red_);
108  greenQuantum(green_);
109  blueQuantum(blue_);
110  alphaQuantum(alpha_);
111 }
112 
113 Magick::Color::Color(const char *x11color_)
114  : _pixel(new PixelPacket),
115  _isValid(true),
116  _pixelOwn(true),
117  _pixelType(RGBPixel)
118 {
119  initPixel();
120 
121  // Use operator = implementation
122  *this=x11color_;
123 }
124 
126  : _pixel(new PixelPacket),
127  _isValid(color_._isValid),
128  _pixelOwn(true),
129  _pixelType(color_._pixelType)
130 {
131  *_pixel=*color_._pixel;
132 }
133 
134 Magick::Color::Color(const PixelPacket &color_)
135  : _pixel(new PixelPacket),
136  _isValid(true),
137  _pixelOwn(true),
138  _pixelType(RGBPixel)
139 {
140  *_pixel=color_;
141 
142  if (color_.opacity != OpaqueOpacity)
143  _pixelType=RGBAPixel;
144 }
145 
146 Magick::Color::Color(const std::string &x11color_)
147  : _pixel(new PixelPacket),
148  _isValid(true),
149  _pixelOwn(true),
150  _pixelType(RGBPixel)
151 {
152  initPixel();
153 
154  // Use operator = implementation
155  *this=x11color_;
156 }
157 
159 {
160  if (_pixelOwn)
161  delete _pixel;
162 
163  _pixel=(PixelPacket *)NULL;
164 }
165 
166 const Magick::Color& Magick::Color::operator=(const char *x11color_)
167 {
168  *this=std::string(x11color_);
169  return(*this);
170 }
171 
173 {
174  // If not being set to ourself
175  if (this != &color_)
176  {
177  // Copy pixel value
178  *_pixel=*color_._pixel;
179 
180  // Validity
181  _isValid=color_._isValid;
182 
183  // Copy pixel type
184  _pixelType=color_._pixelType;
185  }
186  return(*this);
187 }
188 
189 const Magick::Color& Magick::Color::operator=
190  (const MagickCore::PixelPacket &color_)
191 {
192  *_pixel=color_;
193  if (color_.opacity != OpaqueOpacity)
194  _pixelType=RGBAPixel;
195  else
196  _pixelType=RGBPixel;
197 
198  return(*this);
199 }
200 
201 // Set color via X11 color specification string
202 const Magick::Color& Magick::Color::operator=(const std::string &x11color_)
203 {
204  PixelPacket
205  target_color;
206 
207  initPixel();
209  if (QueryColorDatabase(x11color_.c_str(),&target_color,exceptionInfo))
210  {
211  redQuantum( target_color.red );
212  greenQuantum( target_color.green );
213  blueQuantum( target_color.blue );
214  alphaQuantum( target_color.opacity );
215 
216  if (target_color.opacity > OpaqueOpacity)
217  _pixelType=RGBAPixel;
218  else
219  _pixelType=RGBPixel;
220  }
221  else
222  _isValid=false;
223  ThrowPPException(false);
224 
225  return(*this);
226 }
227 
228 Magick::Color::operator std::string() const
229 {
230  char
231  colorbuf[MaxTextExtent];
232 
233  MagickPixelPacket
234  pixel;
235 
236  if (!isValid())
237  return std::string("none");
238 
239  pixel.colorspace=RGBColorspace;
240  pixel.matte=_pixelType == RGBAPixel ? MagickTrue : MagickFalse;
241  pixel.depth=MAGICKCORE_QUANTUM_DEPTH;
242  pixel.red=_pixel->red;
243  pixel.green=_pixel->green;
244  pixel.blue=_pixel->blue;
245  pixel.opacity=_pixel->opacity;
246  GetColorTuple(&pixel,MagickTrue,colorbuf);
247 
248  return(std::string(colorbuf));
249 }
250 
251 bool Magick::Color::isValid(void) const
252 {
253  return(_isValid);
254 }
255 
256 void Magick::Color::isValid(bool valid_)
257 {
258  if (bool(valid_) == bool(isValid()))
259  return;
260 
261  if (!_pixelOwn)
262  {
263  _pixel=new PixelPacket;
264  _pixelOwn=true;
265  }
266 
267  _isValid=valid_;
268 
269  initPixel();
270 }
271 
272 Magick::Color::Color(PixelPacket *rep_,PixelType pixelType_)
273  : _pixel(rep_),
274  _isValid(true),
275  _pixelOwn(false),
276  _pixelType(pixelType_)
277 {
278 }
279 
280 void Magick::Color::pixel(PixelPacket *rep_,PixelType pixelType_)
281 {
282  if (_pixelOwn)
283  delete _pixel;
284 
285  _pixel=rep_;
286  _pixelOwn=false;
287  _isValid=true;
288  _pixelType=pixelType_;
289 }
290 
292  : Color()
293 {
294 }
295 
297  : Color(color_)
298 {
299 }
300 
302  : Color(scaleDoubleToQuantum(shade_),scaleDoubleToQuantum(shade_),
303  scaleDoubleToQuantum(shade_))
304 {
305  alphaQuantum(OpaqueOpacity);
306 }
307 
309 {
310 }
311 
312 void Magick::ColorGray::shade(double shade_)
313 {
314  Quantum gray=scaleDoubleToQuantum(shade_);
315  redQuantum(gray);
316  greenQuantum(gray);
317  blueQuantum(gray);
318 }
319 
320 double Magick::ColorGray::shade(void) const
321 {
322  return(scaleQuantumToDouble(greenQuantum()));
323 }
324 
326 {
327  *static_cast<Magick::Color*>(this)=color_;
328  return(*this);
329 }
330 
332  : Color()
333 {
334 }
335 
337  : Color( color_ )
338 {
339 }
340 
341 Magick::ColorHSL::ColorHSL(double hue_,double saturation_,double luminosity_)
342  : Color()
343 {
344  Quantum
345  blue,
346  green,
347  red;
348 
349  ConvertHSLToRGB(hue_,saturation_,luminosity_,&red,&green,&blue);
350 
351  redQuantum(red);
352  greenQuantum(green);
353  blueQuantum(blue);
354  alphaQuantum(OpaqueOpacity);
355 }
356 
358 {
359 }
360 
362 {
363  *static_cast<Magick::Color*>(this)=color_;
364  return (*this);
365 }
366 
367 void Magick::ColorHSL::hue(double hue_)
368 {
369  double
370  hue,
371  luminosity,
372  saturation;
373 
374  Quantum
375  blue,
376  green,
377  red;
378 
379  ConvertRGBToHSL(redQuantum(),greenQuantum(),blueQuantum(),&hue,&saturation,
380  &luminosity);
381 
382  hue=hue_;
383 
384  ConvertHSLToRGB(hue,saturation,luminosity,&red,&green,&blue);
385 
386  redQuantum(red);
387  greenQuantum(green);
388  blueQuantum(blue);
389 }
390 
391 double Magick::ColorHSL::hue(void) const
392 {
393  double
394  hue,
395  luminosity,
396  saturation;
397 
398  ConvertRGBToHSL(redQuantum(),greenQuantum(),blueQuantum(),&hue,&saturation,
399  &luminosity);
400 
401  return(hue);
402 }
403 
404 void Magick::ColorHSL::luminosity(double luminosity_)
405 {
406  double
407  hue,
408  luminosity,
409  saturation;
410 
411  Quantum
412  blue,
413  green,
414  red;
415 
416  ConvertRGBToHSL(redQuantum(),greenQuantum(),blueQuantum(),&hue,&saturation,
417  &luminosity);
418 
419  luminosity=luminosity_;
420 
421  ConvertHSLToRGB(hue,saturation,luminosity,&red,&green,&blue);
422 
423  redQuantum(red);
424  greenQuantum(green);
425  blueQuantum(blue);
426 }
427 
429 {
430  double
431  hue,
432  saturation,
433  luminosity;
434 
435  ConvertRGBToHSL(redQuantum(),greenQuantum(),blueQuantum(),&hue,&saturation,
436  &luminosity);
437 
438  return(luminosity);
439 }
440 
441 void Magick::ColorHSL::saturation(double saturation_)
442 {
443  double
444  hue,
445  luminosity,
446  saturation;
447 
448  Quantum
449  blue,
450  green,
451  red;
452 
453  ConvertRGBToHSL(redQuantum(),greenQuantum(),blueQuantum(),&hue,&saturation,
454  &luminosity);
455 
456  saturation=saturation_;
457 
458  ConvertHSLToRGB(hue,saturation,luminosity,&red,&green,&blue);
459 
460  redQuantum(red);
461  greenQuantum(green);
462  blueQuantum(blue);
463 }
464 
466 {
467  double
468  hue,
469  luminosity,
470  saturation;
471 
472  ConvertRGBToHSL(redQuantum(),greenQuantum(),blueQuantum(),&hue,&saturation,
473  &luminosity);
474 
475  return(saturation);
476 }
477 
479  : Color()
480 {
481 }
482 
484  : Color((mono_ ? QuantumRange : 0),
485  (mono_ ? QuantumRange : 0),
486  (mono_ ? QuantumRange : 0))
487 {
488  alphaQuantum(OpaqueOpacity);
489 }
490 
492  : Color(color_)
493 {
494 }
495 
497 {
498 }
499 
501 {
502  *static_cast<Magick::Color*>(this)=color_;
503  return(*this);
504 }
505 
506 void Magick::ColorMono::mono(bool mono_)
507 {
508  redQuantum(mono_ ? QuantumRange : 0);
509  greenQuantum(mono_ ? QuantumRange : 0);
510  blueQuantum(mono_ ? QuantumRange : 0);
511 }
512 
513 bool Magick::ColorMono::mono(void) const
514 {
515  return(greenQuantum() == 0);
516 }
517 
519  : Color()
520 {
521 }
522 
524  : Color(color_)
525 {
526 }
527 
528 Magick::ColorRGB::ColorRGB(double red_,double green_,double blue_)
529  : Color(scaleDoubleToQuantum(red_),scaleDoubleToQuantum(green_),
530  scaleDoubleToQuantum(blue_))
531 {
532  alphaQuantum(OpaqueOpacity);
533 }
534 
536 {
537 }
538 
540 {
541  *static_cast<Magick::Color*>(this)=color_;
542  return(*this);
543 }
544 
546  : Color()
547 {
548 }
549 
551  : Color(color_)
552 {
553 }
554 
555 Magick::ColorYUV::ColorYUV(double y_,double u_,double v_)
556  : Color(scaleDoubleToQuantum(y_ + 1.13980 * v_),
557  scaleDoubleToQuantum(y_ - (0.39380 * u_) - (0.58050 * v_)),
558  scaleDoubleToQuantum(y_ + 2.02790 * u_))
559 {
560  alphaQuantum(OpaqueOpacity);
561 }
562 
564 {
565 }
566 
568 {
569  *static_cast<Magick::Color*>(this)=color_;
570  return(*this);
571 }
572 
573 void Magick::ColorYUV::u(double u_)
574 {
575  double V = v();
576  double Y = y();
577 
578  redQuantum(scaleDoubleToQuantum(Y + 1.13980 * V ));
579  greenQuantum(scaleDoubleToQuantum( Y - (0.39380 * u_) - (0.58050 * V)));
580  blueQuantum(scaleDoubleToQuantum( Y + 2.02790 * u_));
581 }
582 
583 double Magick::ColorYUV::u(void) const
584 {
585  return scaleQuantumToDouble((-0.14740 * redQuantum()) - (0.28950 *
586  greenQuantum()) + (0.43690 * blueQuantum()));
587 }
588 
589 void Magick::ColorYUV::v(double v_)
590 {
591  double U = u();
592  double Y = y();
593 
594  redQuantum(scaleDoubleToQuantum( Y + 1.13980 * v_ ));
595  greenQuantum(scaleDoubleToQuantum( Y - (0.39380 * U) - (0.58050 * v_) ));
596  blueQuantum(scaleDoubleToQuantum( Y + 2.02790 * U ));
597 }
598 
599 double Magick::ColorYUV::v(void) const
600 {
601  return scaleQuantumToDouble((0.61500 * redQuantum()) - (0.51500 *
602  greenQuantum()) - (0.10000 * blueQuantum()));
603 }
604 
605 void Magick::ColorYUV::y(double y_)
606 {
607  double U = u();
608  double V = v();
609 
610  redQuantum(scaleDoubleToQuantum(y_ + 1.13980 * V));
611  greenQuantum(scaleDoubleToQuantum(y_ - (0.39380 * U) - (0.58050 * V)));
612  blueQuantum(scaleDoubleToQuantum(y_ + 2.02790 * U));
613 }
614 
615 double Magick::ColorYUV::y(void) const
616 {
617  return scaleQuantumToDouble((0.29900 * redQuantum()) + (0.58700 *
618  greenQuantum()) + (0.11400 * blueQuantum()));
619 }
ColorYUV & operator=(const Color &color_)
Definition: Color.cpp:567
MagickPPExport int operator!=(const Magick::Color &left_, const Magick::Color &right_)
Definition: Color.cpp:36
void isValid(bool valid_)
Definition: Color.cpp:256
Quantum greenQuantum(void) const
Definition: Color.h:419
Quantum alphaQuantum(void) const
Definition: Color.h:397
~ColorRGB(void)
Definition: Color.cpp:535
ColorYUV(void)
Definition: Color.cpp:545
void pixel(PixelPacket *rep_, PixelType pixelType_)
Definition: Color.cpp:280
void redQuantum(Quantum red_)
Definition: Color.h:424
MagickPPExport int operator<(const Magick::Color &left_, const Magick::Color &right_)
Definition: Color.cpp:48
double hue(void) const
Definition: Color.cpp:391
bool isValid(void) const
Definition: Color.cpp:251
Quantum blueQuantum(void) const
Definition: Color.h:408
ColorGray & operator=(const Color &color_)
Definition: Color.cpp:325
MagickPPExport int operator<=(const Magick::Color &left_, const Magick::Color &right_)
Definition: Color.cpp:70
ColorRGB(void)
Definition: Color.cpp:518
MagickPPExport int operator>=(const Magick::Color &left_, const Magick::Color &right_)
Definition: Color.cpp:64
double u(void) const
Definition: Color.cpp:583
#define MagickPPExport
Definition: Include.h:261
PixelPacket * _pixel
Definition: Color.h:170
ColorRGB & operator=(const Color &color_)
Definition: Color.cpp:539
bool mono(void) const
Definition: Color.cpp:513
~ColorYUV(void)
Definition: Color.cpp:563
#define ThrowPPException(quiet)
Definition: Include.h:1524
ColorHSL(void)
Definition: Color.cpp:331
virtual ~Color(void)
Definition: Color.cpp:158
double v(void) const
Definition: Color.cpp:599
double shade(void) const
Definition: Color.cpp:320
void blueQuantum(Quantum blue_)
Definition: Color.h:402
void greenQuantum(Quantum green_)
Definition: Color.h:413
MagickPPExport int operator>(const Magick::Color &left_, const Magick::Color &right_)
Definition: Color.cpp:42
MagickPPExport int operator==(const Magick::Color &left_, const Magick::Color &right_)
Definition: Color.cpp:20
Color & operator=(const Color &color_)
Definition: Color.cpp:172
ColorMono & operator=(const Color &color_)
Definition: Color.cpp:500
double y(void) const
Definition: Color.cpp:615
Quantum redQuantum(void) const
Definition: Color.h:430
#define GetPPException
Definition: Include.h:1520
double saturation(void) const
Definition: Color.cpp:465
Color(void)
Definition: Color.cpp:76
double luminosity(void) const
Definition: Color.cpp:428
ColorHSL & operator=(const Color &color_)
Definition: Color.cpp:361