VisuTwin Canvas
C++ 3D Engine — Metal Backend
Loading...
Searching...
No Matches
visutwin::canvas::Color Struct Reference

RGBA color with floating-point components in [0, 1]. More...

#include <core/math/color.h>

Public Member Functions

Color clone () const
 Color (const Color &other)=default
 Color (const float *arr, size_t size)
 Color (const float r=0.0f, const float g=0.0f, const float b=0.0f, const float a=1.0f)
 Color (const std::vector< float > &arr)
Colorcopy (const Color &rhs)
bool equals (const Color &rhs) const
ColorfromArray (const float *arr, size_t size, size_t offset=0)
ColorfromArray (const std::vector< float > &arr, size_t offset=0)
ColorfromString (const std::string &hex)
Colorgamma (const Color *src=nullptr)
Colorlerp (const Color &lhs, const Color &rhs, float alpha)
Colorlinear (const Color *src=nullptr)
ColormulScalar (float scalar)
bool operator!= (const Color &other) const
Coloroperator= (const Color &other)=default
bool operator== (const Color &other) const
Colorset (float r, float g, float b, float a=1.0f)
void toArray (float *arr, size_t size, size_t offset=0, bool alpha=true) const
std::vector< float > toArray (std::vector< float > arr={}, size_t offset=0, bool alpha=true) const
std::string toString (bool alpha=false, bool asArray=false) const

Public Attributes

float a
float b
float g
float r

Static Public Attributes

static const Color BLACK
static const Color BLUE
static const Color CYAN
static const Color GRAY
static const Color GREEN
static const Color MAGENTA
static const Color RED
static const Color WHITE
static const Color YELLOW

Detailed Description

RGBA color with floating-point components in [0, 1].

The r, g, b components define a color in RGB space. The a (alpha) component defines transparency (1 = fully opaque, 0 = fully transparent).

Definition at line 18 of file color.h.

Constructor & Destructor Documentation

◆ Color() [1/4]

visutwin::canvas::Color::Color ( const float r = 0.0f,
const float g = 0.0f,
const float b = 0.0f,
const float a = 1.0f )
inlineexplicit

Definition at line 27 of file color.h.

References a, b, g, and r.

Referenced by clone(), Color(), copy(), equals(), fromArray(), fromArray(), fromString(), gamma(), lerp(), linear(), mulScalar(), operator!=(), operator=(), operator==(), and set().

◆ Color() [2/4]

visutwin::canvas::Color::Color ( const std::vector< float > & arr)
explicit

Creates a new Color instance from an array.

Parameters
arrThe array to set the color values from (must have at least 3 elements).

Definition at line 22 of file color.cpp.

References a, b, g, and r.

◆ Color() [3/4]

visutwin::canvas::Color::Color ( const float * arr,
size_t size )

Creates a new Color instance from a C-style array.

Parameters
arrThe array to set the color values from.
sizeSize of the array (3 or 4).

Definition at line 37 of file color.cpp.

References a, b, g, and r.

◆ Color() [4/4]

visutwin::canvas::Color::Color ( const Color & other)
default

Copy constructor

References Color().

Member Function Documentation

◆ clone()

Color visutwin::canvas::Color::clone ( ) const
nodiscard

Returns a clone of the specified color.

Returns
A duplicate color object.

Definition at line 52 of file color.cpp.

References a, b, Color(), g, and r.

◆ copy()

Color & visutwin::canvas::Color::copy ( const Color & rhs)

Copies the contents of a source color to a destination color.

Parameters
rhsA color to copy to the specified color.
Returns
Self for chaining.

Definition at line 57 of file color.cpp.

References a, b, Color(), g, and r.

◆ equals()

bool visutwin::canvas::Color::equals ( const Color & rhs) const
nodiscard

Reports whether two colors are equal.

Parameters
rhsThe color to compare to the specified color.
Returns
True if the colors are equal and false otherwise.

Definition at line 66 of file color.cpp.

References a, b, Color(), g, and r.

Referenced by operator!=(), and operator==().

◆ fromArray() [1/2]

Color & visutwin::canvas::Color::fromArray ( const float * arr,
size_t size,
size_t offset = 0 )

Set the values of the color from a C-style array.

Parameters
arrThe array to set the color values from.
sizeSize of the array.
offsetThe zero-based index at which to start copying elements from the array. Default is 0.
Returns
Self for chaining.

Definition at line 158 of file color.cpp.

References a, b, Color(), g, and r.

◆ fromArray() [2/2]

Color & visutwin::canvas::Color::fromArray ( const std::vector< float > & arr,
size_t offset = 0 )

Set the values of the color from an array.

Parameters
arrThe array to set the color values from.
offsetThe zero-based index at which to start copying elements from the array. Default is 0.
Returns
Self for chaining.

Definition at line 149 of file color.cpp.

References a, b, Color(), g, and r.

◆ fromString()

Color & visutwin::canvas::Color::fromString ( const std::string & hex)

Set the values of the color from a string representation '#11223344' or '#112233'.

Parameters
hexA string representation in the format '#RRGGBBAA' or '#RRGGBB'. Where RR, GG, BB, AA are red, green, blue and alpha values. This is the same format used in HTML/CSS.
Returns
Self for chaining.

Definition at line 117 of file color.cpp.

References Color(), and set().

◆ gamma()

