blob: adcdc18ab41880d6e4edc7e2792125fadfe0592c [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"
18#include "Properties.h"
John Reck8f45d4a2018-08-15 10:17:12 -070019#include "utils/Color.h"
John Reck9ce2bf72018-07-02 18:33:32 -070020
21#include <SkColorFilter.h>
John Reck8f45d4a2018-08-15 10:17:12 -070022#include <SkGradientShader.h>
John Reck9ce2bf72018-07-02 18:33:32 -070023#include <SkPaint.h>
John Reck8f45d4a2018-08-15 10:17:12 -070024#include <SkShader.h>
25#include <ui/ColorSpace.h>
John Reck339cf9b2018-07-18 16:32:27 -070026
27#include <algorithm>
28#include <cmath>
John Reck9ce2bf72018-07-02 18:33:32 -070029
John Reck8f45d4a2018-08-15 10:17:12 -070030#include <log/log.h>
31
John Reck9ce2bf72018-07-02 18:33:32 -070032namespace android::uirenderer {
33
34static SkColor makeLight(SkColor color) {
John Reck339cf9b2018-07-18 16:32:27 -070035 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 }
John Reck9ce2bf72018-07-02 18:33:32 -070043}
44
45static SkColor makeDark(SkColor color) {
John Reck339cf9b2018-07-18 16:32:27 -070046 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 }
John Reck9ce2bf72018-07-02 18:33:32 -070054}
55
56static 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
67static void applyColorTransform(ColorTransform transform, SkPaint& paint) {
68 if (transform == ColorTransform::None) return;
69
70 SkColor newColor = transformColor(transform, paint.getColor());
71 paint.setColor(newColor);
72
John Reck8f45d4a2018-08-15 10:17:12 -070073 if (paint.getShader()) {
74 SkShader::GradientInfo info;
75 std::array<SkColor, 10> _colorStorage;
76 std::array<SkScalar, _colorStorage.size()> _offsetStorage;
77 info.fColorCount = _colorStorage.size();
78 info.fColors = _colorStorage.data();
79 info.fColorOffsets = _offsetStorage.data();
80 SkShader::GradientType type = paint.getShader()->asAGradient(&info);
81 ALOGW_IF(type, "Found gradient of type = %d", type);
82
83 if (info.fColorCount <= 10) {
84 switch (type) {
85 case SkShader::kLinear_GradientType:
86 for (int i = 0; i < info.fColorCount; i++) {
87 info.fColors[i] = transformColor(transform, info.fColors[i]);
88 }
89 paint.setShader(SkGradientShader::MakeLinear(info.fPoint, info.fColors,
90 info.fColorOffsets, info.fColorCount,
91 info.fTileMode, info.fGradientFlags, nullptr));
92 break;
93 default:break;
94 }
95
96 }
97 }
98
John Reck9ce2bf72018-07-02 18:33:32 -070099 if (paint.getColorFilter()) {
100 SkBlendMode mode;
101 SkColor color;
102 // TODO: LRU this or something to avoid spamming new color mode filters
103 if (paint.getColorFilter()->asColorMode(&color, &mode)) {
104 color = transformColor(transform, color);
105 paint.setColorFilter(SkColorFilter::MakeModeFilter(color, mode));
106 }
107 }
108}
109
John Reck8f45d4a2018-08-15 10:17:12 -0700110bool transformPaint(ColorTransform transform, SkPaint* paint) {
111 // TODO
112 applyColorTransform(transform, *paint);
113 return true;
John Reck9ce2bf72018-07-02 18:33:32 -0700114}
115
116}; // namespace android::uirenderer