blob: 673041a661ceec22278383be2f6cf76cb96376ff [file] [log] [blame]
John Reck9ce2bf72018-07-02 18:33:32 -07001/*
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"
John Reck9ce2bf72018-07-02 18:33:32 -070018
Brian Salomonbb2499f22022-09-23 12:37:58 -040019#include <SkAndroidFrameworkUtils.h>
John Reck9ce2bf72018-07-02 18:33:32 -070020#include <SkColorFilter.h>
John Reck8f45d4a2018-08-15 10:17:12 -070021#include <SkGradientShader.h>
Brian Salomonbb2499f22022-09-23 12:37:58 -040022#include <SkHighContrastFilter.h>
John Reck9ce2bf72018-07-02 18:33:32 -070023#include <SkPaint.h>
John Reck8f45d4a2018-08-15 10:17:12 -070024#include <SkShader.h>
Brian Salomonbb2499f22022-09-23 12:37:58 -040025#include <log/log.h>
John Reck339cf9b2018-07-18 16:32:27 -070026
27#include <algorithm>
28#include <cmath>
John Reck9ce2bf72018-07-02 18:33:32 -070029
Brian Salomonbb2499f22022-09-23 12:37:58 -040030#include "Properties.h"
31#include "utils/Color.h"
John Reck8f45d4a2018-08-15 10:17:12 -070032
John Reck9ce2bf72018-07-02 18:33:32 -070033namespace android::uirenderer {
34
Lucas Dupin00af5272021-04-29 20:30:01 -070035SkColor makeLight(SkColor color) {
John Reck339cf9b2018-07-18 16:32:27 -070036 Lab lab = sRGBToLab(color);
37 float invertedL = std::min(110 - lab.L, 100.0f);
38 if (invertedL > lab.L) {
39 lab.L = invertedL;
40 return LabToSRGB(lab, SkColorGetA(color));
41 } else {
42 return color;
43 }
John Reck9ce2bf72018-07-02 18:33:32 -070044}
45
Lucas Dupin00af5272021-04-29 20:30:01 -070046SkColor makeDark(SkColor color) {
John Reck339cf9b2018-07-18 16:32:27 -070047 Lab lab = sRGBToLab(color);
48 float invertedL = std::min(110 - lab.L, 100.0f);
49 if (invertedL < lab.L) {
50 lab.L = invertedL;
51 return LabToSRGB(lab, SkColorGetA(color));
52 } else {
53 return color;
54 }
John Reck9ce2bf72018-07-02 18:33:32 -070055}
56
Lucas Dupin00af5272021-04-29 20:30:01 -070057SkColor transformColor(ColorTransform transform, SkColor color) {
John Reck9ce2bf72018-07-02 18:33:32 -070058 switch (transform) {
59 case ColorTransform::Light:
60 return makeLight(color);
61 case ColorTransform::Dark:
62 return makeDark(color);
63 default:
64 return color;
65 }
66}
67
Lucas Dupin00af5272021-04-29 20:30:01 -070068SkColor transformColorInverse(ColorTransform transform, SkColor color) {
69 switch (transform) {
70 case ColorTransform::Dark:
71 return makeLight(color);
72 case ColorTransform::Light:
73 return makeDark(color);
74 default:
75 return color;
76 }
77}
78
John Reck9ce2bf72018-07-02 18:33:32 -070079static void applyColorTransform(ColorTransform transform, SkPaint& paint) {
80 if (transform == ColorTransform::None) return;
81
82 SkColor newColor = transformColor(transform, paint.getColor());
83 paint.setColor(newColor);
84
John Reck8f45d4a2018-08-15 10:17:12 -070085 if (paint.getShader()) {
Brian Salomonbb2499f22022-09-23 12:37:58 -040086 SkAndroidFrameworkUtils::LinearGradientInfo info;
John Reck8f45d4a2018-08-15 10:17:12 -070087 std::array<SkColor, 10> _colorStorage;
88 std::array<SkScalar, _colorStorage.size()> _offsetStorage;
89 info.fColorCount = _colorStorage.size();
90 info.fColors = _colorStorage.data();
91 info.fColorOffsets = _offsetStorage.data();
John Reck8f45d4a2018-08-15 10:17:12 -070092
Brian Salomonbb2499f22022-09-23 12:37:58 -040093 if (SkAndroidFrameworkUtils::ShaderAsALinearGradient(paint.getShader(), &info) &&
94 info.fColorCount <= _colorStorage.size()) {
95 for (int i = 0; i < info.fColorCount; i++) {
96 info.fColors[i] = transformColor(transform, info.fColors[i]);
John Reck8f45d4a2018-08-15 10:17:12 -070097 }
Brian Salomonbb2499f22022-09-23 12:37:58 -040098 paint.setShader(SkGradientShader::MakeLinear(
99 info.fPoints, info.fColors, info.fColorOffsets, info.fColorCount,
100 info.fTileMode, info.fGradientFlags, nullptr));
John Reck8f45d4a2018-08-15 10:17:12 -0700101 }
102 }
103
John Reck9ce2bf72018-07-02 18:33:32 -0700104 if (paint.getColorFilter()) {
105 SkBlendMode mode;
106 SkColor color;
107 // TODO: LRU this or something to avoid spamming new color mode filters
Mike Reed2ee37432019-04-26 09:22:13 -0400108 if (paint.getColorFilter()->asAColorMode(&color, &mode)) {
John Reck9ce2bf72018-07-02 18:33:32 -0700109 color = transformColor(transform, color);
Mike Reed1c2f5fc2019-04-10 16:57:52 -0400110 paint.setColorFilter(SkColorFilters::Blend(color, mode));
John Reck9ce2bf72018-07-02 18:33:32 -0700111 }
112 }
113}
114
John Reck08ee8152018-09-20 16:27:46 -0700115static BitmapPalette paletteForColorHSV(SkColor color) {
116 float hsv[3];
117 SkColorToHSV(color, hsv);
118 return hsv[2] >= .5f ? BitmapPalette::Light : BitmapPalette::Dark;
119}
120
121static BitmapPalette filterPalette(const SkPaint* paint, BitmapPalette palette) {
122 if (palette == BitmapPalette::Unknown || !paint || !paint->getColorFilter()) {
123 return palette;
124 }
125
126 SkColor color = palette == BitmapPalette::Light ? SK_ColorWHITE : SK_ColorBLACK;
127 color = paint->getColorFilter()->filterColor(color);
128 return paletteForColorHSV(color);
129}
130
John Reck8f45d4a2018-08-15 10:17:12 -0700131bool transformPaint(ColorTransform transform, SkPaint* paint) {
132 // TODO
133 applyColorTransform(transform, *paint);
134 return true;
John Reck9ce2bf72018-07-02 18:33:32 -0700135}
136
John Reckf3c724f2018-09-20 13:00:04 -0700137bool transformPaint(ColorTransform transform, SkPaint* paint, BitmapPalette palette) {
John Reck08ee8152018-09-20 16:27:46 -0700138 palette = filterPalette(paint, palette);
John Reckf3c724f2018-09-20 13:00:04 -0700139 bool shouldInvert = false;
140 if (palette == BitmapPalette::Light && transform == ColorTransform::Dark) {
141 shouldInvert = true;
142 }
143 if (palette == BitmapPalette::Dark && transform == ColorTransform::Light) {
144 shouldInvert = true;
145 }
146 if (shouldInvert) {
147 SkHighContrastConfig config;
148 config.fInvertStyle = SkHighContrastConfig::InvertStyle::kInvertLightness;
149 paint->setColorFilter(SkHighContrastFilter::Make(config)->makeComposed(paint->refColorFilter()));
150 }
151 return shouldInvert;
152}
153
Chris Blume7b8a8082018-11-30 15:51:58 -0800154} // namespace android::uirenderer