Color.cpp revision caaaa66e57293e4a6f312649bf472eab84d5c7fe
1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "Color.h"
18
19#include <cmath>
20
21namespace android {
22namespace uirenderer {
23
24static inline bool almostEqual(float a, float b) {
25    return std::abs(a - b) < 1e-2f;
26}
27
28bool transferFunctionCloseToSRGB(const SkColorSpace* colorSpace) {
29    if (colorSpace == nullptr) return true;
30    if (colorSpace->isSRGB()) return true;
31
32    SkColorSpaceTransferFn transferFunction;
33    if (colorSpace->isNumericalTransferFn(&transferFunction)) {
34        // sRGB transfer function params:
35        const float sRGBParamA = 1 / 1.055f;
36        const float sRGBParamB = 0.055f / 1.055f;
37        const float sRGBParamC = 1 / 12.92f;
38        const float sRGBParamD = 0.04045f;
39        const float sRGBParamE = 0.0f;
40        const float sRGBParamF = 0.0f;
41        const float sRGBParamG = 2.4f;
42
43        // This comparison will catch Display P3
44        return
45                almostEqual(sRGBParamA, transferFunction.fA)
46             && almostEqual(sRGBParamB, transferFunction.fB)
47             && almostEqual(sRGBParamC, transferFunction.fC)
48             && almostEqual(sRGBParamD, transferFunction.fD)
49             && almostEqual(sRGBParamE, transferFunction.fE)
50             && almostEqual(sRGBParamF, transferFunction.fF)
51             && almostEqual(sRGBParamG, transferFunction.fG);
52    }
53
54    return false;
55}
56
57}; // namespace uirenderer
58}; // namespace android
59