John Reck | 9ce2bf7 | 2018-07-02 18:33:32 -0700 | [diff] [blame] | 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" |
John Reck | 8f45d4a | 2018-08-15 10:17:12 -0700 | [diff] [blame] | 19 | #include "utils/Color.h" |
John Reck | 9ce2bf7 | 2018-07-02 18:33:32 -0700 | [diff] [blame] | 20 | |
| 21 | #include <SkColorFilter.h> |
John Reck | 8f45d4a | 2018-08-15 10:17:12 -0700 | [diff] [blame] | 22 | #include <SkGradientShader.h> |
John Reck | 9ce2bf7 | 2018-07-02 18:33:32 -0700 | [diff] [blame] | 23 | #include <SkPaint.h> |
John Reck | 8f45d4a | 2018-08-15 10:17:12 -0700 | [diff] [blame] | 24 | #include <SkShader.h> |
John Reck | 339cf9b | 2018-07-18 16:32:27 -0700 | [diff] [blame] | 25 | |
| 26 | #include <algorithm> |
| 27 | #include <cmath> |
John Reck | 9ce2bf7 | 2018-07-02 18:33:32 -0700 | [diff] [blame] | 28 | |
John Reck | 8f45d4a | 2018-08-15 10:17:12 -0700 | [diff] [blame] | 29 | #include <log/log.h> |
John Reck | f3c724f | 2018-09-20 13:00:04 -0700 | [diff] [blame] | 30 | #include <SkHighContrastFilter.h> |
John Reck | 8f45d4a | 2018-08-15 10:17:12 -0700 | [diff] [blame] | 31 | |
John Reck | 9ce2bf7 | 2018-07-02 18:33:32 -0700 | [diff] [blame] | 32 | namespace android::uirenderer { |
| 33 | |
| 34 | static SkColor makeLight(SkColor color) { |
John Reck | 339cf9b | 2018-07-18 16:32:27 -0700 | [diff] [blame] | 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 | } |
John Reck | 9ce2bf7 | 2018-07-02 18:33:32 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | static SkColor makeDark(SkColor color) { |
John Reck | 339cf9b | 2018-07-18 16:32:27 -0700 | [diff] [blame] | 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 | } |
John Reck | 9ce2bf7 | 2018-07-02 18:33:32 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | static 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 | |
| 67 | static 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 Reck | 8f45d4a | 2018-08-15 10:17:12 -0700 | [diff] [blame] | 73 | 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); |
John Reck | 8f45d4a | 2018-08-15 10:17:12 -0700 | [diff] [blame] | 81 | |
| 82 | if (info.fColorCount <= 10) { |
| 83 | switch (type) { |
| 84 | case SkShader::kLinear_GradientType: |
| 85 | for (int i = 0; i < info.fColorCount; i++) { |
| 86 | info.fColors[i] = transformColor(transform, info.fColors[i]); |
| 87 | } |
| 88 | paint.setShader(SkGradientShader::MakeLinear(info.fPoint, info.fColors, |
| 89 | info.fColorOffsets, info.fColorCount, |
| 90 | info.fTileMode, info.fGradientFlags, nullptr)); |
| 91 | break; |
| 92 | default:break; |
| 93 | } |
| 94 | |
| 95 | } |
| 96 | } |
| 97 | |
John Reck | 9ce2bf7 | 2018-07-02 18:33:32 -0700 | [diff] [blame] | 98 | if (paint.getColorFilter()) { |
| 99 | SkBlendMode mode; |
| 100 | SkColor color; |
| 101 | // TODO: LRU this or something to avoid spamming new color mode filters |
Mike Reed | 2ee3743 | 2019-04-26 09:22:13 -0400 | [diff] [blame] | 102 | if (paint.getColorFilter()->asAColorMode(&color, &mode)) { |
John Reck | 9ce2bf7 | 2018-07-02 18:33:32 -0700 | [diff] [blame] | 103 | color = transformColor(transform, color); |
Mike Reed | 1c2f5fc | 2019-04-10 16:57:52 -0400 | [diff] [blame] | 104 | paint.setColorFilter(SkColorFilters::Blend(color, mode)); |
John Reck | 9ce2bf7 | 2018-07-02 18:33:32 -0700 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
John Reck | 08ee815 | 2018-09-20 16:27:46 -0700 | [diff] [blame] | 109 | static BitmapPalette paletteForColorHSV(SkColor color) { |
| 110 | float hsv[3]; |
| 111 | SkColorToHSV(color, hsv); |
| 112 | return hsv[2] >= .5f ? BitmapPalette::Light : BitmapPalette::Dark; |
| 113 | } |
| 114 | |
| 115 | static BitmapPalette filterPalette(const SkPaint* paint, BitmapPalette palette) { |
| 116 | if (palette == BitmapPalette::Unknown || !paint || !paint->getColorFilter()) { |
| 117 | return palette; |
| 118 | } |
| 119 | |
| 120 | SkColor color = palette == BitmapPalette::Light ? SK_ColorWHITE : SK_ColorBLACK; |
| 121 | color = paint->getColorFilter()->filterColor(color); |
| 122 | return paletteForColorHSV(color); |
| 123 | } |
| 124 | |
John Reck | 8f45d4a | 2018-08-15 10:17:12 -0700 | [diff] [blame] | 125 | bool transformPaint(ColorTransform transform, SkPaint* paint) { |
| 126 | // TODO |
| 127 | applyColorTransform(transform, *paint); |
| 128 | return true; |
John Reck | 9ce2bf7 | 2018-07-02 18:33:32 -0700 | [diff] [blame] | 129 | } |
| 130 | |
John Reck | f3c724f | 2018-09-20 13:00:04 -0700 | [diff] [blame] | 131 | bool transformPaint(ColorTransform transform, SkPaint* paint, BitmapPalette palette) { |
John Reck | 08ee815 | 2018-09-20 16:27:46 -0700 | [diff] [blame] | 132 | palette = filterPalette(paint, palette); |
John Reck | f3c724f | 2018-09-20 13:00:04 -0700 | [diff] [blame] | 133 | bool shouldInvert = false; |
| 134 | if (palette == BitmapPalette::Light && transform == ColorTransform::Dark) { |
| 135 | shouldInvert = true; |
| 136 | } |
| 137 | if (palette == BitmapPalette::Dark && transform == ColorTransform::Light) { |
| 138 | shouldInvert = true; |
| 139 | } |
| 140 | if (shouldInvert) { |
| 141 | SkHighContrastConfig config; |
| 142 | config.fInvertStyle = SkHighContrastConfig::InvertStyle::kInvertLightness; |
| 143 | paint->setColorFilter(SkHighContrastFilter::Make(config)->makeComposed(paint->refColorFilter())); |
| 144 | } |
| 145 | return shouldInvert; |
| 146 | } |
| 147 | |
Chris Blume | 7b8a808 | 2018-11-30 15:51:58 -0800 | [diff] [blame] | 148 | } // namespace android::uirenderer |