Skip to content

Commit 5893647

Browse files
pierstitusnyalldawson
authored andcommitted
fix colors for continuous classification with non equally spaced stops
1 parent 912f06d commit 5893647

File tree

1 file changed

+90
-84
lines changed

1 file changed

+90
-84
lines changed

src/gui/raster/qgssinglebandpseudocolorrendererwidget.cpp

Lines changed: 90 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -383,123 +383,129 @@ void QgsSingleBandPseudoColorRendererWidget::on_mClassifyButton_clicked()
383383
entryValues.push_back( min + value * ( max - min ) );
384384
}
385385
}
386+
// for continuous mode take original color map colors
387+
for ( int i = 0; i < numberOfEntries; ++i )
388+
{
389+
entryColors.push_back( colorRamp->color( colorRamp->value( i ) ) );
390+
}
386391
}
387392
}
388-
else if ( mClassificationModeComboBox->itemData( mClassificationModeComboBox->currentIndex() ).toInt() == Quantile )
389-
{ // Quantile
393+
else // for other classification modes interpolate colors linearly
394+
{
390395
numberOfEntries = mNumberOfEntriesSpinBox->value();
391396

392-
int bandNr = mBandComboBox->itemData( bandComboIndex ).toInt();
393-
//QgsRasterHistogram rasterHistogram = mRasterLayer->dataProvider()->histogram( bandNr );
397+
if ( mClassificationModeComboBox->itemData( mClassificationModeComboBox->currentIndex() ).toInt() == Quantile )
398+
{ // Quantile
399+
int bandNr = mBandComboBox->itemData( bandComboIndex ).toInt();
400+
//QgsRasterHistogram rasterHistogram = mRasterLayer->dataProvider()->histogram( bandNr );
394401

395-
double cut1 = std::numeric_limits<double>::quiet_NaN();
396-
double cut2 = std::numeric_limits<double>::quiet_NaN();
402+
double cut1 = std::numeric_limits<double>::quiet_NaN();
403+
double cut2 = std::numeric_limits<double>::quiet_NaN();
397404

398-
QgsRectangle extent = mMinMaxWidget->extent();
399-
int sampleSize = mMinMaxWidget->sampleSize();
405+
QgsRectangle extent = mMinMaxWidget->extent();
406+
int sampleSize = mMinMaxWidget->sampleSize();
400407

401-
// set min and max from histogram, used later to calculate number of decimals to display
402-
mRasterLayer->dataProvider()->cumulativeCut( bandNr, 0.0, 1.0, min, max, extent, sampleSize );
408+
// set min and max from histogram, used later to calculate number of decimals to display
409+
mRasterLayer->dataProvider()->cumulativeCut( bandNr, 0.0, 1.0, min, max, extent, sampleSize );
403410

404-
double intervalDiff;
405-
if ( numberOfEntries > 1 )
406-
{
407-
entryValues.reserve( numberOfEntries );
408-
if ( discrete )
411+
double intervalDiff;
412+
if ( numberOfEntries > 1 )
409413
{
410-
intervalDiff = 1.0 / ( numberOfEntries );
411-
for ( int i = 1; i < numberOfEntries; ++i )
414+
entryValues.reserve( numberOfEntries );
415+
if ( discrete )
412416
{
413-
mRasterLayer->dataProvider()->cumulativeCut( bandNr, 0.0, i * intervalDiff, cut1, cut2, extent, sampleSize );
414-
entryValues.push_back( cut2 );
417+
intervalDiff = 1.0 / ( numberOfEntries );
418+
for ( int i = 1; i < numberOfEntries; ++i )
419+
{
420+
mRasterLayer->dataProvider()->cumulativeCut( bandNr, 0.0, i * intervalDiff, cut1, cut2, extent, sampleSize );
421+
entryValues.push_back( cut2 );
422+
}
423+
entryValues.push_back( std::numeric_limits<double>::infinity() );
415424
}
416-
entryValues.push_back( std::numeric_limits<double>::infinity() );
417-
}
418-
else
419-
{
420-
intervalDiff = 1.0 / ( numberOfEntries - 1 );
421-
for ( int i = 0; i < numberOfEntries; ++i )
425+
else
422426
{
423-
mRasterLayer->dataProvider()->cumulativeCut( bandNr, 0.0, i * intervalDiff, cut1, cut2, extent, sampleSize );
424-
entryValues.push_back( cut2 );
427+
intervalDiff = 1.0 / ( numberOfEntries - 1 );
428+
for ( int i = 0; i < numberOfEntries; ++i )
429+
{
430+
mRasterLayer->dataProvider()->cumulativeCut( bandNr, 0.0, i * intervalDiff, cut1, cut2, extent, sampleSize );
431+
entryValues.push_back( cut2 );
432+
}
425433
}
426434
}
435+
else if ( numberOfEntries == 1 )
436+
{
437+
mRasterLayer->dataProvider()->cumulativeCut( bandNr, 0.0, 0.5, cut1, cut2, extent, sampleSize );
438+
entryValues.push_back( cut2 );
439+
}
427440
}
428-
else if ( numberOfEntries == 1 )
441+
else // EqualInterval
429442
{
430-
mRasterLayer->dataProvider()->cumulativeCut( bandNr, 0.0, 0.5, cut1, cut2, extent, sampleSize );
431-
entryValues.push_back( cut2 );
432-
}
433-
}
434-
else // EqualInterval
435-
{
436-
numberOfEntries = mNumberOfEntriesSpinBox->value();
437-
438-
if ( numberOfEntries > 1 )
439-
{
440-
entryValues.reserve( numberOfEntries );
441-
if ( discrete )
443+
if ( numberOfEntries > 1 )
442444
{
443-
// in discrete mode the lowest value is not an entry and the highest
444-
// value is inf, there are ( numberOfEntries ) of which the first
445-
// and last are not used.
446-
double intervalDiff = ( max - min ) / ( numberOfEntries );
445+
entryValues.reserve( numberOfEntries );
446+
if ( discrete )
447+
{
448+
// in discrete mode the lowest value is not an entry and the highest
449+
// value is inf, there are ( numberOfEntries ) of which the first
450+
// and last are not used.
451+
double intervalDiff = ( max - min ) / ( numberOfEntries );
447452

448-
for ( int i = 1; i < numberOfEntries; ++i )
453+
for ( int i = 1; i < numberOfEntries; ++i )
454+
{
455+
entryValues.push_back( min + i * intervalDiff );
456+
}
457+
entryValues.push_back( std::numeric_limits<double>::infinity() );
458+
}
459+
else
449460
{
450-
entryValues.push_back( min + i * intervalDiff );
461+
//because the highest value is also an entry, there are (numberOfEntries - 1) intervals
462+
double intervalDiff = ( max - min ) / ( numberOfEntries - 1 );
463+
464+
for ( int i = 0; i < numberOfEntries; ++i )
465+
{
466+
entryValues.push_back( min + i * intervalDiff );
467+
}
451468
}
452-
entryValues.push_back( std::numeric_limits<double>::infinity() );
453469
}
454-
else
470+
else if ( numberOfEntries == 1 )
455471
{
456-
//because the highest value is also an entry, there are (numberOfEntries - 1) intervals
457-
double intervalDiff = ( max - min ) / ( numberOfEntries - 1 );
458-
459-
for ( int i = 0; i < numberOfEntries; ++i )
472+
if ( discrete )
473+
{
474+
entryValues.push_back( std::numeric_limits<double>::infinity() );
475+
}
476+
else
460477
{
461-
entryValues.push_back( min + i * intervalDiff );
478+
entryValues.push_back(( max + min ) / 2 );
462479
}
463480
}
464481
}
465-
else if ( numberOfEntries == 1 )
482+
483+
if ( ! colorRamp )
466484
{
467-
if ( discrete )
485+
//hard code color range from blue -> red (previous default)
486+
int colorDiff = 0;
487+
if ( numberOfEntries != 0 )
468488
{
469-
entryValues.push_back( std::numeric_limits<double>::infinity() );
489+
colorDiff = ( int )( 255 / numberOfEntries );
470490
}
471-
else
491+
492+
entryColors.reserve( numberOfEntries );
493+
for ( int i = 0; i < numberOfEntries; ++i )
472494
{
473-
entryValues.push_back(( max + min ) / 2 );
495+
QColor currentColor;
496+
int idx = mInvertCheckBox->isChecked() ? numberOfEntries - i - 1 : i;
497+
currentColor.setRgb( colorDiff*idx, 0, 255 - colorDiff * idx );
498+
entryColors.push_back( currentColor );
474499
}
475500
}
476-
}
477-
478-
if ( ! colorRamp )
479-
{
480-
//hard code color range from blue -> red (previous default)
481-
int colorDiff = 0;
482-
if ( numberOfEntries != 0 )
483-
{
484-
colorDiff = ( int )( 255 / numberOfEntries );
485-
}
486-
487-
entryColors.reserve( numberOfEntries );
488-
for ( int i = 0; i < numberOfEntries; ++i )
489-
{
490-
QColor currentColor;
491-
int idx = mInvertCheckBox->isChecked() ? numberOfEntries - i - 1 : i;
492-
currentColor.setRgb( colorDiff*idx, 0, 255 - colorDiff * idx );
493-
entryColors.push_back( currentColor );
494-
}
495-
}
496-
else
497-
{
498-
entryColors.reserve( numberOfEntries );
499-
for ( int i = 0; i < numberOfEntries; ++i )
501+
else
500502
{
501-
int idx = mInvertCheckBox->isChecked() ? numberOfEntries - i - 1 : i;
502-
entryColors.push_back( colorRamp->color((( double ) idx ) / ( numberOfEntries - 1 ) ) );
503+
entryColors.reserve( numberOfEntries );
504+
for ( int i = 0; i < numberOfEntries; ++i )
505+
{
506+
int idx = mInvertCheckBox->isChecked() ? numberOfEntries - i - 1 : i;
507+
entryColors.push_back( colorRamp->color((( double ) idx ) / ( numberOfEntries - 1 ) ) );
508+
}
503509
}
504510
}
505511

0 commit comments

Comments
 (0)