Audacity 3.2.0
Public Member Functions | Static Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | Friends | List of all members
graphics::Color Struct Referencefinal

Class for storing color in 32-bit format. More...

#include <Color.h>

Public Member Functions

constexpr Color () noexcept
 Constructs a transparent black color. More...
 
constexpr Color (const Color &) noexcept=default
 
constexpr Color (Color &&) noexcept=default
 
Coloroperator= (const Color &) noexcept=default
 
Coloroperator= (Color &&) noexcept=default
 
constexpr Color (uint8_t rr, uint8_t gg, uint8_t bb, uint8_t aa=255) noexcept
 Constructs a color from individual components. More...
 
constexpr Color WithOpacity (float opacity) const noexcept
 Returns a copy of this color with alpha component scaled by opacity. More...
 
constexpr Color WithFloatAlpha (float alpha) const noexcept
 Returns a copy of this color with a new alpha value. Value is clamped to [0, 1]. More...
 
constexpr Color WithAlpha (uint8_t alpha) const noexcept
 Returns a copy of this color with a new alpha value. More...
 
constexpr Color WithoutAlpha () const noexcept
 Returns a copy of this color with the alpha set to the max. More...
 
constexpr uint8_t GetRed () const noexcept
 Returns red component of this color. More...
 
constexpr uint8_t GetGreen () const noexcept
 Returns green component of this color. More...
 
constexpr uint8_t GetBlue () const noexcept
 Returns blue component of this color. More...
 
constexpr uint8_t GetAlpha () const noexcept
 Returns alpha component of this color. More...
 
constexpr uint32_t GetRGBA () const noexcept
 Returns color value as RGBA 32-bit integer. More...
 
constexpr uint32_t GetABGR () const noexcept
 Returns color value as ABGR 32-bit integer. More...
 
constexpr uint32_t GetRGB () const noexcept
 Returns color value as RGB integer. More...
 
Coloroperator+= (Color rhs) noexcept
 
Coloroperator-= (Color rhs) noexcept
 
Coloroperator*= (Color rhs) noexcept
 
bool operator== (const Color &rhs) const noexcept
 
bool operator!= (const Color &rhs) const noexcept
 

Static Public Member Functions

static constexpr Color FromABGR (uint32_t abgr) noexcept
 Constructs a color from ABGR 32-bit integer value. More...
 
static constexpr Color FromFloatRGBA (float r, float g, float b, float a) noexcept
 Constructs a color from individual floating point components. This value is clamped to [0, 1]. More...
 

Private Member Functions

constexpr Color (uint32_t abgr) noexcept
 

Static Private Member Functions

static constexpr uint8_t Add (uint8_t a, uint8_t b) noexcept
 
static constexpr uint8_t Sub (uint8_t a, uint8_t b) noexcept
 
static constexpr uint8_t Mul (uint8_t a, uint8_t b) noexcept
 
static constexpr uint8_t Scale (uint8_t a, float s) noexcept
 
template<typename ScaleType >
static constexpr uint8_t Lerp (uint8_t a, uint8_t b, ScaleType t)
 

Private Attributes

union {
   uint32_t   mABGR
 
   struct {
      uint8_t   mRed
 
      uint8_t   mGreen
 
      uint8_t   mBlue
 
      uint8_t   mAlpha
 
   } 
 
}; 
 

Friends

constexpr Color operator+ (Color lhs, Color rhs) noexcept
 
constexpr Color operator- (Color lhs, Color rhs) noexcept
 
constexpr Color operator* (Color lhs, Color rhs) noexcept
 
template<typename ScaleType >
constexpr Color lerp (Color lhs, Color rhs, ScaleType t)
 Performs linear interpolation between two colors. More...
 

Detailed Description

Class for storing color in 32-bit format.

This class is used to store color in 32-bit ABGR format. It allows to munipulate individual color components and to convert to/from other formats.

Additionally, it provides fixed-point arithmetic for color components.

Definition at line 28 of file Color.h.

Constructor & Destructor Documentation

◆ Color() [1/5]

constexpr graphics::Color::Color ( )
inlineconstexprnoexcept

Constructs a transparent black color.

Definition at line 32 of file Color.h.

32: mABGR(0) {}
uint32_t mABGR
Definition: Color.h:223

Referenced by FromABGR(), FromFloatRGBA(), WithAlpha(), WithFloatAlpha(), WithOpacity(), and WithoutAlpha().

