ge211
2021.5.1
A student game engine
|
|
1 #include "ge211_color.hxx"
10 template<
class T,
class U>
11 static T weighted_average(T t,
double weight, U u) NOEXCEPT
13 auto f1 =
static_cast<double>(t);
14 auto f2 =
static_cast<double>(u);
15 double result = (1 - weight) * f1 + weight * f2;
19 template<
class Whole,
class Field,
class Goal>
20 static Whole adjust_field(Whole result, Field Whole::*field,
21 double weight, Goal goal) NOEXCEPT
23 result.*field = weighted_average(result.*field, weight, goal);
30 return Color{uint8_t(255 * red),
33 uint8_t(255 * alpha)};
40 static Color from_hcma(
double hue,
43 double alpha) NOEXCEPT
45 double H6 = std::fmod(hue, 360.0) / 60.0;
46 double X = C * (1 - std::fabs(std::fmod(H6, 2) - 1));
48 double r1 = 0, g1 = 0, b1 = 0;
74 double alpha) NOEXCEPT
76 double C = (1 - std::fabs(2 * lightness - 1)) * saturation;
77 double m = lightness - 0.5 * C;
78 return from_hcma(hue, C, m, alpha);
82 double alpha) NOEXCEPT
84 double C = value * saturation;
86 return from_hcma(hue, C, m, alpha);
92 weighted_average(red(), weight, that.red()),
93 weighted_average(green(), weight, that.green()),
94 weighted_average(blue(), weight, that.blue()),
95 weighted_average(alpha(), weight, that.alpha())
101 return Color{uint8_t(~red_), uint8_t(~blue_), uint8_t(~green_), alpha_};
106 return to_hsva().rotate_hue(degrees).to_rgba();
111 return to_hsla().lighten(unit_amount).to_rgba();
116 return to_hsla().darken(unit_amount).to_rgba();
121 return to_hsla().saturate(unit_amount).to_rgba();
126 return to_hsla().desaturate(unit_amount).to_rgba();
131 double R = color.red() / 255.0;
132 double G = color.green() / 255.0;
133 double B = color.blue() / 255.0;
135 double M = std::max(R, std::max(G, B));
136 double m = std::min(R, std::min(G, B));
140 (M == R) ? std::fmod((G - B) / C, 6) :
141 (M == G) ? (B - R) / C + 2 :
146 return std::make_tuple(H, C, M, m);
152 std::tie(H, C, M, m) = to_HCMm(*
this);
154 double L = (M + m) / 2;
155 double S = (L == 1) ? 0 : C / (1 - std::fabs(2 * L - 1));
157 return {H, S, L,
alpha() / 255.0};
163 std::tie(H, C, M, m) = to_HCMm(*
this);
166 double S = V == 0 ? 0 : C / V;
168 return {H, S, V,
alpha() / 255.0};
171 SDL_Color Color::to_sdl_() const NOEXCEPT
181 uint32_t Color::to_sdl_(
const SDL_PixelFormat* format)
const NOEXCEPT
183 return SDL_MapRGBA(format, red_, green_, blue_, alpha_);
188 return adjust_field(*
this, &Color::alpha_, unit_amount, 255);
193 return adjust_field(*
this, &Color::alpha_, unit_amount, 0);
199 double alpha) NOEXCEPT
201 , saturation{saturation}
202 , lightness{lightness}
214 result.hue = std::fmod(result.hue + degrees, 360);
240 return adjust_field(*
this, &
HSLA::alpha, unit_amount, 1.0);
245 return adjust_field(*
this, &
HSLA::alpha, unit_amount, 0.0);
254 double value,
double alpha) NOEXCEPT
256 , saturation(saturation)
264 result.hue = std::fmod(result.hue + degrees, 360);
280 return adjust_field(*
this, &
HSVA::value, unit_amount, 1.0);
285 return adjust_field(*
this, &
HSVA::value, unit_amount, 0.0);
290 return adjust_field(*
this, &
HSVA::alpha, unit_amount, 1.0);
295 return adjust_field(*
this, &
HSVA::alpha, unit_amount, 0.0);
HSVA rotate_hue(double degrees) const
Returns a color by rotating the hue, leaving the other properties constant.
HSLA saturate(double unit_amount) const
Produces a fuller tone by saturating the color.
HSLA to_hsla() const
Converts a color to the hue-saturation-lightness (HSL) color model.
double saturation
The fullness of the color, from 0.0 (grey) to 1.0 (fully saturated).
HSVA desaturate(double unit_amount) const
Produces a weaker tone by desaturating the color.
static Color from_rgba(double red, double green, double blue, double alpha=1.0)
Constructs a color with the given components.
HSVA revalue(double unit_amount) const
Produces a brighter color by increasing the value.
double alpha
The opacity of the color, from 0.0 (fully transparent) to 1.0 (fully opaque).
HSLA(double hue, double saturation, double lightness, double alpha=1.0)
Constructs a hue-saturation-lightness-alpha color from its unit interval components.
HSLA desaturate(double unit_amount) const
Produces a weaker tone by desaturating the color.
Color lighten(double unit_amount) const
Produces a tint by lightening the color.
HSVA fade_in(double unit_amount) const
Increases opacity of the color.
Color fade_out(double unit_amount) const
Decreases opacity of the color.
HSLA rotate_hue(double degrees) const
Returns a color by rotating the hue, leaving the other properties.
The game engine namespace.
Color to_rgba() const
Converts color to the RGBA color model.
Representation for the hue-saturation-value-alpha color model.
Representation for the hue-saturation-lightness-alpha color model.
double hue
The hue in degrees from 0 to 360.
uint8_t alpha() const
Gets the alpha (opacity) component of a color.
HSVA devalue(double unit_amount) const
Produces a darker color by decreasing the value.
HSLA lighten(double unit_amount) const
Produces a tint by lightening the color.
HSLA fade_out(double unit_amount) const
Decreases opacity of the color.
double alpha
The opacity of the color, from 0.0 (fully transparent) to 1.0 (fully opaque).
Color blend(double weight, Color that) const
Produces a blend of this color and that, with higher weight (between 0 and 1) increasing the level of...
HSVA fade_out(double unit_amount) const
Decreases opacity of the color.
double saturation
The fullness of the color, from 0,0 (grey) to 1.0 (fully saturated).
HSVA saturate(double unit_amount) const
Produces a fuller tone by saturating the color.
Color darken(double unit_amount) const
Produces a shade by darkening the color.
Color rotate_hue(double degrees) const
Returns a color by rotating the hue, leaving the other properties constant.
HSLA fade_in(double unit_amount) const
Increases opacity of the color.
static Color from_hsla(double hue, double saturation, double lightness, double alpha=1)
Constructs a color given the hue, saturation, lightness, and alpha.
static Color from_hsva(double hue, double saturation, double value, double alpha=1)
Constructs a color given the hue, saturation, value, and alpha.
Color desaturate(double unit_amount) const
Produces a weaker tone by desaturating the color.
Color to_rgba() const
Converts color to the RGBA color model.
double lightness
The lightness of the color, from 0.0 (black) to 1.0 (white).
double value
The brightness of the color, from 0.0 (black) to 1.0 (fully colored).
HSLA darken(double unit_amount) const
Produces a shade by darkening the color.
Color invert() const
Returns the inverse of a color.
HSVA(double hue, double saturation, double value, double alpha=1.0)
Constructs a hue-saturation-value-alpha color from its unit interval components.
HSVA to_hsva() const
Converts a color to the hue-saturation-value (HSV) color model.
Color saturate(double unit_amount) const
Produces a fuller tone by saturating the color.
Color fade_in(double unit_amount) const
Increases opacity of the color.