Skip to content
Snippets Groups Projects
Commit 25a8ef72 authored by Martin Heistermann's avatar Martin Heistermann
Browse files

Refactor IColorColor and fix 'map outside range to alpha 0'.

The map-outside-range-to-alpha-0 option was broken for OM double props,
Instead of adding to the awful amount of duplicated code, I moved
clamping/alpha logic to the colorcode.
parent 5f358d43
Branches
Tags
No related merge requests found
......@@ -88,15 +88,15 @@ void ColorCoder::set_range(float _min, float _max, bool _signed)
}
/// color coding
ACG::Vec4uc ColorCoder::color4(float _v) const
ACG::Vec4uc ColorCoder::color4_raw(float _v) const
{
return signed_mode_ ? color_signed(_v) : color_unsigned(_v);
}
/// color coding
ACG::Vec4f ColorCoder::color_float4(float _v) const
ACG::Vec4f ColorCoder::color_float4_raw(float _v) const
{
ACG::Vec4uc c = color4(_v);
ACG::Vec4uc c = color4_raw(_v);
return (ACG::Vec4f(c[0], c[1], c[2], c[3]) / 255.f);
}
......
......@@ -83,10 +83,10 @@ public:
void set_range(float _min, float _max, bool _signed);
/// color coding
ACG::Vec4uc color4(float _v) const override;
ACG::Vec4uc color4_raw(float _v) const override;
/// color coding
ACG::Vec4f color_float4(float _v) const override;
ACG::Vec4f color_float4_raw(float _v) const override;
\
/// min scalar value
float min() const override;
......
......@@ -45,15 +45,32 @@
#include <ACG/Math/VectorT.hh>
#include <ACG/Config/ACGDefines.hh>
#include <QColor>
#include <algorithm>
namespace {
// With C++17, we can use std::clamp() instead
template<class T>
const T clamp( const T v, const T lo, const T hi)
{
if (v < lo)
return lo;
if (v > hi)
return hi;
return v;
}
}
namespace ACG {
class ACGDLLEXPORT IColorCoder {
public:
virtual ~IColorCoder() = default;
virtual ACG::Vec4uc color4(float _v) const = 0;
virtual ACG::Vec4f color_float4(float _v) const = 0;
// "raw" refers to directly using the value,
// assuming it to be inside [min(),max()] without any checks,
// those are implemented in color4() and color_float4()
virtual ACG::Vec4uc color4_raw(float _v) const = 0;
virtual ACG::Vec4f color_float4_raw(float _v) const = 0;
/// min scalar value
virtual float min() const = 0;
......@@ -61,6 +78,25 @@ public:
/// max scalar value
virtual float max() const = 0;
virtual ACG::Vec4uc color4(float _v) const
{
auto clamped = clamp(_v, min(), max());
auto col = color4_raw(clamped);
if (mapOutsideRangeToAlpha0_ && clamped != _v) {
col[3] = 0;
}
return col;
}
virtual ACG::Vec4f color_float4(float _v) const {
auto clamped = clamp(_v, min(), max());
auto col = color_float4_raw(clamped);
if (mapOutsideRangeToAlpha0_ && clamped != _v) {
col[3] = 0.0f;
}
return col;
}
inline ACG::Vec3uc color(float _v) const {
ACG::Vec4uc c = color4(_v);
return ACG::Vec3uc(c[0], c[1], c[2]);
......@@ -82,8 +118,25 @@ public:
inline ACG::Vec4f operator() (float _v) const {
return color_float4(_v);
}
void setMapOutsideRangeToAlpha0(bool value)
{
mapOutsideRangeToAlpha0_ = value;
}
bool getMapOutsideRangeToAlpha0() const
{
return mapOutsideRangeToAlpha0_;
}
protected:
bool mapOutsideRangeToAlpha0_ = false;
};
} // namespace ACG
#endif // ICOLORCODER_HH
......@@ -43,9 +43,9 @@
namespace ACG {
ACG::Vec4uc LinearTwoColorCoder::color4(float _v) const
ACG::Vec4uc LinearTwoColorCoder::color4_raw(float _v) const
{
ACG::Vec4f c = color_float4(_v);
ACG::Vec4f c = color_float4_raw(_v);
return ACG::Vec4uc(
static_cast<unsigned char>(c[0] * 255 + 0.5),
static_cast<unsigned char>(c[1] * 255 + 0.5),
......@@ -53,7 +53,7 @@ ACG::Vec4uc LinearTwoColorCoder::color4(float _v) const
static_cast<unsigned char>(c[3] * 255 + 0.5));
}
ACG::Vec4f LinearTwoColorCoder::color_float4(float _v) const
ACG::Vec4f LinearTwoColorCoder::color_float4_raw(float _v) const
{
return (1 - _v) * minColor_ + _v * maxColor_;
}
......
......@@ -53,8 +53,8 @@ public:
maxColor_(maxColor)
{}
ACG::Vec4uc color4(float _v) const override;
ACG::Vec4f color_float4(float _v) const override;
ACG::Vec4uc color4_raw(float _v) const override;
ACG::Vec4f color_float4_raw(float _v) const override;
float min() const override { return 0.0; }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment