template<class T = Float>
Magnum::Math::Color4 class

Color in linear RGBA color space.

See Color3 for more information.

Base classes

template<class T>
class Vector4<Float>
Four-component vector.

Public types

using FloatingPointType = Color3<T>::FloatingPointType
Corresponding floating-point type.

Public static functions

static auto red(T red = Implementation::fullChannel<T>(), T alpha = Implementation::fullChannel<T>()) -> Color4<T> constexpr
Red color.
static auto green(T green = Implementation::fullChannel<T>(), T alpha = Implementation::fullChannel<T>()) -> Color4<T> constexpr
Green color.
static auto blue(T blue = Implementation::fullChannel<T>(), T alpha = Implementation::fullChannel<T>()) -> Color4<T> constexpr
Blue color.
static auto cyan(T red = T(0), T alpha = Implementation::fullChannel<T>()) -> Color4<T> constexpr
Cyan color.
static auto magenta(T green = T(0), T alpha = Implementation::fullChannel<T>()) -> Color4<T> constexpr
Magenta color.
static auto yellow(T blue = T(0), T alpha = Implementation::fullChannel<T>()) -> Color4<T> constexpr
Yellow color.
static auto fromHsv(const ColorHsv<FloatingPointType>& hsv, T a = Implementation::fullChannel<T>()) -> Color4<T>
Create RGB color from HSV representation.
static auto fromSrgbAlpha(const Vector4<FloatingPointType>& srgbAlpha) -> Color4<T>
Create linear RGBA color from sRGB + alpha representation.
template<class Integral>
static auto fromSrgbAlpha(const Vector4<Integral>& srgbAlpha) -> Color4<T>
Create linear RGB color from integral sRGB + alpha representation.
static auto fromSrgb(const Vector3<FloatingPointType>& srgb, T a = Implementation::fullChannel<T>()) -> Color4<T>
Create linear RGBA color from sRGB representation.
template<class Integral>
static auto fromSrgb(const Vector3<Integral>& srgb, T a = Implementation::fullChannel<T>()) -> Color4<T>
Create linear RGB color from integral sRGB representation.
static auto fromSrgbAlphaInt(UnsignedInt srgbAlpha) -> Color4<T> new in Git master
Create linear RGBA color from 32-bit sRGB + alpha representation.
static auto fromSrgbAlpha(UnsignedInt srgb) -> Color4<T> deprecated in Git master
Create linear RGBA color from 32-bit sRGB + alpha representation.
static auto fromSrgbInt(UnsignedInt srgb, T a = Implementation::fullChannel<T>()) -> Color4<T>
Create linear RGBA color from 24-bit sRGB + alpha representation.
static auto fromSrgb(UnsignedInt srgb, T a = Implementation::fullChannel<T>()) -> Color4<T> deprecated in Git master
Create linear RGBA color from 24-bit sRGB + alpha representation.
static auto fromLinearRgbaInt(UnsignedInt linear) -> Color4<T> new in Git master
Create linear RGBA color from 32-bit linear representation.
static auto fromLinearRgbInt(UnsignedInt linear, T a = Implementation::fullChannel<T>()) -> Color4<T> new in Git master
Create linear RGBA color from 24-bit linear RGB + alpha representation.
static auto fromXyz(const Vector3<FloatingPointType> xyz, T a = Implementation::fullChannel<T>()) -> Color4<T>
Create RGBA color from CIE XYZ representation

Constructors, destructors, conversion operators

Color4() constexpr noexcept
Default constructor.
Color4(ZeroInitT) explicit constexpr noexcept
Construct a zero color.
Color4(Magnum::NoInitT) explicit noexcept
Construct a vector without initializing the contents.
Color4(T rgb, T alpha = Implementation::fullChannel<T>()) explicit constexpr noexcept
Gray constructor.
Color4(T r, T g, T b, T a = Implementation::fullChannel<T>()) constexpr noexcept
Constructor.
Color4(const Vector3<T>& rgb, T a = Implementation::fullChannel<T>()) constexpr noexcept
Constructor.
template<class U>
Color4(const Vector<4, U>& other) explicit constexpr noexcept
Construct a vector from another of different type.
Color4(const BitVector4& other) explicit constexpr noexcept new in Git master
Construct a vector from a BitVector.
template<class U, class = decltype(Implementation::VectorConverter<4, T, U>::from(std::declval<U>()))>
Color4(const U& other) explicit constexpr
Construct a color from external representation.
Color4(const Vector<4, T>& other) constexpr noexcept
Copy constructor.

Public functions

auto toHsv() const -> ColorHsv<FloatingPointType>
Convert to HSV representation.
auto hue() const -> Deg<FloatingPointType>
Hue.
auto saturation() const -> FloatingPointType
Saturation.
auto value() const -> FloatingPointType
Value.
auto toSrgbAlpha() const -> Vector4<FloatingPointType>
Convert to sRGB + alpha representation.
template<class Integral>
auto toSrgbAlpha() const -> Vector4<Integral>
Convert to integral sRGB + alpha representation.
auto toSrgbAlphaInt() const -> UnsignedInt
Convert to 32-bit integral sRGB + linear alpha representation.
auto toLinearRgbaInt() const -> UnsignedInt
Convert to 32-bit integral linear RGBA representation.
auto toXyz() const -> Vector3<FloatingPointType>
Convert to CIE XYZ representation

Typedef documentation

template<class T>
typedef Color3<T>::FloatingPointType Magnum::Math::Color4<T>::FloatingPointType

Corresponding floating-point type.

For HSV and other color spaces.

Function documentation

template<class T>
static Color4<T> Magnum::Math::Color4<T>::red(T red = Implementation::fullChannel<T>(), T alpha = Implementation::fullChannel<T>()) constexpr

Red color.

Convenience alternative to e.g. Color4{red, 0.0f, 0.0f, alpha}.

template<class T>
static Color4<T> Magnum::Math::Color4<T>::green(T green = Implementation::fullChannel<T>(), T alpha = Implementation::fullChannel<T>()) constexpr

Green color.

Convenience alternative to e.g. Color4{0.0f, green, 0.0f, alpha}.

template<class T>
static Color4<T> Magnum::Math::Color4<T>::blue(T blue = Implementation::fullChannel<T>(), T alpha = Implementation::fullChannel<T>()) constexpr

Blue color.

Convenience alternative to e.g. Color4{0.0f, 0.0f, blue, alpha}.

template<class T>
static Color4<T> Magnum::Math::Color4<T>::cyan(T red = T(0), T alpha = Implementation::fullChannel<T>()) constexpr

Cyan color.

Convenience alternative to e.g. Color4{red, 1.0f, 1.0f, alpha}.

template<class T>
static Color4<T> Magnum::Math::Color4<T>::magenta(T green = T(0), T alpha = Implementation::fullChannel<T>()) constexpr

Magenta color.

Convenience alternative to e.g. Color4{1.0f, green, 1.0f, alpha}.

template<class T>
static Color4<T> Magnum::Math::Color4<T>::yellow(T blue = T(0), T alpha = Implementation::fullChannel<T>()) constexpr

Yellow color.

Convenience alternative to e.g. Color4{1.0f, 1.0f, blue, alpha}.

template<class T>
static Color4<T> Magnum::Math::Color4<T>::fromHsv(const ColorHsv<FloatingPointType>& hsv, T a = Implementation::fullChannel<T>())

Create RGB color from HSV representation.

Parameters
hsv Color in HSV color space
a Alpha value, defaults to 1.0 for floating-point types and maximum positive value for integral types

Hue can overflow the range $ [0.0, 360.0] $ .

template<class T>
static Color4<T> Magnum::Math::Color4<T>::fromSrgbAlpha(const Vector4<FloatingPointType>& srgbAlpha)

Create linear RGBA color from sRGB + alpha representation.

Parameters
srgbAlpha Color in sRGB color space with linear alpha

Applies inverse sRGB curve onto RGB channels of the input, alpha channel is assumed to be linear. See Color3::fromSrgb() for more information.

template<class T> template<class Integral>
static Color4<T> Magnum::Math::Color4<T>::fromSrgbAlpha(const Vector4<Integral>& srgbAlpha)

Create linear RGB color from integral sRGB + alpha representation.

Parameters
srgbAlpha Color in sRGB color space with linear alpha

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Useful in cases where you have for example an 8-bit sRGB + alpha representation and want to create a floating-point linear RGBA color out of it:

Vector4ub srgbAlpha;
auto rgba = Color4::fromSrgbAlpha(srgbAlpha);

