Skip to content

Commit

Permalink
Merge pull request #42450 from m-kuhn/qt6_rand
Browse files Browse the repository at this point in the history
[qt6] Switch from qrand to std::rand
  • Loading branch information
m-kuhn committed Mar 26, 2021
2 parents f98ab57 + b024eea commit 41fbcbe
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/core/qgscolorramp.cpp
Expand Up @@ -401,7 +401,7 @@ QList<QColor> QgsLimitedRandomColorRamp::randomColors( int count,
int safeValMin = std::min( valMin, valMax );

//start hue at random angle
double currentHueAngle = 360.0 * static_cast< double >( qrand() ) / RAND_MAX;
double currentHueAngle = 360.0 * static_cast< double >( std::rand() ) / RAND_MAX;

colors.reserve( count );
for ( int i = 0; i < count; ++i )
Expand All @@ -412,8 +412,8 @@ QList<QColor> QgsLimitedRandomColorRamp::randomColors( int count,
currentHueAngle += 137.50776;
//scale hue to between hueMax and hueMin
h = std::clamp( std::round( ( std::fmod( currentHueAngle, 360.0 ) / 360.0 ) * ( safeHueMax - safeHueMin ) + safeHueMin ), 0.0, 359.0 );
s = std::clamp( ( qrand() % ( safeSatMax - safeSatMin + 1 ) ) + safeSatMin, 0, 255 );
v = std::clamp( ( qrand() % ( safeValMax - safeValMin + 1 ) ) + safeValMin, 0, 255 );
s = std::clamp( ( static_cast<int>( std::rand() ) % ( safeSatMax - safeSatMin + 1 ) ) + safeSatMin, 0, 255 );
v = std::clamp( ( static_cast<int>( std::rand() ) % ( safeValMax - safeValMin + 1 ) ) + safeValMin, 0, 255 );
colors.append( QColor::fromHsv( h, s, v ) );
}
return colors;
Expand Down Expand Up @@ -451,9 +451,9 @@ QColor QgsRandomColorRamp::color( double value ) const
}

//can't use precalculated hues, use a totally random hue
int h = static_cast< int >( 360.0 * qrand() / ( RAND_MAX + 1.0 ) );
int s = ( qrand() % ( DEFAULT_RANDOM_SAT_MAX - DEFAULT_RANDOM_SAT_MIN + 1 ) ) + DEFAULT_RANDOM_SAT_MIN;
int v = ( qrand() % ( maxVal - minVal + 1 ) ) + minVal;
int h = static_cast< int >( 360.0 * std::rand() / ( RAND_MAX + 1.0 ) );
int s = ( std::rand() % ( DEFAULT_RANDOM_SAT_MAX - DEFAULT_RANDOM_SAT_MIN + 1 ) ) + DEFAULT_RANDOM_SAT_MIN;
int v = ( std::rand() % ( maxVal - minVal + 1 ) ) + minVal;
return QColor::fromHsv( h, s, v );
}

Expand All @@ -467,7 +467,7 @@ void QgsRandomColorRamp::setTotalColorCount( const int colorCount )
//similar colors being picked. TODO - investigate alternative "n-visually distinct color" routines

//random offsets
double hueOffset = ( 360.0 * qrand() / ( RAND_MAX + 1.0 ) );
double hueOffset = ( 360.0 * std::rand() / ( RAND_MAX + 1.0 ) );

//try to maximise difference between hues. this is not an ideal implementation, as constant steps
//through the hue wheel are not visually perceived as constant changes in hue
Expand All @@ -479,8 +479,8 @@ void QgsRandomColorRamp::setTotalColorCount( const int colorCount )
for ( int idx = 0; idx < colorCount; ++ idx )
{
int h = static_cast< int >( std::round( currentHue ) ) % 360;
int s = ( qrand() % ( DEFAULT_RANDOM_SAT_MAX - DEFAULT_RANDOM_SAT_MIN + 1 ) ) + DEFAULT_RANDOM_SAT_MIN;
int v = ( qrand() % ( DEFAULT_RANDOM_VAL_MAX - DEFAULT_RANDOM_VAL_MIN + 1 ) ) + DEFAULT_RANDOM_VAL_MIN;
int s = ( std::rand() % ( DEFAULT_RANDOM_SAT_MAX - DEFAULT_RANDOM_SAT_MIN + 1 ) ) + DEFAULT_RANDOM_SAT_MIN;
int v = ( std::rand() % ( DEFAULT_RANDOM_VAL_MAX - DEFAULT_RANDOM_VAL_MIN + 1 ) ) + DEFAULT_RANDOM_VAL_MIN;
mPrecalculatedColors << QColor::fromHsv( h, s, v );
currentHue += hueStep;
}
Expand Down

0 comments on commit 41fbcbe

Please sign in to comment.