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