For conversion from a linear 32-bit representation (i.e, without applying the sRGB curve), use unpack():

Color4ub a{0xff, 0x33, 0x66, 0x99};
Color4 bFromSrgb = Color4::fromSrgbAlpha(a);  // {1.0f, 0.0331f, 0.1329f, 0.6f}
Color4 bFromLinear = Math::unpack<Color4>(a); // {1.0f, 0.2f, 0.4f, 0.6f}

template<class T>
static Color4<T> Magnum::Math::Color4<T>::fromSrgb(const Vector3<FloatingPointType>& srgb, T a = Implementation::fullChannel<T>())

Create linear RGBA color from sRGB representation.

Parameters
srgb Color in sRGB color space
a Alpha value, defaults to 1.0 for floating-point types and maximum positive value for integral types

Applies inverse sRGB curve onto RGB channels of the input. Alpha value is taken as-is. See Color3::fromSrgb() for more information.

template<class T> template<class Integral>
static Color4<T> Magnum::Math::Color4<T>::fromSrgb(const Vector3<Integral>& srgb, T a = Implementation::fullChannel<T>())

Create linear RGB color from integral sRGB representation.

Parameters
srgb Color in sRGB color space
a Linear alpha value, defaults to 1.0 for floating-point types and maximum positive value for integral types

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Same as above, but with alpha as a separate parameter.

template<class T>
static Color4<T> Magnum::Math::Color4<T>::fromSrgbAlphaInt(UnsignedInt srgbAlpha) new in Git master

Create linear RGBA color from 32-bit sRGB + alpha representation.

Parameters
srgbAlpha 32-bit sRGB color with linear alpha

See Color3::fromSrgbInt() for more information and toSrgbAlphaInt() for an inverse operation. There's also a operator""_srgbaf() that does this conversion directly from hexadecimal literals. The following two statements are equivalent:

Color4 a = Color4::fromSrgbAlphaInt(0xff336680);
Color4 b = 0xff336680_srgbaf;

Note that the integral value is endian-dependent (the red channel being in the last byte on little-endian platforms), for conversion from an endian-independent sRGB / linear representation use fromSrgbAlpha(const Vector4<Integral>&) / unpack().

template<class T>
static Color4<T> Magnum::Math::Color4<T>::fromSrgbAlpha(UnsignedInt srgb)

Create linear RGBA color from 32-bit sRGB + alpha representation.

template<class T>
static Color4<T> Magnum::Math::Color4<T>::fromSrgbInt(UnsignedInt srgb, T a = Implementation::fullChannel<T>())

Create linear RGBA color from 24-bit sRGB + alpha representation.

Parameters
srgb 24-bit sRGB color
a Linear alpha value, defaults to 1.0 for floating-point types and maximum positive value for integral types

Same as above, but with alpha as a separate parameter.

template<class T>
static Color4<T> Magnum::Math::Color4<T>::fromSrgb(UnsignedInt srgb, T a = Implementation::fullChannel<T>())

Create linear RGBA color from 24-bit sRGB + alpha representation.

template<class T>
static Color4<T> Magnum::Math::Color4<T>::fromLinearRgbaInt(UnsignedInt linear) new in Git master

Create linear RGBA color from 32-bit linear representation.

Parameters
linear 32-bit linear RGBA color

Compared to fromSrgbAlphaInt() does not peform a sRGB conversion on the input. See toLinearRgbaInt() for an inverse operation, there's also a operator""_rgbaf() that does this conversion directly from hexadecimal literals. The following two statements are equivalent:

Color4 a = Color4::fromLinearRgbaInt(0xff336680);
Color4 b = 0xff336680_rgbaf;

Note that the integral value is endian-dependent (the red channel being in the last byte on little-endian platforms), for conversion from endian-independent linear RGBA representation use unpack<Color4>() on a Color4ub input.

template<class T>
static Color4<T> Magnum::Math::Color4<T>::fromLinearRgbInt(UnsignedInt linear, T a = Implementation::fullChannel<T>()) new in Git master

Create linear RGBA color from 24-bit linear RGB + alpha representation.

Parameters
linear 24-bit linear RGB color
a Linear alpha value, defaults to 1.0 for floating-point types and maximum positive value for integral types

Same as above, but with alpha as a separate parameter.

template<class T>
static Color4<T> Magnum::Math::Color4<T>::fromXyz(const Vector3<FloatingPointType> xyz, T a = Implementation::fullChannel<T>())

