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 hi::inline v1 {
10
37class color {
38public:
39 constexpr color(color const &) noexcept = default;
40 constexpr color(color &&) noexcept = default;
41 constexpr color &operator=(color const &) noexcept = default;
42 constexpr color &operator=(color &&) noexcept = default;
43
44 [[nodiscard]] constexpr explicit color(f16x4 const &other) noexcept : _v(other)
45 {
46 hi_axiom(holds_invariant());
47 }
48
49 [[nodiscard]] constexpr explicit color(f32x4 const &other) noexcept : color(static_cast<f16x4>(other)) {}
50
51 [[nodiscard]] constexpr explicit operator f16x4() const noexcept
52 {
53 return _v;
54 }
55
56 [[nodiscard]] constexpr explicit operator f32x4() const noexcept
57 {
58 return static_cast<f32x4>(_v);
59 }
60
61 [[nodiscard]] constexpr color(float r, float g, float b, float a = 1.0f) noexcept : color(f32x4{r, g, b, a}) {}
62
63 [[nodiscard]] constexpr color() noexcept : color(f32x4{}) {}
64
65 [[nodiscard]] static constexpr color transparent() noexcept
66 {
67 return {0.0f, 0.0f, 0.0f, 0.0f};
68 }
69
70 [[nodiscard]] static constexpr color white() noexcept
71 {
72 return {1.0f, 1.0f, 1.0f, 1.0f};
73 }
74
75 [[nodiscard]] static constexpr color black() noexcept
76 {
77 return {0.0f, 0.0f, 0.0f, 1.0f};
78 }
79
80 [[nodiscard]] constexpr float16 &r() noexcept
81 {
82 return _v.x();
83 }
84
85 [[nodiscard]] constexpr float16 &g() noexcept
86 {
87 return _v.y();
88 }
89
90 [[nodiscard]] constexpr float16 &b() noexcept
91 {
92 return _v.z();
93 }
94
95 [[nodiscard]] constexpr float16 &a() noexcept
96 {
97 return _v.w();
98 }
99
100 [[nodiscard]] constexpr float16 const &r() const noexcept
101 {
102 return _v.x();
103 }
104
105 [[nodiscard]] constexpr float16 const &g() const noexcept
106 {
107 return _v.y();
108 }
109
110 [[nodiscard]] constexpr float16 const &b() const noexcept
111 {
112 return _v.z();
113 }
114
115 [[nodiscard]] constexpr float16 const &a() const noexcept
116 {
117 return _v.w();
118 }
119
120 [[nodiscard]] constexpr bool holds_invariant() const noexcept
121 {
122 return _v.w() >= 0.0 && _v.w() <= 1.0;
123 }
124
125 [[nodiscard]] constexpr friend bool operator==(color const &lhs, color const &rhs) noexcept = default;
126
127 [[nodiscard]] constexpr friend color operator*(color const &lhs, color const &rhs) noexcept
128 {
129 return color{lhs._v * rhs._v};
130 }
131
132 [[nodiscard]] constexpr friend color composit(color const &lhs, color const &rhs) noexcept
133 {
134 return color{composit(lhs._v, rhs._v)};
135 }
136
137 [[nodiscard]] constexpr friend color desaturate(color const &rhs) noexcept
138 {
139 auto rhs_ = f32x4{rhs};
140
141 auto Y = 0.2126f * rhs_.r() + 0.7152f * rhs_.g() + 0.0722f * rhs_.b();
142
143 return color{Y, Y, Y, rhs_.a()};
144 }
145
146private:
147 f16x4 _v;
148};
149
150} // namespace hi::inline v1
This is a RGBA floating point color.
Definition color.hpp:37
Definition float16.hpp:82