~ubuntu-branches/ubuntu/wily/qgis/wily

« back to all changes in this revision

Viewing changes to src/core/symbology-ng/qgsvectorcolorrampv2.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Johan Van de Wauw
  • Date: 2010-07-11 20:23:24 UTC
  • mfrom: (3.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100711202324-5ktghxa7hracohmr
Tags: 1.4.0+12730-3ubuntu1
* Merge from Debian unstable (LP: #540941).
* Fix compilation issues with QT 4.7
* Add build-depends on libqt4-webkit-dev 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
#include "qgsvectorcolorrampv2.h"
 
3
 
 
4
#include "qgssymbollayerv2utils.h"
 
5
 
 
6
#include <stdlib.h> // for random()
 
7
 
 
8
QgsVectorGradientColorRampV2::QgsVectorGradientColorRampV2( QColor color1, QColor color2 )
 
9
    : mColor1( color1 ), mColor2( color2 )
 
10
{
 
11
}
 
12
 
 
13
QgsVectorColorRampV2* QgsVectorGradientColorRampV2::create( const QgsStringMap& props )
 
14
{
 
15
  QColor color1 = DEFAULT_GRADIENT_COLOR1;
 
16
  QColor color2 = DEFAULT_GRADIENT_COLOR2;
 
17
  if ( props.contains( "color1" ) )
 
18
    color1 = QgsSymbolLayerV2Utils::decodeColor( props["color1"] );
 
19
  if ( props.contains( "color2" ) )
 
20
    color2 = QgsSymbolLayerV2Utils::decodeColor( props["color2"] );
 
21
  return new QgsVectorGradientColorRampV2( color1, color2 );
 
22
}
 
23
 
 
24
QColor QgsVectorGradientColorRampV2::color( double value ) const
 
25
{
 
26
  int r = ( int )( mColor1.red() + value * ( mColor2.red() - mColor1.red() ) );
 
27
  int g = ( int )( mColor1.green() + value * ( mColor2.green() - mColor1.green() ) );
 
28
  int b = ( int )( mColor1.blue() + value * ( mColor2.blue() - mColor1.blue() ) );
 
29
 
 
30
  return QColor::fromRgb( r, g, b );
 
31
}
 
32
 
 
33
QgsVectorColorRampV2* QgsVectorGradientColorRampV2::clone() const
 
34
{
 
35
  return new QgsVectorGradientColorRampV2( mColor1, mColor2 );
 
36
}
 
37
 
 
38
QgsStringMap QgsVectorGradientColorRampV2::properties() const
 
39
{
 
40
  QgsStringMap map;
 
41
  map["color1"] = QgsSymbolLayerV2Utils::encodeColor( mColor1 );
 
42
  map["color2"] = QgsSymbolLayerV2Utils::encodeColor( mColor2 );
 
43
  return map;
 
44
}
 
45
 
 
46
//////////////
 
47
 
 
48
 
 
49
QgsVectorRandomColorRampV2::QgsVectorRandomColorRampV2( int count, int hueMin, int hueMax,
 
50
    int satMin, int satMax, int valMin, int valMax )
 
51
    : mCount( count ), mHueMin( hueMin ), mHueMax( hueMax ),
 
52
    mSatMin( satMin ), mSatMax( satMax ), mValMin( valMin ), mValMax( valMax )
 
53
{
 
54
  updateColors();
 
55
}
 
56
 
 
57
QgsVectorColorRampV2* QgsVectorRandomColorRampV2::create( const QgsStringMap& props )
 
58
{
 
59
  int count = DEFAULT_RANDOM_COUNT;
 
60
  int hueMin = DEFAULT_RANDOM_HUE_MIN, hueMax = DEFAULT_RANDOM_HUE_MAX;
 
61
  int satMin = DEFAULT_RANDOM_SAT_MIN, satMax = DEFAULT_RANDOM_SAT_MAX;
 
62
  int valMin = DEFAULT_RANDOM_VAL_MIN, valMax = DEFAULT_RANDOM_VAL_MAX;
 
63
 
 
64
  if ( props.contains( "count" ) ) count = props["count"].toInt();
 
65
  if ( props.contains( "hueMin" ) ) hueMin = props["hueMin"].toInt();
 
66
  if ( props.contains( "hueMax" ) ) hueMax = props["hueMax"].toInt();
 
67
  if ( props.contains( "satMin" ) ) satMin = props["satMin"].toInt();
 
68
  if ( props.contains( "satMax" ) ) satMax = props["satMax"].toInt();
 
69
  if ( props.contains( "valMin" ) ) valMin = props["valMin"].toInt();
 
70
  if ( props.contains( "valMax" ) ) valMax = props["valMax"].toInt();
 
71
 
 
72
  return new QgsVectorRandomColorRampV2( count, hueMin, hueMax, satMin, satMax, valMin, valMax );
 
73
}
 
74
 
 
75
QColor QgsVectorRandomColorRampV2::color( double value ) const
 
76
{
 
77
  int colorCnt = mColors.count();
 
78
  int colorIdx = ( int )( value * colorCnt );
 
79
 
 
80
  if ( colorIdx >= 0 && colorIdx < colorCnt )
 
81
    return mColors.at( colorIdx );
 
82
 
 
83
  return QColor();
 
84
}
 
85
 
 
86
QgsVectorColorRampV2* QgsVectorRandomColorRampV2::clone() const
 
87
{
 
88
  return new QgsVectorRandomColorRampV2( mCount, mHueMin, mHueMax, mSatMin, mSatMax, mValMin, mValMax );
 
89
}
 
90
 
 
91
QgsStringMap QgsVectorRandomColorRampV2::properties() const
 
92
{
 
93
  QgsStringMap map;
 
94
  map["count"] = QString::number( mCount );
 
95
  map["hueMin"] = QString::number( mHueMin );
 
96
  map["hueMax"] = QString::number( mHueMax );
 
97
  map["satMin"] = QString::number( mSatMin );
 
98
  map["satMax"] = QString::number( mSatMax );
 
99
  map["valMin"] = QString::number( mValMin );
 
100
  map["valMax"] = QString::number( mValMax );
 
101
  return map;
 
102
}
 
103
 
 
104
void QgsVectorRandomColorRampV2::updateColors()
 
105
{
 
106
  int h, s, v;
 
107
 
 
108
  mColors.clear();
 
109
  for ( int i = 0; i < mCount; i++ )
 
110
  {
 
111
    h = ( rand() % ( mHueMax - mHueMin + 1 ) ) + mHueMin;
 
112
    s = ( rand() % ( mSatMax - mSatMin + 1 ) ) + mSatMin;
 
113
    v = ( rand() % ( mValMax - mValMin + 1 ) ) + mValMin;
 
114
    mColors.append( QColor::fromHsv( h, s, v ) );
 
115
  }
 
116
}
 
117
 
 
118
 
 
119
////////////
 
120
 
 
121
QgsVectorColorBrewerColorRampV2::QgsVectorColorBrewerColorRampV2( QString schemeName, int colors )
 
122
    : mSchemeName( schemeName ), mColors( colors )
 
123
{
 
124
  loadPalette();
 
125
}
 
126
 
 
127
QgsVectorColorRampV2* QgsVectorColorBrewerColorRampV2::create( const QgsStringMap& props )
 
128
{
 
129
  QString schemeName = DEFAULT_COLORBREWER_SCHEMENAME;
 
130
  int colors = DEFAULT_COLORBREWER_COLORS;
 
131
 
 
132
  if ( props.contains( "schemeName" ) )
 
133
    schemeName = props["schemeName"];
 
134
  if ( props.contains( "colors" ) )
 
135
    colors = props["colors"].toInt();
 
136
 
 
137
  return new QgsVectorColorBrewerColorRampV2( schemeName, colors );
 
138
}
 
139
 
 
140
#include "qgscolorbrewerpalette.h"
 
141
 
 
142
void QgsVectorColorBrewerColorRampV2::loadPalette()
 
143
{
 
144
  mPalette = QgsColorBrewerPalette::listSchemeColors( mSchemeName, mColors );
 
145
}
 
146
 
 
147
QStringList QgsVectorColorBrewerColorRampV2::listSchemeNames()
 
148
{
 
149
  return QgsColorBrewerPalette::listSchemes();
 
150
}
 
151
 
 
152
QList<int> QgsVectorColorBrewerColorRampV2::listSchemeVariants( QString schemeName )
 
153
{
 
154
  return QgsColorBrewerPalette::listSchemeVariants( schemeName );
 
155
}
 
156
 
 
157
QColor QgsVectorColorBrewerColorRampV2::color( double value ) const
 
158
{
 
159
  if ( mPalette.isEmpty() || value < 0 || value > 1 )
 
160
    return QColor( 255, 0, 0 ); // red color as a warning :)
 
161
 
 
162
  int paletteEntry = ( int )( value * mPalette.count() );
 
163
  if ( paletteEntry >= mPalette.count() )
 
164
    paletteEntry = mPalette.count() - 1;
 
165
  return mPalette.at( paletteEntry );
 
166
}
 
167
 
 
168
QgsVectorColorRampV2* QgsVectorColorBrewerColorRampV2::clone() const
 
169
{
 
170
  return new QgsVectorColorBrewerColorRampV2( mSchemeName, mColors );
 
171
}
 
172
 
 
173
QgsStringMap QgsVectorColorBrewerColorRampV2::properties() const
 
174
{
 
175
  QgsStringMap map;
 
176
  map["schemeName"] = mSchemeName;
 
177
  map["colors"] = QString::number( mColors );
 
178
  return map;
 
179
}