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 "semantic_color.hpp"
8#include "../rapid/numeric_array.hpp"
9#include "../assert.hpp"
10
11namespace hi::inline v1 {
12
39class color {
40public:
41 constexpr color(color const&) noexcept = default;
42 constexpr color(color&&) noexcept = default;
43 constexpr color& operator=(color const&) noexcept = default;
44 constexpr color& operator=(color&&) noexcept = default;
45
46 [[nodiscard]] constexpr explicit color(f16x4 const& other) noexcept : _v(other)
47 {
48 hi_axiom(holds_invariant());
49 }
50
51 [[nodiscard]] constexpr explicit color(f32x4 const& other) noexcept : color(static_cast<f16x4>(other)) {}
52
53 [[nodiscard]] constexpr explicit operator f16x4() const noexcept
54 {
55 return _v;
56 }
57
58 [[nodiscard]] constexpr explicit operator f32x4() const noexcept
59 {
60 return static_cast<f32x4>(_v);
61 }
62
63 [[nodiscard]] constexpr color(float r, float g, float b, float a = 1.0f) noexcept : color(f32x4{r, g, b, a}) {}
64
65 [[nodiscard]] constexpr color() noexcept : color(f32x4{}) {}
66
67 [[nodiscard]] constexpr explicit color(hi::semantic_color semantic_color, float alpha = 1.0f) noexcept : _v()
68 {
69 _v.x() = float16::from_uint16_t(0xf900 + static_cast<uint8_t>(semantic_color));
70 _v.y() = float16::from_uint16_t(0x0000);
71 _v.z() = float16::from_uint16_t(0x0000);
72 _v.w() = float16(1.0f);
73 }
74
75 [[nodiscard]] constexpr bool is_semantic() const noexcept
76 {
77 return (_v.x().get() & 0xf900) == 0xf900;
78 }
79
80 constexpr explicit operator semantic_color() const noexcept
81 {
82 hi_axiom(is_semantic());
83 return static_cast<semantic_color>(_v.x().get() & 0xff);
84 }
85
86 // clang-format off
87 [[nodiscard]] static constexpr color blue() noexcept { return color{semantic_color::blue}; }
88 [[nodiscard]] static constexpr color green() noexcept { return color{semantic_color::green}; }
89 [[nodiscard]] static constexpr color indigo() noexcept { return color{semantic_color::indigo}; }
90 [[nodiscard]] static constexpr color orange() noexcept { return color{semantic_color::orange}; }
91 [[nodiscard]] static constexpr color pink() noexcept { return color{semantic_color::pink}; }
92 [[nodiscard]] static constexpr color purple() noexcept { return color{semantic_color::purple}; }
93 [[nodiscard]] static constexpr color red() noexcept { return color{semantic_color::red}; }
94 [[nodiscard]] static constexpr color teal() noexcept { return color{semantic_color::teal}; }
95 [[nodiscard]] static constexpr color yellow() noexcept { return color{semantic_color::yellow}; }
96 [[nodiscard]] static constexpr color gray() noexcept { return color{semantic_color::gray}; }
97 [[nodiscard]] static constexpr color gray2() noexcept { return color{semantic_color::gray2}; }
98 [[nodiscard]] static constexpr color gray3() noexcept { return color{semantic_color::gray3}; }
99 [[nodiscard]] static constexpr color gray4() noexcept { return color{semantic_color::gray4}; }
100 [[nodiscard]] static constexpr color gray5() noexcept { return color{semantic_color::gray5}; }
101 [[nodiscard]] static constexpr color gray6() noexcept { return color{semantic_color::gray6}; }
102 [[nodiscard]] static constexpr color foreground() noexcept { return color{semantic_color::foreground}; }
103 [[nodiscard]] static constexpr color border() noexcept { return color{semantic_color::border}; }
104 [[nodiscard]] static constexpr color fill() noexcept { return color{semantic_color::fill}; }
105 [[nodiscard]] static constexpr color accent() noexcept { return color{semantic_color::accent}; }
106 [[nodiscard]] static constexpr color text_select() noexcept { return color{semantic_color::text_select}; }
107 [[nodiscard]] static constexpr color primary_cursor() noexcept { return color{semantic_color::primary_cursor}; }
108 [[nodiscard]] static constexpr color secondary_cursor() noexcept { return color{semantic_color::secondary_cursor}; }
109 // clang-format on
110
111 [[nodiscard]] static constexpr color transparent() noexcept
112 {
113 return {0.0f, 0.0f, 0.0f, 0.0f};
114 }
115
116 [[nodiscard]] static constexpr color white() noexcept
117 {
118 return {1.0f, 1.0f, 1.0f, 1.0f};
119 }
120
121 [[nodiscard]] static constexpr color black() noexcept
122 {
123 return {0.0f, 0.0f, 0.0f, 1.0f};
124 }
125
126 [[nodiscard]] size_t hash() const noexcept
127 {
128 return std::hash<uint64_t>{}(std::bit_cast<uint64_t>(_v));
129 }
130
131 [[nodiscard]] constexpr float16& r() noexcept
132 {
133 return _v.x();
134 }
135
136 [[nodiscard]] constexpr float16& g() noexcept
137 {
138 return _v.y();
139 }
140
141 [[nodiscard]] constexpr float16& b() noexcept
142 {
143 return _v.z();
144 }
145
146 [[nodiscard]] constexpr float16& a() noexcept
147 {
148 return _v.w();
149 }
150
151 [[nodiscard]] constexpr float16 const& r() const noexcept
152 {
153 return _v.x();
154 }
155
156 [[nodiscard]] constexpr float16 const& g() const noexcept
157 {
158 return _v.y();
159 }
160
161 [[nodiscard]] constexpr float16 const& b() const noexcept
162 {
163 return _v.z();
164 }
165
166 [[nodiscard]] constexpr float16 const& a() const noexcept
167 {
168 return _v.w();
169 }
170
171 [[nodiscard]] constexpr bool holds_invariant() const noexcept
172 {
173 return _v.w() >= 0.0 && _v.w() <= 1.0;
174 }
175
176 [[nodiscard]] constexpr friend bool operator==(color const& lhs, color const& rhs) noexcept = default;
177
178 [[nodiscard]] constexpr friend color operator*(color const& lhs, color const& rhs) noexcept
179 {
180 return color{lhs._v * rhs._v};
181 }
182
183 [[nodiscard]] constexpr friend color composit(color const& lhs, color const& rhs) noexcept
184 {
185 return color{composit(lhs._v, rhs._v)};
186 }
187
188 [[nodiscard]] constexpr friend color desaturate(color const& rhs) noexcept
189 {
190 hilet rhs_ = f32x4{rhs};
191
192 hilet Y = 0.2126f * rhs_.r() + 0.7152f * rhs_.g() + 0.0722f * rhs_.b();
193
194 return color{Y, Y, Y, rhs_.a()};
195 }
196
197private:
198 f16x4 _v;
199};
200
201} // namespace hi::inline v1
202
203template<>
204struct std::hash<hi::color> {
205 [[nodiscard]] size_t operator()(hi::color const &rhs) const noexcept
206 {
207 return rhs.hash();
208 }
209};
#define hilet
Invariant should be the default for variables.
Definition required.hpp:23
This is a RGBA floating point color.
Definition color.hpp:39
Definition float16.hpp:88
T operator()(T... args)
Definition datum.hpp:2458