1 #include "third_party/blink/renderer/platform/graphics/lab_color_space.h"
2 #include "testing/gtest/include/gtest/gtest.h"
3
4 namespace LabColorSpace {
5
6 using blink::FloatPoint3D;
7
8 static constexpr FloatPoint3D rgbReferenceWhite =
9 FloatPoint3D(1.0f, 1.0f, 1.0f);
10 static constexpr FloatPoint3D labReferenceWhite =
11 FloatPoint3D(100.0f, 0.0f, 0.0f);
12 static constexpr float epsilon = 0.0001;
13
14 class LabColorSpaceTest : public testing::Test {
15 public:
AssertColorsEqual(const FloatPoint3D & color1,const FloatPoint3D & color2)16 void AssertColorsEqual(const FloatPoint3D& color1,
17 const FloatPoint3D& color2) {
18 EXPECT_NEAR(color1.X(), color2.X(), epsilon);
19 EXPECT_NEAR(color1.Y(), color2.Y(), epsilon);
20 EXPECT_NEAR(color1.Z(), color2.Z(), epsilon);
21 }
22 };
23
TEST_F(LabColorSpaceTest,XYZTranslation)24 TEST_F(LabColorSpaceTest, XYZTranslation) {
25 sRGBColorSpace colorSpace = sRGBColorSpace();
26
27 // Check whether white transformation is correct
28 FloatPoint3D xyzWhite = colorSpace.toXYZ(rgbReferenceWhite);
29 AssertColorsEqual(xyzWhite, kIlluminantD50);
30
31 FloatPoint3D rgbWhite = colorSpace.fromXYZ(kIlluminantD50);
32 AssertColorsEqual(rgbWhite, rgbReferenceWhite);
33
34 // Check whether transforming sRGB to XYZ and back gives the same RGB values
35 // for some random colors with different r, g, b components.
36 for (unsigned r = 0; r <= 255; r += 40) {
37 for (unsigned g = 0; r <= 255; r += 50) {
38 for (unsigned b = 0; r <= 255; r += 60) {
39 FloatPoint3D rgb = FloatPoint3D(r / 255.0f, g / 255.0f, b / 255.0f);
40 FloatPoint3D xyz = colorSpace.toXYZ(rgb);
41 AssertColorsEqual(rgb, colorSpace.fromXYZ(xyz));
42 }
43 }
44 }
45 }
46
TEST_F(LabColorSpaceTest,LabTranslation)47 TEST_F(LabColorSpaceTest, LabTranslation) {
48 RGBLABTransformer transformer = RGBLABTransformer();
49
50 // Check whether white transformation is correct
51 FloatPoint3D labWhite = transformer.sRGBToLab(rgbReferenceWhite);
52 AssertColorsEqual(labWhite, labReferenceWhite);
53
54 FloatPoint3D rgbWhite = transformer.LabToSRGB(labReferenceWhite);
55 AssertColorsEqual(rgbWhite, rgbReferenceWhite);
56
57 // Check whether transforming sRGB to Lab and back gives the same RGB values
58 // for some random colors with different r, g, b components.
59 for (unsigned r = 0; r <= 255; r += 40) {
60 for (unsigned g = 0; r <= 255; r += 50) {
61 for (unsigned b = 0; r <= 255; r += 60) {
62 FloatPoint3D rgb = FloatPoint3D(r / 255.0f, g / 255.0f, b / 255.0f);
63 FloatPoint3D lab = transformer.sRGBToLab(rgb);
64 AssertColorsEqual(rgb, transformer.LabToSRGB(lab));
65 }
66 }
67 }
68 }
69
70 } // namespace LabColorSpace
71