aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSamuel Fadel <samuelfadel@gmail.com>2016-08-31 09:46:52 -0300
committerSamuel Fadel <samuelfadel@gmail.com>2016-08-31 09:46:52 -0300
commitc0c38e72dd588933c4770d564d22e176fd5b7928 (patch)
tree7b6d03ffdc13d128799c6e139d510e22cc7cf503
parent5d80eef7933cf65c9d68b9d3e9259bc9598c2bd5 (diff)
ColorScale: safer handling of corner cases.
-rw-r--r--colorscale.cpp12
1 files changed, 12 insertions, 0 deletions
diff --git a/colorscale.cpp b/colorscale.cpp
index f41e7d9..f5afcbb 100644
--- a/colorscale.cpp
+++ b/colorscale.cpp
@@ -1,5 +1,9 @@
#include "colorscale.h"
+#include <cmath>
+
+const float EPSILON = 1e-3f;
+
ColorScale::ColorScale(const QColor &firstColor, const QColor &lastColor)
: m_colors{{firstColor, lastColor}}
{
@@ -62,6 +66,14 @@ QColor ColorScale::color(float t) const
return lerp(m_colors.first(), m_colors.last(), t);
}
+ if (fabs(t - m_min) < EPSILON) {
+ return m_colors.first();
+ }
+
+ if (fabs(t - m_max) < EPSILON) {
+ return m_colors.last();
+ }
+
// find which colors in the scale are adjacent to ours
int i = int(t * m_colors.size());
int j = i + 1;