40namespace utility::graphic
47 template<
typename Type>
49 && (std::is_floating_point<Type>::value
50 || std::is_integral<Type>::value);
60 template<CanBeColorComponent Type>
class Color
91 if constexpr (std::is_floating_point<Type>::value) {
92 return (std::max)(Type { 0 }, (std::min)(Type { 1 }, value));
94 return (std::max)(Type { 0 }, (std::min)(Type { 255 }, value));
108 if constexpr (std::is_floating_point<Type>::value) {
109 return clamp(
static_cast<Type
>(value));
111 constexpr long long max = 255;
112 return static_cast<Type
>(
113 (std::max)(0LL, (std::min)(max, value)));
123 if constexpr (std::is_floating_point<Type>::value) {
148 Color(Type red, Type green, Type blue)
163 Color(Type red, Type green, Type blue, Type alpha)
188 :
_red(std::move(other._red))
189 ,
_green(std::move(other._green))
190 ,
_blue(std::move(other._blue))
191 ,
_alpha(std::move(other._alpha))
202 if (
this != &other) {
218 if (
this != &other) {
219 _red = std::move(other._red);
220 _green = std::move(other._green);
221 _blue = std::move(other._blue);
222 _alpha = std::move(other._alpha);
309 void setRGBA(Type red, Type green, Type blue, Type alpha)
335 return !(*
this == other);
384 if constexpr (std::is_floating_point<Type>::value) {
387 + Type { 0.114 } *
_blue);
392 return Color(luminance, luminance, luminance,
_alpha);
416 if constexpr (std::is_floating_point<Type>::value) {
438 if constexpr (std::is_floating_point<Type>::value) {
440 Type oneMinusA = Type { 1 } - a;
444 a + oneMinusA * background.
_alpha);
466 factor =
clamp(factor);
478 factor =
clamp(factor);
516 return static_cast<double>(
_alpha)
564 std::ostream &operator<<(std::ostream &stream,
const Color32Bit &color);
572 std::ostream &operator<<(std::ostream &stream,
const ColorFloat &color);
580 std::ostream &operator<<(std::ostream &stream,
const ColorDouble &color);
RGBA color with arithmetic type components.
Type _green
Green component.
Type getAlpha(void) const
Get the alpha (opacity) component.
Type _blue
Blue component.
Color operator-(const Color &other) const
Component-wise color subtraction (clamped).
bool isOpaque(void) const
Check whether alpha is fully opaque.
void setBlue(Type blue)
Set the blue component.
void setAlpha(Type alpha)
Set the alpha (opacity) component.
Color(void)
Default constructor creating opaque black.
bool operator!=(const Color &other) const
Inequality comparison.
Color darker(Type factor) const
Create a darker version of this color.
Type getRed(void) const
Get the red component.
void setRGBA(Type red, Type green, Type blue, Type alpha)
Set all RGBA components.
Color blendOver(const Color &background) const
Alpha blend this color over another background color.
void setGreen(Type green)
Set the green component.
Color inverted(void) const
Invert the color (keeping alpha unchanged).
Color lerp(const Color &other, Type t) const
Linearly interpolate between this color and another.
Color(Color &&other) noexcept
Move constructor.
static Type clamp(Type value)
Clamp a value to valid range based on type.
Color lighter(Type factor) const
Create a lighter version of this color.
Color(Type red, Type green, Type blue)
Construct color with RGB values (fully opaque).
double alpha01(void) const
Get alpha normalized to [0, 1].
Color operator*(Type scalar) const
Scalar multiplication of color components.
void setRed(Type red)
Set the red component.
Color operator+(const Color &other) const
Component-wise color addition (clamped).
Color & operator=(const Color &other)
Copy assignment operator.
Color withAlpha(Type alpha) const
Return a copy with a different alpha value.
Type _alpha
Alpha (opacity) component.
Color & operator=(Color &&other) noexcept
Move assignment operator.
bool isTransparent(void) const
Check whether alpha is fully transparent.
Type getGreen(void) const
Get the green component.
static constexpr Type maxValue()
Get the maximum value for the type.
Type getBlue(void) const
Get the blue component.
Color(Type red, Type green, Type blue, Type alpha)
Construct color with RGBA values.
bool operator==(const Color &other) const
Equality comparison.
bool operator<(const Color &other) const
Less-than operator for Color.
Color grayscale(void) const
Convert color to grayscale using luminance formula.
Color(const Color &other)
Copy constructor.
static Type clampWide(long long value)
Clamp a wide accumulator into the component range.
Concept to ensure the type can be used as a color component.