1 /*
2  * Copyright (C) 2015 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 #ifndef COLOR_H
17 #define COLOR_H
18 
19 #include <math.h>
20 #include <cutils/compiler.h>
21 #include <system/graphics.h>
22 #include <ui/PixelFormat.h>
23 
24 #include <SkColor.h>
25 #include <SkColorSpace.h>
26 #include <SkImageInfo.h>
27 
28 namespace android {
29 namespace uirenderer {
30 namespace Color {
31 enum Color {
32     Red_500 = 0xFFF44336,
33     Pink_500 = 0xFFE91E63,
34     Purple_500 = 0xFF9C27B0,
35     DeepPurple_500 = 0xFF673AB7,
36     Indigo_500 = 0xFF3F51B5,
37     Blue_500 = 0xFF2196F3,
38     LightBlue_300 = 0xFF4FC3F7,
39     LightBlue_500 = 0xFF03A9F4,
40     Cyan_500 = 0xFF00BCD4,
41     Teal_500 = 0xFF008577,
42     Teal_700 = 0xFF00796B,
43     Green_500 = 0xFF4CAF50,
44     Green_700 = 0xFF388E3C,
45     LightGreen_500 = 0xFF8BC34A,
46     LightGreen_700 = 0xFF689F38,
47     Lime_500 = 0xFFCDDC39,
48     Yellow_500 = 0xFFFFEB3B,
49     Amber_500 = 0xFFFFC107,
50     Orange_500 = 0xFFFF9800,
51     DeepOrange_500 = 0xFFFF5722,
52     Brown_500 = 0xFF795548,
53     Grey_200 = 0xFFEEEEEE,
54     Grey_500 = 0xFF9E9E9E,
55     Grey_700 = 0xFF616161,
56     BlueGrey_500 = 0xFF607D8B,
57     Transparent = 0x00000000,
58     Black = 0xFF000000,
59     White = 0xFFFFFFFF,
60 };
61 }
62 
63 static_assert(Color::White == SK_ColorWHITE, "color format has changed");
64 static_assert(Color::Black == SK_ColorBLACK, "color format has changed");
65 
66 // Array of bright (500 intensity) colors for synthetic content
67 static const Color::Color BrightColors[] = {
68         Color::Red_500,    Color::Pink_500,  Color::Purple_500,     Color::DeepPurple_500,
69         Color::Indigo_500, Color::Blue_500,  Color::LightBlue_500,  Color::Cyan_500,
70         Color::Teal_500,   Color::Green_500, Color::LightGreen_500, Color::Lime_500,
71         Color::Yellow_500, Color::Amber_500, Color::Orange_500,     Color::DeepOrange_500,
72         Color::Brown_500,  Color::Grey_500,  Color::BlueGrey_500,
73 };
74 static constexpr int BrightColorsCount = sizeof(BrightColors) / sizeof(Color::Color);
75 
76 enum class TransferFunctionType : int8_t { None = 0, Full, Limited, Gamma };
77 
78 // Opto-electronic conversion function for the sRGB color space
79 // Takes a linear sRGB value and converts it to a gamma-encoded sRGB value
OECF_sRGB(float linear)80 static constexpr float OECF_sRGB(float linear) {
81     // IEC 61966-2-1:1999
82     return linear <= 0.0031308f ? linear * 12.92f : (powf(linear, 1.0f / 2.4f) * 1.055f) - 0.055f;
83 }
84 
85 // Electro-optical conversion function for the sRGB color space
86 // Takes a gamma-encoded sRGB value and converts it to a linear sRGB value
EOCF_sRGB(float srgb)87 static constexpr float EOCF_sRGB(float srgb) {
88     // IEC 61966-2-1:1999
89     return srgb <= 0.04045f ? srgb / 12.92f : powf((srgb + 0.055f) / 1.055f, 2.4f);
90 }
91 
92 android::PixelFormat ColorTypeToPixelFormat(SkColorType colorType);
93 ANDROID_API SkColorType PixelFormatToColorType(android::PixelFormat format);
94 
95 ANDROID_API sk_sp<SkColorSpace> DataSpaceToColorSpace(android_dataspace dataspace);
96 
97 struct Lab {
98     float L;
99     float a;
100     float b;
101 };
102 
103 Lab sRGBToLab(SkColor color);
104 SkColor LabToSRGB(const Lab& lab, SkAlpha alpha);
105 
106 } /* namespace uirenderer */
107 } /* namespace android */
108 
109 #endif /* COLOR_H */
110