PermalinkThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Please sign in to comment.
Avoid duplicate colors in random paletted raster rendering
When selecting Render type "Paletted/Unique values" for a raster layer that has 23 distinct raster values, QGIS will generate 23 random colors in QgsRandomColorRamp, and then these colors will be queried through QgsPalettedRasterRenderer::classDataFromRaster(). The querying of random colors goes through the generic QgsColorRamp::color(v) interface, which takes a v between 0.0 and 1.0. To turn a raster value index (0,1,...,22) into a value between 0.0 and 1.0, a division by 22 is applied. QgsRandomColorRamp then converts this value back into an index by multiplying by 22 and casting the result to an int. Dividing an integer by 22 and multiplying the result by 22 and casting to an int is not lossless: >>> int(15 / 22 * 22) 14 This causes e.g. the 14th and 15th raster value (0-indexed) to always have the same random color, regardless of how many times you press "Shuffle Random Colors" on a raster with 23 distinct values. Note that this issue is not exhibited at all for rasters with fewer than 23 distinct values, and certain cardinalities exhibit the issue much more than others; e.g. for 50 distinct values, there are 6 duplicates! Apply std::round() so that the correct index is obtained.
- Loading branch information