aboutsummaryrefslogtreecommitdiff
path: root/colorscale.cpp
blob: 32d0f25bd7de3b92170f1b84dd2920f71fdd482d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "colorscale.h"

#include <cmath>

const float EPSILON = 1e-3f;

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<int>(round(255 * r)),
            static_cast<int>(round(255 * g)),
            static_cast<int>(round(255 * b)),
            static_cast<int>(round(255 * a)))
{
}

void Color::getRgbF(float *r, float *g, float *b) const
{
    *r = round(static_cast<float>(this->r) / 255.0f);
    *g = round(static_cast<float>(this->g) / 255.0f);
    *b = round(static_cast<float>(this->b) / 255.0f);
}

void Color::getRgbF(float *r, float *g, float *b, float *a) const
{
    getRgbF(r, g, b);
    *a = round(static_cast<float>(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<int>(round(255 * r)),
           static_cast<int>(round(255 * g)),
           static_cast<int>(round(255 * b)));
}

void Color::setRgbF(float r, float g, float b, float a)
{
    setRgb(static_cast<int>(round(255 * r)),
           static_cast<int>(round(255 * g)),
           static_cast<int>(round(255 * b)),
           static_cast<int>(round(255 * a)));
}

ColorScale::ColorScale(const Color &firstColor, const Color &lastColor)
    : m_colors{{firstColor, lastColor}}
{
    setExtents(0.0f, 1.0f);
}

ColorScale::ColorScale(std::initializer_list<Color> colors)
    : m_colors(colors)
{
    setExtents(0.0f, 1.0f);
}

ColorScale::ColorScale(const std::vector<Color> &colors)
    : m_colors(colors)
{
    setExtents(0.0f, 1.0f);
}

ColorScale::~ColorScale()
{
}

void ColorScale::setExtents(float min, float max)
{
    if (min >= max) {
        return;
    }

    m_min = min;
    m_max = max;
}

Color ColorScale::lerp(const Color &c1, const Color &c2, float _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);
    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;
}

Color ColorScale::color(float t) const
{
    if (t < m_min || t > m_max) {
        return Color();
    }

    // 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.front(), m_colors.back(), t);
    }

    if (fabs(t - m_min) < EPSILON) {
        return m_colors.front();
    }

    if (fabs(t - m_max) < EPSILON) {
        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 Color(m_colors.back());
    }

    // 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);
}