Visual Computing Library
Loading...
Searching...
No Matches
vcl::Color Class Reference

The Color class represents a 32 bit color. More...

#include <vclib/space/core/color.h>

Inheritance diagram for vcl::Color:

Public Types

enum class  Representation { INT_0_255 , FLOAT_0_1 }
 Color representation enumeration. More...
 
enum class  Format { ABGR , ARGB , RGBA , BGRA }
 Color format enumeration. More...
 
enum  ColorABGR : uint32_t {
  Black = 0xff000000 , DarkGray = 0xff404040 , Gray = 0xff808080 , LightGray = 0xffc0c0c0 ,
  White = 0xffffffff , Red = 0xff0000ff , Green = 0xff00ff00 , Blue = 0xffff0000 ,
  Yellow = 0xff00ffff , Cyan = 0xffffff00 , Magenta = 0xffff00ff , LightRed = 0xff8080ff ,
  LightGreen = 0xff80ff80 , LightBlue = 0xffff8080 , LightCyan = 0xffffff80 , LightYellow = 0xff80ffff ,
  LightMagenta = 0xffff80ff , DarkRed = 0xff000040 , DarkGreen = 0xff004000 , DarkBlue = 0xff400000 ,
  DarkCyan = 0xff404000 , DarkYellow = 0xff004040 , DarkMagenta = 0xff400040 , LightBrown = 0xff4080b0 ,
  DarkBrown = 0xff002040 , Brown = 0xff004080
}
 ABGR enum with some standard colors. More...
 
enum class  ColorMap { RedBlue , Parula , GreyShade }
 List of Color Maps supported by the vcl::Color.
 
- Public Types inherited from vcl::Point< Scalar, N >
using BaseMatrixType = Base
 
using ScalarType = Scalar
 The Scalar type of the Point.
 

Public Member Functions

 Color ()
 Default constructor. Initializes a black color (with alpha 255).
 
 Color (ColorABGR cc)
 
 Color (uint32_t cc, Format format)
 
 Color (uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha=255)
 Color constructor.
 
 Color (const Point4< uint8_t > &p)
 
uint8_t red () const
 Returns the red component of this color [0-255].
 
uint8_t green () const
 Returns the green component of this color [0-255].
 
uint8_t blue () const
 Returns the blue component of this color [0-255].
 
uint8_t alpha () const
 Returns the alpha component of this color [0-255].
 
uint8_tred ()
 Returns the red component of this color [0-255].
 
uint8_tgreen ()
 Returns the green component of this color [0-255].
 
uint8_tblue ()
 Returns the blue component of this color [0-255].
 
uint8_talpha ()
 Returns the alpha component of this color [0-255].
 
float redF () const
 Returns the float red component of this color [0-1].
 
float greenF () const
 Returns the float green component of this color [0-1].
 
float blueF () const
 Returns the float blue component of this color [0-1].
 
float alphaF () const
 Returns the float alpha component of this color [0-1].
 
uint8_t hsvHue () const
 Returns the hue color component of this color [0-359].
 
uint8_t hsvSaturation () const
 Returns the saturation color component of this color [0-255].
 
float hsvHueF () const
 Returns the float saturation color component of this color [0-1].
 
float hsvSaturationF () const
 Returns the float saturation color component of this color [0-1].
 
uint32_t abgr () const
 
uint32_t argb () const
 
uint32_t rgba () const
 
uint32_t bgra () const
 
unsigned short bgr5 () const
 Converts the color to an unsigned short in bgr5 format.
 
unsigned short rgb5 () const
 Converts the color to an unsigned short in rgb5 format.
 
void setAlpha (uint8_t alpha)
 Sets the alpha of this color [0-255].
 
void setRed (uint8_t red)
 Sets the red of this color [0-255].
 
void setGreen (uint8_t green)
 Sets the green of this color [0-255].
 
void setBlue (uint8_t blue)
 Sets the blue of this color [0-255].
 
void setRgb (uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha=255)
 Sets the RGB values of this color.
 
void set (uint32_t cc, Format fmt=Format::ABGR)
 
void setAbgr (uint32_t val)
 
void setArgb (uint32_t val)
 
void setRgba (uint32_t val)
 
void setBgra (uint32_t val)
 
void setBgr5 (unsigned short val)
 
void setRgb5 (unsigned short val)
 
void setHsv (uint8_t h, uint8_t s, uint8_t v, uint8_t alpha=255)
 Sets the HSV values of this color.
 
void setAlphaF (float alpha)
 Sets the alpha of this color [0-1].
 
