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-2018 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 14 namespace 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 Magick::Quantum red_,const Magick::Quantum green_, 72 const Magick::Quantum blue_); 73 74 // Construct Color using the specified RGBA values 75 Color(const Magick::Quantum red_,const Magick::Quantum green_, 76 const Magick::Quantum blue_,const Magick::Quantum alpha_); 77 78 // Construct Color using the specified CMYKA values 79 Color(const Magick::Quantum cyan_,const Magick::Quantum magenta_, 80 const Magick::Quantum yellow_,const Magick::Quantum black_, 81 const Magick::Quantum alpha_); 82 83 // Construct Color using the specified color string 84 Color(const char *color_); 85 86 // Copy constructor 87 Color(const Color &color_); 88 89 // Construct color via ImageMagick PixelInfo 90 Color(const PixelInfo &color_); 91 92 // Constructor Color using the specified color string 93 Color(const std::string &color_); 94 95 // Destructor 96 virtual ~Color(void); 97 98 // Assignment operator 99 Color& operator=(const Color &color_); 100 101 // Set color via X11 color specification string 102 const Color& operator=(const char *color); 103 104 // Set color via ImageMagick PixelInfo 105 const Color& operator=(const PixelInfo &color_); 106 107 // Set color via color specification string 108 const Color& operator=(const std::string &color); 109 110 // Return ImageMagick PixelInfo 111 operator PixelInfo() const; 112 113 // Return color specification string 114 operator std::string() const; 115 116 // Returns true if the distance between the other color is less than the 117 // specified distance in a linear three(or four) % dimensional color space. 118 bool isFuzzyEquivalent(const Color &color_,const double fuzz_) const; 119 120 // Does object contain valid color? 121 void isValid(const bool valid_); 122 bool isValid(void) const; 123 124 // Returns pixel type of the color 125 Magick::Color::PixelType pixelType(void) const; 126 127 // Alpha level (range OpaqueAlpha=0 to TransparentAlpha=QuantumRange) 128 void quantumAlpha(const Quantum alpha_); 129 Quantum quantumAlpha(void) const; 130 131 // Black color (range 0 to QuantumRange) 132 void quantumBlack(const Quantum black_); 133 Quantum quantumBlack(void) const; 134 135 // Blue/Yellow color (range 0 to QuantumRange) 136 void quantumBlue(const Quantum blue_); 137 Quantum quantumBlue(void) const; 138 139 // Green/Magenta color (range 0 to QuantumRange) 140 void quantumGreen(const Quantum green_); 141 Quantum quantumGreen(void) const; 142 143 // Red/Cyan color (range 0 to QuantumRange) 144 void quantumRed(const Quantum red_); 145 Quantum quantumRed(void) const; 146 147 protected: 148 149 // Constructor to construct with PixelInfo* 150 // Used to point Color at a pixel in an image 151 Color(PixelInfo *rep_,PixelType pixelType_); 152 153 // Constructor to construct with PixelType 154 Color(PixelType pixelType_); 155 156 // Set pixel 157 // Used to point Color at a pixel in an image 158 void pixel(PixelInfo *rep_,PixelType pixelType_); 159 160 // Scale a value expressed as a double (0-1) to Quantum range (0-QuantumRange) 161 static Quantum scaleDoubleToQuantum(const double double_); 162 163 // Scale a value expressed as a Quantum (0-QuantumRange) to double range (0-1) 164 static double scaleQuantumToDouble(const Quantum quantum_); 165 166 // PixelInfo represents a color pixel: 167 // red = red (range 0 to QuantumRange) 168 // green = green (range 0 to QuantumRange) 169 // blue = blue (range 0 to QuantumRange) 170 // alpha = alpha (range OpaqueAlpha=0 to TransparentAlpha=QuantumRange) 171 // index = PseudoColor colormap index 172 PixelInfo *_pixel; 173 174 private: 175 176 bool _isValid; // Set true if pixel is "valid" 177 bool _pixelOwn; // Set true if we allocated pixel 178 PixelType _pixelType; // Color type supported by _pixel 179 180 // Common initializer for PixelInfo representation 181 void initPixel(); 182 183 void setAlpha(const Magick::Quantum alpha_); 184 185 // Sets the pixel type using the specified PixelInfo. 186 void setPixelType(const PixelInfo &color_); 187 }; 188 189 class MagickPPExport ColorCMYK: public Color 190 { 191 public: 192 193 // Default constructor 194 ColorCMYK(void); 195 196 // Copy constructor 197 ColorCMYK(const Color &color_); 198 199 // Construct ColorCMYK using the specified CMYK values 200 ColorCMYK(const double cyan_,const double magenta_,const double yellow_, 201 const double black_); 202 203 // Construct ColorCMYK using the specified CMYKA values 204 ColorCMYK(const double cyan_,const double magenta_,const double yellow_, 205 const double black_,const double alpha_); 206 207 // Destructor 208 ~ColorCMYK(void); 209 210 // Assignment operator from base class 211 ColorCMYK& operator=(const Color& color_); 212 213 // Alpha level (range 0 to 1.0) 214 void alpha(const double alpha_); 215 double alpha(void) const; 216 217 // Black/Key color (range 0 to 1.0) 218 void black(const double black_); 219 double black(void) const; 220 221 // Black/Key color (range 0.0 to 1.0) 222 void cyan(const double cyan_); 223 double cyan(void) const; 224 225 // Magenta color (range 0 to 1.0) 226 void magenta(const double magenta_); 227 double magenta(void) const; 228 229 // Yellow color (range 0 to 1.0) 230 void yellow(const double yellow_); 231 double yellow(void) const; 232 233 protected: 234 235 // Constructor to construct with PixelInfo* 236 ColorCMYK(PixelInfo *rep_,PixelType pixelType_); 237 }; 238 239 // 240 // Grayscale RGB color 241 // 242 // Grayscale is simply RGB with equal parts of red, green, and blue 243 // All double arguments have a valid range of 0.0 - 1.0. 244 class MagickPPExport ColorGray: public Color 245 { 246 public: 247 248 // Default constructor 249 ColorGray(void); 250 251 // Copy constructor 252 ColorGray(const Color &color_); 253 254 // Construct ColorGray using the specified shade 255 ColorGray(const double shade_); 256 257 // Destructor 258 ~ColorGray(); 259 260 // Shade 261 void shade(const double shade_); 262 double shade(void) const; 263 264 // Assignment operator from base class 265 ColorGray& operator=(const Color& color_); 266 267 protected: 268 269 // Constructor to construct with PixelInfo* 270 ColorGray(PixelInfo *rep_,PixelType pixelType_); 271 }; 272 273 // 274 // HSL Colorspace colors 275 // 276 // All double arguments have a valid range of 0.0 - 1.0. 277 class MagickPPExport ColorHSL: public Color 278 { 279 public: 280 281 // Default constructor 282 ColorHSL(void); 283 284 // Copy constructor 285 ColorHSL(const Color &color_); 286 287 // Construct ColorHSL using the specified HSL values 288 ColorHSL(const double hue_,const double saturation_, 289 const double lightness_); 290 291 // Destructor 292 ~ColorHSL(); 293 294 // Assignment operator from base class 295 ColorHSL& operator=(const Color& color_); 296 297 // Hue color 298 void hue(const double hue_); 299 double hue(void) const; 300 301 // Lightness color 302 void lightness(const double lightness_); 303 double lightness(void) const; 304 305 // Saturation color 306 void saturation(const double saturation_); 307 double saturation(void) const; 308 309 protected: 310 311 // Constructor to construct with PixelInfo* 312 ColorHSL(PixelInfo *rep_,PixelType pixelType_); 313 }; 314 315 // 316 // Monochrome color 317 // 318 // Color arguments are constrained to 'false' (black pixel) and 'true' 319 // (white pixel) 320 class MagickPPExport ColorMono: public Color 321 { 322 public: 323 324 // Default constructor 325 ColorMono(void); 326 327 // Construct ColorMono (false=black, true=white) 328 ColorMono(const bool mono_); 329 330 // Copy constructor 331 ColorMono(const Color &color_); 332 333 // Destructor 334 ~ColorMono(); 335 336 // Assignment operator from base class 337 ColorMono& operator=(const Color& color_); 338 339 // Mono color 340 void mono(const bool mono_); 341 bool mono(void) const; 342 343 protected: 344 345 // Constructor to construct with PixelInfo* 346 ColorMono(PixelInfo* rep_,PixelType pixelType_); 347 }; 348 349 class MagickPPExport ColorRGB: public Color 350 { 351 public: 352 353 // Default constructor 354 ColorRGB(void); 355 356 // Copy constructor 357 ColorRGB(const Color &color_); 358 359 // Construct ColorRGB using the specified RGB values 360 ColorRGB(const double red_,const double green_,const double blue_); 361 362 // Construct ColorRGB using the specified RGBA values 363 ColorRGB(const double red_,const double green_,const double blue_, 364 const double alpha_); 365 366 // Destructor 367 ~ColorRGB(void); 368 369 // Assignment operator from base class 370 ColorRGB& operator=(const Color& color_); 371 372 // Alpha level (range 0 to 1.0) 373 void alpha(const double alpha_); 374 double alpha(void) const; 375 376 // Blue color (range 0.0 to 1.0) 377 void blue(const double blue_); 378 double blue(void) const; 379 380 // Green color (range 0 to 1.0) 381 void green(const double green_); 382 double green(void) const; 383 384 // Red color (range 0 to 1.0) 385 void red(const double red_); 386 double red(void) const; 387 388 protected: 389 390 // Constructor to construct with PixelInfo* 391 ColorRGB(PixelInfo *rep_,PixelType pixelType_); 392 }; 393 394 // 395 // YUV Colorspace color 396 // 397 // Argument ranges: 398 // Y: 0.0 through 1.0 399 // U: -0.5 through 0.5 400 // V: -0.5 through 0.5 401 class MagickPPExport ColorYUV: public Color 402 { 403 public: 404 405 // Default constructor 406 ColorYUV(void); 407 408 // Copy constructor 409 ColorYUV(const Color &color_); 410 411 // Construct ColorYUV using the specified YUV values 412 ColorYUV(const double y_,const double u_,const double v_); 413 414 // Destructor 415 ~ColorYUV(void); 416 417 // Assignment operator from base class 418 ColorYUV& operator=(const Color& color_); 419 420 // Color U (0.0 through 1.0) 421 void u(const double u_); 422 double u(void) const; 423 424 // Color V (-0.5 through 0.5) 425 void v(const double v_); 426 double v(void) const; 427 428 // Color Y (-0.5 through 0.5) 429 void y(const double y_); 430 double y(void) const; 431 432 protected: 433 434 // Constructor to construct with PixelInfo* 435 ColorYUV(PixelInfo *rep_,PixelType pixelType_); 436 437 private: 438 439 void convert(const double y_,const double u_,const double v_); 440 441 }; 442 } // namespace Magick 443 444 #endif // Magick_Color_header 445