From 0f34fd437efb936ef29ac91186321aa7251fbfb1 Mon Sep 17 00:00:00 2001 From: Samuel Fadel Date: Tue, 23 May 2023 11:22:33 +0200 Subject: Massive changes in initial port away from Qt. --- colorscale.cpp | 114 ++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 93 insertions(+), 21 deletions(-) (limited to 'colorscale.cpp') diff --git a/colorscale.cpp b/colorscale.cpp index f5afcbb..32d0f25 100644 --- a/colorscale.cpp +++ b/colorscale.cpp @@ -4,22 +4,94 @@ const float EPSILON = 1e-3f; -ColorScale::ColorScale(const QColor &firstColor, const QColor &lastColor) +Color::Color() + : Color(0, 0, 0, 255) +{ +} + +Color::Color(int r, int g, int b) + : Color(r, g, b, 255) +{ +} + +Color::Color(int r, int g, int b, int a) + : r(r) + , g(g) + , b(b) + , a(a) +{ +} + +Color::Color(float r, float g, float b) + : Color(r, g, b, 0.0f) +{ +} + +Color::Color(float r, float g, float b, float a) + : Color(static_cast(round(255 * r)), + static_cast(round(255 * g)), + static_cast(round(255 * b)), + static_cast(round(255 * a))) +{ +} + +void Color::getRgbF(float *r, float *g, float *b) const +{ + *r = round(static_cast(this->r) / 255.0f); + *g = round(static_cast(this->g) / 255.0f); + *b = round(static_cast(this->b) / 255.0f); +} + +void Color::getRgbF(float *r, float *g, float *b, float *a) const +{ + getRgbF(r, g, b); + *a = round(static_cast(this->a) / 255.0f); +} + +void Color::setRgb(int r, int g, int b) +{ + setRgb(r, g, b, 255); +} + +void Color::setRgb(int r, int g, int b, int a) +{ + this->r = r; + this->g = g; + this->b = b; + this->a = a; +} + +void Color::setRgbF(float r, float g, float b) +{ + setRgb(static_cast(round(255 * r)), + static_cast(round(255 * g)), + static_cast(round(255 * b))); +} + +void Color::setRgbF(float r, float g, float b, float a) +{ + setRgb(static_cast(round(255 * r)), + static_cast(round(255 * g)), + static_cast(round(255 * b)), + static_cast(round(255 * a))); +} + +ColorScale::ColorScale(const Color &firstColor, const Color &lastColor) : m_colors{{firstColor, lastColor}} { - setExtents(0, 1); + setExtents(0.0f, 1.0f); } -ColorScale::ColorScale(std::initializer_list colors) +ColorScale::ColorScale(std::initializer_list colors) : m_colors(colors) { - setExtents(0, 1); + setExtents(0.0f, 1.0f); } -ColorScale::ColorScale(const QList &colors) +ColorScale::ColorScale(const std::vector &colors) : m_colors(colors) { - setExtents(0, 1); + setExtents(0.0f, 1.0f); } ColorScale::~ColorScale() @@ -36,26 +108,26 @@ void ColorScale::setExtents(float min, float max) m_max = max; } -QColor ColorScale::lerp(const QColor &c1, const QColor &c2, float _t) +Color ColorScale::lerp(const Color &c1, const Color &c2, float _t) { - qreal r1, g1, b1, a1; - qreal r2, g2, b2, a2; - qreal t = _t; + float r1, g1, b1, a1; + float r2, g2, b2, a2; + float t = _t; 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); + Color color; + color.setRgbF(r1 * (1.0f - t) + r2 * t, + g1 * (1.0f - t) + g2 * t, + b1 * (1.0f - t) + b2 * t, + a1 * (1.0f - t) + a2 * t); return color; } -QColor ColorScale::color(float t) const +Color ColorScale::color(float t) const { if (t < m_min || t > m_max) { - return QColor(); + return Color(); } // normalize t @@ -63,22 +135,22 @@ QColor ColorScale::color(float t) const // two colors, use a simpler solution if (m_colors.size() == 2) { - return lerp(m_colors.first(), m_colors.last(), t); + return lerp(m_colors.front(), m_colors.back(), t); } if (fabs(t - m_min) < EPSILON) { - return m_colors.first(); + return m_colors.front(); } if (fabs(t - m_max) < EPSILON) { - return m_colors.last(); + return m_colors.back(); } // find which colors in the scale are adjacent to ours int i = int(t * m_colors.size()); int j = i + 1; if (i >= m_colors.size() - 1) { - return QColor(m_colors.last()); + return Color(m_colors.back()); } // normalize t between the two colors -- cgit v1.2.3