blob: b667daf9c8501c62f1ffea2700165ee96b29073f [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>
Kevin Lubick4e8ce462022-12-01 20:29:16 +000020#include <SkBlendMode.h>
John Reck9ce2bf72018-07-02 18:33:32 -070021#include <SkColorFilter.h>
John Reck8f45d4a2018-08-15 10:17:12 -070022#include <SkGradientShader.h>
Brian Salomonbb2499f22022-09-23 12:37:58 -040023#include <SkHighContrastFilter.h>
John Reck9ce2bf72018-07-02 18:33:32 -070024#include <SkPaint.h>
John Reck8f45d4a2018-08-15 10:17:12 -070025#include <SkShader.h>
Brian Salomonbb2499f22022-09-23 12:37:58 -040026#include <log/log.h>
John Reck339cf9b2018-07-18 16:32:27 -070027
28#include <algorithm>
29#include <cmath>
John Reck9ce2bf72018-07-02 18:33:32 -070030
Brian Salomonbb2499f22022-09-23 12:37:58 -040031#include "Properties.h"
32#include "utils/Color.h"
John Reck8f45d4a2018-08-15 10:17:12 -070033
John Reck9ce2bf72018-07-02 18:33:32 -070034namespace android::uirenderer {
35
Lucas Dupin00af5272021-04-29 20:30:01 -070036SkColor makeLight(SkColor color) {
John Reck339cf9b2018-07-18 16:32:27 -070037 Lab lab = sRGBToLab(color);
38 float invertedL = std::min(110 - lab.L, 100.0f);
39 if (invertedL > lab.L) {
40 lab.L = invertedL;
41 return LabToSRGB(lab, SkColorGetA(color));
42 } else {
43 return color;
44 }
John Reck9ce2bf72018-07-02 18:33:32 -070045}
46
Lucas Dupin00af5272021-04-29 20:30:01 -070047SkColor makeDark(SkColor color) {
John Reck339cf9b2018-07-18 16:32:27 -070048 Lab lab = sRGBToLab(color);
49 float invertedL = std::min(110 - lab.L, 100.0f);
50 if (invertedL < lab.L) {
51 lab.L = invertedL;
52 return LabToSRGB(lab, SkColorGetA(color));
53 } else {
54 return color;
55 }
John Reck9ce2bf72018-07-02 18:33:32 -070056}
57
Lucas Dupin00af5272021-04-29 20:30:01 -070058SkColor transformColor(ColorTransform transform, SkColor color) {
John Reck9ce2bf72018-07-02 18:33:32 -070059 switch (transform) {
60 case ColorTransform::Light:
61 return makeLight(color);
62 case ColorTransform::Dark:
63 return makeDark(color);
64 default:
65 return color;
66 }
67}
68
Lucas Dupin00af5272021-04-29 20:30:01 -070069SkColor transformColorInverse(ColorTransform transform, SkColor color) {
70 switch (transform) {
71 case ColorTransform::Dark:
72 return makeLight(color);
73 case ColorTransform::Light:
74 return makeDark(color);
75 default:
76 return color;
77 }
78}
79
John Reck9ce2bf72018-07-02 18:33:32 -070080static void applyColorTransform(ColorTransform transform, SkPaint& paint) {
81 if (transform == ColorTransform::None) return;
82
Tyler Freemane0faa692023-11-16 00:48:54 +000083 if (transform == ColorTransform::Invert) {
84 auto filter = SkHighContrastFilter::Make(
85 {/* grayscale= */ false, SkHighContrastConfig::InvertStyle::kInvertLightness,
86 /* contrast= */ 0.0f});
87
88 if (paint.getColorFilter()) {
89 paint.setColorFilter(SkColorFilters::Compose(filter, paint.refColorFilter()));
90 } else {
91 paint.setColorFilter(filter);
92 }
93 return;
94 }
95
John Reck9ce2bf72018-07-02 18:33:32 -070096 SkColor newColor = transformColor(transform, paint.getColor());
97 paint.setColor(newColor);
98
John Reck8f45d4a2018-08-15 10:17:12 -070099 if (paint.getShader()) {
Brian Salomonbb2499f22022-09-23 12:37:58 -0400100 SkAndroidFrameworkUtils::LinearGradientInfo info;
John Reck8f45d4a2018-08-15 10:17:12 -0700101 std::array<SkColor, 10> _colorStorage;
102 std::array<SkScalar, _colorStorage.size()> _offsetStorage;
103 info.fColorCount = _colorStorage.size();
104 info.fColors = _colorStorage.data();
105 info.fColorOffsets = _offsetStorage.data();
John Reck8f45d4a2018-08-15 10:17:12 -0700106
Brian Salomonbb2499f22022-09-23 12:37:58 -0400107 if (SkAndroidFrameworkUtils::ShaderAsALinearGradient(paint.getShader(), &info) &&
108 info.fColorCount <= _colorStorage.size()) {
109 for (int i = 0; i < info.fColorCount; i++) {
110 info.fColors[i] = transformColor(transform, info.fColors[i]);
John Reck8f45d4a2018-08-15 10:17:12 -0700111 }
Brian Salomonbb2499f22022-09-23 12:37:58 -0400112 paint.setShader(SkGradientShader::MakeLinear(
113 info.fPoints, info.fColors, info.fColorOffsets, info.fColorCount,
114 info.fTileMode, info.fGradientFlags, nullptr));
John Reck8f45d4a2018-08-15 10:17:12 -0700115 }
116 }
117
John Reck9ce2bf72018-07-02 18:33:32 -0700118 if (paint.getColorFilter()) {
119 SkBlendMode mode;
120 SkColor color;
121 // TODO: LRU this or something to avoid spamming new color mode filters
Mike Reed2ee37432019-04-26 09:22:13 -0400122 if (paint.getColorFilter()->asAColorMode(&color, &mode)) {
John Reck9ce2bf72018-07-02 18:33:32 -0700123 color = transformColor(transform, color);
Mike Reed1c2f5fc2019-04-10 16:57:52 -0400124 paint.setColorFilter(SkColorFilters::Blend(color, mode));
John Reck9ce2bf72018-07-02 18:33:32 -0700125 }
126 }
127}
128
John Reck08ee8152018-09-20 16:27:46 -0700129static BitmapPalette paletteForColorHSV(SkColor color) {
130 float hsv[3];
131 SkColorToHSV(color, hsv);
132 return hsv[2] >= .5f ? BitmapPalette::Light : BitmapPalette::Dark;
133}
134
135static BitmapPalette filterPalette(const SkPaint* paint, BitmapPalette palette) {
136 if (palette == BitmapPalette::Unknown || !paint || !paint->getColorFilter()) {
137 return palette;
138 }
139
140 SkColor color = palette == BitmapPalette::Light ? SK_ColorWHITE : SK_ColorBLACK;
141 color = paint->getColorFilter()->filterColor(color);
142 return paletteForColorHSV(color);
143}
144
John Reck8f45d4a2018-08-15 10:17:12 -0700145bool transformPaint(ColorTransform transform, SkPaint* paint) {
146 // TODO
147 applyColorTransform(transform, *paint);
148 return true;
John Reck9ce2bf72018-07-02 18:33:32 -0700149}
150
John Reckf3c724f2018-09-20 13:00:04 -0700151bool transformPaint(ColorTransform transform, SkPaint* paint, BitmapPalette palette) {
John Reck08ee8152018-09-20 16:27:46 -0700152 palette = filterPalette(paint, palette);
John Reckf3c724f2018-09-20 13:00:04 -0700153 bool shouldInvert = false;
154 if (palette == BitmapPalette::Light && transform == ColorTransform::Dark) {
155 shouldInvert = true;
156 }
157 if (palette == BitmapPalette::Dark && transform == ColorTransform::Light) {
158 shouldInvert = true;
159 }
160 if (shouldInvert) {
161 SkHighContrastConfig config;
162 config.fInvertStyle = SkHighContrastConfig::InvertStyle::kInvertLightness;
163 paint->setColorFilter(SkHighContrastFilter::Make(config)->makeComposed(paint->refColorFilter()));
164 }
165 return shouldInvert;
166}
167
Chris Blume7b8a8082018-11-30 15:51:58 -0800168} // namespace android::uirenderer