36template <ColorType Space>
37constexpr Space
lerp_channels(
const Space& from,
const Space& to,
const float amount)
39 return Space{std::lerp(from.ch1(), to.ch1(), amount), std::lerp(from.ch2(), to.ch2(), amount),
40 std::lerp(from.ch3(), to.ch3(), amount),
41 std::lerp(from.alpha(), to.alpha(), amount)};
52constexpr float chroma_scale(
const float from,
const float to,
const float end)
54 return from == end ? 1.f : (to - end) / (from - end);
58export namespace aest::colors
71template <ColorType Space>
103template <LightnessSpace Space = OKLCH, ColorType Color>
109 const auto working = convert<Space>(color);
110 const float lightness = Lightness<Space>::get(working);
111 return convert<Color>(
112 Lightness<Space>::set(working, lightness + (1 - lightness) * clamp(amount), 1.f));
131template <LightnessSpace Space = OKLCH, ColorType Color>
132 requires ConvertibleTo<Space, Color> && ConvertibleTo<Color, Space>
133constexpr Color
darken(
const Color& color,
137 const auto working = convert<Space>(color);
138 return convert<Color>(
139 Lightness<Space>::set(working, Lightness<Space>::get(working) * (1 - clamp(amount)), 0.f));
164template <ColorType Space>
204template <
typename Space =
void, ColorType Color>
208constexpr Color
mix(
const Color& from,
213 using Working = WorkingSpace<Space, Color>;
214 return convert<Color>(
215 Interpolation<Working>::lerp(convert<Working>(from), convert<Working>(to), clamp(amount)));
236template <
typename Space =
void, ColorType From, ColorType To>
237 requires(!std::same_as<From, To>)
238From
mix(
const From& from,
const To& to,
float amount,
In<Space> space = {}) =
delete;
273template <
typename Space =
void, ColorType Color>
277constexpr Color
mix(
const Color& from,
283 using Working = WorkingSpace<Space, Color>;
284 return convert<Color>(Interpolation<Working>::lerp(convert<Working>(from), convert<Working>(to),
285 clamp(amount), mode));
304template <
typename Space =
void, ColorType From, ColorType To>
305 requires(!std::same_as<From, To>)
322template <ColorType Color>
323 requires ConvertibleTo<XYZ, Color>
346template <ColorType Color1, ColorType Color2>
347 requires ConvertibleTo<SRGB, Color1> && ConvertibleTo<SRGB, Color2>
367template <ColorType Space>
414template <SaturationSpace Space = OKLCH, ColorType Color>
420 const auto working = convert<Space>(color);
421 const float saturation = Saturation<Space>::get(working);
422 if constexpr (BoundedSaturationSpace<Space>)
423 return convert<Color>(Saturation<Space>::set(
424 working, saturation + (Saturation<Space>::MAX - saturation) * clamp(amount)));
426 return convert<Color>(
427 Saturation<Space>::set(working, saturation * (1 + non_negative(amount))));
446template <SaturationSpace Space = OKLCH, ColorType Color>
447 requires ConvertibleTo<Space, Color> && ConvertibleTo<Color, Space>
452 const auto working = convert<Space>(color);
453 return convert<Color>(
454 Saturation<Space>::set(working, Saturation<Space>::get(working) * (1 - clamp(amount))));
470template <SaturationSpace Space = OKLCH, ColorType Color>
471 requires ConvertibleTo<Space, Color> && ConvertibleTo<Color, Space>
474 return convert<Color>(Saturation<Space>::set(convert<Space>(color), 0.f));
485template <ColorType Space>
514template <HueSpace Space = OKLCH, ColorType Color>
520 const auto working = convert<Space>(color);
521 return convert<Color>(Hue<Space>::set(working, Hue<Space>::get(working) + degrees));
532template <HueSpace Space = OKLCH, ColorType Color>
533 requires ConvertibleTo<Space, Color> && ConvertibleTo<Color, Space>
536 return rotate_hue<Space>(color, 180.f);
551template <BoundedRGBSpace Color>
552constexpr Color
invert(
const Color& color)
554 return Color{1 - color.red(), 1 - color.green(), 1 - color.blue(), color.alpha()};
558namespace aest::colors
580 const float difference = to - from;
584 if (difference > 180)
586 else if (difference < -180)
590 if (difference > 0 && difference < 180)
592 else if (difference > -180 && difference <= 0)
604 return wrap_hue(std::lerp(from, to, amount));
629 if (fromGray && toGray)
631 return lerp_hue(fromGray ? to : from, toGray ? from : to, amount, mode);
638template <BoundedRGBSpace RGB>
646 static constexpr float get(
const BasicHSL<RGB>& color)
noexcept {
return color.lightness(); }
660 const float lightness,
661 [[maybe_unused]]
const float end)
676 static constexpr float get(
const OKLab& color)
noexcept {
return color.lightness(); }
687 static constexpr OKLab set(
const OKLab& color,
const float lightness,
const float end)
690 return OKLab{lightness, color.
a() * scale, color.
b() * scale, color.
alpha()};
703 static constexpr float get(
const OKLCH& color)
noexcept {
return color.lightness(); }
714 static constexpr OKLCH set(
const OKLCH& color,
const float lightness,
const float end)
727template <RGBGamut Gamut, TransferFunction Transfer>
756 static constexpr XYZ lerp(
const XYZ& from,
const XYZ& to,
const float amount)
787template <BoundedRGBSpace RGB>
833template <BoundedRGBSpace RGB>
923template <BoundedRGBSpace RGB>
927 static constexpr float MAX = 1.f;
934 static constexpr float get(
const BasicHSL<RGB>& color)
noexcept {
return color.saturation(); }
952template <BoundedRGBSpace RGB>
956 static constexpr float MAX = 1.f;
963 static constexpr float get(
const BasicHSV<RGB>& color)
noexcept {
return color.saturation(); }
989 static constexpr float get(
const OKLCH& color)
noexcept {
return color.chroma(); }
1007template <BoundedRGBSpace RGB>
1033template <BoundedRGBSpace RGB>
1064 static constexpr float get(
const OKLCH& color)
noexcept {
return color.hue(); }
constexpr float wrap_hue(const float hue)
Wrap a hue angle to the [0, 360) range.
constexpr ColorOut convert(const ColorIn &color)
Convert a color from one color space to another.
constexpr float chroma_scale(const float from, const float to, const float end)
How much of a color's chroma is left when its lightness moves from from to to on a straight line towa...
constexpr Color complement(const Color &color, In< Space > space={})
The complementary color: the hue turned by 180 degrees.
constexpr float ACHROMATIC_CHROMA
Chroma below which an OKLCH color counts as gray, with no hue.
constexpr Color grayscale(const Color &color, In< Space > space={})
Turns a color gray, keeping its lightness and alpha.
constexpr Color invert(const Color &color)
Inverts each channel of an encoded RGB color, 1 - c, like CSS invert(); alpha is kept.
constexpr Space lerp_channels(const Space &from, const Space &to, const float amount)
Interpolates every channel of two colors, alpha included.
constexpr Color rotate_hue(const Color &color, const float degrees, In< Space > space={})
Turns the hue of a color around the hue circle.
constexpr Color saturate(const Color &color, const float amount, In< Space > space={})
Raises the saturation of a color.
constexpr float lerp_hues(const float from, const bool fromGray, const float to, const bool toGray, const float amount, const HueMode mode)
Interpolates the hues of two colors, either of which may be gray.
constexpr float luminance(const Color &color)
Relative luminance of a color: the light it emits, weighted by how bright each primary looks.
constexpr Color desaturate(const Color &color, const float amount, In< Space > space={})
Moves the saturation of a color a fraction of the way toward gray.
float contrast_ratio(const Color1 &color1, const Color2 &color2)
WCAG 2.x contrast ratio between two colors, such as text and its background.
constexpr Color lighten(const Color &color, const float amount, In< Space > space={})
Moves the lightness of a color a fraction of the way toward white.
constexpr float lerp_hue(float from, float to, const float amount, const HueMode mode)
Interpolates between two hues along the arc mode picks, as in CSS Color 4.
constexpr float CONTRAST_FLARE
Viewing flare WCAG adds to both luminances of a contrast ratio.
constexpr Color mix(const Color &from, const Color &to, const float amount, In< Space > space={})
Blends two colors.
HueMode
How mix() goes around the hue circle between two hues, as in CSS Color 4.
@ longer
The longer arc, at least 180 degrees; equal hues make a full turn.
@ increasing
Clockwise, toward greater hues, wrapping past 360.
@ decreasing
Counterclockwise, toward smaller hues, wrapping past 0.
@ shorter
The shorter arc, at most 180 degrees: the default.
constexpr Color darken(const Color &color, const float amount, In< Space > space={})
Moves the lightness of a color a fraction of the way toward black.
A color as hue, saturation and lightness, over the encoded RGB space RGB.
constexpr float lightness() const noexcept
Get the lightness value in the 0-1 range.
constexpr float saturation() const noexcept
Get the saturation value in the 0-1 range.
constexpr float hue() const noexcept
Get the hue value in the 0-360 range.
A color as hue, saturation and value (brightness), over the encoded RGB space RGB.
constexpr float brightness() const noexcept
Get the brightness value in the 0-1 range.
constexpr float saturation() const noexcept
Get the saturation value in the 0-1 range.
constexpr float hue() const noexcept
Get the hue value in the 0-360 range.
A color in an RGB color space: the primaries and white point of Gamut, with channels encoded by Trans...
A color in OKLCH: OKLab in polar form, as lightness, chroma and hue.
constexpr float lightness() const noexcept
Get the perceived lightness, 1 being D65 white.
constexpr float chroma() const noexcept
Get the chroma, the distance from gray.
constexpr float hue() const noexcept
Get the hue angle in the 0-360 range.
A color in OKLab, the perceptual space by Björn Ottosson.
constexpr float a() const noexcept
Get the green-red axis, negative toward green.
constexpr float lightness() const noexcept
Get the perceived lightness, 1 being D65 white.
constexpr float b() const noexcept
Get the blue-yellow axis, negative toward blue.
A color in the CIE 1931 XYZ color space, relative to the D65 white point.
A SaturationSpace whose saturation has a maximum, Saturation::MAX.
Concept to check if a type is a color.
A color space that convert() can convert ColorIn to: directly through a Converter,...
A color space with a hue, whose Interpolation takes a HueMode.
A color space whose hue can be read and replaced through Hue.
A color space whose colors can be interpolated through Interpolation.
A color space whose lightness can be read and replaced through Lightness.
A color space whose saturation can be read and replaced through Saturation.
constexpr float alpha() const noexcept
Get the alpha channel value in the 0-1 range.
static constexpr BasicHSL< RGB > set(const BasicHSL< RGB > &color, const float hue)
Copy color with a new hue.
static constexpr float get(const BasicHSL< RGB > &color) noexcept
Get the hue of color.
static constexpr BasicHSV< RGB > set(const BasicHSV< RGB > &color, const float hue)
Copy color with a new hue.
static constexpr float get(const BasicHSV< RGB > &color) noexcept
Get the hue of color.
static constexpr float get(const OKLCH &color) noexcept
Get the hue of color.
static constexpr OKLCH set(const OKLCH &color, const float hue)
Copy color with a new hue.
Reads and replaces the hue of a color space, in degrees.
Tag naming the color space an operation works in.
static constexpr BasicHSL< RGB > lerp(const BasicHSL< RGB > &from, const BasicHSL< RGB > &to, const float amount)
Interpolates two colors, the hue along the shorter arc.
static constexpr BasicHSL< RGB > lerp(const BasicHSL< RGB > &from, const BasicHSL< RGB > &to, const float amount, const HueMode mode)
Interpolates two colors, the hue along the arc mode picks.
static constexpr BasicHSV< RGB > lerp(const BasicHSV< RGB > &from, const BasicHSV< RGB > &to, const float amount)
Interpolates two colors, the hue along the shorter arc.
static constexpr BasicHSV< RGB > lerp(const BasicHSV< RGB > &from, const BasicHSV< RGB > &to, const float amount, const HueMode mode)
Interpolates two colors, the hue along the arc mode picks.
static constexpr BasicRGB< Gamut, Transfer > lerp(const BasicRGB< Gamut, Transfer > &from, const BasicRGB< Gamut, Transfer > &to, const float amount)
Interpolates the channels and alpha of two colors.
static constexpr OKLCH lerp(const OKLCH &from, const OKLCH &to, const float amount, const HueMode mode)
Interpolates two colors, the hue along the arc mode picks.
static constexpr OKLCH lerp(const OKLCH &from, const OKLCH &to, const float amount)
Interpolates two colors, the hue along the shorter arc.
static constexpr OKLab lerp(const OKLab &from, const OKLab &to, const float amount)
Interpolates the lightness, a, b and alpha of two colors.
static constexpr XYZ lerp(const XYZ &from, const XYZ &to, const float amount)
Interpolates the tristimulus values and alpha of two colors.
Interpolates between two colors of a color space.
static constexpr BasicHSL< RGB > set(const BasicHSL< RGB > &color, const float lightness, const float end)
Copy color with a new lightness.
static constexpr float get(const BasicHSL< RGB > &color) noexcept
Get the lightness of color.
static constexpr OKLCH set(const OKLCH &color, const float lightness, const float end)
Copy color with a new lightness, on the straight line toward white or black.
static constexpr float get(const OKLCH &color) noexcept
Get the lightness of color.
static constexpr float get(const OKLab &color) noexcept
Get the lightness of color.
static constexpr OKLab set(const OKLab &color, const float lightness, const float end)
Copy color with a new lightness, on the straight line toward white or black.
Reads and replaces the lightness of a color space.
static constexpr float MAX
The saturation of the most colorful colors.
static constexpr float get(const BasicHSL< RGB > &color) noexcept
Get the saturation of color.
static constexpr BasicHSL< RGB > set(const BasicHSL< RGB > &color, const float saturation)
Copy color with a new saturation.
static constexpr float MAX
The saturation of the most colorful colors.
static constexpr float get(const BasicHSV< RGB > &color) noexcept
Get the saturation of color.
static constexpr BasicHSV< RGB > set(const BasicHSV< RGB > &color, const float saturation)
Copy color with a new saturation.
static constexpr float get(const OKLCH &color) noexcept
Get the chroma of color.
static constexpr OKLCH set(const OKLCH &color, const float chroma)
Copy color with a new chroma.
Reads and replaces the saturation of a color space: how far a color is from the gray of its lightness...