1 /*
2  * Copyright (C) 2019 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 #pragma once
17 
18 #include <ui/ColorSpace.h>
19 
20 namespace android {
21 
22 namespace {
23 
24 struct Color {
25     uint8_t r;
26     uint8_t g;
27     uint8_t b;
28     uint8_t a;
29 
30     static const Color RED;
31     static const Color GREEN;
32     static const Color BLUE;
33     static const Color WHITE;
34     static const Color BLACK;
35     static const Color TRANSPARENT;
36 };
37 
38 const Color Color::RED{255, 0, 0, 255};
39 const Color Color::GREEN{0, 255, 0, 255};
40 const Color Color::BLUE{0, 0, 255, 255};
41 const Color Color::WHITE{255, 255, 255, 255};
42 const Color Color::BLACK{0, 0, 0, 255};
43 const Color Color::TRANSPARENT{0, 0, 0, 0};
44 
45 class ColorTransformHelper {
46 public:
DegammaColorSingle(half & s)47     static void DegammaColorSingle(half& s) {
48         if (s <= 0.03928f)
49             s = s / 12.92f;
50         else
51             s = pow((s + 0.055f) / 1.055f, 2.4f);
52     }
53 
DegammaColor(half3 & color)54     static void DegammaColor(half3& color) {
55         DegammaColorSingle(color.r);
56         DegammaColorSingle(color.g);
57         DegammaColorSingle(color.b);
58     }
59 
GammaColorSingle(half & s)60     static void GammaColorSingle(half& s) {
61         if (s <= 0.0031308f) {
62             s = s * 12.92f;
63         } else {
64             s = 1.055f * pow(s, (1.0f / 2.4f)) - 0.055f;
65         }
66     }
67 
GammaColor(half3 & color)68     static void GammaColor(half3& color) {
69         GammaColorSingle(color.r);
70         GammaColorSingle(color.g);
71         GammaColorSingle(color.b);
72     }
73 
applyMatrix(half3 & color,const mat3 & mat)74     static void applyMatrix(half3& color, const mat3& mat) {
75         half3 ret = half3(0);
76 
77         for (int i = 0; i < 3; i++) {
78             for (int j = 0; j < 3; j++) {
79                 ret[i] = ret[i] + color[j] * mat[j][i];
80             }
81         }
82         color = ret;
83     }
84 };
85 } // namespace
86 } // namespace android
87