@@ -318,6 +318,14 @@ QList<QColor> QgsVectorRandomColorRampV2::randomColors( int count,
318
318
int h, s, v;
319
319
QList<QColor> colors;
320
320
321
+ // normalize values
322
+ int safeHueMax = qMax ( hueMin, hueMax );
323
+ int safeHueMin = qMin ( hueMin, hueMax );
324
+ int safeSatMax = qMax ( satMin, satMax );
325
+ int safeSatMin = qMin ( satMin, satMax );
326
+ int safeValMax = qMax ( valMin, valMax );
327
+ int safeValMin = qMin ( valMin, valMax );
328
+
321
329
// start hue at random angle
322
330
double currentHueAngle = 360.0 * ( double )rand () / RAND_MAX;
323
331
@@ -328,9 +336,9 @@ QList<QColor> QgsVectorRandomColorRampV2::randomColors( int count,
328
336
// see http://basecase.org/env/on-rainbows for more details
329
337
currentHueAngle += 137.50776 ;
330
338
// scale hue to between hueMax and hueMin
331
- h = ( fmod ( currentHueAngle, 360.0 ) / 360.0 ) * ( hueMax - hueMin ) + hueMin ;
332
- s = ( rand () % ( satMax - satMin + 1 ) ) + satMin ;
333
- v = ( rand () % ( valMax - valMin + 1 ) ) + valMin ;
339
+ h = qBound ( 0 , qRound (( fmod ( currentHueAngle, 360.0 ) / 360.0 ) * ( safeHueMax - safeHueMin ) + safeHueMin ), 359 ) ;
340
+ s = qBound ( 0 , ( rand () % ( safeSatMax - safeSatMin + 1 ) ) + safeValMax, 255 ) ;
341
+ v = qBound ( 0 , ( rand () % ( safeValMax - safeValMin + 1 ) ) + safeValMin, 255 ) ;
334
342
colors.append ( QColor::fromHsv ( h, s, v ) );
335
343
}
336
344
return colors;
0 commit comments