aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSamuel Fadel <samuelfadel@gmail.com>2016-01-17 20:50:07 +0100
committerSamuel Fadel <samuelfadel@gmail.com>2016-01-17 20:50:07 +0100
commit1b75342fdec4f58aecb35869588a247e9ddc066a (patch)
treec129e44dab5652dcbeaf75a341658d902a9b8a14
parent70fe1f77ec22790ab4bd8ac92a28ceadc41585ae (diff)
ColorScale: minor update to color sampling and generation algorithms.
-rw-r--r--colorscale.cpp5
-rw-r--r--colorscale.h9
2 files changed, 7 insertions, 7 deletions
diff --git a/colorscale.cpp b/colorscale.cpp
index e4e2063..38898c3 100644
--- a/colorscale.cpp
+++ b/colorscale.cpp
@@ -63,15 +63,14 @@ QColor ColorScale::color(float t) const
}
// find which colors in the scale are adjacent to ours
- float step = 1.0 / m_colors.size();
- int i = int(t / step);
+ int i = int(t * m_colors.size());
int j = i + 1;
-
if (i >= m_colors.size() - 1) {
return QColor(m_colors.last());
}
// normalize t between the two colors
+ float step = 1.0f / m_colors.size();
t = (t - i*step) / (j*step - i*step);
return lerp(m_colors[i], m_colors[j], t);
}
diff --git a/colorscale.h b/colorscale.h
index 1be315f..5974cef 100644
--- a/colorscale.h
+++ b/colorscale.h
@@ -19,6 +19,7 @@ public:
void setExtents(float min, float max);
float min() const { return m_min; }
float max() const { return m_max; }
+ int numColors() const { return m_colors.size(); }
template<typename OutputIterator>
void sample(int samples, OutputIterator it) const;
@@ -37,11 +38,11 @@ void ColorScale::sample(int samples, OutputIterator it) const
float step = (max() - min()) / samples;
qreal r, g, b;
- for (float t = min(); t < max(); t += step) {
+ for (float t = min(); samples-- > 0; t += step) {
color(t).getRgbF(&r, &g, &b);
- *it = r; it++;
- *it = g; it++;
- *it = b; it++;
+ *it++ = r;
+ *it++ = g;
+ *it++ = b;
}
}