Here is the caller graph for this function:

◆ Color() [2/5]

constexpr graphics::Color::Color ( const Color )
constexprdefaultnoexcept

◆ Color() [3/5]

constexpr graphics::Color::Color ( Color &&  )
constexprdefaultnoexcept

◆ Color() [4/5]

constexpr graphics::Color::Color ( uint8_t  rr,
uint8_t  gg,
uint8_t  bb,
uint8_t  aa = 255 
)
inlineconstexprnoexcept

Constructs a color from individual components.

Definition at line 40 of file Color.h.

41 : mRed(rr)
42 , mGreen(gg)
43 , mBlue(bb)
44 , mAlpha(aa)
45 {
46 }
uint8_t mBlue
Definition: Color.h:226
uint8_t mRed
Definition: Color.h:226
uint8_t mAlpha
Definition: Color.h:226
uint8_t mGreen
Definition: Color.h:226

◆ Color() [5/5]

constexpr graphics::Color::Color ( uint32_t  abgr)
inlineexplicitconstexprprivatenoexcept

Definition at line 216 of file Color.h.

217 : mABGR(abgr)
218 {
219 }

Member Function Documentation

◆ Add()

static constexpr uint8_t graphics::Color::Add ( uint8_t  a,
uint8_t  b 
)
inlinestaticconstexprprivatenoexcept

Definition at line 179 of file Color.h.

180 {
181 return uint8_t((a + b) & 0xFF);
182 }

◆ FromABGR()

static constexpr Color graphics::Color::FromABGR ( uint32_t  abgr)
inlinestaticconstexprnoexcept

Constructs a color from ABGR 32-bit integer value.

Definition at line 49 of file Color.h.

50 {
51 return Color(abgr);
52 }
constexpr Color() noexcept
Constructs a transparent black color.
Definition: Color.h:32

References Color().

Referenced by graphics::ColorFromABGR().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ FromFloatRGBA()

static constexpr Color graphics::Color::FromFloatRGBA ( float  r,
float  g,
float  b,
float  a 
)
inlinestaticconstexprnoexcept

Constructs a color from individual floating point components. This value is clamped to [0, 1].

Definition at line 55 of file Color.h.

56 {
57 return Color(
58 Color::Scale(255, r), Color::Scale(255, g), Color::Scale(255, b),
59 Color::Scale(255, a));
60 }
static constexpr uint8_t Scale(uint8_t a, float s) noexcept
Definition: Color.h:194

References Color(), and Scale().

Referenced by graphics::ColorFromFloatRGBA().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetABGR()

constexpr uint32_t graphics::Color::GetABGR ( ) const
inlineconstexprnoexcept

Returns color value as ABGR 32-bit integer.

Definition at line 118 of file Color.h.

119 {
120 return mABGR;
121 }

References mABGR.

◆ GetAlpha()

constexpr uint8_t graphics::Color::GetAlpha ( ) const
inlineconstexprnoexcept

Returns alpha component of this color.

Definition at line 105 of file Color.h.

106 {
107 return mAlpha;
108 }

References mAlpha.

◆ GetBlue()

constexpr uint8_t graphics::Color::GetBlue ( ) const
inlineconstexprnoexcept

Returns blue component of this color.

Definition at line 99 of file Color.h.

100 {
101 return mBlue;
102 }

References mBlue.

Referenced by Triplet::SetColor().

Here is the caller graph for this function:

◆ GetGreen()

constexpr uint8_t graphics::Color::GetGreen ( ) const
inlineconstexprnoexcept

Returns green component of this color.

Definition at line 93 of file Color.h.

94 {
95 return mGreen;
96 }

References mGreen.

Referenced by Triplet::SetColor().

Here is the caller graph for this function:

◆ GetRed()

constexpr uint8_t graphics::Color::GetRed ( ) const
inlineconstexprnoexcept

Returns red component of this color.

Definition at line 87 of file Color.h.

88 {
89 return mRed;
90 }

References mRed.

Referenced by Triplet::SetColor().

Here is the caller graph for this function:

◆ GetRGB()

constexpr uint32_t graphics::Color::GetRGB ( ) const
inlineconstexprnoexcept

Returns color value as RGB integer.

Definition at line 124 of file Color.h.

125 {
126 return (uint32_t(mRed) << 16) + (uint32_t(mGreen) << 8) +
127 (uint32_t(mBlue));
128 }