void setRedF (float red)
 Sets the red of this color [0-1].
 
void setGreenF (float green)
 Sets the green of this color [0-1].
 
void setBlueF (float blue)
 Sets the blue of this color [0-1].
 
void setRgbF (float red, float green, float blue, float alpha=1.0)
 Sets the RGB values of this color.
 
void setHsvF (float hf, float sf, float vf, float alpha=1.0)
 Sets the HSV values of this color.
 
bool operator== (const Color &otherColor) const
 Returns true if this color has the same RGB and alpha values as otherColor; otherwise returns false.
 
bool operator!= (const Color &otherColor) const
 Returns false if this color has the same RGB and alpha values as otherColor; otherwise returns true.
 
bool operator< (const Color &otherColor) const
 Returns true if this color is less than otherColor follwing the RGBA order; otherwise returns false.
 
- Public Member Functions inherited from vcl::Point< Scalar, N >
 Point ()
 Constructs a Point object with all components set to zero.
 
template<typename OtherDerived >
 Point (const Eigen::MatrixBase< OtherDerived > &other)
 Constructs a Point object from an Eigen matrix.
 
template<typename... Scalars>
requires (sizeof...(scalars) == N)
 Point (Scalars... scalars)
 Constructs a Point object from a set of scalar values.
 
ScalarTypex ()
 Returns a reference to the x-component of the Point object.
 
const ScalarTypex () const
 Returns a const reference to the x-component of the Point object.
 
ScalarTypey ()
 Returns a reference to the y-component of the Point object.
 
const ScalarTypey () const
 Returns a const reference to the y-component of the Point object.
 
ScalarTypez ()
 Returns a reference to the z-component of the Point object.
 
const ScalarTypez () const
 Returns a const reference to the z-component of the Point object.
 
ScalarTypew ()
 Returns a reference to the w-component of the Point object.
 
const ScalarTypew () const
 Returns a const reference to the w-component of the Point object.
 
ScalarTypeat (uint i)
 
const ScalarTypeat (uint i) const
 
template<typename S >
auto cast () const
 Casts the Point object to a different scalar type.
 
bool isDegenerate () const
 Returns true if at least one of its components is NaN or inf.
 
bool epsilonEquals (const Point &p1, Scalar epsilon=std::numeric_limits< Scalar >::epsilon()) const
 Checks for the equality of two Point objects within a given epsilon tolerance.
 
Scalar angle (const Point &p1) const
 Computes the angle between two Point objects.
 
Scalar dist (const Point &p1) const
 Computes the Euclidean distance between two Point objects.
 
Scalar squaredDist (const Point &p1) const
 Computes the squared Euclidean distance between two Point objects.
 
Point mul (const Point &p1) const
 Multiplies the components of two Point objects.
 
Point div (const Point &p1) const
 Divides the components of two Point objects.
 
constexpr uint size () const
 Returns the size of the Point object.
 
template<typename... Scalars>
requires (sizeof...(scalars) == N)
void set (Scalars... scalars)
 Sets all the components of the Point object from a set of scalar values.
 
auto outerProduct (const Point &p1) const
 Returns the outer product between this point p and p1, which is p * p1^T.
 
void orthoBase (Point &u, Point &v) const
 Computes an Orthonormal Basis starting from this point n.
 
void serialize (std::ostream &os) const
 Serializes the point to the given output stream.
 
void deserialize (std::istream &is)
 Deserializes the point from the given input stream.
 
std::size_t hash () const
 Computes the hash value of the point.
 
template<typename OtherDerived >
Pointoperator= (const Eigen::MatrixBase< OtherDerived > &other)
 Assigns the point to the given Eigen matrix.
 
auto operator<=> (const Point &p1) const
 Compares the point with another point using the spaceship operator.
 
Point operator+ (const Scalar &s) const
 Adds a scalar value to each coordinate of the point.
 
Point operator- (const Scalar &s) const
 Subtracts a scalar value from each coordinate of the point.
 
Scalar operator* (const Point &p1) const
 Computes the dot product of this point with another point.
 
Point operator* (const Eigen::Matrix< Scalar, N+1, N+1 > &m) const
 Returns a new 3D point/vector on which has been applied a TRS 4x4 matrix.
 
Pointoperator+= (const Scalar &s)
 Adds a scalar value to this point.
 
Pointoperator-= (const Scalar &s)
 Subtracts a scalar value from this point.
 
Pointoperator*= (const Eigen::Matrix< Scalar, N+1, N+1 > &m)
 Applies a TRS 4x4 matrix transformation to this point.
 

Friends

