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" |
John Reck | 9ce2bf7 | 2018-07-02 18:33:32 -0700 | [diff] [blame] | 18 | |
Brian Salomon | bb2499f2 | 2022-09-23 12:37:58 -0400 | [diff] [blame] | 19 | #include <SkAndroidFrameworkUtils.h> |
Kevin Lubick | 4e8ce46 | 2022-12-01 20:29:16 +0000 | [diff] [blame] | 20 | #include <SkBlendMode.h> |
John Reck | 9ce2bf7 | 2018-07-02 18:33:32 -0700 | [diff] [blame] | 21 | #include <SkColorFilter.h> |
John Reck | 8f45d4a | 2018-08-15 10:17:12 -0700 | [diff] [blame] | 22 | #include <SkGradientShader.h> |
Brian Salomon | bb2499f2 | 2022-09-23 12:37:58 -0400 | [diff] [blame] | 23 | #include <SkHighContrastFilter.h> |
John Reck | 9ce2bf7 | 2018-07-02 18:33:32 -0700 | [diff] [blame] | 24 | #include <SkPaint.h> |
John Reck | 8f45d4a | 2018-08-15 10:17:12 -0700 | [diff] [blame] | 25 | #include <SkShader.h> |
Brian Salomon | bb2499f2 | 2022-09-23 12:37:58 -0400 | [diff] [blame] | 26 | #include <log/log.h> |
John Reck | 339cf9b | 2018-07-18 16:32:27 -0700 | [diff] [blame] | 27 | |
| 28 | #include <algorithm> |
| 29 | #include <cmath> |
John Reck | 9ce2bf7 | 2018-07-02 18:33:32 -0700 | [diff] [blame] | 30 | |
Brian Salomon | bb2499f2 | 2022-09-23 12:37:58 -0400 | [diff] [blame] | 31 | #include "Properties.h" |
| 32 | #include "utils/Color.h" |
John Reck | 8f45d4a | 2018-08-15 10:17:12 -0700 | [diff] [blame] | 33 | |
John Reck | 9ce2bf7 | 2018-07-02 18:33:32 -0700 | [diff] [blame] | 34 | namespace android::uirenderer { |
| 35 | |
Lucas Dupin | 00af527 | 2021-04-29 20:30:01 -0700 | [diff] [blame] | 36 | SkColor makeLight(SkColor color) { |
John Reck | 339cf9b | 2018-07-18 16:32:27 -0700 | [diff] [blame] | 37 | 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 Reck | 9ce2bf7 | 2018-07-02 18:33:32 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Lucas Dupin | 00af527 | 2021-04-29 20:30:01 -0700 | [diff] [blame] | 47 | SkColor makeDark(SkColor color) { |
John Reck | 339cf9b | 2018-07-18 16:32:27 -0700 | [diff] [blame] | 48 | 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 Reck | 9ce2bf7 | 2018-07-02 18:33:32 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Lucas Dupin | 00af527 | 2021-04-29 20:30:01 -0700 | [diff] [blame] | 58 | SkColor transformColor(ColorTransform transform, SkColor color) { |
John Reck | 9ce2bf7 | 2018-07-02 18:33:32 -0700 | [diff] [blame] | 59 | 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 Dupin | 00af527 | 2021-04-29 20:30:01 -0700 | [diff] [blame] | 69 | SkColor 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 Reck | 9ce2bf7 | 2018-07-02 18:33:32 -0700 | [diff] [blame] | 80 | static void applyColorTransform(ColorTransform transform, SkPaint& paint) { |
| 81 | if (transform == ColorTransform::None) return; |
| 82 | |
Tyler Freeman | e0faa69 | 2023-11-16 00:48:54 +0000 | [diff] [blame] | 83 | 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 Reck | 9ce2bf7 | 2018-07-02 18:33:32 -0700 | [diff] [blame] | 96 | SkColor newColor = transformColor(transform, paint.getColor()); |
| 97 | paint.setColor(newColor); |
| 98 | |
John Reck | 8f45d4a | 2018-08-15 10:17:12 -0700 | [diff] [blame] | 99 | if (paint.getShader()) { |
Brian Salomon | bb2499f2 | 2022-09-23 12:37:58 -0400 | [diff] [blame] | 100 | SkAndroidFrameworkUtils::LinearGradientInfo info; |
John Reck | 8f45d4a | 2018-08-15 10:17:12 -0700 | [diff] [blame] | 101 | 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 Reck | 8f45d4a | 2018-08-15 10:17:12 -0700 | [diff] [blame] | 106 | |
Brian Salomon | bb2499f2 | 2022-09-23 12:37:58 -0400 | [diff] [blame] | 107 | 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 Reck | 8f45d4a | 2018-08-15 10:17:12 -0700 | [diff] [blame] | 111 | } |
Brian Salomon | bb2499f2 | 2022-09-23 12:37:58 -0400 | [diff] [blame] | 112 | paint.setShader(SkGradientShader::MakeLinear( |
| 113 | info.fPoints, info.fColors, info.fColorOffsets, info.fColorCount, |
| 114 | info.fTileMode, info.fGradientFlags, nullptr)); |
John Reck | 8f45d4a | 2018-08-15 10:17:12 -0700 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
John Reck | 9ce2bf7 | 2018-07-02 18:33:32 -0700 | [diff] [blame] | 118 | if (paint.getColorFilter()) { |
| 119 | SkBlendMode mode; |
| 120 | SkColor color; |
| 121 | // TODO: LRU this or something to avoid spamming new color mode filters |
Mike Reed | 2ee3743 | 2019-04-26 09:22:13 -0400 | [diff] [blame] | 122 | if (paint.getColorFilter()->asAColorMode(&color, &mode)) { |
John Reck | 9ce2bf7 | 2018-07-02 18:33:32 -0700 | [diff] [blame] | 123 | color = transformColor(transform, color); |
Mike Reed | 1c2f5fc | 2019-04-10 16:57:52 -0400 | [diff] [blame] | 124 | paint.setColorFilter(SkColorFilters::Blend(color, mode)); |
John Reck | 9ce2bf7 | 2018-07-02 18:33:32 -0700 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
John Reck | 08ee815 | 2018-09-20 16:27:46 -0700 | [diff] [blame] | 129 | static BitmapPalette paletteForColorHSV(SkColor color) { |
| 130 | float hsv[3]; |
| 131 | SkColorToHSV(color, hsv); |
| 132 | return hsv[2] >= .5f ? BitmapPalette::Light : BitmapPalette::Dark; |
| 133 | } |
| 134 | |
| 135 | static BitmapPalette filterPalette(const SkPaint* paint, BitmapPalette palette) { |
| 136 | if (palette == BitmapPalette::Unknown || !paint || !paint->getColorFilter()) { |
| 137 | return palette; |
| 138 | } |
| 139 | |
Brian Osman | f512df0 | 2024-02-20 20:33:07 +0000 | [diff] [blame] | 140 | SkColor4f color = palette == BitmapPalette::Light ? SkColors::kWhite : SkColors::kBlack; |
| 141 | sk_sp<SkColorSpace> srgb = SkColorSpace::MakeSRGB(); |
| 142 | color = paint->getColorFilter()->filterColor4f(color, srgb.get(), srgb.get()); |
| 143 | return paletteForColorHSV(color.toSkColor()); |
John Reck | 08ee815 | 2018-09-20 16:27:46 -0700 | [diff] [blame] | 144 | } |
| 145 | |
John Reck | 8f45d4a | 2018-08-15 10:17:12 -0700 | [diff] [blame] | 146 | bool transformPaint(ColorTransform transform, SkPaint* paint) { |
| 147 | // TODO |
| 148 | applyColorTransform(transform, *paint); |
| 149 | return true; |
John Reck | 9ce2bf7 | 2018-07-02 18:33:32 -0700 | [diff] [blame] | 150 | } |
| 151 | |
John Reck | f3c724f | 2018-09-20 13:00:04 -0700 | [diff] [blame] | 152 | bool transformPaint(ColorTransform transform, SkPaint* paint, BitmapPalette palette) { |
John Reck | 08ee815 | 2018-09-20 16:27:46 -0700 | [diff] [blame] | 153 | palette = filterPalette(paint, palette); |
John Reck | f3c724f | 2018-09-20 13:00:04 -0700 | [diff] [blame] | 154 | bool shouldInvert = false; |
| 155 | if (palette == BitmapPalette::Light && transform == ColorTransform::Dark) { |
| 156 | shouldInvert = true; |
| 157 | } |
| 158 | if (palette == BitmapPalette::Dark && transform == ColorTransform::Light) { |
| 159 | shouldInvert = true; |
| 160 | } |
| 161 | if (shouldInvert) { |
| 162 | SkHighContrastConfig config; |
| 163 | config.fInvertStyle = SkHighContrastConfig::InvertStyle::kInvertLightness; |
| 164 | paint->setColorFilter(SkHighContrastFilter::Make(config)->makeComposed(paint->refColorFilter())); |
| 165 | } |
| 166 | return shouldInvert; |
| 167 | } |
| 168 | |
Chris Blume | 7b8a808 | 2018-11-30 15:51:58 -0800 | [diff] [blame] | 169 | } // namespace android::uirenderer |