@@ -46,66 +46,60 @@ static f32x4 srgbToRgb(f32x4 sRGB) noexcept
4646 return r;
4747}
4848
49+ static const f32x4x4 rgbToXyzMat = f32x4x4
50+ (
51+ 0 .41239079926595934f , 0 .21263900587151027f , 0 .01933081871559182f , 0 .0f ,
52+ 0 .35758433938387800f , 0 .71516867876775600f , 0 .11919477979462598f , 0 .0f ,
53+ 0 .18048078840183430f , 0 .07219231536073371f , 0 .95053215224966070f , 0 .0f ,
54+ 0 .0f , 0 .0f , 0 .0f , 0 .0f
55+ );
56+ static const f32x4x4 xyzToRgbMat = f32x4x4
57+ (
58+ 3 .2409699419045226f , -0 .96924363628087960f , 0 .05563007969699366f , 0 .0f ,
59+ -1 .5373831775700940f , 1 .87596750150772020f , -0 .20397695888897652f , 0 .0f ,
60+ -0 .4986107602930034f , 0 .04155505740717559f , 1 .05697151424287860f , 0 .0f ,
61+ 0 .0f , 0 .0f , 0 .0f , 0 .0f
62+ );
63+
4964/* *
50- * @brief Converts linear RGB color to the XYZ color space. (CIE 1931)
51- * @param rgb target linear RGB color
65+ * @brief Converts linear sRGB color to the CIE XYZ color space.
66+ * @param rgb target linear sRGB color
5267 */
53- static f32x4 rgbToXyz (f32x4 rgb) noexcept
54- {
55- static const auto m = f32x4x4
56- (
57- 0 .4124564f , 0 .2126729f , 0 .0193339f , 0 .0f ,
58- 0 .3575761f , 0 .7151522f , 0 .1191920f , 0 .0f ,
59- 0 .1804375f , 0 .0721750f , 0 .9503041f , 0 .0f ,
60- 0 .0f , 0 .0f , 0 .0f , 0 .0f
61- );
62- return multiply3x3 (m, rgb);
63- }
68+ static f32x4 rgbToXyz (f32x4 rgb) noexcept { return multiply3x3 (rgbToXyzMat, rgb); }
6469/* *
65- * @brief Converts XYZ color to the linear RGB color space. (CIE 1931)
66- * @param xyz target XYZ color
70+ * @brief Converts CIE XYZ color to the linear sRGB color space.
71+ * @param xyz target CIE XYZ color
6772 */
68- static f32x4 xyzToRgb (f32x4 xyz) noexcept
69- {
70- static const auto m = f32x4x4
71- (
72- 3 .2404542f , -0 .9692660f , 0 .0556434f , 0 .0f ,
73- -1 .5371385f , 1 .8760108f , -0 .2040259f , 0 .0f ,
74- -0 .4985314f , 0 .0415560f , 1 .0572252f , 0 .0f ,
75- 0 .0f , 0 .0f , 0 .0f , 0 .0f
76- );
77- return multiply3x3 (m, xyz);
78- }
73+ static f32x4 xyzToRgb (f32x4 xyz) noexcept { return multiply3x3 (xyzToRgbMat, xyz); }
7974
8075/* *
81- * @brief Converts XYZ color to the YXY color space.
82- * @param xyz target XYZ color
76+ * @brief Converts CIE XYZ color to the CIE xyY color space.
77+ * @param xyz target CIE XYZ color
8378 */
84- static f32x4 xyzToYxy (f32x4 xyz) noexcept
79+ static f32x4 xyzToXyy (f32x4 xyz) noexcept
8580{
86- auto y = xyz.getY ();
87- auto inv = 1 .0f / dot3 (xyz, f32x4::one);
88- return f32x4 (y, xyz.getX () * inv, y * inv);
81+ auto a = std::max (xyz.getX () + xyz.getY () + xyz.getZ (), 1e-5f );
82+ return f32x4 (xyz.getX () / a, xyz.getY () / a, xyz.getY ());
8983}
9084/* *
91- * @brief Converts YXY color to the XYZ color space.
92- * @param yxy target YXY color
85+ * @brief Converts CIE xyY color to the CIE XYZ color space.
86+ * @param xyy target CIE xyY color
9387 */
94- static f32x4 yxyToXyz (f32x4 yxy ) noexcept
88+ static f32x4 xyyToXyz (f32x4 xyy ) noexcept
9589{
96- auto x = yxy. getX (), y = yxy .getY (), z = yxy. getZ ( );
97- return f32x4 (x * y / z, x, x * (1 .0f - y - z) / z );
90+ float a = xyy. getZ () / std::max (xyy .getY (), 1e- 5f );
91+ return f32x4 (xyy. getX () * a, xyy. getZ (), (1 .0f - xyy. getX () - xyy. getY ()) * a );
9892}
9993
10094/* *
101- * @brief Converts linear RGB color to the YXY color space.
95+ * @brief Converts linear sRGB color to the CIE xyY color space.
10296 * @param rgb target linear RGB color
10397 */
104- static f32x4 rgbToYxy (f32x4 rgb) noexcept { return xyzToYxy (rgbToXyz (rgb)); }
98+ static f32x4 rgbToXyy (f32x4 rgb) noexcept { return xyzToXyy (rgbToXyz (rgb)); }
10599/* *
106- * @brief Converts YXY color to the linear RGB color space.
107- * @param yxy target YXY color
100+ * @brief Converts CIE xyY color to the linear sRGB color space.
101+ * @param xyy target CIE xyY color
108102 */
109- static f32x4 yxyToRgb (f32x4 yxy ) noexcept { return xyzToRgb (yxyToXyz (yxy )); }
103+ static f32x4 xyyToRgb (f32x4 xyy ) noexcept { return xyzToRgb (xyyToXyz (xyy )); }
110104
111105} // namespace math
0 commit comments