1 /* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include <gtest/gtest.h> 17 18 #include <base/util/color.h> 19 using namespace testing::ext; 20 class ColorTest : public testing::Test { 21 public: SetUpTestSuite()22 static void SetUpTestSuite() {} TearDownTestSuite()23 static void TearDownTestSuite() {} SetUp()24 void SetUp() override {} TearDown()25 void TearDown() override {} 26 }; 27 28 HWTEST_F(ColorTest, SRGBToLinearConv, TestSize.Level1) 29 { 30 // should give near 0.214041144 (depending FPE) 31 float result = BASE_NS::SRGBToLinearConv(0.5f); 32 EXPECT_FLOAT_EQ(result, 0.214041144f); 33 } 34 35 HWTEST_F(ColorTest, SRGBToLinearConvSmall, TestSize.Level1) 36 { 37 float result = BASE_NS::SRGBToLinearConv(0.01f); 38 EXPECT_FLOAT_EQ(result * 12.92f, 0.01f); 39 } 40 41 HWTEST_F(ColorTest, LinearToSRGBConv, TestSize.Level1) 42 { 43 float result = BASE_NS::LinearToSRGBConv(0.5f); 44 EXPECT_FLOAT_EQ(result, 0.735356927f); 45 } 46 47 HWTEST_F(ColorTest, MakeColorFromLinear, TestSize.Level1) 48 { 49 const uint32_t srcCol { 0x00eb3425 }; 50 BASE_NS::Color dstCol = BASE_NS::MakeColorFromLinear(srcCol); 51 EXPECT_FLOAT_EQ(dstCol.x, 0.921568632f); 52 EXPECT_FLOAT_EQ(dstCol.y, 0.203921571f); 53 EXPECT_FLOAT_EQ(dstCol.z, 0.145098045f); 54 EXPECT_FLOAT_EQ(dstCol.w, 0.0f); 55 } 56 57 HWTEST_F(ColorTest, MakeColorFromSRGB, TestSize.Level1) 58 { 59 const uint32_t srcCol { 0x00eb3425 }; 60 BASE_NS::Color dstCol = BASE_NS::MakeColorFromSRGB(srcCol); 61 EXPECT_FLOAT_EQ(dstCol.x, 0.830769956f); 62 EXPECT_FLOAT_EQ(dstCol.y, 0.0343398005f); 63 EXPECT_FLOAT_EQ(dstCol.z, 0.0185002182f); 64 EXPECT_FLOAT_EQ(dstCol.w, 0.0f); 65 } 66 67 HWTEST_F(ColorTest, FromColorToLinear, TestSize.Level1) 68 { 69 BASE_NS::Color srcCol { 0.921568632f, 0.203921571f, 0.145098045f, 0.0f }; 70 uint32_t dstCol = BASE_NS::FromColorToLinear(srcCol); 71 EXPECT_EQ(dstCol, 0x00eb3425); 72 } 73 74 HWTEST_F(ColorTest, FromColorToSRGB, TestSize.Level1) 75 { 76 BASE_NS::Color srcCol { 0.830769956f, 0.0343398005f, 0.0185002182f, 0.0f }; 77 uint32_t dstCol = BASE_NS::FromColorToSRGB(srcCol); 78 EXPECT_EQ(dstCol, 0x00eb3325); 79 } 80 HWTEST_F(ColorTest, FromColorToSRGBmaxValue, TestSize.Level1) 81 { 82 BASE_NS::Color srcCol { 1.0f, 1.0f, 1.0f, 1.0f }; 83 uint32_t dstCol = BASE_NS::FromColorToSRGB(srcCol); 84 EXPECT_EQ(dstCol, 0xffffffff); 85 } 86 HWTEST_F(ColorTest, FromColorToSRGBminValue, TestSize.Level1) 87 { 88 BASE_NS::Color srcCol { .0f, .0f, .0f, .0f }; 89 uint32_t dstCol = BASE_NS::FromColorToSRGB(srcCol); 90 EXPECT_EQ(dstCol, 0x00000000); 91 } 92 93 HWTEST_F(ColorTest, FromColorRGBAToLinear, TestSize.Level1) 94 { 95 BASE_NS::Color srcCol { 0.25f, 0.5f, 0.75f, 1.0f }; 96 uint32_t dstCol = BASE_NS::FromColorRGBAToLinear(srcCol); 97 EXPECT_EQ(dstCol, 0xffbf7f3f); 98 } 99 100 HWTEST_F(ColorTest, FromColorRGBAToSRGB, TestSize.Level1) 101 { 102 BASE_NS::Color srcCol { 0.25f, 0.5f, 0.75f, 1.0f }; // 0.75 = 223.74, 0.5 = 186, 0.25 = 135.79 103 uint32_t dstCol = BASE_NS::FromColorRGBAToSRGB(srcCol); 104 EXPECT_EQ(dstCol, 0xffe0bb88); 105 } 106 107 HWTEST_F(ColorTest, FromColorRGBA, TestSize.Level1) 108 { 109 BASE_NS::Color srcCol { 0.25f, 0.5f, 0.75f, 1.0f }; 110 uint32_t dstCol = BASE_NS::FromColorRGBA(srcCol, 0U); 111 if (true) { 112 EXPECT_EQ(dstCol, 0xffbf7f3f); // result of T9 113 } else { 114 // we set this to fail for long as FromColorRGBAToSRGB has a valid rounding output 115 EXPECT_EQ(dstCol, 0xffe0bb88); // result of T10 116 } 117 }