aest 0.1.0
Collection of C++ utility libraries
Loading...
Searching...
No Matches
Hex.cppm
Go to the documentation of this file.
1
8module;
9#include <bit>
10#include <cstdint>
11#include <format>
12#include <string>
13export module aest.colors:hex;
14
15import :color;
16import :converter;
17import :gamut;
18
19export namespace aest::colors
20{
35template <ColorType Color = SRGB>
37constexpr Color from_rgba(const std::uint8_t r,
38 const std::uint8_t g,
39 const std::uint8_t b,
40 const std::uint8_t a = BYTE_MAX)
41{
42 return convert<Color>(SRGB{to_float(r), to_float(g), to_float(b), to_float(a)});
43}
44
51template <ColorType Color = SRGB>
52 requires ConvertibleTo<Color, SRGB>
53constexpr Color from_rgba(const std::uint32_t rgba)
54{
55 return from_rgba<Color>(byte_at(rgba, 3), byte_at(rgba, 2), byte_at(rgba, 1), byte_at(rgba, 0));
56}
57
64template <ColorType Color = SRGB>
65 requires ConvertibleTo<Color, SRGB>
66constexpr Color from_argb(const std::uint32_t argb)
67{
68 // AARRGGBB -> RRGGBBAA
69 return from_rgba<Color>(std::rotl(argb, 8));
70}
71
79template <ColorType Color = SRGB>
80 requires ConvertibleTo<Color, SRGB>
81constexpr Color from_hex(const std::uint32_t hex)
82{
83 // Shift the hex color code up one byte and fill with 0xFF
84 return from_rgba<Color>(hex << 8 | BYTE_MAX);
85}
86
100template <ColorType Color>
101 requires ConvertibleTo<SRGB, Color>
102[[nodiscard]] constexpr std::uint32_t to_rgba(const Color& color)
103{
104 const SRGB srgb = convert<SRGB>(color);
105 return pack(to_byte(srgb.red()), to_byte(srgb.green()), to_byte(srgb.blue()),
106 to_byte(srgb.alpha()));
107}
108
121template <ColorType Color>
122 requires ConvertibleTo<SRGB, Color>
123[[nodiscard]] constexpr std::uint32_t to_argb(const Color& color)
124{
125 // RRGGBBAA -> AARRGGBB
126 return std::rotr(to_rgba(color), 8);
127}
128
139template <ColorType Color>
140 requires ConvertibleTo<SRGB, Color>
141[[nodiscard]] std::string hex_string(const Color& color)
142{
143 return std::format("#{:06X}", to_rgba(color) >> 8);
144}
145} // namespace aest::colors
constexpr std::uint32_t pack(const std::uint8_t byte3, const std::uint8_t byte2, const std::uint8_t byte1, const std::uint8_t byte0)
Pack four bytes into a 32-bit word, most significant first.
Definition Color.cppm:83
constexpr std::uint8_t to_byte(const float value)
Convert a float in the 0-1 range to a 8-bit color channel value.
Definition Color.cppm:59
constexpr std::uint8_t byte_at(const std::uint32_t word, const std::uint32_t index)
Extract one byte of a 32-bit word.
Definition Color.cppm:70
constexpr std::uint8_t BYTE_MAX
Maximum value for an 8-bit color channel.
Definition Color.cppm:22
constexpr float to_float(const std::uint8_t value)
Convert an 8-bit color channel value to a float in the 0-1 range.
Definition Color.cppm:49
constexpr ColorOut convert(const ColorIn &color)
Convert a color from one color space to another.
BasicRGB< gamut::SRGB, tf::SRGB > SRGB
sRGB with gamma-encoded channels: the encoding of hex codes, 8-bit channels and CSS colors.
Definition Gamut.cppm:370
constexpr Color from_rgba(const std::uint8_t r, const std::uint8_t g, const std::uint8_t b, const std::uint8_t a=BYTE_MAX)
Build a color from 8-bit SRGB channels.
Definition Hex.cppm:37
constexpr Color from_hex(const std::uint32_t hex)
Build a color from an SRGB hex code, a 32-bit integer in 0xRRGGBB format, as in from_hex(0xFF7F50).
Definition Hex.cppm:81
constexpr Color from_argb(const std::uint32_t argb)
Build a color from a 32-bit SRGB integer in 0xAARRGGBB format.
Definition Hex.cppm:66
constexpr std::uint32_t to_argb(const Color &color)
Convert a color to SRGB and pack it into a 32-bit integer in 0xAARRGGBB format.
Definition Hex.cppm:123
constexpr std::uint32_t to_rgba(const Color &color)
Convert a color to SRGB and pack it into a 32-bit integer in 0xRRGGBBAA format.
Definition Hex.cppm:102
std::string hex_string(const Color &color)
Convert a color to SRGB and write it as a hex code in #RRGGBB format, without alpha.
Definition Hex.cppm:141
constexpr float green() const noexcept
Get the green channel value.
Definition Gamut.cppm:330
constexpr float red() const noexcept
Get the red channel value.
Definition Gamut.cppm:324
constexpr float blue() const noexcept
Get the blue channel value.
Definition Gamut.cppm:336
A color space that convert() can convert ColorIn to: directly through a Converter,...
constexpr float alpha() const noexcept
Get the alpha channel value in the 0-1 range.
Definition Color.cppm:175