aest 0.1.0
Collection of C++ utility libraries
Loading...
Searching...
No Matches
Operations.cppm
Go to the documentation of this file.
1
7module;
8#include <algorithm>
9#include <cmath>
10#include <concepts>
11#include <cstdint>
12export module aest.colors:operations;
13
14import :color;
15import :converter;
16import :cylindrical;
17import :gamut;
18import :oklab;
19import :transfer;
20import :space;
21
22namespace aest::colors
23{
25constexpr float CONTRAST_FLARE = 0.05f;
26
36template <ColorType Space>
37constexpr Space lerp_channels(const Space& from, const Space& to, const float amount)
38{
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)};
42}
43
52constexpr float chroma_scale(const float from, const float to, const float end)
53{
54 return from == end ? 1.f : (to - end) / (from - end);
55}
56} // namespace aest::colors
57
58export namespace aest::colors
59{
71template <ColorType Space>
72struct Lightness;
73
79template <typename T>
80concept LightnessSpace = ColorType<T> && requires(const T& color, const float value) {
81 { Lightness<T>::get(color) } -> std::same_as<float>;
82 { Lightness<T>::set(color, value, value) } -> std::same_as<T>;
83};
84
103template <LightnessSpace Space = OKLCH, ColorType Color>
105constexpr Color lighten(const Color& color,
106 const float amount,
107 [[maybe_unused]] In<Space> space = {})
108{
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));
113}
114
131template <LightnessSpace Space = OKLCH, ColorType Color>
132 requires ConvertibleTo<Space, Color> && ConvertibleTo<Color, Space>
133constexpr Color darken(const Color& color,
134 const float amount,
135 [[maybe_unused]] In<Space> space = {})
136{
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));
140}
141
147enum class HueMode : std::uint8_t
148{
153};
154
164template <ColorType Space>
166
172template <typename T>
173concept InterpolationSpace = ColorType<T> && requires(const T& color, const float amount) {
174 { Interpolation<T>::lerp(color, color, amount) } -> std::same_as<T>;
175};
176
204template <typename Space = void, ColorType Color>
208constexpr Color mix(const Color& from,
209 const Color& to,
210 const float amount,
211 [[maybe_unused]] In<Space> space = {})
212{
213 using Working = WorkingSpace<Space, Color>;
214 return convert<Color>(
215 Interpolation<Working>::lerp(convert<Working>(from), convert<Working>(to), clamp(amount)));
216}
217
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;
239
244template <typename T>
246 InterpolationSpace<T> && requires(const T& color, const float amount, const HueMode mode) {
247 { Interpolation<T>::lerp(color, color, amount, mode) } -> std::same_as<T>;
248 };
249
273template <typename Space = void, ColorType Color>
277constexpr Color mix(const Color& from,
278 const Color& to,
279 const float amount,
280 const HueMode mode,
281 [[maybe_unused]] In<Space> space = {})
282{
283 using Working = WorkingSpace<Space, Color>;
284 return convert<Color>(Interpolation<Working>::lerp(convert<Working>(from), convert<Working>(to),
285 clamp(amount), mode));
286}
287
304template <typename Space = void, ColorType From, ColorType To>
305 requires(!std::same_as<From, To>)
306From mix(const From& from, const To& to, float amount, HueMode mode, In<Space> space = {}) = delete;
307
322template <ColorType Color>
323 requires ConvertibleTo<XYZ, Color>
324constexpr float luminance(const Color& color)
325{
326 return convert<XYZ>(color).y();
327}
328
346template <ColorType Color1, ColorType Color2>
347 requires ConvertibleTo<SRGB, Color1> && ConvertibleTo<SRGB, Color2>
348float contrast_ratio(const Color1& color1, const Color2& color2)
349{
350 const float luminance1 = luminance(convert<SRGB>(color1));
351 const float luminance2 = luminance(convert<SRGB>(color2));
352 return (std::max(luminance1, luminance2) + CONTRAST_FLARE)
353 / (std::min(luminance1, luminance2) + CONTRAST_FLARE);
354}
355
367template <ColorType Space>
369
375template <typename T>
376concept SaturationSpace = ColorType<T> && requires(const T& color, const float value) {
377 { Saturation<T>::get(color) } -> std::same_as<float>;
378 { Saturation<T>::set(color, value) } -> std::same_as<T>;
379};
380
385template <typename T>
387 { Saturation<T>::MAX } -> std::convertible_to<float>;
388};
389
414template <SaturationSpace Space = OKLCH, ColorType Color>
416constexpr Color saturate(const Color& color,
417 const float amount,
418 [[maybe_unused]] In<Space> space = {})
419{
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)));
425 else
426 return convert<Color>(
427 Saturation<Space>::set(working, saturation * (1 + non_negative(amount))));
428}
429
446template <SaturationSpace Space = OKLCH, ColorType Color>
447 requires ConvertibleTo<Space, Color> && ConvertibleTo<Color, Space>
448constexpr Color desaturate(const Color& color,
449 const float amount,
450 [[maybe_unused]] In<Space> space = {})
451{
452 const auto working = convert<Space>(color);
453 return convert<Color>(
454 Saturation<Space>::set(working, Saturation<Space>::get(working) * (1 - clamp(amount))));
455}
456
470template <SaturationSpace Space = OKLCH, ColorType Color>
471 requires ConvertibleTo<Space, Color> && ConvertibleTo<Color, Space>
472constexpr Color grayscale(const Color& color, [[maybe_unused]] In<Space> space = {})
473{
474 return convert<Color>(Saturation<Space>::set(convert<Space>(color), 0.f));
475}
476
485template <ColorType Space>
486struct Hue;
487
492template <typename T>
493concept HueSpace = ColorType<T> && requires(const T& color, const float value) {
494 { Hue<T>::get(color) } -> std::same_as<float>;
495 { Hue<T>::set(color, value) } -> std::same_as<T>;
496};
497
514template <HueSpace Space = OKLCH, ColorType Color>
516constexpr Color rotate_hue(const Color& color,
517 const float degrees,
518 [[maybe_unused]] In<Space> space = {})
519{
520 const auto working = convert<Space>(color);
521 return convert<Color>(Hue<Space>::set(working, Hue<Space>::get(working) + degrees));
522}
523
532template <HueSpace Space = OKLCH, ColorType Color>
533 requires ConvertibleTo<Space, Color> && ConvertibleTo<Color, Space>
534constexpr Color complement(const Color& color, [[maybe_unused]] In<Space> space = {})
535{
536 return rotate_hue<Space>(color, 180.f);
537}
538
551template <BoundedRGBSpace Color>
552constexpr Color invert(const Color& color)
553{
554 return Color{1 - color.red(), 1 - color.green(), 1 - color.blue(), color.alpha()};
555}
556} // namespace aest::colors
557
558namespace aest::colors
559{
566constexpr float ACHROMATIC_CHROMA = 1e-4f;
567
577constexpr float lerp_hue(float from, float to, const float amount, const HueMode mode)
578{
579 // Unwrap one end by a full turn so that the plain lerp follows the arc
580 const float difference = to - from;
581 switch (mode)
582 {
583 case HueMode::shorter:
584 if (difference > 180)
585 from += 360;
586 else if (difference < -180)
587 to += 360;
588 break;
589 case HueMode::longer:
590 if (difference > 0 && difference < 180)
591 from += 360;
592 else if (difference > -180 && difference <= 0)
593 to += 360;
594 break;
596 if (to < from)
597 to += 360;
598 break;
600 if (from < to)
601 from += 360;
602 break;
603 }
604 return wrap_hue(std::lerp(from, to, amount));
605}
606
622constexpr float lerp_hues(const float from,
623 const bool fromGray,
624 const float to,
625 const bool toGray,
626 const float amount,
627 const HueMode mode)
628{
629 if (fromGray && toGray)
630 return 0.f;
631 return lerp_hue(fromGray ? to : from, toGray ? from : to, amount, mode);
632}
633
638template <BoundedRGBSpace RGB>
639struct Lightness<BasicHSL<RGB>>
640{
646 static constexpr float get(const BasicHSL<RGB>& color) noexcept { return color.lightness(); }
647
659 static constexpr BasicHSL<RGB> set(const BasicHSL<RGB>& color,
660 const float lightness,
661 [[maybe_unused]] const float end)
662 {
663 return BasicHSL<RGB>{color.hue(), color.saturation(), lightness, color.alpha()};
664 }
665};
666
668template <>
670{
676 static constexpr float get(const OKLab& color) noexcept { return color.lightness(); }
677
687 static constexpr OKLab set(const OKLab& color, const float lightness, const float end)
688 {
689 const float scale = chroma_scale(color.lightness(), lightness, end);
690 return OKLab{lightness, color.a() * scale, color.b() * scale, color.alpha()};
691 }
692};
693
695template <>
697{
703 static constexpr float get(const OKLCH& color) noexcept { return color.lightness(); }
704
714 static constexpr OKLCH set(const OKLCH& color, const float lightness, const float end)
715 {
716 return OKLCH{lightness, color.chroma() * chroma_scale(color.lightness(), lightness, end),
717 color.hue(), color.alpha()};
718 }
719};
720
727template <RGBGamut Gamut, TransferFunction Transfer>
728struct Interpolation<BasicRGB<Gamut, Transfer>>
729{
739 const float amount)
740 {
741 return lerp_channels(from, to, amount);
742 }
743};
744
746template <>
748{
756 static constexpr XYZ lerp(const XYZ& from, const XYZ& to, const float amount)
757 {
758 return lerp_channels(from, to, amount);
759 }
760};
761
766template <>
768{
776 static constexpr OKLab lerp(const OKLab& from, const OKLab& to, const float amount)
777 {
778 return lerp_channels(from, to, amount);
779 }
780};
781
787template <BoundedRGBSpace RGB>
789{
801 static constexpr BasicHSL<RGB> lerp(const BasicHSL<RGB>& from,
802 const BasicHSL<RGB>& to,
803 const float amount,
804 const HueMode mode)
805 {
806 return BasicHSL<RGB>{lerp_hues(from.hue(), from.saturation() == 0, to.hue(),
807 to.saturation() == 0, amount, mode),
808 std::lerp(from.saturation(), to.saturation(), amount),
809 std::lerp(from.lightness(), to.lightness(), amount),
810 std::lerp(from.alpha(), to.alpha(), amount)};
811 }
812
820 static constexpr BasicHSL<RGB> lerp(const BasicHSL<RGB>& from,
821 const BasicHSL<RGB>& to,
822 const float amount)
823 {
824 return lerp(from, to, amount, HueMode::shorter);
825 }
826};
827
833template <BoundedRGBSpace RGB>
835{
847 static constexpr BasicHSV<RGB> lerp(const BasicHSV<RGB>& from,
848 const BasicHSV<RGB>& to,
849 const float amount,
850 const HueMode mode)
851 {
852 return BasicHSV<RGB>{lerp_hues(from.hue(), from.saturation() == 0, to.hue(),
853 to.saturation() == 0, amount, mode),
854 std::lerp(from.saturation(), to.saturation(), amount),
855 std::lerp(from.brightness(), to.brightness(), amount),
856 std::lerp(from.alpha(), to.alpha(), amount)};
857 }
858
866 static constexpr BasicHSV<RGB> lerp(const BasicHSV<RGB>& from,
867 const BasicHSV<RGB>& to,
868 const float amount)
869 {
870 return lerp(from, to, amount, HueMode::shorter);
871 }
872};
873
879template <>
881{
894 static constexpr OKLCH lerp(const OKLCH& from,
895 const OKLCH& to,
896 const float amount,
897 const HueMode mode)
898 {
899 return OKLCH{std::lerp(from.lightness(), to.lightness(), amount),
900 std::lerp(from.chroma(), to.chroma(), amount),
901 lerp_hues(from.hue(), from.chroma() < ACHROMATIC_CHROMA, to.hue(),
902 to.chroma() < ACHROMATIC_CHROMA, amount, mode),
903 std::lerp(from.alpha(), to.alpha(), amount)};
904 }
905
913 static constexpr OKLCH lerp(const OKLCH& from, const OKLCH& to, const float amount)
914 {
915 return lerp(from, to, amount, HueMode::shorter);
916 }
917};
918
923template <BoundedRGBSpace RGB>
925{
927 static constexpr float MAX = 1.f;
928
934 static constexpr float get(const BasicHSL<RGB>& color) noexcept { return color.saturation(); }
935
942 static constexpr BasicHSL<RGB> set(const BasicHSL<RGB>& color, const float saturation)
943 {
944 return BasicHSL<RGB>{color.hue(), saturation, color.lightness(), color.alpha()};
945 }
946};
947
952template <BoundedRGBSpace RGB>
954{
956 static constexpr float MAX = 1.f;
957
963 static constexpr float get(const BasicHSV<RGB>& color) noexcept { return color.saturation(); }
964
971 static constexpr BasicHSV<RGB> set(const BasicHSV<RGB>& color, const float saturation)
972 {
973 return BasicHSV<RGB>{color.hue(), saturation, color.brightness(), color.alpha()};
974 }
975};
976
981template <>
983{
989 static constexpr float get(const OKLCH& color) noexcept { return color.chroma(); }
990
997 static constexpr OKLCH set(const OKLCH& color, const float chroma)
998 {
999 return OKLCH{color.lightness(), chroma, color.hue(), color.alpha()};
1000 }
1001};
1002
1007template <BoundedRGBSpace RGB>
1008struct Hue<BasicHSL<RGB>>
1009{
1015 static constexpr float get(const BasicHSL<RGB>& color) noexcept { return color.hue(); }
1016
1023 static constexpr BasicHSL<RGB> set(const BasicHSL<RGB>& color, const float hue)
1024 {
1025 return BasicHSL<RGB>{hue, color.saturation(), color.lightness(), color.alpha()};
1026 }
1027};
1028
1033template <BoundedRGBSpace RGB>
1034struct Hue<BasicHSV<RGB>>
1035{
1041 static constexpr float get(const BasicHSV<RGB>& color) noexcept { return color.hue(); }
1042
1049 static constexpr BasicHSV<RGB> set(const BasicHSV<RGB>& color, const float hue)
1050 {
1051 return BasicHSV<RGB>{hue, color.saturation(), color.brightness(), color.alpha()};
1052 }
1053};
1054
1056template <>
1057struct Hue<OKLCH>
1058{
1064 static constexpr float get(const OKLCH& color) noexcept { return color.hue(); }
1065
1072 static constexpr OKLCH set(const OKLCH& color, const float hue)
1073 {
1074 return OKLCH{color.lightness(), color.chroma(), hue, color.alpha()};
1075 }
1076};
1077} // namespace aest::colors
constexpr float wrap_hue(const float hue)
Wrap a hue angle to the [0, 360) range.
Definition Color.cppm:118
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...
Definition Gamut.cppm:296
A color in OKLCH: OKLab in polar form, as lightness, chroma and hue.
Definition OkLab.cppm:111
constexpr float lightness() const noexcept
Get the perceived lightness, 1 being D65 white.
Definition OkLab.cppm:139
constexpr float chroma() const noexcept
Get the chroma, the distance from gray.
Definition OkLab.cppm:145
constexpr float hue() const noexcept
Get the hue angle in the 0-360 range.
Definition OkLab.cppm:151
A color in OKLab, the perceptual space by Björn Ottosson.
Definition OkLab.cppm:56
constexpr float a() const noexcept
Get the green-red axis, negative toward green.
Definition OkLab.cppm:90
constexpr float lightness() const noexcept
Get the perceived lightness, 1 being D65 white.
Definition OkLab.cppm:84
constexpr float b() const noexcept
Get the blue-yellow axis, negative toward blue.
Definition OkLab.cppm:96
A color in the CIE 1931 XYZ color space, relative to the D65 white point.
Definition Color.cppm:390
A SaturationSpace whose saturation has a maximum, Saturation::MAX.
Concept to check if a type is a color.
Definition Color.cppm:448
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.
Definition Color.cppm:175
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.
Definition Space.cppm:30
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...