std::ostream & operator<< (std::ostream &out, const Color &c)
 Overload of stream operator to allow a pretty print of a vcl::Color.
 

Additional Inherited Members

- Static Public Attributes inherited from vcl::Point< Scalar, N >
static const uint DIM = N
 DIM: the number of dimensions of the Point.
 

Detailed Description

The Color class represents a 32 bit color.

The class is a specialization of the Point4 class, where each component is an unsigned char (0, 1, 2, 3 are respectively the red, green, blue and alpha).

Internally, the class stores four unsigned chars (uint8_t), that can be reinterpreted as a single unsigned integer (uint32_t) that stores a color in the ABGR format.

The class provides some useful methods to convert the color from/to different formats.

Member Enumeration Documentation

◆ ColorABGR

ABGR enum with some standard colors.

It can be used to initialize a color with an ABGR integer.

◆ Format

Color format enumeration.

The color format enumeration is used to convert the color from/to different 32 bit formats. The notation used tells the order of the components in the integer representation of the color.

For example, the ABGR format means that the first byte of the integer represents the alpha, the second byte represents the blue, the third byte represents the green and the fourth byte represents the red.

◆ Representation

Color representation enumeration.

The color representation enumeration is used to convert the color from/to different representations. The notation used tells the range of the components in the representation.

For example, the INT_0_255 format means that the components are integers in the range [0-255], while the FLOAT_0_1 format means that the components are floating points in the range [0-1].

This enumeration is not used directly in this class, but it is useful in conversion functions that ask for the representation of the color.

Constructor & Destructor Documentation

◆ Color()

vcl::Color::Color ( uint8_t  red,
uint8_t  green,
uint8_t  blue,
uint8_t  alpha = 255 
)
inline

Color constructor.

Parameters
[in]redred component
[in]greengreen component
[in]blueblue component
[in]alphaalpha component (default 255)

Member Function Documentation

◆ alpha() [1/2]

uint8_t & vcl::Color::alpha ( )
inline

Returns the alpha component of this color [0-255].

Returns
alpha component of this color

◆ alpha() [2/2]

uint8_t vcl::Color::alpha ( ) const
inline

Returns the alpha component of this color [0-255].

Returns
alpha component of this color

◆ alphaF()

float vcl::Color::alphaF ( ) const
inline

Returns the float alpha component of this color [0-1].

Returns
float alpha component of this color

◆ bgr5()

unsigned short vcl::Color::bgr5 ( ) const
inline

Converts the color to an unsigned short in bgr5 format.

The first (most significant) 5 bits are for blue, the next 5 bits are for green, and the last 5 bits are for red.

Returns
an unsigned short containing the converted color.

◆ blue() [1/2]

uint8_t & vcl::Color::blue ( )
inline

Returns the blue component of this color [0-255].

Returns
blue component of this color

◆ blue() [2/2]

uint8_t vcl::Color::blue ( ) const
inline

Returns the blue component of this color [0-255].

Returns
blue component of this color

◆ blueF()

float vcl::Color::blueF ( ) const
inline

Returns the float blue component of this color [0-1].

Returns
float blue component of this color

◆ green() [1/2]

uint8_t & vcl::Color::green ( )
inline

Returns the green component of this color [0-255].

Returns
green component of this color

◆ green() [2/2]

uint8_t vcl::Color::green ( ) const
inline

Returns the green component of this color [0-255].

Returns
green component of this color

◆ greenF()

float vcl::Color::greenF ( ) const
inline

Returns the float green component of this color [0-1].

Returns
float green component of this color

◆ hsvHue()

uint8_t vcl::Color::hsvHue ( ) const
inline

Returns the hue color component of this color [0-359].

Returns
hue color component of this color

◆ hsvHueF()

float vcl::Color::hsvHueF ( ) const
inline

Returns the float saturation color component of this color [0-1].

Returns
float saturation color component of this color

◆ hsvSaturation()

uint8_t vcl::Color::hsvSaturation ( ) const
inline

Returns the saturation color component of this color [0-255].

Returns
saturation color component of this color

◆ hsvSaturationF()

float vcl::Color::hsvSaturationF ( ) const
inline

Returns the float saturation color component of this color [0-1].

Returns
float saturation color component of this color

◆ operator!=()

bool vcl::Color::operator!= ( const Color otherColor) const
inline

Returns false if this color has the same RGB and alpha values as otherColor; otherwise returns true.

Parameters
otherColor
Returns

◆ operator<()

bool vcl::Color::operator< ( const Color otherColor) const
inline

Returns true if this color is less than otherColor follwing the RGBA order; otherwise returns false.

