AUI Framework  master
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
AColor.h
    1/*
    2 * AUI Framework - Declarative UI toolkit for modern C++20
    3 * Copyright (C) 2020-2025 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 {
   27public:
   28    constexpr AColor() : glm::vec4(0, 0, 0, 1.f) {}
   29    constexpr AColor(const glm::vec4& v) : glm::vec4(v) {}
   30
   31    API_AUI_CORE AColor(const AString& s);
   32    constexpr AColor(float scalar) : glm::vec4(scalar) {}
   33    constexpr AColor(float r, float g, float b) : glm::vec4(r, g, b, 1.f) {}
   34    constexpr AColor(float r, float g, float b, float a) : glm::vec4(r, g, b, a) {}
   35
   42    constexpr AColor(unsigned int color)
   43      : glm::vec4(((color >> 24) & 0xff) / 255.f, ((color >> 16) & 0xff) / 255.f, ((color >> 8) & 0xff) / 255.f,
   44                  ((color) & 0xff) / 255.f) {}
   45
   52    static constexpr AColor fromAARRGGBB(unsigned int color) {
   53        return {
   54            ((color >> 16) & 0xff) / 255.f,
   55            ((color >> 8) & 0xff) / 255.f,
   56            ((color) & 0xff) / 255.f,
   57            ((color >> 24) & 0xff) / 255.f,
   58        };
   59    }
   60
   67    static constexpr AColor fromRRGGBB(unsigned int color) {
   68        return {
   69            ((color >> 16) & 0xff) / 255.f,
   70            ((color >> 8) & 0xff) / 255.f,
   71            ((color) & 0xff) / 255.f,
   72            1,
   73        };
   74    }
   75
   76    constexpr AColor operator*(float other) const { return AColor(x * other, y * other, z * other, w * other); }
   77
   78    API_AUI_CORE AString toString() const;
   79
   80    API_AUI_CORE float readabilityOfForegroundColor(const AColor& foreground);
   81
   85    [[nodiscard]]
   86    AColor opacify(float alpha) const noexcept {
   87        AColor c = *this;
   88        c.a += alpha;
   89        return c;
   90    }
   91
   95    [[nodiscard]]
   96    AColor transparentize(float alpha) const noexcept {
   97        AColor c = *this;
   98        c.a -= alpha;
   99        return c;
  100    }
  101
  107    inline constexpr AColor mul(float d) const {
  108        return glm::clamp(glm::vec4(r * d, g * d, b * d, a), glm::vec4(0.f), glm::vec4(1.f));
  109    }
  110    inline constexpr AColor darker(float d) const { return mul(1.f - d); }
  111    inline constexpr AColor lighter(float d) const { return mul(1.f + d); }
  112
  113    bool isFullyTransparent() const { return a < 0.001f; }
  114    bool isFullyOpaque() const { return a > 0.999f; }
  115
  116    AColor readableBlackOrWhite() const {
  117        return glm::length2(glm::vec3 { *this }) > 1.5f ? fromRRGGBB(0) : fromRRGGBB(0xffffff);
  118    }
  119
  120    AColor opacify(float d) { return { x, y, z, a * d }; }
  121
  122    static const AColor BLACK;
  123    static const AColor WHITE;
  124    static const AColor RED;
  125    static const AColor GREEN;
  126    static const AColor BLUE;
  127    static const AColor GRAY;
  128    static const AColor TRANSPARENT_BLACK;
  129    static const AColor TRANSPARENT_WHITE;
  130};
  131
  132inline const AColor AColor::BLACK = { 0.f, 0.f, 0.f, 1.f };
  133inline const AColor AColor::WHITE = { 1.f, 1.f, 1.f, 1.f };
  134inline const AColor AColor::RED = { 1.f, 0.f, 0.f, 1.f };
  135inline const AColor AColor::GREEN = { 0.f, 1.f, 0.f, 1.f };
  136inline const AColor AColor::BLUE = { 0.f, 0.f, 1.f, 1.f };
  137inline const AColor AColor::GRAY = { 0.5f, 0.5f, 0.5f, 1.f };
  138inline const AColor AColor::TRANSPARENT_BLACK = { 0.f, 0.f, 0.f, 0.f };
  139inline const AColor AColor::TRANSPARENT_WHITE = { 0.f, 0.f, 0.f, 1.f };
  140
  141inline std::ostream& operator<<(std::ostream& o, const AColor& color) {
  142    o << "#";
  143    char buf[16];
  144    if (!color.isFullyOpaque()) {
  145        std::snprintf(buf, sizeof(buf), "%02x", uint8_t(color.a * 255.f));
  146        o << buf;
  147    }
  148    std::snprintf(
  149        buf, sizeof(buf), "%02x%02x%02x", uint8_t(color.r * 255.f), uint8_t(color.g * 255.f), uint8_t(color.b * 255.f));
  150    o << buf;
  151    return o;
  152}
  153
  160inline constexpr AColor operator""_argb(unsigned long long v) { return AColor::fromAARRGGBB(unsigned(v)); }
  161
  168inline constexpr AColor operator""_rgb(unsigned long long v) {
  169    assert(("_rgb literal should be in 0xrrggbb format, not 0xaarrggbb" && !(v & 0xff000000u)));
  170    return AColor::fromRRGGBB(unsigned(v));
  171}
  172
  177struct API_AUI_CORE AColorHSV {
  178    aui::float_within_0_1 hue = 0.f;
  179    aui::float_within_0_1 saturation = 0.f;
  180    aui::float_within_0_1 value = 0.f;
  181
  182    [[nodiscard]] static AColorHSV fromRGB(AColor color) noexcept;
  183
  184    [[nodiscard]] AColor toRGB() const noexcept;
  185};
  186
  187template<>
  188struct std::hash<AColor> {
  189    auto operator()(const AColor& c) {
  190        return std::hash<std::string_view>{}({ reinterpret_cast<const char*>(&c), sizeof(c) });
  191    }
  192};
Represents a 4-component floating point color (RGBA).
Definition AColor.h:26
constexpr AColor(unsigned int color)
Construct with hex integer.
Definition AColor.h:42
constexpr AColor mul(float d) const
Multiply all color components except alpha channel (rgb * d, a)
Definition AColor.h:107
AColor transparentize(float alpha) const noexcept
Decreases the alpha channel by the given value.
Definition AColor.h:96
AColor opacify(float alpha) const noexcept
Increases the alpha channel by the given value.
Definition AColor.h:86
static constexpr AColor fromRRGGBB(unsigned int color)
Construct with hex integer.
Definition AColor.h:67
static constexpr AColor fromAARRGGBB(unsigned int color)
Construct with hex integer.
Definition AColor.h:52
Represents a Unicode character string.
Definition AString.h:38
Represents a 3-component floating point color (HSV).
Definition AColor.h:177