HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
color.hpp
1// Copyright Take Vos 2021.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "../rapid/numeric_array.hpp"
8
9namespace tt {
10
36class color {
37public:
38 constexpr color(color const &) noexcept = default;
39 constexpr color(color &&) noexcept = default;
40 constexpr color &operator=(color const &) noexcept = default;
41 constexpr color &operator=(color &&) noexcept = default;
42
43 [[nodiscard]] constexpr explicit color(f32x4 other) noexcept : _v(std::move(other))
44 {
45 tt_axiom(is_valid());
46 }
47 [[nodiscard]] constexpr explicit operator f32x4() const noexcept
48 {
49 return _v;
50 }
51
52 [[nodiscard]] constexpr color() noexcept : _v(0.0, 0.0, 0.0, 1.0) {}
53 [[nodiscard]] constexpr color(float r, float g, float b, float a = 1.0) noexcept : _v(r, g, b, a) {}
54 [[nodiscard]] constexpr color(double r, double g, double b, double a = 1.0) noexcept :
55 _v(static_cast<float>(r), static_cast<float>(g), static_cast<float>(b), static_cast<float>(a))
56 {
57 }
58
59 [[nodiscard]] static constexpr color transparent() noexcept
60 {
61 return {0.0f, 0.0f, 0.0f, 0.0f};
62 }
63
64 [[nodiscard]] static constexpr color white() noexcept
65 {
66 return {1.0f, 1.0f, 1.0f, 1.0f};
67 }
68
69 [[nodiscard]] static constexpr color black() noexcept
70 {
71 return {0.0f, 0.0f, 0.0f, 1.0f};
72 }
73
74 [[nodiscard]] constexpr float &r() noexcept
75 {
76 return _v.x();
77 }
78
79 [[nodiscard]] constexpr float &g() noexcept
80 {
81 return _v.y();
82 }
83
84 [[nodiscard]] constexpr float &b() noexcept
85 {
86 return _v.z();
87 }
88
89 [[nodiscard]] constexpr float &a() noexcept
90 {
91 return _v.w();
92 }
93
94 [[nodiscard]] constexpr float const &r() const noexcept
95 {
96 return _v.x();
97 }
98
99 [[nodiscard]] constexpr float const &g() const noexcept
100 {
101 return _v.y();
102 }
103
104 [[nodiscard]] constexpr float const &b() const noexcept
105 {
106 return _v.z();
107 }
108
109 [[nodiscard]] constexpr float const &a() const noexcept
110 {
111 return _v.w();
112 }
113
114 [[nodiscard]] constexpr bool is_valid() const noexcept
115 {
116 return _v.w() >= 0.0 && _v.w() <= 1.0;
117 }
118
119 [[nodiscard]] constexpr friend bool operator==(color const &lhs, color const &rhs) noexcept
120 {
121 return lhs._v == rhs._v;
122 }
123
124 [[nodiscard]] constexpr friend color operator*(color const &lhs, color const &rhs) noexcept
125 {
126 return color{lhs._v * rhs._v};
127 }
128
129 [[nodiscard]] constexpr friend color composit(color const &lhs, color const &rhs) noexcept
130 {
131 return color{composit(lhs._v, rhs._v)};
132 }
133
134 [[nodiscard]] constexpr friend color pre_multiply_alpha(color const &rhs) noexcept
135 {
136 auto r = rhs._v * rhs.a();
137 r.a() = rhs.a();
138 return color{r};
139 }
140
141private:
142 f32x4 _v;
143};
144
145} // namespace tt
This is a RGBA floating point color.
Definition color.hpp:36
T move(T... args)