新增颜色结构

This commit is contained in:
2026-01-04 09:58:31 +08:00
parent 2999362194
commit 09b6481136

View File

@@ -1,19 +1,21 @@
#pragma once
#include "types.h"
#include <cmath>
namespace mirai {
/**
* @brief 颜色类型 (RGBA)
*/
template<typename T = f32>
struct color {
f64 r{0.0}; ///< 红色分量 (0-1)
f64 g{0.0}; ///< 绿色分量 (0-1)
f64 b{0.0}; ///< 蓝色分量 (0-1)
f64 a{1.0}; ///< 透明度 (0-1)
T r{0.0}; ///< 红色分量 (0-1)
T g{0.0}; ///< 绿色分量 (0-1)
T b{0.0}; ///< 蓝色分量 (0-1)
T a{1.0}; ///< 透明度 (0-1)
constexpr color() noexcept = default;
constexpr color(f64 r_, f64 g_, f64 b_, f64 a_ = 1.0) noexcept : r(r_), g(g_), b(b_), a(a_) {
constexpr color(T r_, T g_, T b_, T a_ = 1.0) noexcept : r(r_), g(g_), b(b_), a(a_) {
}
[[nodiscard]] constexpr color operator+(const color& other) const noexcept {
@@ -40,10 +42,10 @@ namespace mirai {
*/
[[nodiscard]] static constexpr color from_rgb(u8 r_, u8 g_, u8 b_, u8 a_ = 255) noexcept {
return color{
static_cast<f64>(r_) / 255.0,
static_cast<f64>(g_) / 255.0,
static_cast<f64>(b_) / 255.0,
static_cast<f64>(a_) / 255.0
static_cast<T>(r_) / 255.0,
static_cast<T>(g_) / 255.0,
static_cast<T>(b_) / 255.0,
static_cast<T>(a_) / 255.0
};
}
@@ -61,15 +63,13 @@ namespace mirai {
static_cast<u8>(hex & 0xFF)
);
}
else {
// 0xRRGGBBAA 格式
return from_rgb(
static_cast<u8>((hex >> 24) & 0xFF),
static_cast<u8>((hex >> 16) & 0xFF),
static_cast<u8>((hex >> 8) & 0xFF),
static_cast<u8>(hex & 0xFF)
);
}
// 0xRRGGBBAA 格式
return from_rgb(
static_cast<u8>((hex >> 24) & 0xFF),
static_cast<u8>((hex >> 16) & 0xFF),
static_cast<u8>((hex >> 8) & 0xFF),
static_cast<u8>(hex & 0xFF)
);
}
/**
@@ -195,9 +195,9 @@ namespace mirai {
[[nodiscard]] static constexpr color green() noexcept { return color{0.0, 1.0, 0.0, 1.0}; }
[[nodiscard]] static constexpr color blue() noexcept { return color{0.0, 0.0, 1.0, 1.0}; }
[[nodiscard]] static constexpr color transparent() noexcept { return color{0.0, 0.0, 0.0, 0.0}; }
[[nodiscard]] static constexpr color yellow() noexcept { return color{1.0f, 1.0f, 0.0f, 1.0f}; }
[[nodiscard]] static constexpr color cyan() noexcept { return color{0.0f, 1.0f, 1.0f, 1.0f}; }
[[nodiscard]] static constexpr color magenta() noexcept { return color{1.0f, 0.0f, 1.0f, 1.0f}; }
[[nodiscard]] static constexpr color gray() noexcept { return color{0.5f, 0.5f, 0.5f, 1.0f}; }
[[nodiscard]] static constexpr color yellow() noexcept { return color{1.0, 1.0, 0.0, 1.0}; }
[[nodiscard]] static constexpr color cyan() noexcept { return color{0.0, 1.0, 1.0, 1.0}; }
[[nodiscard]] static constexpr color magenta() noexcept { return color{1.0, 0.0, 1.0, 1.0}; }
[[nodiscard]] static constexpr color gray() noexcept { return color{0.5, 0.5, 0.5, 1.0}; }
};
}