1 #include "ge211_color.h" 9 template<
class T,
class U>
10 static T weighted_average(T t,
double weight, U u) noexcept
12 double f1 =
static_cast<double>(t);
13 double f2 =
static_cast<double>(u);
14 double result = (1 - weight) * f1 + weight * f2;
18 template<
class T,
class Field>
19 static T adjust_field(T result, Field T::*field,
20 double weight,
double goal) noexcept
22 result.*field = weighted_average(result.*field, weight, goal);
28 return Color{uint8_t(255 * red),
31 uint8_t(255 * alpha)};
38 static Color from_hcma(
double hue,
41 double alpha) noexcept
43 double H6 = std::fmod(hue, 360.0) / 60.0;
44 double X = C * (1 - std::fabs(std::fmod(H6, 2) - 1));
46 double r1 = 0, g1 = 0, b1 = 0;
72 double alpha) noexcept
74 double C = (1 - std::fabs(2 * lightness - 1)) * saturation;
75 double m = lightness - 0.5 * C;
76 return from_hcma(hue, C, m, alpha);
80 double alpha) noexcept
82 double C = value * saturation;
84 return from_hcma(hue, C, m, alpha);
89 return Color{uint8_t(~red_), uint8_t(~blue_), uint8_t(~green_), alpha_};
94 return to_hsva().
rotate_hue(degrees).to_rgba();
99 return to_hsla().
lighten(unit_amount).to_rgba();
104 return to_hsla().
darken(unit_amount).to_rgba();
109 return to_hsla().
saturate(unit_amount).to_rgba();
114 return to_hsla().
desaturate(unit_amount).to_rgba();
117 static std::tuple<double, double, double, double> to_HCMm(
Color color) noexcept
119 double R = color.red() / 255.0;
120 double G = color.green() / 255.0;
121 double B = color.blue() / 255.0;
123 double M = std::max(R, std::max(G, B));
124 double m = std::min(R, std::min(G, B));
128 (M == R)? std::fmod((G - B) / C, 6) :
129 (M == G)? (B - R) / C + 2 :
134 return std::make_tuple(H, C, M, m);
140 std::tie(H, C, M, m) = to_HCMm(*
this);
142 double L = (M + m) / 2;
143 double S = (L == 1)? 0 : C / (1 - std::fabs(2 * L - 1));
145 return {H, S, L,
alpha() / 255.0};
151 std::tie(H, C, M, m) = to_HCMm(*
this);
154 double S = V == 0 ? 0 : C / V;
156 return {H, S, V,
alpha() / 255.0};
159 SDL_Color Color::to_sdl_() const noexcept
169 uint32_t Color::to_sdl_(
const SDL_PixelFormat* format)
const noexcept
171 return SDL_MapRGBA(format, red_, green_, blue_, alpha_);
176 return adjust_field(*
this, &Color::alpha_, unit_amount, 255);
181 return adjust_field(*
this, &Color::alpha_, unit_amount, 0);
192 result.
hue = std::fmod(result.hue + degrees, 360);
218 return adjust_field(*
this, &
HSLA::alpha, unit_amount, 1.0);
223 return adjust_field(*
this, &
HSLA::alpha, unit_amount, 0.0);
234 result.
hue = std::fmod(result.hue + degrees, 360);
250 return adjust_field(*
this, &
HSVA::value, unit_amount, 1.0);
255 return adjust_field(*
this, &
HSVA::value, unit_amount, 0.0);
260 return adjust_field(*
this, &
HSVA::alpha, unit_amount, 1.0);
265 return adjust_field(*
this, &
HSVA::alpha, unit_amount, 0.0);
HSLA fade_out(double unit_amount) const noexcept
Decreases opacity of the color.
static Color from_hsva(double hue, double saturation, double value, double alpha=1) noexcept
Constructs a color given the hue, saturation, value, and alpha.
Color invert() const noexcept
Returns the inverse of a color.
double saturation
The fullness of the color, from 0,0 (grey) to 1.0 (fully saturated).
double hue
The hue in degrees from 0 to 360.
uint8_t alpha() const noexcept
Gets the alpha (opacity) component of a color.
double value
The brightness of the color, from 0.0 (black) to 1.0 (fully colored).
Representation for the hue-saturation-value-alpha color model.
HSVA to_hsva() const noexcept
Converts a color to the hue-saturation-value (HSV) color model.
Representation for the hue-saturation-lightness-alpha color model.
Color rotate_hue(double degrees) const noexcept
Returns a color by rotating the hue, leaving the other properties constant.
The game engine namespace.
Color fade_in(double unit_amount) const noexcept
Increases opacity of the color.
HSVA fade_in(double unit_amount) const noexcept
Increases opacity of the color.
HSVA fade_out(double unit_amount) const noexcept
Decreases opacity of the color.
Color saturate(double unit_amount) const noexcept
Produces a fuller tone by saturating the color.
double lightness
The lightness of the color, from 0.0 (black) to 1.0 (white).
HSLA lighten(double unit_amount) const noexcept
Produces a tint by lightening the color.
HSLA saturate(double unit_amount) const noexcept
Produces a fuller tone by saturating the color.
HSLA to_hsla() const noexcept
Converts a color to the hue-saturation-lightness (HSL) color model.
HSVA saturate(double unit_amount) const noexcept
Produces a fuller tone by saturating the color.
Color darken(double unit_amount) const noexcept
Produces a shade by darkening the color.
HSLA darken(double unit_amount) const noexcept
Produces a shade by darkening the color.
HSVA revalue(double unit_amount) const noexcept
Produces a brighter color by increasing the value.
HSVA rotate_hue(double degrees) const noexcept
Returns a color by rotating the hue, leaving the other properties constant.
Color fade_out(double unit_amount) const noexcept
Decreases opacity of the color.
Color desaturate(double unit_amount) const noexcept
Produces a weaker tone by desaturating the color.
static Color from_rgba(double red, double green, double blue, double alpha=1.0) noexcept
Constructs a color with the given components.
double saturation
The fullness of the color, from 0.0 (grey) to 1.0 (fully saturated).
HSVA desaturate(double unit_amount) const noexcept
Produces a weaker tone by desaturating the color.
HSLA fade_in(double unit_amount) const noexcept
Increases opacity of the color.
double hue
The hue in degrees from 0 to 360.
Color to_rgba() const noexcept
Converts color to the RGBA color model.
double alpha
The opacity of the color, from 0.0 (fully transparent) to 1.0 (fully opaque).
static Color from_hsla(double hue, double saturation, double lightness, double alpha=1) noexcept
Constructs a color given the hue, saturation, lightness, and alpha.
HSLA rotate_hue(double degrees) const noexcept
Returns a color by rotating the hue, leaving the other properties.
HSVA devalue(double unit_amount) const noexcept
Produces a darker color by decreasing the value.
Color lighten(double unit_amount) const noexcept
Produces a tint by lightening the color.
Color to_rgba() const noexcept
Converts color to the RGBA color model.
double alpha
The opacity of the color, from 0.0 (fully transparent) to 1.0 (fully opaque).
HSLA desaturate(double unit_amount) const noexcept
Produces a weaker tone by desaturating the color.