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