blob: cd4fae86aa52a0c846df983d147e92d5b950bc76 [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
83 SkColor newColor = transformColor(transform, paint.getColor());
84 paint.setColor(newColor);
85
John Reck8f45d4a2018-08-15 10:17:12 -070086 if (paint.getShader()) {
Brian Salomonbb2499f22022-09-23 12:37:58 -040087 SkAndroidFrameworkUtils::LinearGradientInfo info;
John Reck8f45d4a2018-08-15 10:17:12 -070088 std::array<SkColor, 10> _colorStorage;
89 std::array<SkScalar, _colorStorage.size()> _offsetStorage;
90 info.fColorCount = _colorStorage.size();
91 info.fColors = _colorStorage.data();
92 info.fColorOffsets = _offsetStorage.data();
John Reck8f45d4a2018-08-15 10:17:12 -070093
Brian Salomonbb2499f22022-09-23 12:37:58 -040094 if (SkAndroidFrameworkUtils::ShaderAsALinearGradient(paint.getShader(), &info) &&
95 info.fColorCount <= _colorStorage.size()) {
96 for (int i = 0; i < info.fColorCount; i++) {
97 info.fColors[i] = transformColor(transform, info.fColors[i]);
John Reck8f45d4a2018-08-15 10:17:12 -070098 }
Brian Salomonbb2499f22022-09-23 12:37:58 -040099 paint.setShader(SkGradientShader::MakeLinear(
100 info.fPoints, info.fColors, info.fColorOffsets, info.fColorCount,
101 info.fTileMode, info.fGradientFlags, nullptr));
John Reck8f45d4a2018-08-15 10:17:12 -0700102 }
103 }
104
John Reck9ce2bf72018-07-02 18:33:32 -0700105 if (paint.getColorFilter()) {
106 SkBlendMode mode;
107 SkColor color;
108 // TODO: LRU this or something to avoid spamming new color mode filters
Mike Reed2ee37432019-04-26 09:22:13 -0400109 if (paint.getColorFilter()->asAColorMode(&color, &mode)) {
John Reck9ce2bf72018-07-02 18:33:32 -0700110 color = transformColor(transform, color);
Mike Reed1c2f5fc2019-04-10 16:57:52 -0400111 paint.setColorFilter(SkColorFilters::Blend(color, mode));
John Reck9ce2bf72018-07-02 18:33:32 -0700112 }
113 }
114}
115
John Reck08ee8152018-09-20 16:27:46 -0700116static BitmapPalette paletteForColorHSV(SkColor color) {
117 float hsv[3];
118 SkColorToHSV(color, hsv);
119 return hsv[2] >= .5f ? BitmapPalette::Light : BitmapPalette::Dark;
120}
121
122static BitmapPalette filterPalette(const SkPaint* paint, BitmapPalette palette) {
123 if (palette == BitmapPalette::Unknown || !paint || !paint->getColorFilter()) {
124 return palette;
125 }
126
127 SkColor color = palette == BitmapPalette::Light ? SK_ColorWHITE : SK_ColorBLACK;
128 color = paint->getColorFilter()->filterColor(color);
129 return paletteForColorHSV(color);
130}
131
John Reck8f45d4a2018-08-15 10:17:12 -0700132bool transformPaint(ColorTransform transform, SkPaint* paint) {
133 // TODO
134 applyColorTransform(transform, *paint);
135 return true;
John Reck9ce2bf72018-07-02 18:33:32 -0700136}
137
John Reckf3c724f2018-09-20 13:00:04 -0700138bool transformPaint(ColorTransform transform, SkPaint* paint, BitmapPalette palette) {
John Reck08ee8152018-09-20 16:27:46 -0700139 palette = filterPalette(paint, palette);
John Reckf3c724f2018-09-20 13:00:04 -0700140 bool shouldInvert = false;
141 if (palette == BitmapPalette::Light && transform == ColorTransform::Dark) {
142 shouldInvert = true;
143 }
144 if (palette == BitmapPalette::Dark && transform == ColorTransform::Light) {
145 shouldInvert = true;
146 }
147 if (shouldInvert) {
148 SkHighContrastConfig config;
149 config.fInvertStyle = SkHighContrastConfig::InvertStyle::kInvertLightness;
150 paint->setColorFilter(SkHighContrastFilter::Make(config)->makeComposed(paint->refColorFilter()));
151 }
152 return shouldInvert;
153}
154
Chris Blume7b8a8082018-11-30 15:51:58 -0800155} // namespace android::uirenderer