References mBlue, mGreen, and mRed.

◆ GetRGBA()

constexpr uint32_t graphics::Color::GetRGBA ( ) const
inlineconstexprnoexcept

Returns color value as RGBA 32-bit integer.

Definition at line 111 of file Color.h.

112 {
113 return (uint32_t(mRed) << 24) + (uint32_t(mGreen) << 16) +
114 (uint32_t(mBlue) << 8) + (uint32_t(mAlpha));
115 }

References mAlpha, mBlue, mGreen, and mRed.

◆ Lerp()

template<typename ScaleType >
static constexpr uint8_t graphics::Color::Lerp ( uint8_t  a,
uint8_t  b,
ScaleType  t 
)
inlinestaticconstexprprivate

Definition at line 200 of file Color.h.

201 {
202 static_assert(std::is_floating_point<ScaleType>::value);
203
204 const auto lerpValue = (a + (b - a) * t);
205
206 const auto roundLerpValue =
207 static_cast<int16_t>(lerpValue + ScaleType(0.5));
208
209 const auto ui8LerpValue =
210 static_cast<uint8_t>(std::max<decltype(roundLerpValue)>(
211 0, std::min<decltype(roundLerpValue)>(255, roundLerpValue)));
212
213 return ui8LerpValue;
214 }
int min(int a, int b)

References min().

Here is the call graph for this function:

◆ Mul()

static constexpr uint8_t graphics::Color::Mul ( uint8_t  a,
uint8_t  b 
)
inlinestaticconstexprprivatenoexcept

Definition at line 189 of file Color.h.

190 {
191 return uint8_t((uint16_t(a) * uint16_t(b) / 255) & 0xFF);
192 }

◆ operator!=()

bool graphics::Color::operator!= ( const Color rhs) const
inlinenoexcept

Definition at line 173 of file Color.h.

174 {
175 return mABGR != rhs.mABGR;
176 }

References mABGR.

◆ operator*=()

Color & graphics::Color::operator*= ( Color  rhs)
inlinenoexcept

Definition at line 164 of file Color.h.

165 {
166 return *this = *this * rhs;
167 }

◆ operator+=()

Color & graphics::Color::operator+= ( Color  rhs)
inlinenoexcept

Definition at line 154 of file Color.h.

155 {
156 return *this = *this + rhs;
157 }

◆ operator-=()

Color & graphics::Color::operator-= ( Color  rhs)
inlinenoexcept

Definition at line 159 of file Color.h.

160 {
161 return *this = *this - rhs;
162 }

◆ operator=() [1/2]

Color & graphics::Color::operator= ( Color &&  )
defaultnoexcept

◆ operator=() [2/2]

Color & graphics::Color::operator= ( const Color )
defaultnoexcept

◆ operator==()

bool graphics::Color::operator== ( const Color rhs) const
inlinenoexcept

Definition at line 169 of file Color.h.

170 {
171 return mABGR == rhs.mABGR;
172 }

References mABGR.

◆ Scale()

static constexpr uint8_t graphics::Color::Scale ( uint8_t  a,
float  s 
)
inlinestaticconstexprprivatenoexcept

Definition at line 194 of file Color.h.

195 {
196 return uint8_t(a * std::max(0.0f, std::min(1.0f, s)) + 0.5f);
197 }

References min().

Referenced by FromFloatRGBA(), WithFloatAlpha(), and WithOpacity().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Sub()

static constexpr uint8_t graphics::Color::Sub ( uint8_t  a,
uint8_t  b 
)
inlinestaticconstexprprivatenoexcept

Definition at line 184 of file Color.h.

185 {
186 return (a > b) ? a - b : 0;
187 }

◆ WithAlpha()

constexpr Color graphics::Color::WithAlpha ( uint8_t  alpha) const
inlineconstexprnoexcept

Returns a copy of this color with a new alpha value.

Definition at line 75 of file Color.h.

76 {
77 return Color(mRed, mGreen, mBlue, alpha);
78 }

References Color(), mBlue, mGreen, and mRed.

Here is the call graph for this function:

◆ WithFloatAlpha()

constexpr Color graphics::Color::WithFloatAlpha ( float  alpha) const
inlineconstexprnoexcept

Returns a copy of this color with a new alpha value. Value is clamped to [0, 1].

Definition at line 69 of file Color.h.

70 {
71 return Color(mRed, mGreen, mBlue, Scale(255, alpha));
72 }

