From c0c38e72dd588933c4770d564d22e176fd5b7928 Mon Sep 17 00:00:00 2001 From: Samuel Fadel Date: Wed, 31 Aug 2016 09:46:52 -0300 Subject: ColorScale: safer handling of corner cases. --- colorscale.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) 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 + +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; -- cgit v1.2.3