Chris Craik | 54fa17f | 2015-11-25 14:14:53 -0800 | [diff] [blame] | 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 | |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 19 | #include <math.h> |
Derek Sollenberger | be3876c | 2018-04-20 16:13:31 -0400 | [diff] [blame] | 20 | #include <system/graphics.h> |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 21 | |
Chris Craik | 54fa17f | 2015-11-25 14:14:53 -0800 | [diff] [blame] | 22 | #include <SkColor.h> |
Romain Guy | caaaa66 | 2017-03-27 00:40:21 -0700 | [diff] [blame] | 23 | #include <SkColorSpace.h> |
Chris Craik | 54fa17f | 2015-11-25 14:14:53 -0800 | [diff] [blame] | 24 | |
| 25 | namespace android { |
| 26 | namespace uirenderer { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 27 | namespace Color { |
| 28 | enum Color { |
| 29 | Red_500 = 0xFFF44336, |
| 30 | Pink_500 = 0xFFE91E63, |
| 31 | Purple_500 = 0xFF9C27B0, |
| 32 | DeepPurple_500 = 0xFF673AB7, |
| 33 | Indigo_500 = 0xFF3F51B5, |
| 34 | Blue_500 = 0xFF2196F3, |
| 35 | LightBlue_300 = 0xFF4FC3F7, |
| 36 | LightBlue_500 = 0xFF03A9F4, |
| 37 | Cyan_500 = 0xFF00BCD4, |
Andrew Sapperstein | 9720a34 | 2018-03-22 19:46:27 +0000 | [diff] [blame] | 38 | Teal_500 = 0xFF008577, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 39 | Teal_700 = 0xFF00796B, |
| 40 | Green_500 = 0xFF4CAF50, |
| 41 | Green_700 = 0xFF388E3C, |
| 42 | LightGreen_500 = 0xFF8BC34A, |
| 43 | LightGreen_700 = 0xFF689F38, |
| 44 | Lime_500 = 0xFFCDDC39, |
| 45 | Yellow_500 = 0xFFFFEB3B, |
| 46 | Amber_500 = 0xFFFFC107, |
| 47 | Orange_500 = 0xFFFF9800, |
| 48 | DeepOrange_500 = 0xFFFF5722, |
| 49 | Brown_500 = 0xFF795548, |
| 50 | Grey_200 = 0xFFEEEEEE, |
| 51 | Grey_500 = 0xFF9E9E9E, |
| 52 | Grey_700 = 0xFF616161, |
| 53 | BlueGrey_500 = 0xFF607D8B, |
| 54 | Transparent = 0x00000000, |
| 55 | Black = 0xFF000000, |
| 56 | White = 0xFFFFFFFF, |
| 57 | }; |
| 58 | } |
Chris Craik | 54fa17f | 2015-11-25 14:14:53 -0800 | [diff] [blame] | 59 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 60 | static_assert(Color::White == SK_ColorWHITE, "color format has changed"); |
| 61 | static_assert(Color::Black == SK_ColorBLACK, "color format has changed"); |
Chris Craik | 54fa17f | 2015-11-25 14:14:53 -0800 | [diff] [blame] | 62 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 63 | // Array of bright (500 intensity) colors for synthetic content |
| 64 | static const Color::Color BrightColors[] = { |
| 65 | Color::Red_500, Color::Pink_500, Color::Purple_500, Color::DeepPurple_500, |
| 66 | Color::Indigo_500, Color::Blue_500, Color::LightBlue_500, Color::Cyan_500, |
| 67 | Color::Teal_500, Color::Green_500, Color::LightGreen_500, Color::Lime_500, |
| 68 | Color::Yellow_500, Color::Amber_500, Color::Orange_500, Color::DeepOrange_500, |
| 69 | Color::Brown_500, Color::Grey_500, Color::BlueGrey_500, |
| 70 | }; |
| 71 | static constexpr int BrightColorsCount = sizeof(BrightColors) / sizeof(Color::Color); |
Chris Craik | 54fa17f | 2015-11-25 14:14:53 -0800 | [diff] [blame] | 72 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 73 | enum class TransferFunctionType : int8_t { None = 0, Full, Limited, Gamma }; |
Romain Guy | caaaa66 | 2017-03-27 00:40:21 -0700 | [diff] [blame] | 74 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 75 | // Opto-electronic conversion function for the sRGB color space |
| 76 | // Takes a linear sRGB value and converts it to a gamma-encoded sRGB value |
| 77 | static constexpr float OECF_sRGB(float linear) { |
| 78 | // IEC 61966-2-1:1999 |
| 79 | return linear <= 0.0031308f ? linear * 12.92f : (powf(linear, 1.0f / 2.4f) * 1.055f) - 0.055f; |
| 80 | } |
Romain Guy | 8762e33 | 2016-10-12 12:14:07 -0700 | [diff] [blame] | 81 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 82 | // Opto-electronic conversion function for the sRGB color space |
| 83 | // Takes a linear sRGB value and converts it to a gamma-encoded sRGB value |
| 84 | // This function returns the input unmodified if linear blending is not enabled |
| 85 | static constexpr float OECF(float linear) { |
Romain Guy | 8762e33 | 2016-10-12 12:14:07 -0700 | [diff] [blame] | 86 | #ifdef ANDROID_ENABLE_LINEAR_BLENDING |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 87 | return OECF_sRGB(linear); |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 88 | #else |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 89 | return linear; |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 90 | #endif |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 91 | } |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 92 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 93 | // Electro-optical conversion function for the sRGB color space |
| 94 | // Takes a gamma-encoded sRGB value and converts it to a linear sRGB value |
| 95 | static constexpr float EOCF_sRGB(float srgb) { |
| 96 | // IEC 61966-2-1:1999 |
| 97 | return srgb <= 0.04045f ? srgb / 12.92f : powf((srgb + 0.055f) / 1.055f, 2.4f); |
| 98 | } |
Romain Guy | 8762e33 | 2016-10-12 12:14:07 -0700 | [diff] [blame] | 99 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 100 | // Electro-optical conversion function for the sRGB color space |
| 101 | // Takes a gamma-encoded sRGB value and converts it to a linear sRGB value |
| 102 | // This function returns the input unmodified if linear blending is not enabled |
| 103 | static constexpr float EOCF(float srgb) { |
Romain Guy | 8762e33 | 2016-10-12 12:14:07 -0700 | [diff] [blame] | 104 | #ifdef ANDROID_ENABLE_LINEAR_BLENDING |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 105 | return EOCF_sRGB(srgb); |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 106 | #else |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 107 | return srgb; |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 108 | #endif |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 109 | } |
Romain Guy | caaaa66 | 2017-03-27 00:40:21 -0700 | [diff] [blame] | 110 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 111 | // Returns whether the specified color space's transfer function can be |
| 112 | // approximated with the native sRGB transfer function. This method |
| 113 | // returns true for sRGB, gamma 2.2 and Display P3 for instance |
| 114 | bool transferFunctionCloseToSRGB(const SkColorSpace* colorSpace); |
Derek Sollenberger | be3876c | 2018-04-20 16:13:31 -0400 | [diff] [blame] | 115 | |
| 116 | sk_sp<SkColorSpace> DataSpaceToColorSpace(android_dataspace dataspace); |
Chris Craik | 54fa17f | 2015-11-25 14:14:53 -0800 | [diff] [blame] | 117 | } /* namespace uirenderer */ |
| 118 | } /* namespace android */ |
| 119 | |
Romain Guy | efb4b06 | 2017-02-27 11:00:04 -0800 | [diff] [blame] | 120 | #endif /* COLOR_H */ |