AUI Framework  master
Cross-platform module-based framework for developing C++20 desktop applications
AColor.h
1/*
2 * AUI Framework - Declarative UI toolkit for modern C++20
3 * Copyright (C) 2020-2024 Alex2772 and Contributors
4 *
5 * SPDX-License-Identifier: MPL-2.0
6 *
7 * This Source Code Form is subject to the terms of the Mozilla Public
8 * License, v. 2.0. If a copy of the MPL was not distributed with this
9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 */
11
12#pragma once
13
14#include <glm/glm.hpp>
15#include <AUI/Core.h>
16#include <glm/gtx/norm.hpp>
17#include <ostream>
18#include "AUI/Traits/values.h"
19
20class AString;
21
26class AColor: public glm::vec4
27{
28public:
29 constexpr AColor(): glm::vec4(0, 0, 0, 1.f)
30 {
31 }
32 constexpr AColor(const glm::vec4& v): glm::vec4(v){}
33
34 API_AUI_CORE AColor(const AString& s);
35 constexpr AColor(float scalar) : glm::vec4(scalar) {}
36 constexpr AColor(float r, float g, float b) : glm::vec4(r, g, b, 1.f) {}
37 constexpr AColor(float r, float g, float b, float a) : glm::vec4(r, g, b, a) {}
38
44 constexpr AColor(unsigned int color) : glm::vec4(
45 ((color >> 24) & 0xff) / 255.f,
46 ((color >> 16) & 0xff) / 255.f,
47 ((color >> 8) & 0xff) / 255.f,
48 ((color) & 0xff) / 255.f) {}
49
55 static constexpr AColor fromAARRGGBB(unsigned int color)
56 {
57 return {
58 ((color >> 16) & 0xff) / 255.f,
59 ((color >> 8) & 0xff) / 255.f,
60 ((color) & 0xff) / 255.f,
61 ((color >> 24) & 0xff) / 255.f,
62 };
63 }
64
70 static constexpr AColor fromRRGGBB(unsigned int color)
71 {
72 return {
73 ((color >> 16) & 0xff) / 255.f,
74 ((color >> 8) & 0xff) / 255.f,
75 ((color) & 0xff) / 255.f,
76 1,
77 };
78 }
79
80 constexpr AColor operator*(float other) const
81 {
82 return AColor(x * other, y * other, z * other, w * other);
83 }
84
85 API_AUI_CORE AString toString() const;
86
87 API_AUI_CORE float readabilityOfForegroundColor(const AColor &foreground);
88
89
93 [[nodiscard]]
94 AColor opacify(float alpha) const noexcept {
95 AColor c = *this;
96 c.a += alpha;
97 return c;
98 }
99
103 [[nodiscard]]
104 AColor transparentize(float alpha) const noexcept {
105 AColor c = *this;
106 c.a -= alpha;
107 return c;
108 }
109
115 inline constexpr AColor mul(float d) const {
116 return glm::clamp(glm::vec4(r * d, g * d, b * d, a), glm::vec4(0.f), glm::vec4(1.f));
117 }
118 inline constexpr AColor darker(float d) const {
119 return mul(1.f - d);
120 }
121 inline constexpr AColor lighter(float d) const {
122 return mul(1.f + d);
123 }
124
125 bool isFullyTransparent() const {
126 return a < 0.001f;
127 }
128 bool isFullyOpaque() const {
129 return a > 0.999f;
130 }
131
132 AColor readableBlackOrWhite() const {
133 return glm::length2(glm::vec3{*this}) > 1.5f ? fromRRGGBB(0) : fromRRGGBB(0xffffff);
134 }
135
136 AColor opacify(float d) {
137 return {x, y, z, a * d};
138 }
139
140 static const AColor BLACK;
141 static const AColor WHITE;
142 static const AColor RED;
143 static const AColor GREEN;
144 static const AColor BLUE;
145 static const AColor GRAY;
146};
147
148inline const AColor AColor::BLACK = {0.f, 0.f, 0.f, 1.f};
149inline const AColor AColor::WHITE = {1.f, 1.f, 1.f, 1.f};
150inline const AColor AColor::RED = {1.f, 0.f, 0.f, 1.f};
151inline const AColor AColor::GREEN = {0.f, 1.f, 0.f, 1.f};
152inline const AColor AColor::BLUE = {0.f, 0.f, 1.f, 1.f};
153inline const AColor AColor::GRAY = {0.5f, 0.5f, 0.5f, 1.f};
154
155
156inline std::ostream& operator<<(std::ostream& o, const AColor& color) {
157 o << "#";
158 char buf[16];
159 if (!color.isFullyOpaque()) {
160 std::snprintf(buf, sizeof(buf), "%02x", uint8_t(color.a * 255.f));
161 o << buf;
162 }
163 std::snprintf(buf, sizeof(buf), "%02x%02x%02x", uint8_t(color.r * 255.f), uint8_t(color.g * 255.f), uint8_t(color.b * 255.f));
164 o << buf;
165 return o;
166}
167
173inline constexpr AColor operator""_argb(unsigned long long v)
174{
175 return AColor::fromAARRGGBB(unsigned(v));
176}
177
178
184inline constexpr AColor operator""_rgb(unsigned long long v)
185{
186 assert(("_rgb literal should be in 0xrrggbb format, not 0xaarrggbb" && !(v & 0xff000000u)));
187 return AColor::fromRRGGBB(unsigned(v));
188}
189
190
191struct API_AUI_CORE AColorHSV {
192 aui::float_within_0_1 hue = 0.f;
193 aui::float_within_0_1 saturation = 0.f;
194 aui::float_within_0_1 value = 0.f;
195
196 [[nodiscard]] static AColorHSV fromRGB(AColor color) noexcept;
197
198 [[nodiscard]] AColor toRGB() const noexcept;
199};
Represents a 4-component floating point color.
Definition: AColor.h:27
constexpr AColor mul(float d) const
Multiply all color components except alpha channel (rgb * d, a)
Definition: AColor.h:115
AColor transparentize(float alpha) const noexcept
Decreases the alpha channel by the given value.
Definition: AColor.h:104
AColor opacify(float alpha) const noexcept
Increases the alpha channel by the given value.
Definition: AColor.h:94
Represents a Unicode character string.
Definition: AString.h:37
Definition: AColor.h:191
Clamps the possible values for a number to the specified range: [min;max].
Definition: values.h:452