Mike Klein | 6bfe3f5 | 2017-05-05 13:49:00 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 9 | #include "include/core/SkBlendMode.h" |
| 10 | #include "include/core/SkCanvas.h" |
| 11 | #include "include/core/SkColor.h" |
| 12 | #include "include/core/SkFont.h" |
| 13 | #include "include/core/SkPaint.h" |
| 14 | #include "include/core/SkTypeface.h" |
| 15 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "tools/ToolUtils.h" |
Mike Klein | 6bfe3f5 | 2017-05-05 13:49:00 -0400 | [diff] [blame] | 17 | |
| 18 | // Hue, Saturation, Color, and Luminosity blend modes are oddballs. |
| 19 | // They nominally convert their inputs to unpremul, then to HSL, then |
| 20 | // mix-and-match H,S,and L from Src and Dst, then convert back, then premul. |
| 21 | // |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 22 | // In practice that's slow, so instead we pick the color with the correct |
| 23 | // Hue, and then (approximately) apply the other's Saturation and/or Luminosity. |
| 24 | // This isn't just an optimization... it's how the modes are specified. |
| 25 | // |
Mike Klein | 6bfe3f5 | 2017-05-05 13:49:00 -0400 | [diff] [blame] | 26 | // Each mode's name describes the Src H,S,L components to keep, taking the |
| 27 | // others from Dst, where Color == Hue + Saturation. Color and Luminosity |
| 28 | // are each other's complements; Hue and Saturation have no complement. |
| 29 | // |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 30 | // All these modes were originally designed to operate on gamma-encoded values, |
| 31 | // and that's what everyone's used to seeing. It's unclear wehther they make |
| 32 | // any sense in a gamma-correct world. They certainly won't look at all similar. |
| 33 | // |
Mike Klein | 6bfe3f5 | 2017-05-05 13:49:00 -0400 | [diff] [blame] | 34 | // We have had many inconsistent implementations of these modes. |
| 35 | // This GM tries to demonstrate unambigously how they should work. |
| 36 | // |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 37 | // To go along with our inconsistent implementations, there are two slightly |
| 38 | // inconsistent specifications of how to perform these blends, |
| 39 | // web: https://www.w3.org/TR/compositing-1/#blendingnonseparable |
| 40 | // KHR: https://www.khronos.org/registry/OpenGL/extensions/KHR/KHR_blend_equation_advanced.txt |
| 41 | // It looks like these are meant to be identical, but they disagree on at least ClipColor(). |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 42 | // |
Mike Klein | ba8ca0a | 2017-05-07 00:25:16 -0400 | [diff] [blame] | 43 | // I think the KHR version is just wrong... it produces values >1. So we use the web version. |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 44 | |
| 45 | static float min(float r, float g, float b) { return SkTMin(r, SkTMin(g, b)); } |
| 46 | static float max(float r, float g, float b) { return SkTMax(r, SkTMax(g, b)); } |
| 47 | |
| 48 | static float sat(float r, float g, float b) { return max(r,g,b) - min(r,g,b); } |
| 49 | static float lum(float r, float g, float b) { return r*0.30f + g*0.59f + b*0.11f; } |
| 50 | |
| 51 | // The two SetSat() routines in the specs look different, but they're logically equivalent. |
| 52 | // Both map the minimum channel to 0, maximum to s, and scale the middle proportionately. |
| 53 | // The KHR version has done a better job at simplifying its math, so we use it here. |
| 54 | static void set_sat(float* r, float* g, float* b, float s) { |
Mike Klein | bc63fd4 | 2017-05-06 23:52:28 -0400 | [diff] [blame] | 55 | float mn = min(*r,*g,*b), |
| 56 | mx = max(*r,*g,*b); |
| 57 | auto channel = [=](float c) { |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 58 | return mx == mn ? 0 |
| 59 | : (c - mn) * s / (mx - mn); |
| 60 | }; |
| 61 | *r = channel(*r); |
| 62 | *g = channel(*g); |
| 63 | *b = channel(*b); |
| 64 | } |
Mike Klein | ba8ca0a | 2017-05-07 00:25:16 -0400 | [diff] [blame] | 65 | static void clip_color(float* r, float* g, float* b) { |
Mike Klein | bc63fd4 | 2017-05-06 23:52:28 -0400 | [diff] [blame] | 66 | float l = lum(*r,*g,*b), |
| 67 | mn = min(*r,*g,*b), |
| 68 | mx = max(*r,*g,*b); |
| 69 | auto clip = [=](float c) { |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 70 | if (mn < 0) { c = l + (c - l) * ( l) / (l - mn); } |
Mike Klein | ba8ca0a | 2017-05-07 00:25:16 -0400 | [diff] [blame] | 71 | if (mx > 1) { c = l + (c - l) * (1 - l) / (mx - l); } |
| 72 | SkASSERT(-0.0001f < c); // This may end up very slightly negative... |
| 73 | SkASSERT( c <= 1); |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 74 | return c; |
| 75 | }; |
| 76 | *r = clip(*r); |
| 77 | *g = clip(*g); |
| 78 | *b = clip(*b); |
| 79 | } |
Mike Klein | ba8ca0a | 2017-05-07 00:25:16 -0400 | [diff] [blame] | 80 | static void set_lum(float* r, float* g, float* b, float l) { |
| 81 | float diff = l - lum(*r,*g,*b); |
| 82 | *r += diff; |
| 83 | *g += diff; |
| 84 | *b += diff; |
| 85 | clip_color(r,g,b); |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | |
| 89 | static void hue(float dr, float dg, float db, |
| 90 | float* sr, float* sg, float* sb) { |
| 91 | // Hue of Src, Saturation and Luminosity of Dst. |
| 92 | float R = *sr, |
| 93 | G = *sg, |
| 94 | B = *sb; |
| 95 | set_sat(&R,&G,&B, sat(dr,dg,db)); |
| 96 | set_lum(&R,&G,&B, lum(dr,dg,db)); |
| 97 | *sr = R; |
| 98 | *sg = G; |
| 99 | *sb = B; |
| 100 | } |
| 101 | |
| 102 | static void saturation(float dr, float dg, float db, |
| 103 | float* sr, float* sg, float* sb) { |
| 104 | // Saturation of Src, Hue and Luminosity of Dst |
| 105 | float R = dr, |
| 106 | G = dg, |
| 107 | B = db; |
| 108 | set_sat(&R,&G,&B, sat(*sr,*sg,*sb)); |
Mike Klein | ba8ca0a | 2017-05-07 00:25:16 -0400 | [diff] [blame] | 109 | set_lum(&R,&G,&B, lum( dr, dg, db)); // This may seem redundant, but it is not. |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 110 | *sr = R; |
| 111 | *sg = G; |
| 112 | *sb = B; |
| 113 | } |
| 114 | |
| 115 | static void color(float dr, float dg, float db, |
| 116 | float* sr, float* sg, float* sb) { |
| 117 | // Hue and Saturation of Src, Luminosity of Dst. |
| 118 | float R = *sr, |
| 119 | G = *sg, |
| 120 | B = *sb; |
| 121 | set_lum(&R,&G,&B, lum(dr,dg,db)); |
| 122 | *sr = R; |
| 123 | *sg = G; |
| 124 | *sb = B; |
| 125 | } |
| 126 | |
| 127 | static void luminosity(float dr, float dg, float db, |
| 128 | float* sr, float* sg, float* sb) { |
| 129 | // Luminosity of Src, Hue and Saturation of Dst. |
| 130 | float R = dr, |
| 131 | G = dg, |
| 132 | B = db; |
| 133 | set_lum(&R,&G,&B, lum(*sr,*sg,*sb)); |
| 134 | *sr = R; |
| 135 | *sg = G; |
| 136 | *sb = B; |
| 137 | } |
| 138 | |
| 139 | static SkColor blend(SkColor dst, SkColor src, |
Brian Osman | fbc6d7f | 2018-08-15 11:16:06 -0400 | [diff] [blame] | 140 | void (*mode)(float,float,float, float*,float*,float*)) { |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 141 | |
| 142 | SkASSERT(SkColorGetA(dst) == 0xff |
| 143 | && SkColorGetA(src) == 0xff); // Not fundamental, just simplifying for this GM. |
| 144 | |
Brian Osman | fbc6d7f | 2018-08-15 11:16:06 -0400 | [diff] [blame] | 145 | SkColor4f d = SkColor4f::FromColor(dst), |
| 146 | s = SkColor4f::FromColor(src); |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 147 | |
Mike Klein | f484ac6 | 2017-05-06 15:44:38 -0400 | [diff] [blame] | 148 | mode( d.fR, d.fG, d.fB, |
| 149 | &s.fR, &s.fG, &s.fB); |
Mike Klein | f484ac6 | 2017-05-06 15:44:38 -0400 | [diff] [blame] | 150 | |
Mike Klein | f484ac6 | 2017-05-06 15:44:38 -0400 | [diff] [blame] | 151 | return s.toSkColor(); |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 152 | } |
Mike Klein | 6bfe3f5 | 2017-05-05 13:49:00 -0400 | [diff] [blame] | 153 | |
| 154 | DEF_SIMPLE_GM(hsl, canvas, 600, 100) { |
Hal Canary | df2d27e | 2019-01-08 09:38:02 -0500 | [diff] [blame] | 155 | SkPaint paint; |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 156 | SkFont font(ToolUtils::create_portable_typeface()); |
Mike Klein | 6bfe3f5 | 2017-05-05 13:49:00 -0400 | [diff] [blame] | 157 | |
Mike Klein | ba8ca0a | 2017-05-07 00:25:16 -0400 | [diff] [blame] | 158 | const char* comment = "HSL blend modes are correct when you see no circles in the squares."; |
Hal Canary | df2d27e | 2019-01-08 09:38:02 -0500 | [diff] [blame] | 159 | canvas->drawString(comment, 10,10, font, paint); |
Mike Klein | 6bfe3f5 | 2017-05-05 13:49:00 -0400 | [diff] [blame] | 160 | |
| 161 | // Just to keep things reaaaal simple, we'll only use opaque colors. |
| 162 | SkPaint bg, fg; |
| 163 | bg.setColor(0xff00ff00); // Fully-saturated bright green, H = 120°, S = 100%, L = 50%. |
| 164 | fg.setColor(0xff7f3f7f); // Partly-saturated dim magenta, H = 300°, S = ~33%, L = ~37%. |
| 165 | |
| 166 | struct { |
| 167 | SkBlendMode mode; |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 168 | void (*reference)(float,float,float, float*,float*,float*); |
Mike Klein | 6bfe3f5 | 2017-05-05 13:49:00 -0400 | [diff] [blame] | 169 | } tests[] = { |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 170 | { SkBlendMode::kSrc, nullptr }, |
| 171 | { SkBlendMode::kDst, nullptr }, |
| 172 | { SkBlendMode::kHue, hue }, |
| 173 | { SkBlendMode::kSaturation, saturation }, |
| 174 | { SkBlendMode::kColor, color }, |
| 175 | { SkBlendMode::kLuminosity, luminosity }, |
Mike Klein | 6bfe3f5 | 2017-05-05 13:49:00 -0400 | [diff] [blame] | 176 | }; |
| 177 | for (auto test : tests) { |
| 178 | canvas->drawRect({20,20,80,80}, bg); |
| 179 | |
| 180 | fg.setBlendMode(test.mode); |
| 181 | canvas->drawRect({20,20,80,80}, fg); |
| 182 | |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 183 | if (test.reference) { |
Mike Klein | ba8ca0a | 2017-05-07 00:25:16 -0400 | [diff] [blame] | 184 | SkPaint ref; |
Brian Osman | fbc6d7f | 2018-08-15 11:16:06 -0400 | [diff] [blame] | 185 | ref.setColor(blend(bg.getColor(), fg.getColor(), test.reference)); |
Mike Klein | ba8ca0a | 2017-05-07 00:25:16 -0400 | [diff] [blame] | 186 | canvas->drawCircle(50,50, 20, ref); |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 187 | } |
Mike Klein | 6bfe3f5 | 2017-05-05 13:49:00 -0400 | [diff] [blame] | 188 | |
Hal Canary | df2d27e | 2019-01-08 09:38:02 -0500 | [diff] [blame] | 189 | canvas->drawString(SkBlendMode_Name(test.mode), 20, 90, font, paint); |
Mike Klein | 6bfe3f5 | 2017-05-05 13:49:00 -0400 | [diff] [blame] | 190 | |
| 191 | canvas->translate(100,0); |
| 192 | } |
| 193 | } |