aboutsummaryrefslogtreecommitdiff
path: root/colorscale.cpp
diff options
context:
space:
mode:
authorSamuel Fadel <samuelfadel@gmail.com>2015-05-18 18:33:50 -0300
committerSamuel Fadel <samuelfadel@gmail.com>2015-05-21 18:09:02 -0300
commita96f9f1a2688c215c478cfbee5748b4bb2043a43 (patch)
tree0f46c89bc7eaa08994faa1095b480baaa1f64c72 /colorscale.cpp
parent54571b4a4dcc076923325ee09ad348f389fc25a5 (diff)
Updated UI.
- Removed unnecessary UI elements from QML file; - Added the ColorScale class and implemented glyph color mapping from class labels; - Mark geometry nodes of individual glyphs as dirty when updating the scene graph.
Diffstat (limited to 'colorscale.cpp')
-rw-r--r--colorscale.cpp72
1 files changed, 72 insertions, 0 deletions
diff --git a/colorscale.cpp b/colorscale.cpp
new file mode 100644
index 0000000..50fedae
--- /dev/null
+++ b/colorscale.cpp
@@ -0,0 +1,72 @@
+#include "colorscale.h"
+
+ColorScale::ColorScale(const QColor &firstColor, const QColor &lastColor)
+ : m_colors{firstColor, lastColor}
+{
+ setExtents(0, 1);
+}
+
+ColorScale::ColorScale(std::initializer_list<QColor> colors)
+ : m_colors(colors)
+{
+ setExtents(0, 1);
+}
+
+ColorScale::ColorScale(const QList<QColor> &colors)
+ : m_colors(colors)
+{
+ setExtents(0, 1);
+}
+
+ColorScale::~ColorScale()
+{
+}
+
+void ColorScale::setExtents(qreal min, qreal max)
+{
+ if (min >= max)
+ return;
+
+ m_min = min;
+ m_max = max;
+}
+
+static QColor lerp(const QColor &c1, const QColor &c2, qreal t)
+{
+ qreal r1, g1, b1, a1;
+ qreal r2, g2, b2, a2;
+
+ c1.getRgbF(&r1, &g1, &b1, &a1);
+ c2.getRgbF(&r2, &g2, &b2, &a2);
+ QColor color;
+ color.setRgbF(r1 * (1 - t) + r2 * t,
+ g1 * (1 - t) + g2 * t,
+ b1 * (1 - t) + b2 * t,
+ a1 * (1 - t) + a2 * t);
+ return color;
+}
+
+QColor ColorScale::color(qreal t) const
+{
+ if (t < m_min || t > m_max)
+ return QColor();
+
+ // normalize t
+ t = (t - m_min) / (m_max - m_min);
+
+ // two colors, use a simpler solution
+ if (m_colors.size() == 2)
+ return lerp(m_colors.first(), m_colors.last(), t);
+
+ // find which colors in the scale are adjacent to ours
+ qreal step = 1.0 / m_colors.size();
+ int i = (int) (t / step);
+ int j = i + 1;
+
+ if (i >= m_colors.size() - 1)
+ return QColor(m_colors.last());
+
+ // normalize t between the two colors
+ t = (t - i*step) / (j*step - i*step);
+ return lerp(m_colors[i], m_colors[j], t);
+}