QGIS API Documentation  2.2.0-Valmiera
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsvectorcolorrampv2.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsvectorcolorrampv2.cpp
3  ---------------------
4  begin : November 2009
5  copyright : (C) 2009 by Martin Dobias
6  email : wonder dot sk at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include "qgsvectorcolorrampv2.h"
17 #include "qgscolorbrewerpalette.h"
18 #include "qgscptcityarchive.h"
19 
20 #include "qgssymbollayerv2utils.h"
21 #include "qgsapplication.h"
22 #include "qgslogger.h"
23 
24 #include <stdlib.h> // for random()
25 #include <QTime>
26 
28 
29 static QColor _interpolate( QColor c1, QColor c2, double value )
30 {
31  int r = ( int )( c1.red() + value * ( c2.red() - c1.red() ) );
32  int g = ( int )( c1.green() + value * ( c2.green() - c1.green() ) );
33  int b = ( int )( c1.blue() + value * ( c2.blue() - c1.blue() ) );
34  int a = ( int )( c1.alpha() + value * ( c2.alpha() - c1.alpha() ) );
35 
36  return QColor::fromRgb( r, g, b, a );
37 }
38 
40 
42  bool discrete, QgsGradientStopsList stops )
43  : mColor1( color1 ), mColor2( color2 ), mDiscrete( discrete ), mStops( stops )
44 {
45 }
46 
48 {
49  // color1 and color2
52  if ( props.contains( "color1" ) )
53  color1 = QgsSymbolLayerV2Utils::decodeColor( props["color1"] );
54  if ( props.contains( "color2" ) )
55  color2 = QgsSymbolLayerV2Utils::decodeColor( props["color2"] );
56 
57  //stops
59  if ( props.contains( "stops" ) )
60  {
61  foreach ( QString stop, props["stops"].split( ':' ) )
62  {
63  int i = stop.indexOf( ';' );
64  if ( i == -1 )
65  continue;
66 
67  QColor c = QgsSymbolLayerV2Utils::decodeColor( stop.mid( i + 1 ) );
68  stops.append( QgsGradientStop( stop.left( i ).toDouble(), c ) );
69  }
70  }
71 
72  // discrete vs. continuous
73  bool discrete = false;
74  if ( props.contains( "discrete" ) )
75  {
76  if ( props["discrete"] == "1" )
77  discrete = true;
78  }
79 
80  // search for information keys starting with "info_"
82  for ( QgsStringMap::const_iterator it = props.constBegin();
83  it != props.constEnd(); ++it )
84  {
85  if ( it.key().startsWith( "info_" ) )
86  info[ it.key().mid( 5 )] = it.value();
87  }
88 
89  QgsVectorGradientColorRampV2* r = new QgsVectorGradientColorRampV2( color1, color2, discrete, stops );
90  r->setInfo( info );
91  return r;
92 }
93 
95 {
96  if ( index <= 0 )
97  {
98  return 0;
99  }
100  else if ( index >= mStops.size() + 1 )
101  {
102  return 1;
103  }
104  else
105  {
106  return mStops[index-1].offset;
107  }
108 }
109 
110 QColor QgsVectorGradientColorRampV2::color( double value ) const
111 {
112  if ( mStops.isEmpty() )
113  {
114  if ( mDiscrete )
115  return mColor1;
116  return _interpolate( mColor1, mColor2, value );
117  }
118  else
119  {
120  double lower = 0, upper = 0;
121  QColor c1 = mColor1, c2;
122  for ( QgsGradientStopsList::const_iterator it = mStops.begin(); it != mStops.end(); ++it )
123  {
124  if ( it->offset > value )
125  {
126  if ( mDiscrete )
127  return c1;
128 
129  upper = it->offset;
130  c2 = it->color;
131 
132  return upper == lower ? c1 : _interpolate( c1, c2, ( value - lower ) / ( upper - lower ) );
133  }
134  lower = it->offset;
135  c1 = it->color;
136  }
137 
138  if ( mDiscrete )
139  return c1;
140 
141  upper = 1;
142  c2 = mColor2;
143  return upper == lower ? c1 : _interpolate( c1, c2, ( value - lower ) / ( upper - lower ) );
144  }
145 }
146 
148 {
150  mDiscrete, mStops );
151  r->setInfo( mInfo );
152  return r;
153 }
154 
156 {
157  QgsStringMap map;
158  map["color1"] = QgsSymbolLayerV2Utils::encodeColor( mColor1 );
159  map["color2"] = QgsSymbolLayerV2Utils::encodeColor( mColor2 );
160  if ( !mStops.isEmpty() )
161  {
162  QStringList lst;
163  for ( QgsGradientStopsList::const_iterator it = mStops.begin(); it != mStops.end(); ++it )
164  {
165  lst.append( QString( "%1;%2" ).arg( it->offset ).arg( QgsSymbolLayerV2Utils::encodeColor( it->color ) ) );
166  }
167  map["stops"] = lst.join( ":" );
168  }
169 
170  map["discrete"] = mDiscrete ? "1" : "0";
171 
172  for ( QgsStringMap::const_iterator it = mInfo.constBegin();
173  it != mInfo.constEnd(); ++it )
174  {
175  map["info_" + it.key()] = it.value();
176  }
177 
178  return map;
179 }
181 {
182  if ( discrete == mDiscrete )
183  return;
184 
185  // if going to/from Discrete, re-arrange stops
186  // this will only work when stops are equally-spaced
187  QgsGradientStopsList newStops;
188  if ( discrete )
189  {
190  // re-arrange stops offset
191  int numStops = mStops.count() + 2;
192  int i = 1;
193  for ( QgsGradientStopsList::const_iterator it = mStops.begin();
194  it != mStops.end(); ++it )
195  {
196  newStops.append( QgsGradientStop(( double ) i / numStops, it->color ) );
197  if ( i == numStops - 1 )
198  break;
199  i++;
200  }
201  // replicate last color
202  newStops.append( QgsGradientStop(( double ) i / numStops, mColor2 ) );
203  }
204  else
205  {
206  // re-arrange stops offset, remove duplicate last color
207  int numStops = mStops.count() + 2;
208  int i = 1;
209  for ( QgsGradientStopsList::const_iterator it = mStops.begin();
210  it != mStops.end(); ++it )
211  {
212  newStops.append( QgsGradientStop(( double ) i / ( numStops - 2 ), it->color ) );
213  if ( i == numStops - 3 )
214  break;
215  i++;
216  }
217  }
218  mStops = newStops;
219  mDiscrete = discrete;
220 }
221 
223 {
224  //copy color ramp stops to a QGradient
225  gradient->setColorAt( 0, mColor1 );
226  gradient->setColorAt( 1, mColor2 );
227 
228  for ( QgsGradientStopsList::const_iterator it = mStops.begin();
229  it != mStops.end(); ++it )
230  {
231  gradient->setColorAt( it->offset , it->color );
232  }
233 }
234 
235 
237 
238 
240  int satMin, int satMax, int valMin, int valMax )
241  : mCount( count ), mHueMin( hueMin ), mHueMax( hueMax ),
242  mSatMin( satMin ), mSatMax( satMax ), mValMin( valMin ), mValMax( valMax )
243 {
244  updateColors();
245 }
246 
248 {
253 
254  if ( props.contains( "count" ) ) count = props["count"].toInt();
255  if ( props.contains( "hueMin" ) ) hueMin = props["hueMin"].toInt();
256  if ( props.contains( "hueMax" ) ) hueMax = props["hueMax"].toInt();
257  if ( props.contains( "satMin" ) ) satMin = props["satMin"].toInt();
258  if ( props.contains( "satMax" ) ) satMax = props["satMax"].toInt();
259  if ( props.contains( "valMin" ) ) valMin = props["valMin"].toInt();
260  if ( props.contains( "valMax" ) ) valMax = props["valMax"].toInt();
261 
262  return new QgsVectorRandomColorRampV2( count, hueMin, hueMax, satMin, satMax, valMin, valMax );
263 }
264 
266 {
267  if ( mColors.size() < 1 ) return 0;
268  return index / mColors.size() - 1;
269 }
270 
271 QColor QgsVectorRandomColorRampV2::color( double value ) const
272 {
273  int colorCnt = mColors.count();
274  int colorIdx = ( int )( value * colorCnt );
275 
276  if ( colorIdx >= 0 && colorIdx < colorCnt )
277  return mColors.at( colorIdx );
278 
279  return QColor();
280 }
281 
283 {
285 }
286 
288 {
289  QgsStringMap map;
290  map["count"] = QString::number( mCount );
291  map["hueMin"] = QString::number( mHueMin );
292  map["hueMax"] = QString::number( mHueMax );
293  map["satMin"] = QString::number( mSatMin );
294  map["satMax"] = QString::number( mSatMax );
295  map["valMin"] = QString::number( mValMin );
296  map["valMax"] = QString::number( mValMax );
297  return map;
298 }
299 
301 {
302  int h, s, v;
303 
304  mColors.clear();
305  //start hue at random angle
306  double currentHueAngle = 360.0 * ( double )rand() / RAND_MAX;
307 
308  for ( int i = 0; i < mCount; i++ )
309  {
310  //increment hue by golden ratio (approx 137.507 degrees)
311  //as this minimises hue nearness as count increases
312  //see http://basecase.org/env/on-rainbows for more details
313  currentHueAngle += 137.50776;
314  //scale hue to between mHueMax and mHueMin
315  h = ( fmod( currentHueAngle, 360.0 ) / 360.0 ) * ( mHueMax - mHueMin ) + mHueMin;
316  s = ( rand() % ( mSatMax - mSatMin + 1 ) ) + mSatMin;
317  v = ( rand() % ( mValMax - mValMin + 1 ) ) + mValMin;
318  mColors.append( QColor::fromHsv( h, s, v ) );
319  }
320 }
321 
323 
325 {
326  srand( QTime::currentTime().msec() );
327 }
328 
330 {
331 
332 }
333 
335 {
336  return INT_MAX;
337 }
338 
339 double QgsRandomColorsV2::value( int index ) const
340 {
341  Q_UNUSED( index );
342  return 0.0;
343 }
344 
345 QColor QgsRandomColorsV2::color( double value ) const
346 {
347  Q_UNUSED( value );
348  int minVal = 130;
349  int maxVal = 255;
350  int h = 1 + ( int )( 360.0 * rand() / ( RAND_MAX + 1.0 ) );
352  int v = ( rand() % ( maxVal - minVal + 1 ) ) + minVal;
353  return QColor::fromHsv( h, s, v );
354 }
355 
356 QString QgsRandomColorsV2::type() const
357 {
358  return "randomcolors";
359 }
360 
362 {
363  return new QgsRandomColorsV2();
364 }
365 
367 {
368  return QgsStringMap();
369 }
370 
372 
374  : mSchemeName( schemeName ), mColors( colors )
375 {
376  loadPalette();
377 }
378 
380 {
383 
384  if ( props.contains( "schemeName" ) )
385  schemeName = props["schemeName"];
386  if ( props.contains( "colors" ) )
387  colors = props["colors"].toInt();
388 
389  return new QgsVectorColorBrewerColorRampV2( schemeName, colors );
390 }
391 
393 {
395 }
396 
398 {
400 }
401 
403 {
404  return QgsColorBrewerPalette::listSchemeVariants( schemeName );
405 }
406 
408 {
409  if ( mPalette.size() < 1 ) return 0;
410  return index / mPalette.size() - 1;
411 }
412 
413 QColor QgsVectorColorBrewerColorRampV2::color( double value ) const
414 {
415  if ( mPalette.isEmpty() || value < 0 || value > 1 )
416  return QColor( 255, 0, 0 ); // red color as a warning :)
417 
418  int paletteEntry = ( int )( value * mPalette.count() );
419  if ( paletteEntry >= mPalette.count() )
420  paletteEntry = mPalette.count() - 1;
421  return mPalette.at( paletteEntry );
422 }
423 
425 {
427 }
428 
430 {
431  QgsStringMap map;
432  map["schemeName"] = mSchemeName;
433  map["colors"] = QString::number( mColors );
434  return map;
435 }
436 
437 
439 
440 
441 QgsCptCityColorRampV2::QgsCptCityColorRampV2( QString schemeName, QString variantName,
442  bool doLoadFile )
444  mSchemeName( schemeName ), mVariantName( variantName ),
445  mVariantList( QStringList() ), mFileLoaded( false ), mMultiStops( false )
446 {
447  // TODO replace this with hard-coded data in the default case
448  // don't load file if variant is missing
449  if ( doLoadFile && ( variantName != QString() || mVariantList.isEmpty() ) )
450  loadFile();
451 }
452 
453 QgsCptCityColorRampV2::QgsCptCityColorRampV2( QString schemeName, QStringList variantList,
454  QString variantName, bool doLoadFile )
456  mSchemeName( schemeName ), mVariantName( variantName ),
457  mVariantList( variantList ), mFileLoaded( false ), mMultiStops( false )
458 {
460 
461  // TODO replace this with hard-coded data in the default case
462  // don't load file if variant is missing
463  if ( doLoadFile && ( variantName != QString() || mVariantList.isEmpty() ) )
464  loadFile();
465 }
466 
468 {
471 
472  if ( props.contains( "schemeName" ) )
473  schemeName = props["schemeName"];
474  if ( props.contains( "variantName" ) )
475  variantName = props["variantName"];
476 
477  return new QgsCptCityColorRampV2( schemeName, variantName );
478 }
479 
481 {
482  QgsCptCityColorRampV2* ramp = new QgsCptCityColorRampV2( "", "", false );
483  ramp->copy( this );
484  return ramp;
485 }
486 
488 {
489  if ( ! other )
490  return;
491  mColor1 = other->color1();
492  mColor2 = other->color2();
493  mDiscrete = other->isDiscrete();
494  mStops = other->stops();
495  mSchemeName = other->mSchemeName;
496  mVariantName = other->mVariantName;
497  mVariantList = other->mVariantList;
498  mFileLoaded = other->mFileLoaded;
499 }
500 
502 {
505  // add author and copyright information
506  // TODO also add COPYING.xml file/link?
508  info["cpt-city-gradient"] = "<cpt-city>/" + mSchemeName + mVariantName + ".svg";
509  QString copyingFilename = copyingFileName();
510  copyingFilename.remove( QgsCptCityArchive::defaultBaseDir() );
511  info["cpt-city-license"] = "<cpt-city>" + copyingFilename;
512  ramp->setInfo( info );
513  return ramp;
514 }
515 
516 
518 {
519  QgsStringMap map;
520  map["schemeName"] = mSchemeName;
521  map["variantName"] = mVariantName;
522  return map;
523 }
524 
525 
527 {
528  if ( mSchemeName == "" )
529  return QString();
530  else
531  {
532  return QgsCptCityArchive::defaultBaseDir() + QDir::separator() + mSchemeName + mVariantName + ".svg";
533  }
534 }
535 
537 {
538  return QgsCptCityArchive::findFileName( "COPYING.xml", QFileInfo( fileName() ).dir().path(),
540 }
541 
543 {
544  return QgsCptCityArchive::findFileName( "DESC.xml", QFileInfo( fileName() ).dir().path(),
546 }
547 
549 {
551 }
552 
554 {
555  if ( mFileLoaded )
556  {
557  QgsDebugMsg( "File already loaded for " + mSchemeName + mVariantName );
558  return true;
559  }
560 
561  // get filename
562  QString filename = fileName();
563  if ( filename.isNull() )
564  {
565  QgsDebugMsg( "Couldn't get fileName() for " + mSchemeName + mVariantName );
566  return false;
567  }
568 
569  QgsDebugMsg( QString( "filename= %1 loaded=%2" ).arg( filename ).arg( mFileLoaded ) );
570 
571  // get color ramp from svg file
572  QMap< double, QPair<QColor, QColor> > colorMap =
574 
575  // add colors to palette
576  mFileLoaded = false;
577  mStops.clear();
578  QMap<double, QPair<QColor, QColor> >::const_iterator it, prev;
579  // first detect if file is gradient is continuous or dicrete
580  // discrete: stop contains 2 colors and first color is identical to previous second
581  // multi: stop contains 2 colors and no relation with previous stop
582  mDiscrete = false;
583  mMultiStops = false;
584  it = prev = colorMap.constBegin();
585  while ( it != colorMap.constEnd() )
586  {
587  // look for stops that contain multiple values
588  if ( it != colorMap.constBegin() && ( it.value().first != it.value().second ) )
589  {
590  if ( it.value().first == prev.value().second )
591  {
592  mDiscrete = true;
593  break;
594  }
595  else
596  {
597  mMultiStops = true;
598  break;
599  }
600  }
601  prev = it;
602  ++it;
603  }
604 
605  // fill all stops
606  it = prev = colorMap.constBegin();
607  while ( it != colorMap.constEnd() )
608  {
609  if ( mDiscrete )
610  {
611  // mPalette << qMakePair( it.key(), it.value().second );
612  mStops.append( QgsGradientStop( it.key(), it.value().second ) );
613  }
614  else
615  {
616  // mPalette << qMakePair( it.key(), it.value().first );
617  mStops.append( QgsGradientStop( it.key(), it.value().first ) );
618  if (( mMultiStops ) &&
619  ( it.key() != 0.0 && it.key() != 1.0 ) )
620  {
621  mStops.append( QgsGradientStop( it.key(), it.value().second ) );
622  }
623  }
624  prev = it;
625  ++it;
626  }
627 
628  // remove first and last items (mColor1 and mColor2)
629  if ( ! mStops.isEmpty() && mStops.first().offset == 0.0 )
630  mColor1 = mStops.takeFirst().color;
631  if ( ! mStops.isEmpty() && mStops.last().offset == 1.0 )
632  mColor2 = mStops.takeLast().color;
633 
634  mFileLoaded = true;
635  return true;
636 }
637