Parameters
otherColor
Returns

◆ operator==()

bool vcl::Color::operator== ( const Color otherColor) const
inline

Returns true if this color has the same RGB and alpha values as otherColor; otherwise returns false.

Parameters
otherColor
Returns

◆ red() [1/2]

uint8_t & vcl::Color::red ( )
inline

Returns the red component of this color [0-255].

Returns
red component of this color

◆ red() [2/2]

uint8_t vcl::Color::red ( ) const
inline

Returns the red component of this color [0-255].

Returns
red component of this color

◆ redF()

float vcl::Color::redF ( ) const
inline

Returns the float red component of this color [0-1].

Returns
float red component of this color

◆ rgb5()

unsigned short vcl::Color::rgb5 ( ) const
inline

Converts the color to an unsigned short in rgb5 format.

The first (most significant) 5 bits are for red, the next 5 bits are for green, and the last 5 bits are for blue.

Returns
an unsigned short containing the converted color.

◆ setAlpha()

void vcl::Color::setAlpha ( uint8_t  alpha)
inline

Sets the alpha of this color [0-255].

Parameters
[in]alpha

◆ setAlphaF()

void vcl::Color::setAlphaF ( float  alpha)
inline

Sets the alpha of this color [0-1].

Parameters
[in]alpha

◆ setBgr5()

void vcl::Color::setBgr5 ( unsigned short  val)
inline

Set the color values from an unsigned 5-5-5 BGR value.

The input value is interpreted as follows:

  • The 5 least significant bits represent the red component.
  • The next 5 bits represent the green component.
  • The 5 most significant bits represent the blue component.

Each color component is scaled from 0 to 255 by multiplying the value by 8.

Parameters
[in]valThe unsigned 5-5-5 BGR value to set.

◆ setBlue()

void vcl::Color::setBlue ( uint8_t  blue)
inline

Sets the blue of this color [0-255].

Parameters
[in]blue

◆ setBlueF()

void vcl::Color::setBlueF ( float  blue)
inline

Sets the blue of this color [0-1].

Parameters
[in]blue

◆ setGreen()

void vcl::Color::setGreen ( uint8_t  green)
inline

Sets the green of this color [0-255].

Parameters
[in]green

◆ setGreenF()

void vcl::Color::setGreenF ( float  green)
inline

Sets the green of this color [0-1].

Parameters
[in]green

◆ setHsv()

void vcl::Color::setHsv ( uint8_t  h,
uint8_t  s,
uint8_t  v,
uint8_t  alpha = 255 
)
inline

Sets the HSV values of this color.

All the values must be in the range 0-255.

Parameters
[in]h
[in]s
[in]v
[in]alpha

◆ setHsvF()

void vcl::Color::setHsvF ( float  hf,
float  sf,
float  vf,
float  alpha = 1.0 
)
inline

Sets the HSV values of this color.

All the values must be in the range 0-1. The color will be converted in RGBA and then stored in this color.

Parameters
[in]hf
[in]sf
[in]vf
[in]alpha

◆ setRed()

void vcl::Color::setRed ( uint8_t  red)
inline

Sets the red of this color [0-255].

Parameters
[in]red

◆ setRedF()

void vcl::Color::setRedF ( float  red)
inline

Sets the red of this color [0-1].

Parameters
[in]red

◆ setRgb()

void vcl::Color::setRgb ( uint8_t  red,
uint8_t  green,
uint8_t  blue,
uint8_t  alpha = 255 
)
inline

Sets the RGB values of this color.

All the values must be in the range 0-255.

Parameters
[in]red
[in]green
[in]blue
[in]alpha

◆ setRgb5()

void vcl::Color::setRgb5 ( unsigned short  val)
inline

Set the color values from an unsigned 5-5-5 RGB value.

The input value is interpreted as follows:

  • The 5 least significant bits represent the blue component.
  • The next 5 bits represent the green component.
  • The 5 most significant bits represent the red component.

Each color component is scaled from 0 to 255 by multiplying the value by 8.

Parameters
[in]valThe unsigned 5-5-5 RGB value to set.

◆ setRgbF()

void vcl::Color::setRgbF ( float  red,
float  green,
float  blue,
float  alpha = 1.0 
)
inline

Sets the RGB values of this color.

All the values must be in the range 0-1.

Parameters
[in]redred component of the color in as a float value between 0 and 1.
[in]greengreen component of the color in as a float value between 0 and 1.
[in]blueblue component of the color in as a float value between 0 and 1.
[in]alpha: alpha component of the color in as a float value between 0 and 1.

The documentation for this class was generated from the following file: