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