aest 0.1.0
Collection of C++ utility libraries
Loading...
Searching...
No Matches
Hex.cppm File Reference

Partition aest.colors:hex: hex codes, 8-bit channels and packed 32-bit colors, read and written as SRGB for any color space. More...

#include <bit>
#include <cstdint>
#include <format>
#include <string>
import aest.colors:gamut;

Go to the source code of this file.

Functions

template<ColorType Color = SRGB>
requires ConvertibleTo<Color, SRGB>
constexpr Color aest::colors::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.
template<ColorType Color = SRGB>
requires ConvertibleTo<Color, SRGB>
constexpr Color aest::colors::from_rgba (const std::uint32_t rgba)
 Build a color from a 32-bit SRGB integer in 0xRRGGBBAA format.
template<ColorType Color = SRGB>
requires ConvertibleTo<Color, SRGB>
constexpr Color aest::colors::from_argb (const std::uint32_t argb)
 Build a color from a 32-bit SRGB integer in 0xAARRGGBB format.
template<ColorType Color = SRGB>
requires ConvertibleTo<Color, SRGB>
constexpr Color aest::colors::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).
template<ColorType Color>
requires ConvertibleTo<SRGB, Color>
constexpr std::uint32_t aest::colors::to_rgba (const Color &color)
 Convert a color to SRGB and pack it into a 32-bit integer in 0xRRGGBBAA format.
template<ColorType Color>
requires ConvertibleTo<SRGB, Color>
constexpr std::uint32_t aest::colors::to_argb (const Color &color)
 Convert a color to SRGB and pack it into a 32-bit integer in 0xAARRGGBB format.
template<ColorType Color>
requires ConvertibleTo<SRGB, Color>
std::string aest::colors::hex_string (const Color &color)
 Convert a color to SRGB and write it as a hex code in #RRGGBB format, without alpha.

Detailed Description

Partition aest.colors:hex: hex codes, 8-bit channels and packed 32-bit colors, read and written as SRGB for any color space.

Author
Davide Pierotti

Definition in file Hex.cppm.

Function Documentation

◆ from_argb()

template<ColorType Color = SRGB>
requires ConvertibleTo<Color, SRGB>
Color aest::colors::from_argb ( const std::uint32_t argb)
constexprexport

Build a color from a 32-bit SRGB integer in 0xAARRGGBB format.

Template Parameters
ColorThe color space to build, SRGB by default
Parameters
[in]argbThe color as a 32-bit integer
Returns
The unpacked color, converted to Color.

Definition at line 66 of file Hex.cppm.

◆ from_hex()

template<ColorType Color = SRGB>
requires ConvertibleTo<Color, SRGB>
Color aest::colors::from_hex ( const std::uint32_t hex)
constexprexport

Build a color from an SRGB hex code, a 32-bit integer in 0xRRGGBB format, as in from_hex(0xFF7F50).

Template Parameters
ColorThe color space to build, SRGB by default
Parameters
[in]hexThe color as an HTML color code
Returns
The color, fully opaque, converted to Color.

Definition at line 81 of file Hex.cppm.

◆ from_rgba() [1/2]

template<ColorType Color = SRGB>
requires ConvertibleTo<Color, SRGB>
Color aest::colors::from_rgba ( const std::uint32_t rgba)
constexprexport

Build a color from a 32-bit SRGB integer in 0xRRGGBBAA format.

Template Parameters
ColorThe color space to build, SRGB by default
Parameters
[in]rgbaThe color as a 32-bit integer
Returns
The unpacked color, converted to Color.

Definition at line 53 of file Hex.cppm.

◆ from_rgba() [2/2]

template<ColorType Color = SRGB>
requires ConvertibleTo<Color, SRGB>
Color aest::colors::from_rgba ( const std::uint8_t r,
const std::uint8_t g,
const std::uint8_t b,
const std::uint8_t a = BYTE_MAX )
constexprexport

Build a color from 8-bit SRGB channels.

The bytes are SRGB, the encoding of hex codes and CSS colors, whatever the target space: they are scaled to the 0-1 range and converted to Color, as in from_rgba<HSL>(0, 120, 80).

Template Parameters
ColorThe color space to build, SRGB by default
Parameters
[in]rRed channel value
[in]gGreen channel value
[in]bBlue channel value
[in]aAlpha channel value (default 255)
Returns
The color in Color.

Definition at line 37 of file Hex.cppm.

◆ hex_string()

template<ColorType Color>
requires ConvertibleTo<SRGB, Color>
std::string aest::colors::hex_string ( const Color & color)
nodiscardexport

Convert a color to SRGB and write it as a hex code in #RRGGBB format, without alpha.

A color outside the SRGB gamut clips, as with to_rgba().

Template Parameters
ColorThe color space of color
Parameters
[in]colorThe color to write
Returns
A new string containing the hex code.

Definition at line 141 of file Hex.cppm.

◆ to_argb()

template<ColorType Color>
requires ConvertibleTo<SRGB, Color>
std::uint32_t aest::colors::to_argb ( const Color & color)
nodiscardconstexprexport

Convert a color to SRGB and pack it into a 32-bit integer in 0xAARRGGBB format.

A color outside the SRGB gamut clips, as with to_rgba().

Template Parameters
ColorThe color space of color
Parameters
[in]colorThe color to pack
Returns
The SRGB color as a 32-bit integer.
Note
Channels are rounded to the nearest byte, so the conversion loses precision.

Definition at line 123 of file Hex.cppm.

◆ to_rgba()

template<ColorType Color>
requires ConvertibleTo<SRGB, Color>
std::uint32_t aest::colors::to_rgba ( const Color & color)
nodiscardconstexprexport

Convert a color to SRGB and pack it into a 32-bit integer in 0xRRGGBBAA format.

A color outside the SRGB gamut clips. Unlike pack_rgba8(), which stores a color's own encoding, this always writes SRGB.

Template Parameters
ColorThe color space of color
Parameters
[in]colorThe color to pack
Returns
The SRGB color as a 32-bit integer.
Note
Channels are rounded to the nearest byte, so the conversion loses precision.

Definition at line 102 of file Hex.cppm.