References Color(), mBlue, mGreen, mRed, and Scale().

Here is the call graph for this function:

◆ WithOpacity()

constexpr Color graphics::Color::WithOpacity ( float  opacity) const
inlineconstexprnoexcept

Returns a copy of this color with alpha component scaled by opacity.

Definition at line 63 of file Color.h.

64 {
65 return Color(mRed, mGreen, mBlue, Scale(mAlpha, opacity));
66 }

References Color(), mAlpha, mBlue, mGreen, mRed, and Scale().

Here is the call graph for this function:

◆ WithoutAlpha()

constexpr Color graphics::Color::WithoutAlpha ( ) const
inlineconstexprnoexcept

Returns a copy of this color with the alpha set to the max.

Definition at line 81 of file Color.h.

82 {
83 return Color(mRed, mGreen, mBlue, uint8_t(255));
84 }

References Color(), mBlue, mGreen, and mRed.

Here is the call graph for this function:

Friends And Related Function Documentation

◆ lerp

template<typename ScaleType >
constexpr Color lerp ( Color  lhs,
Color  rhs,
ScaleType  t 
)
friend

Performs linear interpolation between two colors.

Definition at line 233 of file Color.h.

234{
235 return { Color::Lerp(lhs.mRed, rhs.mRed, t),
236 Color::Lerp(lhs.mGreen, rhs.mGreen, t),
237 Color::Lerp(lhs.mBlue, rhs.mBlue, t),
238 Color::Lerp(lhs.mAlpha, rhs.mAlpha, t) };
239}
static constexpr uint8_t Lerp(uint8_t a, uint8_t b, ScaleType t)
Definition: Color.h:200

◆ operator*

constexpr Color operator* ( Color  lhs,
Color  rhs 
)
friend

Definition at line 144 of file Color.h.

145 {
146 return Color(
147 Mul(lhs.mRed, rhs.mRed), Mul(lhs.mGreen, rhs.mGreen),
148 Mul(lhs.mBlue, rhs.mBlue), Mul(lhs.mAlpha, rhs.mAlpha));
149 }
static constexpr uint8_t Mul(uint8_t a, uint8_t b) noexcept
Definition: Color.h:189

◆ operator+

constexpr Color operator+ ( Color  lhs,
Color  rhs 
)
friend

Definition at line 130 of file Color.h.

131 {
132 return Color(
133 Add(lhs.mRed, rhs.mRed), Add(lhs.mGreen, rhs.mGreen),
134 Add(lhs.mBlue, rhs.mBlue), Add(lhs.mAlpha, rhs.mAlpha));
135 }
static constexpr uint8_t Add(uint8_t a, uint8_t b) noexcept
Definition: Color.h:179

◆ operator-

constexpr Color operator- ( Color  lhs,
Color  rhs 
)
friend

Definition at line 137 of file Color.h.

138 {
139 return Color(
140 Sub(lhs.mRed, rhs.mRed), Sub(lhs.mGreen, rhs.mGreen),
141 Sub(lhs.mBlue, rhs.mBlue), Sub(lhs.mAlpha, rhs.mAlpha));
142 }
static constexpr uint8_t Sub(uint8_t a, uint8_t b) noexcept
Definition: Color.h:184

Member Data Documentation

◆ 

union { ... } graphics::Color::@111

◆ mABGR

uint32_t graphics::Color::mABGR

Definition at line 223 of file Color.h.

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

◆ mAlpha

uint8_t graphics::Color::mAlpha

Definition at line 226 of file Color.h.

Referenced by GetAlpha(), GetRGBA(), and WithOpacity().

◆ mBlue

uint8_t graphics::Color::mBlue

Definition at line 226 of file Color.h.

Referenced by GetBlue(), GetRGB(), GetRGBA(), WithAlpha(), WithFloatAlpha(), WithOpacity(), and WithoutAlpha().

◆ mGreen

uint8_t graphics::Color::mGreen

Definition at line 226 of file Color.h.

Referenced by GetGreen(), GetRGB(), GetRGBA(), WithAlpha(), WithFloatAlpha(), WithOpacity(), and WithoutAlpha().

◆ mRed

uint8_t graphics::Color::mRed

Definition at line 226 of file Color.h.

Referenced by GetRed(), GetRGB(), GetRGBA(), WithAlpha(), WithFloatAlpha(), WithOpacity(), and WithoutAlpha().


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