1 /*************************************************************************** 2 qgscoloreffect.h 3 ---------------- 4 begin : March 2015 5 copyright : (C) 2015 Nyall Dawson 6 email : nyall dot dawson at gmail dot com 7 ***************************************************************************/ 8 9 /*************************************************************************** 10 * * 11 * This program is free software; you can redistribute it and/or modify * 12 * it under the terms of the GNU General Public License as published by * 13 * the Free Software Foundation; either version 2 of the License, or * 14 * (at your option) any later version. * 15 * * 16 ***************************************************************************/ 17 #ifndef QGSCOLOREFFECT_H 18 #define QGSCOLOREFFECT_H 19 20 #include "qgis_core.h" 21 #include "qgspainteffect.h" 22 #include "qgsimageoperation.h" 23 #include "qgis_sip.h" 24 #include <QPainter> 25 26 /** 27 * \ingroup core 28 * \class QgsColorEffect 29 * \brief A paint effect which alters the colors (e.g., brightness, contrast) in a 30 * source picture. 31 * 32 * \since QGIS 2.9 33 */ 34 35 class CORE_EXPORT QgsColorEffect : public QgsPaintEffect SIP_NODEFAULTCTORS 36 { 37 38 public: 39 40 /** 41 * Creates a new QgsColorEffect effect from a properties string map. 42 * \param map encoded properties string map 43 * \returns new QgsColorEffect 44 */ 45 static QgsPaintEffect *create( const QVariantMap &map ) SIP_FACTORY; 46 47 QgsColorEffect(); 48 type()49 QString type() const override { return QStringLiteral( "color" ); } 50 QVariantMap properties() const override; 51 void readProperties( const QVariantMap &props ) override; 52 QgsColorEffect *clone() const override SIP_FACTORY; 53 54 /** 55 * Sets the brightness modification for the effect. 56 * \param brightness Valid values are between -255 and 255, where 0 represents 57 * no change, negative values indicate darkening and positive values indicate 58 * lightening 59 * \see setBrightness 60 */ 61 void setBrightness( int brightness ); 62 63 /** 64 * Returns the brightness modification for the effect. 65 * \returns brightness value. Values are between -255 and 255, where 0 represents 66 * no change, negative values indicate darkening and positive values indicate 67 * lightening 68 * \see setBrightness 69 */ brightness()70 int brightness() const { return mBrightness; } 71 72 /** 73 * Sets the contrast modification for the effect. 74 * \param contrast Valid values are between -100 and 100, where 0 represents 75 * no change, negative values indicate less contrast and positive values indicate 76 * greater contrast 77 * \see setContrast 78 */ 79 void setContrast( int contrast ); 80 81 /** 82 * Returns the contrast modification for the effect. 83 * \returns contrast value. Values are between -100 and 100, where 0 represents 84 * no change, negative values indicate less contrast and positive values indicate 85 * greater contrast 86 * \see setContrast 87 */ contrast()88 int contrast() const { return mContrast; } 89 90 /** 91 * Sets the saturation modification for the effect. 92 * \param saturation Valid values are between 0 and 2.0, where 1.0 represents 93 * no change, 0.0 represents totally desaturated (grayscale), and positive values indicate 94 * greater saturation 95 * \see saturation 96 */ setSaturation(double saturation)97 void setSaturation( double saturation ) { mSaturation = saturation; } 98 99 /** 100 * Returns the saturation modification for the effect. 101 * \returns saturation value. Values are between 0 and 2.0, where 1.0 represents 102 * no change, 0.0 represents totally desaturated (grayscale), and positive values indicate 103 * greater saturation 104 * \see setSaturation 105 */ saturation()106 double saturation() const { return mSaturation; } 107 108 /** 109 * Sets whether the effect should convert a picture to grayscale. 110 * \param grayscaleMode method for grayscale conversion 111 * \see grayscaleMode 112 */ setGrayscaleMode(QgsImageOperation::GrayscaleMode grayscaleMode)113 void setGrayscaleMode( QgsImageOperation::GrayscaleMode grayscaleMode ) { mGrayscaleMode = grayscaleMode; } 114 115 /** 116 * Returns whether the effect will convert a picture to grayscale. 117 * \returns method for grayscale conversion 118 * \see setGrayscaleMode 119 */ grayscaleMode()120 QgsImageOperation::GrayscaleMode grayscaleMode() const { return mGrayscaleMode; } 121 122 /** 123 * Sets whether the effect should colorize a picture. 124 * \param colorizeOn set to TRUE to enable colorization 125 * \see colorizeOn 126 * \see setColorizeColor 127 * \see setColorizeStrength 128 */ setColorizeOn(bool colorizeOn)129 void setColorizeOn( bool colorizeOn ) { mColorizeOn = colorizeOn; } 130 131 /** 132 * Returns whether the effect will colorize a picture. 133 * \returns TRUE if colorization is enabled 134 * \see setColorizeOn 135 * \see colorizeColor 136 * \see colorizeStrength 137 */ colorizeOn()138 bool colorizeOn() const { return mColorizeOn; } 139 140 /** 141 * Sets the color used for colorizing a picture. This is only used if 142 * setColorizeOn() is set to TRUE. 143 * \param colorizeColor colorization color 144 * \see colorizeColor 145 * \see setColorizeOn 146 * \see setColorizeStrength 147 */ 148 void setColorizeColor( const QColor &colorizeColor ); 149 150 /** 151 * Returns the color used for colorizing a picture. This is only used if 152 * colorizeOn() is set to TRUE. 153 * \returns colorization color 154 * \see setColorizeColor 155 * \see colorizeOn 156 * \see colorizeStrength 157 */ colorizeColor()158 QColor colorizeColor() const { return mColorizeColor; } 159 160 /** 161 * Sets the strength for colorizing a picture. This is only used if 162 * setColorizeOn() is set to TRUE. 163 * \param colorizeStrength colorization strength, between 0 and 100 164 * \see colorizeStrength 165 * \see setColorizeOn 166 * \see setColorizeColor 167 */ setColorizeStrength(int colorizeStrength)168 void setColorizeStrength( int colorizeStrength ) { mColorizeStrength = colorizeStrength; } 169 170 /** 171 * Returns the strength used for colorizing a picture. This is only used if 172 * setColorizeOn() is set to TRUE. 173 * \returns colorization strength, between 0 and 100 174 * \see setColorizeStrength 175 * \see colorizeOn 176 * \see colorizeColor 177 */ colorizeStrength()178 int colorizeStrength() const { return mColorizeStrength; } 179 180 /** 181 * Sets the \a opacity for the effect. 182 * \param opacity double between 0 and 1 inclusive, where 0 is fully transparent 183 * and 1 is fully opaque 184 * \see opacity() 185 */ setOpacity(const double opacity)186 void setOpacity( const double opacity ) { mOpacity = opacity; } 187 188 /** 189 * Returns the opacity for the effect. 190 * \returns opacity value between 0 and 1 inclusive, where 0 is fully transparent 191 * and 1 is fully opaque. 192 * \see setOpacity() 193 */ opacity()194 double opacity() const { return mOpacity; } 195 196 /** 197 * Sets the blend mode for the effect 198 * \param mode blend mode used for drawing the effect on to a destination 199 * paint device 200 * \see blendMode 201 */ setBlendMode(const QPainter::CompositionMode mode)202 void setBlendMode( const QPainter::CompositionMode mode ) { mBlendMode = mode; } 203 204 /** 205 * Returns the blend mode for the effect 206 * \returns blend mode used for drawing the effect on to a destination 207 * paint device 208 * \see setBlendMode 209 */ blendMode()210 QPainter::CompositionMode blendMode() const { return mBlendMode; } 211 212 protected: 213 214 void draw( QgsRenderContext &context ) override; 215 216 private: 217 218 double mOpacity = 1.0; 219 QPainter::CompositionMode mBlendMode = QPainter::CompositionMode_SourceOver; 220 int mBrightness = 0; 221 int mContrast = 0; 222 double mSaturation = 1.0; 223 QgsImageOperation::GrayscaleMode mGrayscaleMode = QgsImageOperation::GrayscaleOff; 224 bool mColorizeOn = false; 225 QColor mColorizeColor; 226 int mColorizeStrength = 100; 227 }; 228 229 #endif // QGSBLUREFFECT_H 230 231