Create RGBA color from CIE XYZ representation

Parameters
xyz Color in CIE XYZ color space
a Alpha value, defaults to 1.0 for floating-point types and maximum positive value for integral types

Applies transformation matrix, returning the input in linear RGB color space. See Color3::fromXyz() for more information.

template<class T>
Magnum::Math::Color4<T>::Color4() constexpr noexcept

Default constructor.

Equivalent to Color4(ZeroInitT).

template<class T>
Magnum::Math::Color4<T>::Color4(ZeroInitT) explicit constexpr noexcept

Construct a zero color.

All components are set to zero.

template<class T>
Magnum::Math::Color4<T>::Color4(T rgb, T alpha = Implementation::fullChannel<T>()) explicit constexpr noexcept

Gray constructor.

Parameters
rgb RGB value
alpha Alpha value, defaults to 1.0 for floating-point types and maximum positive value for integral types

template<class T>
Magnum::Math::Color4<T>::Color4(T r, T g, T b, T a = Implementation::fullChannel<T>()) constexpr noexcept

Constructor.

Parameters
r R value
g G value
b B value
a A value, defaults to 1.0 for floating-point types and maximum positive value for integral types.

template<class T>
Magnum::Math::Color4<T>::Color4(const Vector3<T>& rgb, T a = Implementation::fullChannel<T>()) constexpr noexcept

Constructor.

Parameters
rgb Three-component color
a A value

template<class T> template<class U>
Magnum::Math::Color4<T>::Color4(const Vector<4, U>& other) explicit constexpr noexcept

Construct a vector from another of different type.

Performs only default casting on the values, no rounding or anything else. Example usage:

Vector4 floatingPoint{1.3f, 2.7f, -15.0f, 7.0f};
Vector4i integral{floatingPoint}; // {1, 2, -15, 7}

template<class T>
Magnum::Math::Color4<T>::Color4(const BitVector4& other) explicit constexpr noexcept new in Git master

Construct a vector from a BitVector.

Bits that are unset are converted to 0, set bits to 1. If you need a different behavior, for example converting a bit mask to 0 or 255 for a color representation, use lerp(const Vector<size, T>&, const Vector<size, T>&, const BitVector<size>&) instead, for example:

BitVector3 mask = ;
Vector3ub a = Math::lerp(Vector3ub{0}, Vector3ub{255}, mask);

template<class T>
ColorHsv<FloatingPointType> Magnum::Math::Color4<T>::toHsv() const

Convert to HSV representation.

The alpha channel is not subject to any conversion, so it is ignored.

template<class T>
Deg<FloatingPointType> Magnum::Math::Color4<T>::hue() const

Hue.

Returns Hue in range $ [0.0\degree, 360.0\degree] $ .

template<class T>
FloatingPointType Magnum::Math::Color4<T>::saturation() const

Saturation.

Returns Saturation in range $ [0.0, 1.0] $ .

template<class T>
FloatingPointType Magnum::Math::Color4<T>::value() const

Value.

Returns Value in range $ [0.0, 1.0] $ .

template<class T>
Vector4<FloatingPointType> Magnum::Math::Color4<T>::toSrgbAlpha() const

Convert to sRGB + alpha representation.

Assuming the color is in linear RGB, applies sRGB curve onto the RGB channels, returning the color represented in sRGB color space. Alpha channel is kept linear. See Color3::toSrgb() for more information.

template<class T> template<class Integral>
Vector4<Integral> Magnum::Math::Color4<T>::toSrgbAlpha() const

Convert to integral sRGB + alpha representation.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Useful in cases where you have a floating-point linear RGBA color and want to create for example an 8-bit sRGB + alpha representation out of it:

Color4 color;
Vector4ub srgbAlpha = color.toSrgbAlpha<UnsignedByte>();

For conversion to a linear 32-bit representation (i.e, without applying the sRGB curve), use pack():

Color4 a{1.0f, 0.2f, 0.4f, 0.6f};
Vector4ub bSrgb = a.toSrgbAlpha<UnsignedByte>(); // {0xff, 0x7c, 0xaa, 0x99}
Color4ub bLinear = Math::pack<Color4ub>(a);      // {0xff, 0x33, 0x66, 0x99}

template<class T>
UnsignedInt Magnum::Math::Color4<T>::toSrgbAlphaInt() const

