summaryrefslogtreecommitdiff
path: root/src/mbgl/util/color.cpp
blob: 55f1b654365408b8a1e7a368236bcbcd49c588b2 (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
#include <mbgl/util/color.hpp>
#include <mbgl/util/string.hpp>

#include <csscolorparser/csscolorparser.hpp>

namespace mbgl {

optional<Color> Color::parse(const std::string& s) {
    auto css_color = CSSColorParser::parse(s);

    // Premultiply the color.
    if (css_color) {
        const float factor = css_color->a / 255;
        return {{
            css_color->r * factor,
            css_color->g * factor,
            css_color->b * factor,
            css_color->a
        }};
    } else {
        return {};
    }
}

std::string Color::stringify() const {
    std::array<double, 4> array = toArray();
    return "rgba(" +
        util::toString(array[0]) + "," +
        util::toString(array[1]) + "," +
        util::toString(array[2]) + "," +
        util::toString(array[3]) + ")";
}

std::array<double, 4> Color::toArray() const {
    if (a == 0) {
        return {{ 0, 0, 0, 0 }};
    } else {
        return {{
            r * 255 / a,
            g * 255 / a,
            b * 255 / a,
            a,
        }};
    }
}

} // namespace mbgl