1 #ifndef INCLUDE_AL_COLOR_HPP
2 #define INCLUDE_AL_COLOR_HPP
87 Color(
const T *rgba) :
r(rgba[0]),
g(rgba[1]),
b(rgba[2]),
a(rgba[3]) {}
91 Color(
float gray = 1.f,
float a = 1.f) :
r(gray),
g(gray),
b(gray),
a(
a) {}
131 const RGB &rgb()
const {
return *(
const RGB *)(
components); }
136 return set(c.
r, c.
g, c.
b);
142 return set(re, gr, bl);
162 return set(rgba[0], rgba[1], rgba[2], rgba[3]);
195 return v.
r ==
r && v.
g ==
g && v.
b ==
b && v.
a ==
a;
205 return set(
r - v.r,
g - v.g,
b - v.b,
a - v.a);
208 return set(
r * v.r,
g * v.g,
b * v.b,
a * v.a);
211 return set(
r / v.r,
g / v.g,
b / v.b,
a / v.a);
213 Color &operator+=(
float v) {
return set(
r + v,
g + v,
b + v,
a + v); }
214 Color &operator-=(
float v) {
return set(
r - v,
g - v,
b - v,
a - v); }
215 Color &operator*=(
float v) {
return set(
r * v,
g * v,
b * v,
a * v); }
216 Color &operator/=(
float v) {
return set(
r / v,
g / v,
b / v,
a / v); }
223 Color operator+(
float v)
const {
return Color(*
this) += v; }
224 Color operator-(
float v)
const {
return Color(*
this) -= v; }
225 Color operator*(
float v)
const {
return Color(*
this) *= v; }
226 Color operator/(
float v)
const {
return Color(*
this) /= v; }
230 for (
int i = 0; i < 4; ++i) {
232 v < 0.f ? v = 0.f : (v >
max ? v =
max : 0);
250 return (c - *
this) * amt + *
this;
254 float tof(uint8_t v) {
return float(v) * (1.f / 255.f); }
291 :
r(gray),
g(gray),
b(gray),
a(
a) {}
312 Colori(
const Lab &lab,
float a = 1.f) :
a(static_cast<uint8_t>(
a * 255)) {
324 Colori(
const Luv &luv,
float a = 1.f) :
a(static_cast<uint8_t>(
a * 255)) {
342 return set(toi(v.
r), toi(v.
g), toi(v.
b), toi(v.
a));
377 return set(re, gr, bl);
393 uint8_t toi(
float v) {
return uint8_t(v * 255.f); }
414 HSV(
const T *hsv) :
h(hsv[0]),
s(hsv[1]),
v(hsv[2]) {}
520 RGB(
const T *rgb) :
r(rgb[0]),
g(rgb[1]),
b(rgb[2]) {}
523 RGB(
float gray = 1.f) :
r(gray),
g(gray),
b(gray) {}
572 return set(rgb[0], rgb[1], rgb[2]);
605 return v.
r ==
r && v.
g ==
g && v.
b ==
b;
611 RGB &operator+=(
const RGB &v) {
return set(
r + v.
r,
g + v.
g,
b + v.
b); }
612 RGB &operator-=(
const RGB &v) {
return set(
r - v.r,
g - v.g,
b - v.b); }
613 RGB &operator*=(
const RGB &v) {
return set(
r * v.r,
g * v.g,
b * v.b); }
614 RGB &operator/=(
const RGB &v) {
return set(
r / v.r,
g / v.g,
b / v.b); }
615 RGB &operator+=(
float v) {
return set(
r + v,
g + v,
b + v); }
616 RGB &operator-=(
float v) {
return set(
r - v,
g - v,
b - v); }
617 RGB &operator*=(
float v) {
return set(
r * v,
g * v,
b * v); }
618 RGB &operator/=(
float v) {
return set(
r / v,
g / v,
b / v); }
620 RGB operator-()
const {
return RGB(-
r, -
g, -
b); }
621 RGB operator+(
const RGB &v)
const {
return RGB(*
this) += v; }
622 RGB operator-(
const RGB &v)
const {
return RGB(*
this) -= v; }
623 RGB operator*(
const RGB &v)
const {
return RGB(*
this) *= v; }
624 RGB operator/(
const RGB &v)
const {
return RGB(*
this) /= v; }
625 RGB operator+(
float v)
const {
return RGB(*
this) += v; }
626 RGB operator-(
float v)
const {
return RGB(*
this) -= v; }
627 RGB operator*(
float v)
const {
return RGB(*
this) *= v; }
628 RGB operator/(
float v)
const {
return RGB(*
this) /= v; }
632 for (
int i = 0; i < 3; ++i) {
634 v < 0.f ? v = 0.f : (v >
max ? v =
max : 0);
646 float luminance()
const {
return r * 0.299f +
g * 0.587f +
b * 0.114f; }
650 return (v - *
this) * amt + *
this;
671 CIE_XYZ(
const T *xyz) : x(xyz[0]),
y(xyz[1]),
z(xyz[2]) {}
737 Lab(
float l = 1,
float a = 1,
float b = 1) : l(l),
a(
a),
b(
b) {}
765 const float &
operator[](
int i)
const {
return components[i]; }
842 const float &
operator[](
int i)
const {
return components[i]; }
909 Luv(
float l = 1,
float u = 1,
float v = 1) : l(l),
u(
u),
v(
v) {}
937 const float &
operator[](
int i)
const {
return components[i]; }
987 HCLuv(
float h = 1,
float c = 1,
float l = 1) : h(h),
c(
c),
l(
l) {}
1063 inline RGB operator+(
float s,
const RGB &c) {
return c + s; }
1064 inline RGB operator-(
float s,
const RGB &c) {
return -c + s; }
1065 inline RGB operator*(
float s,
const RGB &c) {
return c * s; }
1066 inline RGB operator/(
float s,
const RGB &c) {
1067 return RGB(s / c.r, s / c.g, s / c.b);
1071 return set(
float(v.
r) / 255.f,
float(v.
g) / 255.f,
float(v.
b) / 255.f);
1074 inline Color operator+(
float s,
const Color &c) {
return c + s; }
1075 inline Color operator-(
float s,
const Color &c) {
return -c + s; }
1076 inline Color operator*(
float s,
const Color &c) {
return c * s; }
1077 inline Color operator/(
float s,
const Color &c) {
1078 return Color(s / c.r, s / c.g, s / c.b, s / c.a);
1112 return set(toi(v.
r), toi(v.
g), toi(v.
b), 255);
1115 inline HSV operator*(
float s,
const HSV &c) {
return c * s; }
T max(const T &v1, const T &v2, const T &v3)
float z
< green component in [0, 1]
CIE_XYZ(float x=0, float y=1, float z=1)
float y
< red component in [0, 1]
CIE_XYZ & operator=(const Lab &v)
Set from Lab color.
float & operator[](int i)
Set color component at index with no bounds checking.
const float & operator[](int i) const
Get color component at index with no bounds checking.
CIE_XYZ & operator=(const Luv &v)
Set from Luv color.
CIE_XYZ & operator=(const RGB &v)
Set from RGB color.
CIE_XYZ & operator=(const Color &v)
Set from RGBA color.
CIE_XYZ & operator=(const Colori &v)
Set from RGBA color.
CIE_XYZ & operator=(const HSV &v)
Set from HSV color.
float components[3]
CIE_XYZ component vector.
float a
Alpha component in [0, 1].
Color & operator=(const HCLab &v)
Set RGB components from HCLab.
Color & operator=(const CIE_XYZ &v)
Set RGB components from CIE_XYZ.
Color & set(float re, float gr, float bl, float al)
Set RGBA components.
Color(const Luv &luv, float a=1.f)
float r
Red component in [0, 1].
Color(float gray=1.f, float a=1.f)
Color(const HCLab &hclab, float a=1.f)
Color & set(const T *rgba)
Set from an array of RGBA components.
Color(const CIE_XYZ &xyz, float a=1.f)
float b
Blue component in [0, 1].
Color & operator=(const Luv &v)
Set RGB components from Luv.
Color & set(float re, float gr, float bl)
Set RGB components.
float g
Green component in [0, 1].
Color & set(const Color &c, float al)
Set RGB from another color and alpha from argument.
Color inverse() const
Returns inverted color.
float components[4]
RGBA component vector.
Color & operator=(const HCLuv &v)
Set RGB components from HCLuv.
Color(const HSV &hsv, float a=1.f)
Color(const RGB &rgb, float a=1.f)
Color & operator=(const Lab &v)
Set RGB components from Lab.
Color & set(float v)
Set from gray value.
Color & set(float v, float al)
Set from gray value and alpha.
const float & operator[](int i) const
Get color component at index with no bounds checking.
Color blackAndWhite() const
Returns nearest black or white color.
float & operator[](int i)
Set color component at index with no bounds checking.
bool operator!=(const Color &v) const
Return true if components are not equal, false otherwise.
float luminance() const
Returns luminance value.
Color & clamp(float max=1.f)
Clamp all components into [0,max] range.
Color(const HCLuv &hcluv, float a=1.f)
Color(const Lab &lab, float a=1.f)
bool operator==(const Color &v) const
Return true if all components are equal, false otherwise.
Color & invert()
Invert RGB components.
Color(float r, float g, float b, float a=1.f)
Color mix(const Color &c, float amt=0.5f) const
Returns self linearly mixed with another color (0 = none)
Color & operator=(float v)
Set from gray value.
uint8_t r
Red component in [0, 255].
Colori(const Lab &lab, float a=1.f)
Colori(const HSV &hsv, uint8_t a=255)
Colori & set(uint8_t re, uint8_t gr, uint8_t bl, uint8_t al)
Set RGBA components.
Colori & set(uint8_t v)
Set from gray value.
Colori inverse() const
Returns inverted color.
Colori & set(uint8_t v, uint8_t al)
Set from gray value and alpha.
Colori(const HCLab &hclab, float a=1.f)
Colori & set(uint8_t re, uint8_t gr, uint8_t bl)
Set RGB components.
Colori & operator=(const Color &v)
Set from floating-point color.
uint32_t rgba
RGBA components packed into 32-bit integer.
uint8_t g
Green component in [0, 255].
Colori & operator=(const HCLab &v)
Set RGB components from HCLab.
Colori & operator=(const Luv &v)
Set RGB components from Luv.
const uint8_t & operator[](int i) const
Get color component at index with no bounds checking.
Colori & operator=(const Lab &v)
Set RGB components from Lab.
uint8_t & operator[](int i)
Set color component at index with no bounds checking.
Colori(const CIE_XYZ &xyz, float a=1.f)
Colori & operator=(const CIE_XYZ &v)
Set RGB components from CIE_XYZ.
Colori & operator=(const HCLuv &v)
Set RGB components from HCLuv.
Colori(uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
Colori(const RGB &rgb, uint8_t a=255)
Colori & invert()
Invert RGB components.
uint8_t b
Blue component in [0, 255].
uint8_t a
Alpha component in [0, 255].
uint8_t components[4]
RGBA component vector.
Colori(const Luv &luv, float a=1.f)
Colori(const HCLuv &hcluv, float a=1.f)
Colori(uint8_t gray=255, uint8_t a=255)
HCLab & operator=(const HSV &v)
Set from HSV color.
float c
< hue component in [0, 1]
HCLab operator*(float a) const
Get new HCLab with value component multiplied by a scalar.
HCLab & operator=(const Colori &v)
Set from RGBA color.
HCLab & wrapHue()
Wrap hue value into valid interval [0, 1)
HCLab & operator=(const Color &v)
Set from RGBA color.
HCLab & rotateHue(float dh)
Rotate hue in interval [0, 1)
const float & operator[](int i) const
Get color component at index with no bounds checking.
HCLab & operator=(const RGB &v)
Set from RGB color.
HCLab & operator=(const Lab &v)
Set from Lab color.
HCLab & operator*=(float a)
Multiply luminance component by a scalar.
float & operator[](int i)
Set color component at index with no bounds checking.
HCLab & operator=(const CIE_XYZ &v)
Set from CIE_XYZ color.
HCLab(float h=1, float c=1, float l=1)
float & operator[](int i)
Set color component at index with no bounds checking.
HCLuv & operator=(const RGB &w)
Set from RGB color.
float c
< hue component in [0, 1]
HCLuv operator*(float a) const
Get new HCLuv with value component multiplied by a scalar.
const float & operator[](int i) const
Get color component at index with no bounds checking.
HCLuv & rotateHue(float dh)
Rotate hue in interval [0, 1)
HCLuv & operator=(const Colori &w)
Set from RGBA color.
HCLuv & operator=(const CIE_XYZ &w)
Set from CIE_XYZ color.
HCLuv(float h=1, float c=1, float l=1)
HCLuv & operator=(const Luv &w)
Set from Luv color.
HCLuv & operator=(const Color &w)
Set from RGBA color.
HCLuv & operator=(const HSV &w)
Set from HSV color.
HCLuv & operator*=(float a)
Multiply luminance component by a scalar.
HCLuv & wrapHue()
Wrap hue value into valid interval [0, 1)
Color represented by hue, saturation, and value.
HSV & operator=(const HCLuv &v)
Set from HCLuv color.
float h
Hue component in [0, 1].
HSV & operator*=(float a)
Multiply value component by a scalar.
HSV & wrapHue()
Wrap hue value into valid interval [0, 1)
HSV operator*(float a) const
Get new HSV with value component multiplied by a scalar.
HSV & operator=(const HCLab &v)
Set from HCLab color.
HSV(float h=0, float s=1, float v=1)
float & operator[](int i)
Set color component at index with no bounds checking.
float components[3]
HSV component vector.
HSV & operator=(const Color &v)
Set from RGBA color.
HSV & operator=(const RGB &v)
Set from RGB color.
float v
Value component in [0, 1].
HSV & operator=(const Luv &v)
Set from Luv color.
HSV & operator=(const CIE_XYZ &v)
Set from CIE_XYZ color.
const float & operator[](int i) const
Get color component at index with no bounds checking.
HSV & operator=(const Colori &v)
Set from RGBA color.
HSV & rotateHue(float dh)
Rotate hue in interval [0, 1)
float s
Saturation component in [0, 1].
HSV & operator=(const Lab &v)
Set from Lab color.
Lab & operator=(const Colori &v)
Set from RGBA color.
Lab & operator*=(float c)
Multiply lightness component by a scalar.
Lab operator*(float c) const
Get new Lab with value component multiplied by a scalar.
Lab & operator=(const CIE_XYZ &v)
Set from CIE_XYZ color.
const float & operator[](int i) const
Get color component at index with no bounds checking.
float & operator[](int i)
Set color component at index with no bounds checking.
float a
< Lightness component in [0, 100]
Lab(float l=1, float a=1, float b=1)
Lab & operator=(const Color &v)
Set from RGBA color.
Lab & operator=(const HSV &v)
Set from HSV color.
Lab & operator=(const RGB &v)
Set from RGB color.
Luv & operator=(const HCLuv &w)
Set from HCLuv color.
float u
< Lightness component in [0, 100]
Luv & operator=(const HSV &w)
Set from HSV color.
Luv & operator*=(float a)
Multiply lightness component by a scalar.
Luv operator*(float a) const
Get new Luv with value component multiplied by a scalar.
Luv & operator=(const Colori &w)
Set from RGBA color.
Luv & operator=(const RGB &w)
Set from RGB color.
const float & operator[](int i) const
Get color component at index with no bounds checking.
float & operator[](int i)
Set color component at index with no bounds checking.
Luv & operator=(const Color &w)
Set from RGBA color.
Luv & operator=(const CIE_XYZ &w)
Set from CIE_XYZ color.
Luv(float l=1, float u=1, float v=1)
Color represented by red, green, and blue components.
RGB & operator=(float v)
Set from gray value.
RGB & operator=(const CIE_XYZ &v)
Set RGB components from CIE_XYZ.
RGB inverse() const
Returns inverted color.
RGB & operator=(const Lab &v)
Set RGB components from Lab.
RGB & operator=(const Color &v)
Set RGB components from Color.
bool operator==(const RGB &v) const
Return true if all components are equal, false otherwise.
float luminance() const
Returns luminance value (following ITU-R BT.601)
const float & operator[](int i) const
Get color component at index with no bounds checking.
float b
Blue component in [0, 1].
float r
Red component in [0, 1].
RGB & operator=(const Luv &v)
Set RGB components from Luv.
RGB(float r, float g, float b)
RGB & clamp(float max=1.f)
Clamp all components into [0,max] range.
RGB mix(const RGB &v, float amt=0.5f) const
Returns self linearly mixed with another color (0 = none)
RGB & set(const T *rgb)
Set from an array of RGB components.
RGB & operator=(const HCLuv &v)
Set RGB components from HCLuv.
RGB & set(float re, float gr, float bl)
Set from RGB components.
float & operator[](int i)
Set color component at index with no bounds checking.
RGB & set(const RGB &v)
Set from another RGB.
RGB & operator=(const HCLab &v)
Set RGB components from HCLab.
RGB & operator=(const HSV &v)
Set RGB components from HSV.
RGB & invert()
Invert RGB components.
float g
Green component in [0, 1].
RGB & set(float v)
Set from gray value.
bool operator!=(const RGB &v) const
Return true if components are not equal, false otherwise.
float components[3]
RGB component vector.