1 /*
2 * Copyright (C) 2018 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
17 #include "CanvasTransform.h"
18 #include "Properties.h"
19 #include "utils/Color.h"
20
21 #include <SkColorFilter.h>
22 #include <SkGradientShader.h>
23 #include <SkPaint.h>
24 #include <SkShader.h>
25
26 #include <algorithm>
27 #include <cmath>
28
29 #include <log/log.h>
30 #include <SkHighContrastFilter.h>
31
32 namespace android::uirenderer {
33
makeLight(SkColor color)34 SkColor makeLight(SkColor color) {
35 Lab lab = sRGBToLab(color);
36 float invertedL = std::min(110 - lab.L, 100.0f);
37 if (invertedL > lab.L) {
38 lab.L = invertedL;
39 return LabToSRGB(lab, SkColorGetA(color));
40 } else {
41 return color;
42 }
43 }
44
makeDark(SkColor color)45 SkColor makeDark(SkColor color) {
46 Lab lab = sRGBToLab(color);
47 float invertedL = std::min(110 - lab.L, 100.0f);
48 if (invertedL < lab.L) {
49 lab.L = invertedL;
50 return LabToSRGB(lab, SkColorGetA(color));
51 } else {
52 return color;
53 }
54 }
55
transformColor(ColorTransform transform,SkColor color)56 SkColor transformColor(ColorTransform transform, SkColor color) {
57 switch (transform) {
58 case ColorTransform::Light:
59 return makeLight(color);
60 case ColorTransform::Dark:
61 return makeDark(color);
62 default:
63 return color;
64 }
65 }
66
transformColorInverse(ColorTransform transform,SkColor color)67 SkColor transformColorInverse(ColorTransform transform, SkColor color) {
68 switch (transform) {
69 case ColorTransform::Dark:
70 return makeLight(color);
71 case ColorTransform::Light:
72 return makeDark(color);
73 default:
74 return color;
75 }
76 }
77
applyColorTransform(ColorTransform transform,SkPaint & paint)78 static void applyColorTransform(ColorTransform transform, SkPaint& paint) {
79 if (transform == ColorTransform::None) return;
80
81 SkColor newColor = transformColor(transform, paint.getColor());
82 paint.setColor(newColor);
83
84 if (paint.getShader()) {
85 SkShader::GradientInfo info;
86 std::array<SkColor, 10> _colorStorage;
87 std::array<SkScalar, _colorStorage.size()> _offsetStorage;
88 info.fColorCount = _colorStorage.size();
89 info.fColors = _colorStorage.data();
90 info.fColorOffsets = _offsetStorage.data();
91 SkShader::GradientType type = paint.getShader()->asAGradient(&info);
92
93 if (info.fColorCount <= 10) {
94 switch (type) {
95 case SkShader::kLinear_GradientType:
96 for (int i = 0; i < info.fColorCount; i++) {
97 info.fColors[i] = transformColor(transform, info.fColors[i]);
98 }
99 paint.setShader(SkGradientShader::MakeLinear(info.fPoint, info.fColors,
100 info.fColorOffsets, info.fColorCount,
101 info.fTileMode, info.fGradientFlags, nullptr));
102 break;
103 default:break;
104 }
105
106 }
107 }
108
109 if (paint.getColorFilter()) {
110 SkBlendMode mode;
111 SkColor color;
112 // TODO: LRU this or something to avoid spamming new color mode filters
113 if (paint.getColorFilter()->asAColorMode(&color, &mode)) {
114 color = transformColor(transform, color);
115 paint.setColorFilter(SkColorFilters::Blend(color, mode));
116 }
117 }
118 }
119
paletteForColorHSV(SkColor color)120 static BitmapPalette paletteForColorHSV(SkColor color) {
121 float hsv[3];
122 SkColorToHSV(color, hsv);
123 return hsv[2] >= .5f ? BitmapPalette::Light : BitmapPalette::Dark;
124 }
125
filterPalette(const SkPaint * paint,BitmapPalette palette)126 static BitmapPalette filterPalette(const SkPaint* paint, BitmapPalette palette) {
127 if (palette == BitmapPalette::Unknown || !paint || !paint->getColorFilter()) {
128 return palette;
129 }
130
131 SkColor color = palette == BitmapPalette::Light ? SK_ColorWHITE : SK_ColorBLACK;
132 color = paint->getColorFilter()->filterColor(color);
133 return paletteForColorHSV(color);
134 }
135
transformPaint(ColorTransform transform,SkPaint * paint)136 bool transformPaint(ColorTransform transform, SkPaint* paint) {
137 // TODO
138 applyColorTransform(transform, *paint);
139 return true;
140 }
141
transformPaint(ColorTransform transform,SkPaint * paint,BitmapPalette palette)142 bool transformPaint(ColorTransform transform, SkPaint* paint, BitmapPalette palette) {
143 palette = filterPalette(paint, palette);
144 bool shouldInvert = false;
145 if (palette == BitmapPalette::Light && transform == ColorTransform::Dark) {
146 shouldInvert = true;
147 }
148 if (palette == BitmapPalette::Dark && transform == ColorTransform::Light) {
149 shouldInvert = true;
150 }
151 if (shouldInvert) {
152 SkHighContrastConfig config;
153 config.fInvertStyle = SkHighContrastConfig::InvertStyle::kInvertLightness;
154 paint->setColorFilter(SkHighContrastFilter::Make(config)->makeComposed(paint->refColorFilter()));
155 }
156 return shouldInvert;
157 }
158
159 } // namespace android::uirenderer
160