HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
color.hpp
Go to the documentation of this file.
1// Copyright Take Vos 2021-2022.
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
9#pragma once
10
11#include "semantic_color.hpp"
12#include "../SIMD/module.hpp"
13#include "../geometry/module.hpp"
14#include "../utility/module.hpp"
15
16namespace hi { inline namespace v1 {
17
42class color {
43public:
44 constexpr color(color const&) noexcept = default;
45 constexpr color(color&&) noexcept = default;
46 constexpr color& operator=(color const&) noexcept = default;
47 constexpr color& operator=(color&&) noexcept = default;
48
49 [[nodiscard]] constexpr explicit color(f16x4 const& other) noexcept : _v(other)
50 {
51 hi_axiom(holds_invariant());
52 }
53
54 [[nodiscard]] constexpr explicit color(f32x4 const& other) noexcept : color(static_cast<f16x4>(other)) {}
55
56 [[nodiscard]] constexpr explicit operator f16x4() const noexcept
57 {
58 return _v;
59 }
60
61 [[nodiscard]] constexpr explicit operator f32x4() const noexcept
62 {
63 return static_cast<f32x4>(_v);
64 }
65
66 [[nodiscard]] constexpr color(float r, float g, float b, float a = 1.0f) noexcept : color(f32x4{r, g, b, a}) {}
67
68 [[nodiscard]] constexpr color() noexcept : color(f32x4{}) {}
69
70 [[nodiscard]] constexpr explicit color(hi::semantic_color semantic_color, float alpha = 1.0f) noexcept : _v()
71 {
72 _v.x() = float16::from_uint16_t(0xf900 + static_cast<uint8_t>(semantic_color));
73 _v.y() = float16::from_uint16_t(0x0000);
74 _v.z() = float16::from_uint16_t(0x0000);
75 _v.w() = float16(1.0f);
76 }
77
78 [[nodiscard]] constexpr bool is_semantic() const noexcept
79 {
80 return (_v.x().get() & 0xf900) == 0xf900;
81 }
82
83 constexpr explicit operator semantic_color() const noexcept
84 {
85 hi_axiom(is_semantic());
86 return static_cast<semantic_color>(_v.x().get() & 0xff);
87 }
88
89 // clang-format off
90 [[nodiscard]] static constexpr color blue() noexcept { return color{semantic_color::blue}; }
91 [[nodiscard]] static constexpr color green() noexcept { return color{semantic_color::green}; }
92 [[nodiscard]] static constexpr color indigo() noexcept { return color{semantic_color::indigo}; }
93 [[nodiscard]] static constexpr color orange() noexcept { return color{semantic_color::orange}; }
94 [[nodiscard]] static constexpr color pink() noexcept { return color{semantic_color::pink}; }
95 [[nodiscard]] static constexpr color purple() noexcept { return color{semantic_color::purple}; }
96 [[nodiscard]] static constexpr color red() noexcept { return color{semantic_color::red}; }
97 [[nodiscard]] static constexpr color teal() noexcept { return color{semantic_color::teal}; }
98 [[nodiscard]] static constexpr color yellow() noexcept { return color{semantic_color::yellow}; }
99 [[nodiscard]] static constexpr color gray() noexcept { return color{semantic_color::gray}; }
100 [[nodiscard]] static constexpr color gray2() noexcept { return color{semantic_color::gray2}; }
101 [[nodiscard]] static constexpr color gray3() noexcept { return color{semantic_color::gray3}; }
102 [[nodiscard]] static constexpr color gray4() noexcept { return color{semantic_color::gray4}; }
103 [[nodiscard]] static constexpr color gray5() noexcept { return color{semantic_color::gray5}; }
104 [[nodiscard]] static constexpr color gray6() noexcept { return color{semantic_color::gray6}; }
105 [[nodiscard]] static constexpr color foreground() noexcept { return color{semantic_color::foreground}; }
106 [[nodiscard]] static constexpr color border() noexcept { return color{semantic_color::border}; }
107 [[nodiscard]] static constexpr color fill() noexcept { return color{semantic_color::fill}; }
108 [[nodiscard]] static constexpr color accent() noexcept { return color{semantic_color::accent}; }
109 [[nodiscard]] static constexpr color text_select() noexcept { return color{semantic_color::text_select}; }
110 [[nodiscard]] static constexpr color primary_cursor() noexcept { return color{semantic_color::primary_cursor}; }
111 [[nodiscard]] static constexpr color secondary_cursor() noexcept { return color{semantic_color::secondary_cursor}; }
112 // clang-format on
113
114 [[nodiscard]] static constexpr color transparent() noexcept
115 {
116 return {0.0f, 0.0f, 0.0f, 0.0f};
117 }
118
119 [[nodiscard]] static constexpr color white() noexcept
120 {
121 return {1.0f, 1.0f, 1.0f, 1.0f};
122 }
123
124 [[nodiscard]] static constexpr color black() noexcept
125 {
126 return {0.0f, 0.0f, 0.0f, 1.0f};
127 }
128
129 [[nodiscard]] size_t hash() const noexcept
130 {
131 return std::hash<uint64_t>{}(std::bit_cast<uint64_t>(_v));
132 }
133
134 [[nodiscard]] constexpr float16& r() noexcept
135 {
136 return _v.x();
137 }
138
139 [[nodiscard]] constexpr float16& g() noexcept
140 {
141 return _v.y();
142 }
143
144 [[nodiscard]] constexpr float16& b() noexcept
145 {
146 return _v.z();
147 }
148
149 [[nodiscard]] constexpr float16& a() noexcept
150 {
151 return _v.w();
152 }
153
154 [[nodiscard]] constexpr float16 r() const noexcept
155 {
156 return _v.x();
157 }
158
159 [[nodiscard]] constexpr float16 g() const noexcept
160 {
161 return _v.y();
162 }
163
164 [[nodiscard]] constexpr float16 b() const noexcept
165 {
166 return _v.z();
167 }
168
169 [[nodiscard]] constexpr float16 a() const noexcept
170 {
171 return _v.w();
172 }
173
174 [[nodiscard]] constexpr bool holds_invariant() const noexcept
175 {
176 return _v.w() >= 0.0 && _v.w() <= 1.0;
177 }
178
179 [[nodiscard]] constexpr friend bool operator==(color const& lhs, color const& rhs) noexcept
180 {
181 return equal(lhs._v, rhs._v);
182 }
183
184 [[nodiscard]] constexpr friend color operator*(color const& lhs, color const& rhs) noexcept
185 {
186 return color{lhs._v * rhs._v};
187 }
188
189 [[nodiscard]] constexpr friend color composit(color const& lhs, color const& rhs) noexcept
190 {
191 return color{composit(lhs._v, rhs._v)};
192 }
193
194 [[nodiscard]] constexpr friend color desaturate(color const& rhs) noexcept
195 {
196 hilet rhs_ = f32x4{rhs};
197
198 hilet Y = 0.2126f * rhs_.r() + 0.7152f * rhs_.g() + 0.0722f * rhs_.b();
199
200 return color{Y, Y, Y, rhs_.a()};
201 }
202
212 [[nodiscard]] constexpr friend color operator*(matrix3 const& lhs, color const& rhs) noexcept
213 {
214 hi_axiom(rhs.holds_invariant());
215 auto r = color{
216 get<0>(lhs) * static_cast<f32x4>(rhs).xxxx() + get<1>(lhs) * static_cast<f32x4>(rhs).yyyy() +
217 get<2>(lhs) * static_cast<f32x4>(rhs).zzzz() + get<3>(lhs)};
218
219 r.a() = rhs.a();
220 return r;
221 }
222
223 [[nodiscard]] constexpr friend color operator*(identity3 const& lhs, color const& rhs) noexcept
224 {
225 return rhs;
226 }
227
228private:
229 f16x4 _v;
230};
231
232}} // namespace hi::v1
233
234template<>
235struct std::hash<hi::color> {
236 [[nodiscard]] size_t operator()(hi::color const& rhs) const noexcept
237 {
238 return rhs.hash();
239 }
240};
defines the semantic_color type.
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:238
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
semantic_color
Semantic colors.
Definition semantic_color.hpp:20
@ other
The gui_event does not have associated data.
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
This is a RGBA floating point color.
Definition color.hpp:42
constexpr friend color operator*(matrix3 const &lhs, color const &rhs) noexcept
Transform a color by a color matrix.
Definition color.hpp:212
Identity transform.
Definition identity.hpp:20
A 2D or 3D homogenius matrix for transforming homogenious vectors and points.
Definition matrix.hpp:33
T operator()(T... args)