Line data Source code
1 : /**
2 : Copyright (c) 2023-2024 Stappler LLC <admin@stappler.dev>
3 :
4 : Permission is hereby granted, free of charge, to any person obtaining a copy
5 : of this software and associated documentation files (the "Software"), to deal
6 : in the Software without restriction, including without limitation the rights
7 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 : copies of the Software, and to permit persons to whom the Software is
9 : furnished to do so, subject to the following conditions:
10 :
11 : The above copyright notice and this permission notice shall be included in
12 : all copies or substantial portions of the Software.
13 :
14 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 : THE SOFTWARE.
21 : **/
22 :
23 : #ifndef CORE_GEOM_SPCOLORHCT_H_
24 : #define CORE_GEOM_SPCOLORHCT_H_
25 :
26 : #include "SPColorCam16.h"
27 :
28 : namespace STAPPLER_VERSIONIZED stappler::geom {
29 :
30 : struct alignas(16) ColorHCT {
31 : struct alignas(16) Values {
32 31506 : float hue;
33 26874 : float chroma;
34 26794 : float tone;
35 26790 : float alpha;
36 :
37 31506 : bool operator==(const Values& right) const = default;
38 119748 : bool operator!=(const Values& right) const = default;
39 : };
40 :
41 : static ColorHCT progress(const ColorHCT &a, const ColorHCT &b, float p);
42 :
43 : // returns closest possible HCT, that can be represented in sRGB by given HCT
44 : static ColorHCT solveColorHCT(Cam16Float h, Cam16Float c, Cam16Float t, float a);
45 : static Color4F solveColor4F(Cam16Float h, Cam16Float c, Cam16Float t, float a);
46 :
47 2240 : constexpr ColorHCT() : data({0.0f, 50.0f, 0.0f, 1.0f}), color(Color4F::BLACK) { }
48 :
49 28164 : ColorHCT(float h, float c, float t, float a)
50 28164 : : data({Cam16::sanitizeDegrees(h), c, t, a}), color() {
51 28164 : color = solveColor4F(data.hue, data.chroma, data.tone, data.alpha);
52 28164 : }
53 :
54 18622 : ColorHCT(const Values &d)
55 18622 : : data(d), color(solveColor4F(Cam16::sanitizeDegrees(data.hue), data.chroma, data.tone, data.alpha)) { }
56 :
57 100 : explicit ColorHCT(const Color4F& c) {
58 100 : auto cam = Cam16::create(c);
59 100 : data.hue = cam.hue;
60 100 : data.chroma = cam.chroma;
61 100 : data.tone = Cam16::LstarFromColor4F(c);
62 100 : data.alpha = c.a;
63 100 : color = c;
64 100 : }
65 :
66 25 : ColorHCT(const Color4F& c, float a)
67 25 : : ColorHCT(c) {
68 25 : data.alpha = a;
69 25 : }
70 :
71 59036 : Color4F asColor4F() const { return color; }
72 :
73 : ColorHCT &operator=(const Color4F &c) { *this = ColorHCT(c); return *this; }
74 : ColorHCT &operator=(const ColorHCT &c) = default;
75 :
76 712 : inline operator Color4F () const {
77 712 : return asColor4F();
78 : }
79 :
80 7152 : bool operator==(const ColorHCT& right) const = default;
81 894 : bool operator!=(const ColorHCT& right) const = default;
82 :
83 4020 : Values data;
84 3132 : Color4F color;
85 : };
86 :
87 : std::ostream & operator<<(std::ostream & stream, const ColorHCT & obj);
88 :
89 : }
90 :
91 : namespace STAPPLER_VERSIONIZED stappler {
92 :
93 : template <> inline
94 4653 : geom::ColorHCT progress<geom::ColorHCT>(const geom::ColorHCT &a, const geom::ColorHCT &b, float p) {
95 4653 : return geom::ColorHCT::progress(a, b, p);
96 : }
97 :
98 : }
99 :
100 : #endif /* CORE_GEOM_SPCOLORHCT_H_ */
|