App: handle option 'outside invisible' directly in ColorGradient

This commit is contained in:
wmayer
2022-04-01 08:38:27 +02:00
parent 6f4d0fa1ab
commit 5fe0a63201
2 changed files with 100 additions and 72 deletions

View File

@@ -90,13 +90,13 @@ ColorField::ColorField (const ColorModel &rclModel, float fMin, float fMax, std:
}
ColorField::ColorField (const ColorField &rclCF)
: colorModel(rclCF.colorModel),
fMin(rclCF.fMin),
fMax(rclCF.fMax),
fAscent(rclCF.fAscent),
fConstant(rclCF.fConstant),
ctColors(rclCF.ctColors),
colorField(rclCF.colorField)
: colorModel(rclCF.colorModel)
, fMin(rclCF.fMin)
, fMax(rclCF.fMax)
, fAscent(rclCF.fAscent)
, fConstant(rclCF.fConstant)
, ctColors(rclCF.ctColors)
, colorField(rclCF.colorField)
{
}
@@ -128,15 +128,12 @@ void ColorField::setColorModel (const ColorModel &rclModel)
void ColorField::rebuild ()
{
std::size_t usInd1, usInd2, usStep, i;
colorField.resize(ctColors);
usStep = std::min<std::size_t>(ctColors / (colorModel.getCountColors() - 1), ctColors - 1);
usInd1 = 0;
usInd2 = usStep;
for (i = 0; i < (colorModel.getCountColors() - 1); i++) {
std::size_t usStep = std::min<std::size_t>(ctColors / (colorModel.getCountColors() - 1), ctColors - 1);
std::size_t usInd1 = 0;
std::size_t usInd2 = usStep;
for (std::size_t i = 0; i < (colorModel.getCountColors() - 1); i++) {
interpolate(colorModel.colors[i], usInd1, colorModel.colors[i+1], usInd2);
usInd1 = usInd2;
if ((i + 1) == (colorModel.getCountColors() - 2))
@@ -152,21 +149,19 @@ void ColorField::rebuild ()
// fuellt das Array von Farbe 1, Index 1 bis Farbe 2, Index 2
void ColorField::interpolate (Color clCol1, std::size_t usInd1, Color clCol2, std::size_t usInd2)
{
std::size_t i;
float ucR, ucG, ucB;
float fR, fG, fB, fStep = 1.0f, fLen = float(usInd2 - usInd1);
float fStep = 1.0f, fLen = float(usInd2 - usInd1);
colorField[usInd1] = clCol1;
colorField[usInd2] = clCol2;
fR = (float(clCol2.r) - float(clCol1.r)) / fLen;
fG = (float(clCol2.g) - float(clCol1.g)) / fLen;
fB = (float(clCol2.b) - float(clCol1.b)) / fLen;
float fR = (float(clCol2.r) - float(clCol1.r)) / fLen;
float fG = (float(clCol2.g) - float(clCol1.g)) / fLen;
float fB = (float(clCol2.b) - float(clCol1.b)) / fLen;
for (i = (usInd1 + 1); i < usInd2; i++) {
ucR = clCol1.r + fR * fStep;
ucG = clCol1.g + fG * fStep;
ucB = clCol1.b + fB * fStep;
for (std::size_t i = (usInd1 + 1); i < usInd2; i++) {
float ucR = clCol1.r + fR * fStep;
float ucG = clCol1.g + fG * fStep;
float ucB = clCol1.b + fB * fStep;
colorField[i] = Color(ucR, ucG, ucB);
fStep += 1.0f;
}
@@ -174,23 +169,23 @@ void ColorField::interpolate (Color clCol1, std::size_t usInd1, Color clCol2, st
ColorGradient::ColorGradient ()
: tStyle(ZERO_BASED),
outsideGrayed(false),
tColorModel(0)
: tStyle(ZERO_BASED)
, visibility(Visibility::Default)
, tColorModel(0)
{
createStandardPacks();
setColorModel();
set(-1.0f, 1.0f, 13, ZERO_BASED, false);
set(-1.0f, 1.0f, 13, ZERO_BASED, Visibility::Default);
}
ColorGradient::ColorGradient (float fMin, float fMax, std::size_t usCtColors, TStyle tS, bool bOG)
: tStyle(tS),
outsideGrayed(false),
tColorModel(0)
ColorGradient::ColorGradient (float fMin, float fMax, std::size_t usCtColors, TStyle tS, VisibilityFlags flags)
: tStyle(tS)
, visibility(Visibility::Default)
, tColorModel(0)
{
createStandardPacks();
setColorModel();
set(fMin, fMax, usCtColors, tS, bOG);
set(fMin, fMax, usCtColors, tS, flags);
}
void ColorGradient::createStandardPacks()
@@ -211,7 +206,7 @@ std::vector<std::string> ColorGradient::getColorModelNames() const
return names;
}
void ColorGradient::set (float fMin, float fMax, std::size_t usCt, TStyle tS, bool bOG)
void ColorGradient::set (float fMin, float fMax, std::size_t usCt, TStyle tS, VisibilityFlags flags)
{
auto bounds = std::minmax(fMin, fMax);
if (bounds.second <= bounds.first) {
@@ -222,7 +217,7 @@ void ColorGradient::set (float fMin, float fMax, std::size_t usCt, TStyle tS, bo
_fMax = bounds.second;
ctColors = std::max<std::size_t>(usCt, getMinColors());
tStyle = tS;
outsideGrayed = bOG;
visibility = flags;
rebuild();
}
@@ -298,7 +293,7 @@ void ColorGradient::setColorModel ()
}
ColorLegend::ColorLegend ()
: outsideGrayed(false)
: outsideGrayed(false)
{
// default blue, green, red
colorFields.emplace_back(0, 0, 1);
@@ -415,8 +410,7 @@ std::size_t ColorLegend::addMax (const std::string &rclName)
bool ColorLegend::remove (std::size_t ulPos)
{
if (ulPos < colorFields.size())
{
if (ulPos < colorFields.size()) {
colorFields.erase(colorFields.begin() + ulPos);
names.erase(names.begin() + ulPos);
values.erase(values.begin() + ulPos);
@@ -429,8 +423,7 @@ bool ColorLegend::remove (std::size_t ulPos)
void ColorLegend::removeFirst ()
{
if (colorFields.size() > 0)
{
if (colorFields.size() > 0) {
colorFields.erase(colorFields.begin());
names.erase(names.begin());
values.erase(values.begin());
@@ -439,8 +432,7 @@ void ColorLegend::removeFirst ()
void ColorLegend::removeLast ()
{
if (colorFields.size() > 0)
{
if (colorFields.size() > 0) {
colorFields.erase(colorFields.end()-1);
names.erase(names.end()-1);
values.erase(values.end()-1);
@@ -452,14 +444,12 @@ void ColorLegend::resize (std::size_t ulCt)
if ((ulCt < 2) || (ulCt == colorFields.size()))
return;
if (ulCt > colorFields.size())
{
if (ulCt > colorFields.size()) {
int k = ulCt - colorFields.size();
for (int i = 0; i < k; i++)
addMin("new");
}
else
{
else {
int k = colorFields.size() - ulCt;
for (int i = 0; i < k; i++)
removeLast();
@@ -468,13 +458,12 @@ void ColorLegend::resize (std::size_t ulCt)
bool ColorLegend::setColor (std::size_t ulPos, float ucRed, float ucGreen, float ucBlue)
{
if (ulPos < names.size())
{
if (ulPos < names.size()) {
colorFields[ulPos] = Color(ucRed, ucGreen, ucBlue);
return true;
}
else
return false;
return false;
}
// color as 0x00rrggbb
@@ -488,8 +477,7 @@ bool ColorLegend::setColor (std::size_t ulPos, unsigned long ulColor)
bool ColorLegend::setText (std::size_t ulPos, const std::string &rclName)
{
if (ulPos < names.size())
{
if (ulPos < names.size()) {
names[ulPos] = rclName;
return true;
}

View File

@@ -25,6 +25,7 @@
#define APP_COLORMODEL_H
#include "Material.h"
#include <Base/Bitmask.h>
#include <algorithm>
#include <deque>
@@ -32,6 +33,21 @@
#include <vector>
namespace App
{
enum class Visibility {
Default,
Grayed,
Invisible
};
using VisibilityFlags = Base::Flags<Visibility>;
}
ENABLE_BITMASK_OPERATORS(App::Visibility)
namespace App
{
@@ -62,7 +78,7 @@ public:
std::size_t getCountColors() const {
return colors.size();
}
std::vector<Color> colors;
std::vector<Color> colors;
};
class AppExport ColorModelBlueGreenRed : public ColorModel
@@ -301,7 +317,7 @@ inline Color ColorField::getColor (float fVal) const
float r = (float)(i+1)/(float)ct;
if (t < r) {
// calculate the exact position in the subrange
float s = t * (float)ct - (float)i;
float s = t * float(ct) - float(i);
Color c1 = colorModel.colors[i];
Color c2 = colorModel.colors[i+1];
col.r = (1.0f-s) * c1.r + s * c2.r;
@@ -316,7 +332,7 @@ inline Color ColorField::getColor (float fVal) const
inline std::size_t ColorField::getColorIndex (float fVal) const
{
return (std::size_t)(std::min<int>(std::max<int>(int(fConstant + fAscent * fVal), 0), int(ctColors - 1)));
return std::size_t(std::min<int>(std::max<int>(int(fConstant + fAscent * fVal), 0), int(ctColors - 1)));
}
@@ -326,35 +342,44 @@ public:
enum TStyle { FLOW, ZERO_BASED };
ColorGradient ();
ColorGradient (float fMin, float fMax, std::size_t usCtColors, TStyle tS, bool bOG = false);
ColorGradient (float fMin, float fMax, std::size_t usCtColors, TStyle tS, VisibilityFlags fl = Visibility::Default);
ColorGradient (const ColorGradient &) = default;
ColorGradient& operator = (const ColorGradient &) = default;
void set (float fMin, float fMax, std::size_t usCt, TStyle tS, bool bOG);
void set (float fMin, float fMax, std::size_t usCt, TStyle tS, VisibilityFlags fl);
void setRange (float fMin, float fMax) {
set(fMin, fMax, ctColors, tStyle, outsideGrayed);
set(fMin, fMax, ctColors, tStyle, visibility);
}
void getRange (float &rfMin, float &rfMax) const {
rfMin = _fMin; rfMax = _fMax;
}
bool isOutOfRange(float fVal) const {
return ((fVal < _fMin) || (fVal > _fMax));
}
std::size_t getCountColors () const {
return ctColors;
}
void setCountColors (std::size_t usCt) {
set(_fMin, _fMax, usCt, tStyle, outsideGrayed);
set(_fMin, _fMax, usCt, tStyle, visibility);
}
void setStyle (TStyle tS) {
set(_fMin, _fMax, ctColors, tS, outsideGrayed);
set(_fMin, _fMax, ctColors, tS, visibility);
}
std::size_t getMinColors () const;
TStyle getStyle () const {
return tStyle;
}
void setOutsideGrayed (bool bGrayed) {
outsideGrayed = bGrayed;
void setOutsideGrayed (bool value) {
visibility.setFlag(Visibility::Grayed, value);
}
bool isOutsideGrayed () const {
return outsideGrayed;
return visibility.testFlag(Visibility::Grayed);
}
void setOutsideInvisible (bool value) {
visibility.setFlag(Visibility::Invisible, value);
}
bool isOutsideInvisible () const {
return visibility.testFlag(Visibility::Invisible);
}
void setColorModel (std::size_t tModel);
std::size_t getColorModelType () const {
@@ -372,6 +397,9 @@ public:
inline Color getColor (float fVal) const;
inline std::size_t getColorIndex (float fVal) const;
private:
inline Color _getColor (float fVal) const;
protected:
void createStandardPacks();
@@ -380,7 +408,7 @@ protected:
TStyle tStyle;
float _fMin, _fMax;
std::size_t ctColors;
bool outsideGrayed;
VisibilityFlags visibility;
std::size_t tColorModel;
ColorModelPack currentModelPack;
std::vector<ColorModelPack> modelPacks;
@@ -491,8 +519,19 @@ inline float ColorLegend::getMaxValue () const
inline Color ColorGradient::getColor (float fVal) const
{
if (outsideGrayed) {
if ((fVal < _fMin) || (fVal > _fMax))
Color color = _getColor(fVal);
if (isOutsideInvisible()) {
if (isOutOfRange(fVal))
color.a = 0.8f;
}
return color;
}
inline Color ColorGradient::_getColor (float fVal) const
{
if (isOutsideGrayed()) {
if (isOutOfRange(fVal))
return Color(0.5f, 0.5f, 0.5f);
}
@@ -505,8 +544,9 @@ inline Color ColorGradient::getColor (float fVal) const
else
return colorField2.getColor(fVal);
}
else
else {
return colorField1.getColor(fVal);
}
}
default:
@@ -526,10 +566,11 @@ inline std::size_t ColorGradient::getColorIndex (float fVal) const
if (fVal < 0.0f)
return colorField1.getColorIndex(fVal);
else
return (std::size_t)(colorField1.getCountColors() + colorField2.getColorIndex(fVal));
return std::size_t(colorField1.getCountColors() + colorField2.getColorIndex(fVal));
}
else
else {
return colorField1.getColorIndex(fVal);
}
}
default:
@@ -550,9 +591,8 @@ inline const ColorModel& ColorGradient::getColorModel () const
else
return currentModelPack.totalModel;
}
else {
return currentModelPack.totalModel;
}
return currentModelPack.totalModel;
}
} // namespace App