Convert to 32-bit integral sRGB + linear alpha representation.

See Color3::toSrgb() const for more information and fromSrgbAlphaInt() for an inverse operation. Use rgb() together with Color3::toSrgbInt() to output a 24-bit sRGB color. Note that the integral value is endian-dependent (the red channel being in the last byte on little-endian platforms), for conversion to an endian-independent sRGB representation use toSrgbAlpha<UnsignedByte>() const.

template<class T>
UnsignedInt Magnum::Math::Color4<T>::toLinearRgbaInt() const

Convert to 32-bit integral linear RGBA representation.

Compared to toSrgbAlphaInt() does not perform a sRGB conversion on the output. See fromLinearRgbaInt() for an inverse operation. Use rgb() together with Color3::toLinearRgbInt() to output a 24-bit linear RGBA color. Note that the integral value is endian-dependent (the red channel being in the last byte on little-endian platforms), for conversion to an endian-independent linear representation use pack<Color4ub>().

template<class T>
Vector3<FloatingPointType> Magnum::Math::Color4<T>::toXyz() const

Convert to CIE XYZ representation

Assuming the color is in linear RGB, applies transformation matrix, returning the color in CIE XYZ color space. The alpha channel is not subject to any conversion, so it is ignored. See Color3::toXyz() for more information.

Please note that xyz(), x(), y() and z() do not correspond to primaries in CIE XYZ color space, but are rather aliases to rgb(), r(), g() and b().

template<class T>
Color4<UnsignedByte> operator""_rgba(unsigned long long value) constexpr

8bit-per-channel linear RGBA literal

Unpacks the literal into four 8-bit values. Example usage:

using namespace Math::Literals;
Color4ub a = 0x33b27fcc_rgba;   // {0x33, 0xb2, 0x7f, 0xcc}

template<class T>
Vector4<UnsignedByte> operator""_srgba(unsigned long long value) constexpr

8bit-per-channel sRGB + alpha literal

Unpacks the literal into four 8-bit values without any colorspace conversion. Behaves identically to operator""_rgba() though it doesn't return a Color4 type to indicate that the resulting value is not linear RGBA. Use this literal to document that given value is in sRGB + alpha. Example usage:

using namespace Math::Literals;
Vector4ub a = 0x33b27fcc_srgba; // {0x33, 0xb2, 0x7f, 0xcc}

template<class T>
Color4<Float> operator""_rgbaf(unsigned long long value) constexpr

Float linear RGBA literal.

Equivalent to calling Color4::fromLinearRgbaInt() on the literal value. Example usage:

using namespace Math::Literals;
Color4 a = 0x33b27fcc_rgbaf;    // {0.2f, 0.698039f, 0.498039f, 0.8f}

template<class T>
Color4<Float> operator""_srgbaf(unsigned long long value)

Float sRGB + alpha literal.

Calls Color4::fromSrgbAlphaInt() on the literal value. Example usage:

using namespace Math::Literals;
Color4 a = 0x33b27fcc_srgbaf;   // {0.0331048f, 0.445201f, 0.212231f, 0.8f}

template<class T>
Debug& operator<<(Debug& debug, const Color4<UnsignedByte>& value)

Debug output operator.

If Debug::Flag::Color is enabled or Debug::color was set immediately before, prints the value as an ANSI 24bit color escape sequence using two successive Unicode block characters (to have it roughly square). To preserve at least some information when text is copied, the square consists of one of the five ░▒▓█ shades. The square shade is calculated as a product of Color4::value() and Color4::a(). If calculated color value is less than alpha, the colored square has the color set for both background and foreground, otherwise the background is left at the default.

If Debug::Flag::Color is enabled and Debug::Flag::DisableColors is set, only the shaded character is used, without any ANSI color escape sequence.

If Debug::Flag::Color is not enabled, the value is printed as a hex color (e.g. #ff33aaff). Other underlying types are handled by operator<<(Debug&, const Vector<size, T>&).

For example, the following snippet:

Debug{Debug::Flag::Color}
    << 0x3bd26700_rgba << 0x3bd26733_rgba << 0x3bd26766_rgba
    << 0x3bd26799_rgba << 0x3bd267cc_rgba << 0x3bd267ff_rgba;

prints the following on terminals that support it:

      ░░ ▒▒ ▓▓ ██