Color & visutwin::canvas::Color::gamma ( const Color * src = nullptr)

Converts the color from linear to gamma color space.

Parameters
srcThe color to convert to gamma color space. If not set, the operation is done in place.
Returns
Self for chaining.

Definition at line 99 of file color.cpp.

References a, b, Color(), g, and r.

Referenced by visutwin::canvas::GlbParser::createFromModel(), visutwin::canvas::GlbParser::createFromPrepared(), and visutwin::canvas::GlbParser::parse().

◆ lerp()

Color & visutwin::canvas::Color::lerp ( const Color & lhs,
const Color & rhs,
float alpha )

Returns the result of a linear interpolation between two specified colors.

Parameters
lhsThe color to interpolate from.
rhsThe color to interpolate to.
alphaThe value controlling the point of interpolation. Between 0 and 1, the linear interpolant will occur on a straight line between lhs and rhs. Outside of this range, the linear interpolant will occur on a ray extrapolated from this line.
Returns
Self for chaining.

Definition at line 80 of file color.cpp.

References a, b, Color(), g, and r.

◆ linear()

Color & visutwin::canvas::Color::linear ( const Color * src = nullptr)

Converts the color from gamma to linear color space.

Parameters
srcThe color to convert to linear color space. If not set, the operation is done in place.
Returns
Self for chaining.

Definition at line 89 of file color.cpp.

References a, b, Color(), g, and r.

Referenced by visutwin::canvas::MetalUniformBinder::setLightingUniforms().

◆ mulScalar()

Color & visutwin::canvas::Color::mulScalar ( float scalar)

Multiplies RGB elements of a Color by a number. Note that the alpha value is left unchanged.

Parameters
scalarThe number to multiply by.
Returns
Self for chaining.

Definition at line 109 of file color.cpp.

References b, Color(), g, and r.

◆ operator!=()

bool visutwin::canvas::Color::operator!= ( const Color & other) const

Definition at line 246 of file color.cpp.

References Color(), and equals().

◆ operator=()

Color & visutwin::canvas::Color::operator= ( const Color & other)
default

References a, b, Color(), g, and r.

◆ operator==()

bool visutwin::canvas::Color::operator== ( const Color & other) const

Definition at line 241 of file color.cpp.

References Color(), and equals().

◆ set()

Color & visutwin::canvas::Color::set ( float r,
float g,
float b,
float a = 1.0f )

Assign values to the color components, including alpha.

Parameters
rThe value for red (0-1).
gThe value for green (0-1).
bThe value for blue (0-1).
aThe value for the alpha (0-1), defaults to 1.
Returns
Self for chaining.

Definition at line 71 of file color.cpp.

References a, b, Color(), g, and r.

Referenced by fromString().

◆ toArray() [1/2]

void visutwin::canvas::Color::toArray ( float * arr,
size_t size,
size_t offset = 0,
bool alpha = true ) const

Copies the color to a C-style array.

Parameters
arrThe array to populate with the color's number components.
sizeSize of the target array.
offsetThe zero-based index at which to start copying elements to the array. Default is 0.
alphaIf true, the output array will include the alpha value.

Definition at line 220 of file color.cpp.

References a, b, g, and r.

◆ toArray() [2/2]

std::vector< float > visutwin::canvas::Color::toArray ( std::vector< float > arr = {},
size_t offset = 0,
bool alpha = true ) const
nodiscard

Converts the color to an array.

Parameters
arrThe array to populate with the color's number components. If empty, a new array is created.
offsetThe zero-based index at which to start copying elements to the array. Default is 0.
alphaIf true, the output array will include the alpha value.
Returns
The color as an array.

Definition at line 202 of file color.cpp.

References a, b, g, and r.

◆ toString()

std::string visutwin::canvas::Color::toString ( bool alpha = false,
bool asArray = false ) const
nodiscard

Converts the color to a string form. The format is '#RRGGBBAA', where RR, GG, BB, AA are the red, green, blue and alpha values. When the alpha value is not included (the default), this is the same format as used in HTML/CSS.

Parameters
alphaIf true, the output string will include the alpha value.
asArrayIf true, the output will be an array of numbers. Defaults to false.
Returns
The color in string form.

Definition at line 167 of file color.cpp.

References a, b, g, and r.

Member Data Documentation

◆ a

◆ b

◆ BLACK

const Color visutwin::canvas::Color::BLACK
static

Definition at line 174 of file color.h.

◆ BLUE

const Color visutwin::canvas::Color::BLUE
static

Definition at line 175 of file color.h.

◆ CYAN

const Color visutwin::canvas::Color::CYAN
static

Definition at line 176 of file color.h.

◆ g

◆ GRAY

const Color visutwin::canvas::Color::GRAY
static

Definition at line 177 of file color.h.

◆ GREEN

const Color visutwin::canvas::Color::GREEN
static

Definition at line 178 of file color.h.

◆ MAGENTA

const Color visutwin::canvas::Color::MAGENTA
static

Definition at line 179 of file color.h.

◆ r

◆ RED

const Color visutwin::canvas::Color::RED
static

Definition at line 180 of file color.h.

◆ WHITE

const Color visutwin::canvas::Color::WHITE
static

Definition at line 181 of file color.h.

◆ YELLOW

const Color visutwin::canvas::Color::YELLOW
static

Definition at line 182 of file color.h.


The documentation for this struct was generated from the following files: