diff options
Diffstat (limited to 'continuouscolorscale.cpp')
-rw-r--r-- | continuouscolorscale.cpp | 16 |
1 files changed, 8 insertions, 8 deletions
diff --git a/continuouscolorscale.cpp b/continuouscolorscale.cpp index c729c6b..b7f2c93 100644 --- a/continuouscolorscale.cpp +++ b/continuouscolorscale.cpp @@ -9,8 +9,12 @@ ContinuousColorScale::ContinuousColorScale(std::initializer_list<QColor> colors) QColor ContinuousColorScale::color(float t) const { - if (t < m_min || t > m_max) { - return Color(); + if (t <= m_min) { + return m_colors.front(); + } + + if (t >= m_max) { + return m_colors.back(); } // normalize t @@ -18,14 +22,10 @@ QColor ContinuousColorScale::color(float t) const // find which colors in the scale are adjacent to ours float step = 1.0 / m_colors.size(); - int i = static_cast<int>(t / step); - - if (i >= m_colors.size() - 1) { - return m_colors.back(); - } + size_t i = static_cast<size_t>(t * m_colors.size()); // normalize t between the two colors - int j = i + 1; + size_t j = i + 1; t = (t - i*step) / (j*step - i*step); return m_colors[i + round(t)]; } |