154fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik/*
254fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik * Copyright (C) 2015 The Android Open Source Project
354fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik *
454fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik * Licensed under the Apache License, Version 2.0 (the "License");
554fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik * you may not use this file except in compliance with the License.
654fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik * You may obtain a copy of the License at
754fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik *
854fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik *      http://www.apache.org/licenses/LICENSE-2.0
954fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik *
1054fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik * Unless required by applicable law or agreed to in writing, software
1154fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik * distributed under the License is distributed on an "AS IS" BASIS,
1254fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1354fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik * See the License for the specific language governing permissions and
1454fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik * limitations under the License.
1554fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik */
1654fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik#ifndef COLOR_H
1754fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik#define COLOR_H
1854fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik
19253f2c213f6ecda63b6872aee77bd30d5ec07c82Romain Guy#include <math.h>
20253f2c213f6ecda63b6872aee77bd30d5ec07c82Romain Guy
2154fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik#include <SkColor.h>
22caaaa66e57293e4a6f312649bf472eab84d5c7feRomain Guy#include <SkColorSpace.h>
2354fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik
2454fa17f667c285a5c9225e238c8132dfe830ef36Chris Craiknamespace android {
2554fa17f667c285a5c9225e238c8132dfe830ef36Chris Craiknamespace uirenderer {
2654fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik    namespace Color {
2754fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik        enum Color {
2854fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik            Red_500 = 0xFFF44336,
2954fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik            Pink_500 = 0xFFE91E63,
3054fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik            Purple_500 = 0xFF9C27B0,
3154fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik            DeepPurple_500 = 0xFF673AB7,
3254fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik            Indigo_500 = 0xFF3F51B5,
3354fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik            Blue_500 = 0xFF2196F3,
3454fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik            LightBlue_300 = 0xFF4FC3F7,
3554fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik            LightBlue_500 = 0xFF03A9F4,
3654fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik            Cyan_500 = 0xFF00BCD4,
3754fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik            Teal_500 = 0xFF009688,
3854fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik            Teal_700 = 0xFF00796B,
3954fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik            Green_500 = 0xFF4CAF50,
4054fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik            Green_700 = 0xFF388E3C,
4154fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik            LightGreen_500 = 0xFF8BC34A,
4254fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik            LightGreen_700 = 0xFF689F38,
4354fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik            Lime_500 = 0xFFCDDC39,
4454fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik            Yellow_500 = 0xFFFFEB3B,
4554fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik            Amber_500 = 0xFFFFC107,
4654fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik            Orange_500 = 0xFFFF9800,
4754fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik            DeepOrange_500 = 0xFFFF5722,
4854fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik            Brown_500 = 0xFF795548,
4954fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik            Grey_200 = 0xFFEEEEEE,
5054fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik            Grey_500 = 0xFF9E9E9E,
5154fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik            Grey_700 = 0xFF616161,
5254fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik            BlueGrey_500 = 0xFF607D8B,
5354fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik            Transparent = 0x00000000,
5454fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik            Black = 0xFF000000,
5554fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik            White = 0xFFFFFFFF,
5654fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik        };
5754fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik    }
5854fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik
5954fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik    static_assert(Color::White == SK_ColorWHITE, "color format has changed");
6054fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik    static_assert(Color::Black == SK_ColorBLACK, "color format has changed");
6154fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik
6254fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik    // Array of bright (500 intensity) colors for synthetic content
6354fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik    static const Color::Color BrightColors[] = {
6454fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik        Color::Red_500,
6554fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik        Color::Pink_500,
6654fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik        Color::Purple_500,
6754fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik        Color::DeepPurple_500,
6854fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik        Color::Indigo_500,
6954fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik        Color::Blue_500,
7054fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik        Color::LightBlue_500,
7154fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik        Color::Cyan_500,
7254fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik        Color::Teal_500,
7354fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik        Color::Green_500,
7454fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik        Color::LightGreen_500,
7554fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik        Color::Lime_500,
7654fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik        Color::Yellow_500,
7754fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik        Color::Amber_500,
7854fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik        Color::Orange_500,
7954fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik        Color::DeepOrange_500,
8054fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik        Color::Brown_500,
8154fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik        Color::Grey_500,
8254fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik        Color::BlueGrey_500,
8354fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik    };
8454fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik    static constexpr int BrightColorsCount = sizeof(BrightColors) / sizeof(Color::Color);
8554fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik
86caaaa66e57293e4a6f312649bf472eab84d5c7feRomain Guy    enum class TransferFunctionType : int8_t {
87caaaa66e57293e4a6f312649bf472eab84d5c7feRomain Guy        None = 0,
88caaaa66e57293e4a6f312649bf472eab84d5c7feRomain Guy        Full,
89caaaa66e57293e4a6f312649bf472eab84d5c7feRomain Guy        Limited,
90caaaa66e57293e4a6f312649bf472eab84d5c7feRomain Guy        Gamma
91caaaa66e57293e4a6f312649bf472eab84d5c7feRomain Guy    };
92caaaa66e57293e4a6f312649bf472eab84d5c7feRomain Guy
93253f2c213f6ecda63b6872aee77bd30d5ec07c82Romain Guy    // Opto-electronic conversion function for the sRGB color space
9491627f61705a68cd18fdbce1b8190a2ffdec5f10Romain Guy    // Takes a linear sRGB value and converts it to a gamma-encoded sRGB value
95253f2c213f6ecda63b6872aee77bd30d5ec07c82Romain Guy    static constexpr float OECF_sRGB(float linear) {
96253f2c213f6ecda63b6872aee77bd30d5ec07c82Romain Guy        // IEC 61966-2-1:1999
97253f2c213f6ecda63b6872aee77bd30d5ec07c82Romain Guy        return linear <= 0.0031308f ?
98253f2c213f6ecda63b6872aee77bd30d5ec07c82Romain Guy                linear * 12.92f : (powf(linear, 1.0f / 2.4f) * 1.055f) - 0.055f;
998762e332e3797fb41929a1c6069207f4906ca329Romain Guy    }
1008762e332e3797fb41929a1c6069207f4906ca329Romain Guy
1018762e332e3797fb41929a1c6069207f4906ca329Romain Guy    // Opto-electronic conversion function for the sRGB color space
10291627f61705a68cd18fdbce1b8190a2ffdec5f10Romain Guy    // Takes a linear sRGB value and converts it to a gamma-encoded sRGB value
1038762e332e3797fb41929a1c6069207f4906ca329Romain Guy    // This function returns the input unmodified if linear blending is not enabled
1048762e332e3797fb41929a1c6069207f4906ca329Romain Guy    static constexpr float OECF(float linear) {
1058762e332e3797fb41929a1c6069207f4906ca329Romain Guy#ifdef ANDROID_ENABLE_LINEAR_BLENDING
1068762e332e3797fb41929a1c6069207f4906ca329Romain Guy        return OECF_sRGB(linear);
107253f2c213f6ecda63b6872aee77bd30d5ec07c82Romain Guy#else
108253f2c213f6ecda63b6872aee77bd30d5ec07c82Romain Guy        return linear;
109253f2c213f6ecda63b6872aee77bd30d5ec07c82Romain Guy#endif
110253f2c213f6ecda63b6872aee77bd30d5ec07c82Romain Guy    }
111253f2c213f6ecda63b6872aee77bd30d5ec07c82Romain Guy
112253f2c213f6ecda63b6872aee77bd30d5ec07c82Romain Guy    // Electro-optical conversion function for the sRGB color space
11391627f61705a68cd18fdbce1b8190a2ffdec5f10Romain Guy    // Takes a gamma-encoded sRGB value and converts it to a linear sRGB value
114253f2c213f6ecda63b6872aee77bd30d5ec07c82Romain Guy    static constexpr float EOCF_sRGB(float srgb) {
115253f2c213f6ecda63b6872aee77bd30d5ec07c82Romain Guy        // IEC 61966-2-1:1999
116253f2c213f6ecda63b6872aee77bd30d5ec07c82Romain Guy        return srgb <= 0.04045f ? srgb / 12.92f : powf((srgb + 0.055f) / 1.055f, 2.4f);
1178762e332e3797fb41929a1c6069207f4906ca329Romain Guy    }
1188762e332e3797fb41929a1c6069207f4906ca329Romain Guy
1198762e332e3797fb41929a1c6069207f4906ca329Romain Guy    // Electro-optical conversion function for the sRGB color space
12091627f61705a68cd18fdbce1b8190a2ffdec5f10Romain Guy    // Takes a gamma-encoded sRGB value and converts it to a linear sRGB value
1218762e332e3797fb41929a1c6069207f4906ca329Romain Guy    // This function returns the input unmodified if linear blending is not enabled
1228762e332e3797fb41929a1c6069207f4906ca329Romain Guy    static constexpr float EOCF(float srgb) {
1238762e332e3797fb41929a1c6069207f4906ca329Romain Guy#ifdef ANDROID_ENABLE_LINEAR_BLENDING
1248762e332e3797fb41929a1c6069207f4906ca329Romain Guy        return EOCF_sRGB(srgb);
125253f2c213f6ecda63b6872aee77bd30d5ec07c82Romain Guy#else
126253f2c213f6ecda63b6872aee77bd30d5ec07c82Romain Guy        return srgb;
127253f2c213f6ecda63b6872aee77bd30d5ec07c82Romain Guy#endif
128253f2c213f6ecda63b6872aee77bd30d5ec07c82Romain Guy    }
129caaaa66e57293e4a6f312649bf472eab84d5c7feRomain Guy
130caaaa66e57293e4a6f312649bf472eab84d5c7feRomain Guy    // Returns whether the specified color space's transfer function can be
131caaaa66e57293e4a6f312649bf472eab84d5c7feRomain Guy    // approximated with the native sRGB transfer function. This method
132caaaa66e57293e4a6f312649bf472eab84d5c7feRomain Guy    // returns true for sRGB, gamma 2.2 and Display P3 for instance
133caaaa66e57293e4a6f312649bf472eab84d5c7feRomain Guy    bool transferFunctionCloseToSRGB(const SkColorSpace* colorSpace);
13454fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik} /* namespace uirenderer */
13554fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik} /* namespace android */
13654fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik
137efb4b06493fe7b1604c762a448b13c7af2845a8dRomain Guy#endif /* COLOR_H */
138