00001
00002 #include "qgsvectorcolorrampv2.h"
00003
00004 #include "qgssymbollayerv2utils.h"
00005
00006 #include <stdlib.h>
00007
00008 QgsVectorGradientColorRampV2::QgsVectorGradientColorRampV2( QColor color1, QColor color2 )
00009 : mColor1( color1 ), mColor2( color2 )
00010 {
00011 }
00012
00013 QgsVectorColorRampV2* QgsVectorGradientColorRampV2::create( const QgsStringMap& props )
00014 {
00015 QColor color1 = DEFAULT_GRADIENT_COLOR1;
00016 QColor color2 = DEFAULT_GRADIENT_COLOR2;
00017 if ( props.contains( "color1" ) )
00018 color1 = QgsSymbolLayerV2Utils::decodeColor( props["color1"] );
00019 if ( props.contains( "color2" ) )
00020 color2 = QgsSymbolLayerV2Utils::decodeColor( props["color2"] );
00021
00022 StopsMap stops;
00023 if ( props.contains( "stops" ) )
00024 {
00025 foreach( QString stop, props["stops"].split( ':' ) )
00026 {
00027 int i = stop.indexOf( ';' );
00028 if ( i == -1 ) continue;
00029
00030 QColor c = QgsSymbolLayerV2Utils::decodeColor( stop.mid( i + 1 ) );
00031 stops.insert( stop.left( i ).toDouble(), c );
00032 }
00033 }
00034
00035 QgsVectorGradientColorRampV2* r = new QgsVectorGradientColorRampV2( color1, color2 );
00036 r->setStops( stops );
00037 return r;
00038 }
00039
00040 static QColor _interpolate( QColor c1, QColor c2, double value )
00041 {
00042 int r = ( int )( c1.red() + value * ( c2.red() - c1.red() ) );
00043 int g = ( int )( c1.green() + value * ( c2.green() - c1.green() ) );
00044 int b = ( int )( c1.blue() + value * ( c2.blue() - c1.blue() ) );
00045
00046 return QColor::fromRgb( r, g, b );
00047 }
00048
00049 QColor QgsVectorGradientColorRampV2::color( double value ) const
00050 {
00051 if ( mStops.isEmpty() )
00052 {
00053 return _interpolate( mColor1, mColor2, value );
00054 }
00055 else
00056 {
00057 double lower = 0, upper;
00058 QColor c1 = mColor1, c2;
00059 for ( StopsMap::const_iterator it = mStops.begin(); it != mStops.end(); ++it )
00060 {
00061 if ( it.key() >= value )
00062 {
00063 upper = it.key();
00064 c2 = it.value();
00065
00066 return upper == lower ? c1 : _interpolate( c1, c2, ( value - lower ) / ( upper - lower ) );
00067 }
00068 lower = it.key();
00069 c1 = it.value();
00070 }
00071
00072 upper = 1;
00073 c2 = mColor2;
00074 return upper == lower ? c1 : _interpolate( c1, c2, ( value - lower ) / ( upper - lower ) );
00075 }
00076 }
00077
00078 QgsVectorColorRampV2* QgsVectorGradientColorRampV2::clone() const
00079 {
00080 QgsVectorGradientColorRampV2* r = new QgsVectorGradientColorRampV2( mColor1, mColor2 );
00081 r->setStops( mStops );
00082 return r;
00083 }
00084
00085 QgsStringMap QgsVectorGradientColorRampV2::properties() const
00086 {
00087 QgsStringMap map;
00088 map["color1"] = QgsSymbolLayerV2Utils::encodeColor( mColor1 );
00089 map["color2"] = QgsSymbolLayerV2Utils::encodeColor( mColor2 );
00090 if ( !mStops.isEmpty() )
00091 {
00092 QStringList lst;
00093 for ( StopsMap::const_iterator it = mStops.begin(); it != mStops.end(); ++it )
00094 {
00095 lst.append( QString( "%1;%2" ).arg( it.key() ).arg( QgsSymbolLayerV2Utils::encodeColor( it.value() ) ) );
00096 }
00097 map["stops"] = lst.join( ":" );
00098 }
00099 return map;
00100 }
00101
00103
00104
00105 QgsVectorRandomColorRampV2::QgsVectorRandomColorRampV2( int count, int hueMin, int hueMax,
00106 int satMin, int satMax, int valMin, int valMax )
00107 : mCount( count ), mHueMin( hueMin ), mHueMax( hueMax ),
00108 mSatMin( satMin ), mSatMax( satMax ), mValMin( valMin ), mValMax( valMax )
00109 {
00110 updateColors();
00111 }
00112
00113 QgsVectorColorRampV2* QgsVectorRandomColorRampV2::create( const QgsStringMap& props )
00114 {
00115 int count = DEFAULT_RANDOM_COUNT;
00116 int hueMin = DEFAULT_RANDOM_HUE_MIN, hueMax = DEFAULT_RANDOM_HUE_MAX;
00117 int satMin = DEFAULT_RANDOM_SAT_MIN, satMax = DEFAULT_RANDOM_SAT_MAX;
00118 int valMin = DEFAULT_RANDOM_VAL_MIN, valMax = DEFAULT_RANDOM_VAL_MAX;
00119
00120 if ( props.contains( "count" ) ) count = props["count"].toInt();
00121 if ( props.contains( "hueMin" ) ) hueMin = props["hueMin"].toInt();
00122 if ( props.contains( "hueMax" ) ) hueMax = props["hueMax"].toInt();
00123 if ( props.contains( "satMin" ) ) satMin = props["satMin"].toInt();
00124 if ( props.contains( "satMax" ) ) satMax = props["satMax"].toInt();
00125 if ( props.contains( "valMin" ) ) valMin = props["valMin"].toInt();
00126 if ( props.contains( "valMax" ) ) valMax = props["valMax"].toInt();
00127
00128 return new QgsVectorRandomColorRampV2( count, hueMin, hueMax, satMin, satMax, valMin, valMax );
00129 }
00130
00131 QColor QgsVectorRandomColorRampV2::color( double value ) const
00132 {
00133 int colorCnt = mColors.count();
00134 int colorIdx = ( int )( value * colorCnt );
00135
00136 if ( colorIdx >= 0 && colorIdx < colorCnt )
00137 return mColors.at( colorIdx );
00138
00139 return QColor();
00140 }
00141
00142 QgsVectorColorRampV2* QgsVectorRandomColorRampV2::clone() const
00143 {
00144 return new QgsVectorRandomColorRampV2( mCount, mHueMin, mHueMax, mSatMin, mSatMax, mValMin, mValMax );
00145 }
00146
00147 QgsStringMap QgsVectorRandomColorRampV2::properties() const
00148 {
00149 QgsStringMap map;
00150 map["count"] = QString::number( mCount );
00151 map["hueMin"] = QString::number( mHueMin );
00152 map["hueMax"] = QString::number( mHueMax );
00153 map["satMin"] = QString::number( mSatMin );
00154 map["satMax"] = QString::number( mSatMax );
00155 map["valMin"] = QString::number( mValMin );
00156 map["valMax"] = QString::number( mValMax );
00157 return map;
00158 }
00159
00160 void QgsVectorRandomColorRampV2::updateColors()
00161 {
00162 int h, s, v;
00163
00164 mColors.clear();
00165 for ( int i = 0; i < mCount; i++ )
00166 {
00167 h = ( rand() % ( mHueMax - mHueMin + 1 ) ) + mHueMin;
00168 s = ( rand() % ( mSatMax - mSatMin + 1 ) ) + mSatMin;
00169 v = ( rand() % ( mValMax - mValMin + 1 ) ) + mValMin;
00170 mColors.append( QColor::fromHsv( h, s, v ) );
00171 }
00172 }
00173
00174
00176
00177 QgsVectorColorBrewerColorRampV2::QgsVectorColorBrewerColorRampV2( QString schemeName, int colors )
00178 : mSchemeName( schemeName ), mColors( colors )
00179 {
00180 loadPalette();
00181 }
00182
00183 QgsVectorColorRampV2* QgsVectorColorBrewerColorRampV2::create( const QgsStringMap& props )
00184 {
00185 QString schemeName = DEFAULT_COLORBREWER_SCHEMENAME;
00186 int colors = DEFAULT_COLORBREWER_COLORS;
00187
00188 if ( props.contains( "schemeName" ) )
00189 schemeName = props["schemeName"];
00190 if ( props.contains( "colors" ) )
00191 colors = props["colors"].toInt();
00192
00193 return new QgsVectorColorBrewerColorRampV2( schemeName, colors );
00194 }
00195
00196 #include "qgscolorbrewerpalette.h"
00197
00198 void QgsVectorColorBrewerColorRampV2::loadPalette()
00199 {
00200 mPalette = QgsColorBrewerPalette::listSchemeColors( mSchemeName, mColors );
00201 }
00202
00203 QStringList QgsVectorColorBrewerColorRampV2::listSchemeNames()
00204 {
00205 return QgsColorBrewerPalette::listSchemes();
00206 }
00207
00208 QList<int> QgsVectorColorBrewerColorRampV2::listSchemeVariants( QString schemeName )
00209 {
00210 return QgsColorBrewerPalette::listSchemeVariants( schemeName );
00211 }
00212
00213 QColor QgsVectorColorBrewerColorRampV2::color( double value ) const
00214 {
00215 if ( mPalette.isEmpty() || value < 0 || value > 1 )
00216 return QColor( 255, 0, 0 );
00217
00218 int paletteEntry = ( int )( value * mPalette.count() );
00219 if ( paletteEntry >= mPalette.count() )
00220 paletteEntry = mPalette.count() - 1;
00221 return mPalette.at( paletteEntry );
00222 }
00223
00224 QgsVectorColorRampV2* QgsVectorColorBrewerColorRampV2::clone() const
00225 {
00226 return new QgsVectorColorBrewerColorRampV2( mSchemeName, mColors );
00227 }
00228
00229 QgsStringMap QgsVectorColorBrewerColorRampV2::properties() const
00230 {
00231 QgsStringMap map;
00232 map["schemeName"] = mSchemeName;
00233 map["colors"] = QString::number( mColors );
00234 return map;
00235 }