summaryrefslogtreecommitdiff
path: root/include/SSVOpenHexagon/Utils/Color.hpp
blob: 4d01fbb9b8a36f14bd5b1aee82b07378be7503bc (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
// Copyright (c) 2013-2020 Vittorio Romeo
// License: Academic Free License ("AFL") v. 3.0
// AFL License page: https://opensource.org/licenses/AFL-3.0

#pragma once

#include "SSVOpenHexagon/Global/Assert.hpp"

#include <SFML/Graphics/Color.hpp>
#include <SFML/Graphics/Glsl.hpp>

#include <cmath>

namespace hg::Utils {

[[nodiscard, gnu::always_inline, gnu::pure]] inline sf::Color getColorDarkened(
    sf::Color mColor, const float mMultiplier) noexcept
{
    SSVOH_ASSERT(mMultiplier != 0.f);

    mColor.r = static_cast<float>(mColor.r) / mMultiplier;
    mColor.b = static_cast<float>(mColor.b) / mMultiplier;
    mColor.g = static_cast<float>(mColor.g) / mMultiplier;

    return mColor;
}

[[nodiscard, gnu::always_inline, gnu::pure]] inline sf::Color transformHue(
    const sf::Color& in, const float H) noexcept
{
    const float u{std::cos(H * 3.14f / 180.f)};
    const float w{std::sin(H * 3.14f / 180.f)};

    return sf::Color{
        //
        static_cast<sf::Uint8>((.701 * u + .168 * w) * in.r +
                               (-.587 * u + .330 * w) * in.g +
                               (-.114 * u - .497 * w) * in.b),

        static_cast<sf::Uint8>((-.299 * u - .328 * w) * in.r +
                               (.413 * u + .035 * w) * in.g +
                               (-.114 * u + .292 * w) * in.b),

        static_cast<sf::Uint8>((-.3 * u + 1.25 * w) * in.r +
                               (-.588 * u - 1.05 * w) * in.g +
                               (.886 * u - .203 * w) * in.b),

        static_cast<sf::Uint8>(255) //
    };
}

[[nodiscard, gnu::always_inline, gnu::pure]] inline sf::Color getColorFromHue(
    const float hue) noexcept
{
    SSVOH_ASSERT(hue >= 0.f && hue <= 1.f);

    const int i = std::floor(hue * 6.f);

    const float f = (hue * 6.f) - i;
    const float q = 1.f - f;
    const float t = f;

    const auto ret = [](const float r, const float g, const float b)
    { return sf::Color(r * 255.f, g * 255.f, b * 255.f); };

    switch(i)
    {
        case 0: return ret(1.f, t, 0.f);
        case 1: return ret(q, 1.f, 0.f);
        case 2: return ret(0.f, 1.f, t);
        case 3: return ret(0.f, q, 1.f);
        case 4: return ret(t, 0.f, 1.f);
    }

    return ret(1.f, 0.f, q);
}

[[nodiscard, gnu::always_inline, gnu::pure]] inline constexpr sf::Uint8
componentClamp(const float value) noexcept
{
    if(value > 255.f)
    {
        return sf::Uint8(255);
    }

    if(value < 0.f)
    {
        return sf::Uint8(0);
    }

    return static_cast<sf::Uint8>(value);
}

[[nodiscard, gnu::always_inline, gnu::pure]] inline sf::Glsl::Vec3 toGLSLVec3(
    const sf::Color& color) noexcept
{
    return {//
        static_cast<float>(color.r) / 255.f,
        static_cast<float>(color.g) / 255.f,
        static_cast<float>(color.b) / 255.f};
}

[[nodiscard, gnu::always_inline, gnu::pure]] inline sf::Glsl::Vec4 toGLSLVec4(
    const sf::Color& color) noexcept
{
    return {//
        static_cast<float>(color.r) / 255.f,
        static_cast<float>(color.g) / 255.f,
        static_cast<float>(color.b) / 255.f,
        static_cast<float>(color.a) / 255.f};
}

} // namespace hg::Utils