Quantum GIS API Documentation  1.7.5-Wroclaw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
qgsvectorcolorrampv2.cpp
Go to the documentation of this file.
1 
2 #include "qgsvectorcolorrampv2.h"
3 
5 
6 #include <stdlib.h> // for random()
7 
9  : mColor1( color1 ), mColor2( color2 )
10 {
11 }
12 
14 {
17  if ( props.contains( "color1" ) )
18  color1 = QgsSymbolLayerV2Utils::decodeColor( props["color1"] );
19  if ( props.contains( "color2" ) )
20  color2 = QgsSymbolLayerV2Utils::decodeColor( props["color2"] );
21 
23  if ( props.contains( "stops" ) )
24  {
25  foreach( QString stop, props["stops"].split( ':' ) )
26  {
27  int i = stop.indexOf( ';' );
28  if ( i == -1 ) continue;
29 
30  QColor c = QgsSymbolLayerV2Utils::decodeColor( stop.mid( i + 1 ) );
31  stops.insert( stop.left( i ).toDouble(), c );
32  }
33  }
34 
36  r->setStops( stops );
37  return r;
38 }
39 
40 static QColor _interpolate( QColor c1, QColor c2, double value )
41 {
42  int r = ( int )( c1.red() + value * ( c2.red() - c1.red() ) );
43  int g = ( int )( c1.green() + value * ( c2.green() - c1.green() ) );
44  int b = ( int )( c1.blue() + value * ( c2.blue() - c1.blue() ) );
45 
46  return QColor::fromRgb( r, g, b );
47 }
48 
49 QColor QgsVectorGradientColorRampV2::color( double value ) const
50 {
51  if ( mStops.isEmpty() )
52  {
53  return _interpolate( mColor1, mColor2, value );
54  }
55  else
56  {
57  double lower = 0, upper;
58  QColor c1 = mColor1, c2;
59  for ( StopsMap::const_iterator it = mStops.begin(); it != mStops.end(); ++it )
60  {
61  if ( it.key() >= value )
62  {
63  upper = it.key();
64  c2 = it.value();
65 
66  return upper == lower ? c1 : _interpolate( c1, c2, ( value - lower ) / ( upper - lower ) );
67  }
68  lower = it.key();
69  c1 = it.value();
70  }
71 
72  upper = 1;
73  c2 = mColor2;
74  return upper == lower ? c1 : _interpolate( c1, c2, ( value - lower ) / ( upper - lower ) );
75  }
76 }
77 
79 {
81  r->setStops( mStops );
82  return r;
83 }
84 
86 {
87  QgsStringMap map;
90  if ( !mStops.isEmpty() )
91  {
92  QStringList lst;
93  for ( StopsMap::const_iterator it = mStops.begin(); it != mStops.end(); ++it )
94  {
95  lst.append( QString( "%1;%2" ).arg( it.key() ).arg( QgsSymbolLayerV2Utils::encodeColor( it.value() ) ) );
96  }
97  map["stops"] = lst.join( ":" );
98  }
99  return map;
100 }
101 
103 
104 
106  int satMin, int satMax, int valMin, int valMax )
107  : mCount( count ), mHueMin( hueMin ), mHueMax( hueMax ),
108  mSatMin( satMin ), mSatMax( satMax ), mValMin( valMin ), mValMax( valMax )
109 {
110  updateColors();
111 }
112 
114 {
119 
120  if ( props.contains( "count" ) ) count = props["count"].toInt();
121  if ( props.contains( "hueMin" ) ) hueMin = props["hueMin"].toInt();
122  if ( props.contains( "hueMax" ) ) hueMax = props["hueMax"].toInt();
123  if ( props.contains( "satMin" ) ) satMin = props["satMin"].toInt();
124  if ( props.contains( "satMax" ) ) satMax = props["satMax"].toInt();
125  if ( props.contains( "valMin" ) ) valMin = props["valMin"].toInt();
126  if ( props.contains( "valMax" ) ) valMax = props["valMax"].toInt();
127 
128  return new QgsVectorRandomColorRampV2( count, hueMin, hueMax, satMin, satMax, valMin, valMax );
129 }
130 
131 QColor QgsVectorRandomColorRampV2::color( double value ) const
132 {
133  int colorCnt = mColors.count();
134  int colorIdx = ( int )( value * colorCnt );
135 
136  if ( colorIdx >= 0 && colorIdx < colorCnt )
137  return mColors.at( colorIdx );
138 
139  return QColor();
140 }
141 
143 {
145 }
146 
148 {
149  QgsStringMap map;
150  map["count"] = QString::number( mCount );
151  map["hueMin"] = QString::number( mHueMin );
152  map["hueMax"] = QString::number( mHueMax );
153  map["satMin"] = QString::number( mSatMin );
154  map["satMax"] = QString::number( mSatMax );
155  map["valMin"] = QString::number( mValMin );
156  map["valMax"] = QString::number( mValMax );
157  return map;
158 }
159 
161 {
162  int h, s, v;
163 
164  mColors.clear();
165  for ( int i = 0; i < mCount; i++ )
166  {
167  h = ( rand() % ( mHueMax - mHueMin + 1 ) ) + mHueMin;
168  s = ( rand() % ( mSatMax - mSatMin + 1 ) ) + mSatMin;
169  v = ( rand() % ( mValMax - mValMin + 1 ) ) + mValMin;
170  mColors.append( QColor::fromHsv( h, s, v ) );
171  }
172 }
173 
174 
176 
178  : mSchemeName( schemeName ), mColors( colors )
179 {
180  loadPalette();
181 }
182 
184 {
187 
188  if ( props.contains( "schemeName" ) )
189  schemeName = props["schemeName"];
190  if ( props.contains( "colors" ) )
191  colors = props["colors"].toInt();
192 
193  return new QgsVectorColorBrewerColorRampV2( schemeName, colors );
194 }
195 
196 #include "qgscolorbrewerpalette.h"
197 
199 {
201 }
202 
204 {
206 }
207 
209 {
210  return QgsColorBrewerPalette::listSchemeVariants( schemeName );
211 }
212 
213 QColor QgsVectorColorBrewerColorRampV2::color( double value ) const
214 {
215  if ( mPalette.isEmpty() || value < 0 || value > 1 )
216  return QColor( 255, 0, 0 ); // red color as a warning :)
217 
218  int paletteEntry = ( int )( value * mPalette.count() );
219  if ( paletteEntry >= mPalette.count() )
220  paletteEntry = mPalette.count() - 1;
221  return mPalette.at( paletteEntry );
222 }
223 
225 {
227 }
228 
230 {
231  QgsStringMap map;
232  map["schemeName"] = mSchemeName;
233  map["colors"] = QString::number( mColors );
234  return map;
235 }