Merge branch 'blender2.7'
[blender.git] / tests / gtests / blenlib / BLI_math_color_test.cc
1 /* Apache License, Version 2.0 */
2
3 #include "testing/testing.h"
4
5 #include "BLI_math.h"
6
7 TEST(math_color, RGBToHSVRoundtrip)
8 {
9         float orig_rgb[3] = {0.1f, 0.2f, 0.3f};
10         float hsv[3], rgb[3];
11         rgb_to_hsv_v(orig_rgb, hsv);
12         hsv_to_rgb_v(hsv, rgb);
13         EXPECT_V3_NEAR(orig_rgb, rgb, 1e-5);
14 }
15
16 TEST(math_color, RGBToHSLRoundtrip)
17 {
18         float orig_rgb[3] = {0.1f, 0.2f, 0.3f};
19         float hsl[3], rgb[3];
20         rgb_to_hsl_v(orig_rgb, hsl);
21         hsl_to_rgb_v(hsl, rgb);
22         EXPECT_V3_NEAR(orig_rgb, rgb, 1e-5);
23 }
24
25 TEST(math_color, RGBToYUVRoundtrip)
26 {
27         float orig_rgb[3] = {0.1f, 0.2f, 0.3f};
28         float yuv[3], rgb[3];
29         rgb_to_yuv(orig_rgb[0], orig_rgb[1], orig_rgb[2],
30                    &yuv[0], &yuv[1], &yuv[2], BLI_YUV_ITU_BT709);
31         yuv_to_rgb(yuv[0], yuv[1], yuv[2],
32                    &rgb[0], &rgb[1], &rgb[2], BLI_YUV_ITU_BT709);
33         EXPECT_V3_NEAR(orig_rgb, rgb, 1e-4);
34 }
35
36 TEST(math_color, RGBToYCCRoundtrip)
37 {
38         float orig_rgb[3] = {0.1f, 0.2f, 0.3f};
39         float ycc[3], rgb[3];
40
41         rgb_to_ycc(orig_rgb[0], orig_rgb[1], orig_rgb[2],
42                    &ycc[0], &ycc[1], &ycc[2],
43                    BLI_YCC_ITU_BT601);
44         ycc_to_rgb(ycc[0], ycc[1], ycc[2],
45                    &rgb[0], &rgb[1], &rgb[2],
46                    BLI_YCC_ITU_BT601);
47         EXPECT_V3_NEAR(orig_rgb, rgb, 1e-3);
48
49         rgb_to_ycc(orig_rgb[0], orig_rgb[1], orig_rgb[2],
50                    &ycc[0], &ycc[1], &ycc[2],
51                    BLI_YCC_ITU_BT709);
52         ycc_to_rgb(ycc[0], ycc[1], ycc[2],
53                    &rgb[0], &rgb[1], &rgb[2],
54                    BLI_YCC_ITU_BT709);
55         EXPECT_V3_NEAR(orig_rgb, rgb, 1e-3);
56
57         rgb_to_ycc(orig_rgb[0], orig_rgb[1], orig_rgb[2],
58                    &ycc[0], &ycc[1], &ycc[2],
59                    BLI_YCC_JFIF_0_255);
60         ycc_to_rgb(ycc[0], ycc[1], ycc[2],
61                    &rgb[0], &rgb[1], &rgb[2],
62                    BLI_YCC_JFIF_0_255);
63         EXPECT_V3_NEAR(orig_rgb, rgb, 1e-3);
64 }
65
66 TEST(math_color, LinearRGBTosRGBNearZero)
67 {
68         float linear_color = 0.002f;
69         float srgb_color = linearrgb_to_srgb(linear_color);
70         EXPECT_NEAR(0.02584f, srgb_color, 1e-5);
71 }
72
73 TEST(math_color, LinearRGBTosRGB)
74 {
75         float linear_color = 0.75f;
76         float srgb_color = linearrgb_to_srgb(linear_color);
77         EXPECT_NEAR(0.880824f, srgb_color, 1e-5);
78 }
79
80 TEST(math_color, LinearRGBTosRGBRoundtrip)
81 {
82         const int N = 50;
83         int i;
84         for (i = 0; i < N; ++i) {
85                 float orig_linear_color = (float) i / N;
86                 float srgb_color = linearrgb_to_srgb(orig_linear_color);
87                 float linear_color = srgb_to_linearrgb(srgb_color);
88                 EXPECT_NEAR(orig_linear_color, linear_color, 1e-5);
89         }
90 }