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 toHalf3Color37 half3 toHalf3() { return half3{r / 255.0f, g / 255.0f, b / 255.0f}; } 38 toHalf4Color39 half4 toHalf4() { return half4{r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f}; } 40 }; 41 42 const Color Color::RED{255, 0, 0, 255}; 43 const Color Color::GREEN{0, 255, 0, 255}; 44 const Color Color::BLUE{0, 0, 255, 255}; 45 const Color Color::WHITE{255, 255, 255, 255}; 46 const Color Color::BLACK{0, 0, 0, 255}; 47 const Color Color::TRANSPARENT{0, 0, 0, 0}; 48 49 class ColorTransformHelper { 50 public: DegammaColorSingle(half & s)51 static void DegammaColorSingle(half& s) { 52 if (s <= 0.03928f) 53 s = s / 12.92f; 54 else 55 s = pow((s + 0.055f) / 1.055f, 2.4f); 56 } 57 DegammaColor(half3 & color)58 static void DegammaColor(half3& color) { 59 DegammaColorSingle(color.r); 60 DegammaColorSingle(color.g); 61 DegammaColorSingle(color.b); 62 } 63 GammaColorSingle(half & s)64 static void GammaColorSingle(half& s) { 65 if (s <= 0.0031308f) { 66 s = s * 12.92f; 67 } else { 68 s = 1.055f * pow(s, (1.0f / 2.4f)) - 0.055f; 69 } 70 } 71 GammaColor(half3 & color)72 static void GammaColor(half3& color) { 73 GammaColorSingle(color.r); 74 GammaColorSingle(color.g); 75 GammaColorSingle(color.b); 76 } 77 applyMatrix(half3 & color,const mat3 & mat)78 static void applyMatrix(half3& color, const mat3& mat) { 79 half3 ret = half3(0); 80 81 for (int i = 0; i < 3; i++) { 82 for (int j = 0; j < 3; j++) { 83 ret[i] = ret[i] + color[j] * mat[j][i]; 84 } 85 } 86 color = ret; 87 } 88 toHalf3(const Color & color)89 static half3 toHalf3(const Color& color) { 90 return half3{color.r / 255.0f, color.g / 255.0f, color.b / 255.0f}; 91 } 92 toHalf4(const Color & color)93 static half4 toHalf4(const Color& color) { 94 return half4{color.r / 255.0f, color.g / 255.0f, color.b / 255.0f, color.a / 255.0f}; 95 } 96 }; 97 } // namespace 98 } // namespace android 99