nCine::Primitives::Color class

Four-channel color with 8-bit integers per component.

Stores the red, green, blue and alpha channels as std::uint8_t values in the range 0-255 (member order follows the host endianness so that the channels pack into a single 32-bit integer). Provides packing into RGBA/ARGB/ABGR/BGRA integers, hexadecimal and array construction, channel-wise and scalar arithmetic, and conversion to and from the normalized floating-point Colorf.

Constructors, destructors, conversion operators

Color() constexpr noexcept
Default constructor, creates a fully transparent black color.
Color(NoInitT) explicit noexcept
Color(std::uint8_t red, std::uint8_t green, std::uint8_t blue) constexpr noexcept
Constructs an opaque color from three channels.
Color(std::uint8_t red, std::uint8_t green, std::uint8_t blue, std::uint8_t alpha) constexpr noexcept
Constructs a color from four channels.
Color(std::uint32_t hex) explicit
Constructs an opaque color from a 24-bit hexadecimal RGB code.
Color(const std::uint8_t channels[NumChannels]) explicit
Constructs a color from a four-channel array.
Color(const Colorf& color) explicit

Public functions

auto Rgba() const -> std::uint32_t
Returns the color packed as a single RGBA integer.
auto Argb() const -> std::uint32_t
Returns the color packed as a single ARGB integer.
auto Abgr() const -> std::uint32_t
Returns the color packed as a single ABGR integer.
auto Bgra() const -> std::uint32_t
Returns the color packed as a single BGRA integer.
auto Data() const -> const std::uint8_t*
Returns a pointer to the channel array.
auto Data() -> std::uint8_t*
void Set(std::uint8_t red, std::uint8_t green, std::uint8_t blue, std::uint8_t alpha) constexpr
Sets all four channels.
void Set(std::uint8_t red, std::uint8_t green, std::uint8_t blue)
Sets the three color channels, keeping the alpha unchanged.
void Set(std::uint32_t hex)
Sets the channels from a 24-bit hexadecimal RGB code.
void SetVec(const std::uint8_t channels[NumChannels])
Sets all four channels from an array.
void SetAlpha(std::uint8_t alpha)
Sets the alpha channel.
auto operator=(const Colorf& color) -> Color&
Assigns from a normalized floating-point color.
auto operator==(const Color& color) const -> bool
auto operator!=(const Color& color) const -> bool
auto operator+=(const Color& v) -> Color&
auto operator-=(const Color& v) -> Color&
auto operator*=(const Color& color) -> Color&
auto operator*=(float scalar) -> Color&
Multiplies all channels by a scalar.
auto operator+(const Color& color) const -> Color
auto operator-(const Color& color) const -> Color
auto operator*(const Color& color) const -> Color
auto operator*(float scalar) const -> Color
Multiplies all channels by a scalar.

Public variables

std::uint8_t R
Red.
std::uint8_t G
Green.
std::uint8_t B
Blue.
std::uint8_t A
Alpha.

Constants

static std::int32_t NumChannels constexpr
Number of channels.

Predefined colors

static const Color Black
static const Color White
static const Color Red
static const Color Green
static const Color Blue
static const Color Yellow
static const Color Magenta
static const Color Cyan

Function documentation

std::uint8_t* nCine::Primitives::Color::Data()

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Color operator""_rgb(unsigned long long value) constexpr

8bit-per-channel RGB color literal

using namespace nCine::Primitives::Literals;
Color a = 0x5eb233bb_rgb;   // R: 0x5e, G: 0xb2, B: 0x33, A: 0xff

See Color for more information.

Color operator""_rgba(unsigned long long value) constexpr

8bit-per-channel RGBA color literal

using namespace nCine::Primitives::Literals;
Color a = 0x5eb233bb_rgba;  // R: 0x5e, G: 0xb2, B: 0x33, A: 0xbb

See Color for more information.