Skip to content

Commit

Permalink
fix color legend
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomei committed Jul 7, 2024
1 parent f0b618a commit 79e2245
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 46 deletions.
24 changes: 10 additions & 14 deletions agrolib/gis/color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ Crit3DColorScale::Crit3DColorScale()

_minimum = NODATA;
_maximum = NODATA;
_isRangeBlocked = false;
_isFixedRange = false;
_isHideOutliers = false;

_classification = classificationMethod::EqualInterval;
}
Expand Down Expand Up @@ -179,10 +180,11 @@ bool setDTMScale(Crit3DColorScale* myScale)

bool setLAIScale(Crit3DColorScale* myScale)
{
myScale->initialize(3, 256);
myScale->initialize(4, 256);

myScale->keyColor[0] = Crit3DColor(200, 150, 0); /*!< ocra */
myScale->keyColor[1] = Crit3DColor(32, 150, 32); /*!< dark green */
myScale->keyColor[0] = Crit3DColor(200, 160, 0); /*!< ocra */
myScale->keyColor[1] = Crit3DColor(160, 160, 0); /*!< yellow */
myScale->keyColor[1] = Crit3DColor(32, 160, 32); /*!< dark green */
myScale->keyColor[2] = Crit3DColor(0, 255, 0); /*!< green */

return(myScale->classify());
Expand All @@ -205,19 +207,13 @@ bool setTemperatureScale(Crit3DColorScale* myScale)

bool setSlopeStabilityScale(Crit3DColorScale* myScale)
{
myScale->initialize(11, 220);
myScale->initialize(5, 256);

myScale->keyColor[0] = Crit3DColor(128, 0, 128); /*!< violet */
myScale->keyColor[1] = Crit3DColor(255, 0, 0); /*!< red */
myScale->keyColor[2] = Crit3DColor(255, 255, 0); /*!< yellow */
myScale->keyColor[3] = Crit3DColor(128, 196, 0); /*!< yellow/green */
myScale->keyColor[4] = Crit3DColor(64, 196, 64); /*!< green */
myScale->keyColor[5] = Crit3DColor(64, 196, 64); /*!< green */
myScale->keyColor[6] = Crit3DColor(64, 196, 64); /*!< green */
myScale->keyColor[7] = Crit3DColor(64, 196, 64); /*!< green */
myScale->keyColor[8] = Crit3DColor(64, 196, 64); /*!< green */
myScale->keyColor[9] = Crit3DColor(64, 196, 64); /*!< green */
myScale->keyColor[10] = Crit3DColor(64, 196, 64); /*!< green */
myScale->keyColor[3] = Crit3DColor(196, 196, 32); /*!< yellow/green */
myScale->keyColor[4] = Crit3DColor(64, 196, 64); /*!< green */

return(myScale->classify());
}
Expand All @@ -239,7 +235,7 @@ bool setAnomalyScale(Crit3DColorScale* myScale)

bool setPrecipitationScale(Crit3DColorScale* myScale)
{
myScale->initialize(6, 256);
myScale->initialize(6, 252);

myScale->keyColor[0] = Crit3DColor(255, 255, 255); /*!< white */
myScale->keyColor[1] = Crit3DColor(0, 0, 255); /*!< blue */
Expand Down
21 changes: 13 additions & 8 deletions agrolib/gis/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
private:
unsigned int _nrColors, _nrKeyColors;
std::vector<Crit3DColor> color;
float _minimum, _maximum;
bool _isRangeBlocked;
double _minimum, _maximum;
bool _isFixedRange;
bool _isHideOutliers;
int _classification;

public:
Expand All @@ -38,18 +39,22 @@
unsigned int nrColors() { return _nrColors; }
unsigned int nrKeyColors() { return _nrKeyColors; }

float minimum() { return _minimum; }
void setMinimum(float min) { _minimum = min; }
double minimum() { return _minimum; }
void setMinimum(double min) { _minimum = min; }

float maximum() { return _maximum; }
void setMaximum(float max) { _maximum = max; }
double maximum() { return _maximum; }
void setMaximum(double max) { _maximum = max; }

Crit3DColor* getColor(float myValue);
unsigned int getColorIndex(float myValue);

bool setRange(float minimum, float maximum);
void setRangeBlocked(bool blocked) { _isRangeBlocked = blocked; }
bool isRangeBlocked() { return _isRangeBlocked; }

void setFixedRange(bool fixedRange) { _isFixedRange = fixedRange; }
bool isFixedRange() { return _isFixedRange; }

void setHideOutliers(bool hideOutliers) { _isHideOutliers = hideOutliers; }
bool isHideOutliers() { return _isHideOutliers; }
};

bool setDefaultScale(Crit3DColorScale* myScale);
Expand Down
2 changes: 1 addition & 1 deletion agrolib/gis/gis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ namespace gis
myGrid->maximum = maximum;
myGrid->minimum = minimum;

if (! myGrid->colorScale->isRangeBlocked())
if (! myGrid->colorScale->isFixedRange())
{
myGrid->colorScale->setRange(minimum, maximum);
}
Expand Down
15 changes: 9 additions & 6 deletions agrolib/graphics/colorLegend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ void ColorLegend::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)

