HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
color_intf.hpp
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 "../geometry/geometry.hpp"
13#include "../utility/utility.hpp"
14#include "../macros.hpp"
15#include <hikocpu/hikocpu.hpp>
16#include <cstdint>
17#include <bit>
18#include <functional>
19
20hi_export_module(hikogui.color_intf);
21
22
23hi_export namespace hi { inline namespace v1 {
24
49class color {
50public:
51 constexpr color(color const&) noexcept = default;
52 constexpr color(color&&) noexcept = default;
53 constexpr color& operator=(color const&) noexcept = default;
54 constexpr color& operator=(color&&) noexcept = default;
55
56 [[nodiscard]] constexpr explicit color(f16x4 const& other) noexcept : _v(other)
57 {
58 hi_axiom(holds_invariant());
59 }
60
61 [[nodiscard]] constexpr explicit color(f32x4 const& other) noexcept : color(static_cast<f16x4>(other)) {}
62
63 [[nodiscard]] constexpr explicit operator f16x4() const noexcept
64 {
65 return _v;
66 }
67
68 [[nodiscard]] constexpr explicit operator f32x4() const noexcept
69 {
70 return static_cast<f32x4>(_v);
71 }
72
73 [[nodiscard]] constexpr color(float r, float g, float b, float a = 1.0f) noexcept : color(f32x4{r, g, b, a}) {}
74
75 [[nodiscard]] constexpr color() noexcept : color(f32x4{}) {}
76
77 [[nodiscard]] constexpr explicit color(hi::semantic_color semantic_color, float alpha = 1.0f) noexcept : _v()
78 {
79 _v.x() = half(std::in_place, 0xf900 + static_cast<uint8_t>(semantic_color));
80 _v.y() = half(std::in_place, 0x0000);
81 _v.z() = half(std::in_place, 0x0000);
82 _v.w() = half(1.0f);
83 }
84
85 [[nodiscard]] constexpr bool is_semantic() const noexcept
86 {
87 return (_v.x().intrinsic() & 0xf900) == 0xf900;
88 }
89
90 constexpr explicit operator semantic_color() const noexcept
91 {
92 hi_axiom(is_semantic());
93 return static_cast<semantic_color>(_v.x().intrinsic() & 0xff);
94 }
95
96 // clang-format off
97 [[nodiscard]] constexpr static color blue() noexcept { return color{semantic_color::blue}; }
98 [[nodiscard]] constexpr static color green() noexcept { return color{semantic_color::green}; }
99 [[nodiscard]] constexpr static color indigo() noexcept { return color{semantic_color::indigo}; }
100 [[nodiscard]] constexpr static color orange() noexcept { return color{semantic_color::orange}; }
101 [[nodiscard]] constexpr static color pink() noexcept { return color{semantic_color::pink}; }
102 [[nodiscard]] constexpr static color purple() noexcept { return color{semantic_color::purple}; }
103 [[nodiscard]] constexpr static color red() noexcept { return color{semantic_color::red}; }
104 [[nodiscard]] constexpr static color teal() noexcept { return color{semantic_color::teal}; }
105 [[nodiscard]] constexpr static color yellow() noexcept { return color{semantic_color::yellow}; }
106 [[nodiscard]] constexpr static color gray() noexcept { return color{semantic_color::gray}; }
107 [[nodiscard]] constexpr static color gray2() noexcept { return color{semantic_color::gray2}; }
108 [[nodiscard]] constexpr static color gray3() noexcept { return color{semantic_color::gray3}; }
109 [[nodiscard]] constexpr static color gray4() noexcept { return color{semantic_color::gray4}; }
110 [[nodiscard]] constexpr static color gray5() noexcept { return color{semantic_color::gray5}; }
111 [[nodiscard]] constexpr static color gray6() noexcept { return color{semantic_color::gray6}; }
112 [[nodiscard]] constexpr static color foreground() noexcept { return color{semantic_color::foreground}; }
113 [[nodiscard]] constexpr static color border() noexcept { return color{semantic_color::border}; }
114 [[nodiscard]] constexpr static color fill() noexcept { return color{semantic_color::fill}; }
115 [[nodiscard]] constexpr static color accent() noexcept { return color{semantic_color::accent}; }
116 [[nodiscard]] constexpr static color text_select() noexcept { return color{semantic_color::text_select}; }
117 [[nodiscard]] constexpr static color primary_cursor() noexcept { return color{semantic_color::primary_cursor}; }
118 [[nodiscard]] constexpr static color secondary_cursor() noexcept { return color{semantic_color::secondary_cursor}; }
119 // clang-format on
120
121 [[nodiscard]] constexpr static color transparent() noexcept
122 {
123 return {0.0f, 0.0f, 0.0f, 0.0f};
124 }
125
126 [[nodiscard]] constexpr static color white() noexcept
127 {
128 return {1.0f, 1.0f, 1.0f, 1.0f};
129 }
130
131 [[nodiscard]] constexpr static color black() noexcept
132 {
133 return {0.0f, 0.0f, 0.0f, 1.0f};
134 }
135
136 [[nodiscard]] size_t hash() const noexcept
137 {
138 return std::hash<uint64_t>{}(std::bit_cast<uint64_t>(_v));
139 }
140
141 [[nodiscard]] constexpr half& r() noexcept
142 {
143 return _v.x();
144 }
145
146 [[nodiscard]] constexpr half& g() noexcept
147 {
148 return _v.y();
149 }
150
151 [[nodiscard]] constexpr half& b() noexcept
152 {
153 return _v.z();
154 }
155
156 [[nodiscard]] constexpr half& a() noexcept
157 {
158 return _v.w();
159 }
160
161 [[nodiscard]] constexpr half r() const noexcept
162 {
163 return _v.x();
164 }
165
166 [[nodiscard]] constexpr half g() const noexcept
167 {
168 return _v.y();
169 }
170
171 [[nodiscard]] constexpr half b() const noexcept
172 {
173 return _v.z();
174 }
175
176 [[nodiscard]] constexpr half a() const noexcept
177 {
178 return _v.w();
179 }
180
181 [[nodiscard]] constexpr bool holds_invariant() const noexcept
182 {
183 return _v.w() >= 0.0 && _v.w() <= 1.0;
184 }
185
186 [[nodiscard]] constexpr friend bool operator==(color const& lhs, color const& rhs) noexcept
187 {
188 return equal(lhs._v, rhs._v);
189 }
190
191 [[nodiscard]] constexpr friend color operator*(color const& lhs, color const& rhs) noexcept
192 {
193 return color{lhs._v * rhs._v};
194 }
195
205 [[nodiscard]] constexpr friend color operator*(matrix3 const& lhs, color const& rhs) noexcept
206 {
207 hi_axiom(rhs.holds_invariant());
208 auto r = color{
209 get<0>(lhs) * static_cast<f32x4>(rhs).xxxx() + get<1>(lhs) * static_cast<f32x4>(rhs).yyyy() +
210 get<2>(lhs) * static_cast<f32x4>(rhs).zzzz() + get<3>(lhs)};
211
212 r.a() = rhs.a();
213 return r;
214 }
215
216private:
217 f16x4 _v;
218};
219
220}} // namespace hi::v1
221
222template<>
223struct std::hash<hi::color> {
224 [[nodiscard]] size_t operator()(hi::color const& rhs) const noexcept
225 {
226 return rhs.hash();
227 }
228};
defines the semantic_color type.
semantic_color
Semantic colors.
Definition semantic_color.hpp:25
@ other
The gui_event does not have associated data.
The HikoGUI namespace.
Definition array_generic.hpp:20
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
Definition half.hpp:27
Definition simd_intf.hpp:18
This is a RGBA floating point color.
Definition color_intf.hpp:49
constexpr friend color operator*(matrix3 const &lhs, color const &rhs) noexcept
Transform a color by a color matrix.
Definition color_intf.hpp:205
A 2D or 3D homogenius matrix for transforming homogenious vectors and points.
Definition matrix3.hpp:36
T operator()(T... args)