From a96f9f1a2688c215c478cfbee5748b4bb2043a43 Mon Sep 17 00:00:00 2001 From: Samuel Fadel Date: Mon, 18 May 2015 18:33:50 -0300 Subject: 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. --- colorscale.cpp | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 colorscale.cpp (limited to 'colorscale.cpp') 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 colors) + : m_colors(colors) +{ + setExtents(0, 1); +} + +ColorScale::ColorScale(const QList &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); +} -- cgit v1.2.3