if (this->colorScale == nullptr) return;
// check
if (this->colorScale == nullptr)
return;
if (isEqual(this->colorScale->minimum(), NODATA)
|| isEqual(this->colorScale->maximum(), NODATA)) return;
|| isEqual(this->colorScale->maximum(), NODATA))
return;

QPainter painter(this);
Crit3DColor* myColor;
Expand All @@ -36,14 +39,14 @@ void ColorLegend::paintEvent(QPaintEvent *event)
const int BLANK_DX = 16;
int legendWidth = painter.window().width() - BLANK_DX*2;
unsigned int nrStep = this->colorScale->nrColors();
float step = (colorScale->maximum() - colorScale->minimum()) / float(nrStep);
double step = (colorScale->maximum() - colorScale->minimum()) / double(nrStep);
double dx = double(legendWidth) / double(nrStep+1);
unsigned int stepText = MAXVALUE(nrStep / 4, 1);
QString valueStr;
int nrDigits;
double dblValue, shiftFatctor;

float value = this->colorScale->minimum();
double value = this->colorScale->minimum();
for (unsigned int i = 0; i <= nrStep; i++)
{
dblValue = double(value);
Expand All @@ -53,13 +56,13 @@ void ColorLegend::paintEvent(QPaintEvent *event)

if ((i % stepText) == 0)
{
if (isEqual(dblValue, 0))
if (isEqual(dblValue, 0) || isEqual(dblValue, 1))
{
nrDigits = 1;
}
else
{
nrDigits = int(ceil(log10(abs(dblValue))));
nrDigits = abs(ceil(log10(fabs(dblValue))));
}

// negative numbers
Expand Down
4 changes: 2 additions & 2 deletions agrolib/graphics/mapGraphicsRasterObject.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*!
\file mapGraphicsRasterObject.cpp
\abstract draw raster in MapGraphics widget
\abstract draws a lat-lon raster in the MapGraphics widget
This file is part of CRITERIA-3D distribution.
Expand Down Expand Up @@ -381,7 +381,7 @@ bool RasterObject::drawRaster(gis::Crit3DRasterGrid *myRaster, QPainter* myPaint
}

// dynamic color scale
if (! myRaster->colorScale->isRangeBlocked())
if (! myRaster->colorScale->isFixedRange())
{
if (this->isLatLon)
{
Expand Down
16 changes: 12 additions & 4 deletions agrolib/graphics/mapGraphicsRasterUtm.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*!
\file mapGraphicsRasterObject.cpp
\file mapGraphicsRasterUtm.cpp
\abstract draw raster in MapGraphics widget
\abstract draws a UTM raster in the MapGraphics widget
This file is part of CRITERIA-3D distribution.
Expand Down Expand Up @@ -29,6 +29,7 @@
#include "commonConstants.h"
#include "mapGraphicsRasterUtm.h"
#include "basicMath.h"
#include "color.h"

#include <math.h>
#include <QMenu>
Expand Down Expand Up @@ -326,7 +327,7 @@ bool RasterUtmObject::drawRaster(QPainter* painter)
}

// dynamic color scale
if (! _rasterPointer->colorScale->isRangeBlocked())
if (! _rasterPointer->colorScale->isFixedRange())
{
gis::updateColorScale(_rasterPointer, rasterWindow);
roundColorScale(_rasterPointer->colorScale, 4, true);
Expand Down Expand Up @@ -362,10 +363,17 @@ bool RasterUtmObject::drawRaster(QPainter* painter)
// raster value
float value = _rasterPointer->value[rowCenter][colCenter];

// skip the NODATA value
// check NODATA value (transparent)
if (isEqual(value, _rasterPointer->header->flag) || isEqual(value, NODATA))
continue;

// check outliers (transparent)
if (_rasterPointer->colorScale->isHideOutliers())
{
if (value < _rasterPointer->colorScale->minimum() || value > _rasterPointer->colorScale->maximum())
continue;
}

// set color
myColor = _rasterPointer->colorScale->getColor(value);
myQColor = QColor(myColor->red, myColor->green, myColor->blue);
Expand Down
26 changes: 16 additions & 10 deletions bin/CRITERIA3D/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1215,7 +1215,7 @@ void MainWindow::on_actionView_Aspect_triggered()
{
myProject.radiationMaps->aspectMap->colorScale->setMinimum(0);
myProject.radiationMaps->aspectMap->colorScale->setMaximum(360);
myProject.radiationMaps->aspectMap->colorScale->setRangeBlocked(true);
myProject.radiationMaps->aspectMap->colorScale->setFixedRange(true);
setCircolarScale(myProject.radiationMaps->aspectMap->colorScale);
setCurrentRasterOutput(myProject.radiationMaps->aspectMap);
ui->labelOutputRaster->setText("Aspect °");
Expand Down Expand Up @@ -2322,7 +2322,7 @@ void MainWindow::on_actionCriteria3D_run_models_triggered()
}


void MainWindow::showCriteria3DVariable(criteria3DVariable var, int layerIndex, bool isFixedRange, float minimum, float maximum)
void MainWindow::showCriteria3DVariable(criteria3DVariable var, int layerIndex, bool isFixedRange, double minimum, double maximum)
{
if (! myProject.isCriteria3DInitialized)
{
Expand Down Expand Up @@ -2381,12 +2381,18 @@ void MainWindow::showCriteria3DVariable(criteria3DVariable var, int layerIndex,
// set range
if (isFixedRange)
{
myProject.criteria3DMap.colorScale->setRange(minimum, maximum);
myProject.criteria3DMap.colorScale->setRangeBlocked(true);
if (! isEqual(minimum, NODATA))
myProject.criteria3DMap.colorScale->setMinimum(minimum);
if (! isEqual(maximum, NODATA))
myProject.criteria3DMap.colorScale->setMaximum(maximum);

myProject.criteria3DMap.colorScale->setFixedRange(true);
myProject.criteria3DMap.colorScale->setHideOutliers(true);
}
else
{
myProject.criteria3DMap.colorScale->setRangeBlocked(false);
myProject.criteria3DMap.colorScale->setFixedRange(false);
myProject.criteria3DMap.colorScale->setHideOutliers(false);
}

setCurrentRasterOutput(&(myProject.criteria3DMap));
Expand Down Expand Up @@ -3177,7 +3183,7 @@ void MainWindow::on_actionHide_Geomap_triggered()

void MainWindow::on_actionView_surfaceWaterContent_automatic_range_triggered()
{
showCriteria3DVariable(volumetricWaterContent, 0, false, NODATA, NODATA);
showCriteria3DVariable(volumetricWaterContent, 0, true, 0.1, NODATA);
}


Expand Down Expand Up @@ -3216,20 +3222,20 @@ void MainWindow::on_actionView_degreeOfSaturation_automatic_range_triggered()
void MainWindow::on_actionView_degreeOfSaturation_fixed_range_triggered()
{
int layerIndex = ui->layerNrEdit->value();
showCriteria3DVariable(degreeOfSaturation, layerIndex, true, 0.0, 1.0);
showCriteria3DVariable(degreeOfSaturation, layerIndex, true, 0.2, 1.0);
}


void MainWindow::on_actionView_factor_of_safety_triggered()
{
int layerIndex = std::max(1, ui->layerNrEdit->value());
showCriteria3DVariable(factorOfSafety, layerIndex, true, 0, 10);
showCriteria3DVariable(factorOfSafety, layerIndex, true, 0, 4);
}


void MainWindow::on_actionView_factor_of_safety_minimum_triggered()
{
showCriteria3DVariable(minimumFactorOfSafety, NODATA, true, 0, 10);
showCriteria3DVariable(minimumFactorOfSafety, NODATA, true, 0, 4);
}


Expand Down Expand Up @@ -3267,7 +3273,7 @@ void MainWindow::on_layerNrEdit_valueChanged(int layerIndex)

if (view3DVariable && current3DlayerIndex != 0)
{
if (myProject.criteria3DMap.colorScale->isRangeBlocked())
if (myProject.criteria3DMap.colorScale->isFixedRange())
{
showCriteria3DVariable(current3DVariable, layerIndex, true,
myProject.criteria3DMap.colorScale->minimum(),
Expand Down
2 changes: 1 addition & 1 deletion bin/CRITERIA3D/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@

void showMeteoVariable(meteoVariable var);
void showSnowVariable(meteoVariable var);
void showCriteria3DVariable(criteria3DVariable var, int layerIndex, bool isFixedRange, float minimum, float maximum);
void showCriteria3DVariable(criteria3DVariable var, int layerIndex, bool isFixedRange, double minimum, double maximum);

bool setRadiationAsCurrentVariable();
bool startModels(QDateTime firstTime, QDateTime lastTime);
Expand Down

0 comments on commit 79e2245

Please sign in to comment.