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 | |
| 8 | #include "gm.h" |
| 9 | #include "sk_tool_utils.h" |
| 10 | |
| 11 | // Hue, Saturation, Color, and Luminosity blend modes are oddballs. |
| 12 | // They nominally convert their inputs to unpremul, then to HSL, then |
| 13 | // mix-and-match H,S,and L from Src and Dst, then convert back, then premul. |
| 14 | // |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 15 | // In practice that's slow, so instead we pick the color with the correct |
| 16 | // Hue, and then (approximately) apply the other's Saturation and/or Luminosity. |
| 17 | // This isn't just an optimization... it's how the modes are specified. |
| 18 | // |
Mike Klein | 6bfe3f5 | 2017-05-05 13:49:00 -0400 | [diff] [blame] | 19 | // Each mode's name describes the Src H,S,L components to keep, taking the |
| 20 | // others from Dst, where Color == Hue + Saturation. Color and Luminosity |
| 21 | // are each other's complements; Hue and Saturation have no complement. |
| 22 | // |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 23 | // All these modes were originally designed to operate on gamma-encoded values, |
| 24 | // and that's what everyone's used to seeing. It's unclear wehther they make |
| 25 | // any sense in a gamma-correct world. They certainly won't look at all similar. |
| 26 | // |
Mike Klein | 6bfe3f5 | 2017-05-05 13:49:00 -0400 | [diff] [blame] | 27 | // We have had many inconsistent implementations of these modes. |
| 28 | // This GM tries to demonstrate unambigously how they should work. |
| 29 | // |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 30 | // To go along with our inconsistent implementations, there are two slightly |
| 31 | // inconsistent specifications of how to perform these blends, |
| 32 | // web: https://www.w3.org/TR/compositing-1/#blendingnonseparable |
| 33 | // KHR: https://www.khronos.org/registry/OpenGL/extensions/KHR/KHR_blend_equation_advanced.txt |
| 34 | // It looks like these are meant to be identical, but they disagree on at least ClipColor(). |
| 35 | // I think the KHR version is just wrong... it produces values >1. |
| 36 | // |
| 37 | // We'll draw reference colors according to both specs, testing colors where they differ. |
| 38 | |
| 39 | static float min(float r, float g, float b) { return SkTMin(r, SkTMin(g, b)); } |
| 40 | static float max(float r, float g, float b) { return SkTMax(r, SkTMax(g, b)); } |
| 41 | |
| 42 | static float sat(float r, float g, float b) { return max(r,g,b) - min(r,g,b); } |
| 43 | static float lum(float r, float g, float b) { return r*0.30f + g*0.59f + b*0.11f; } |
| 44 | |
| 45 | // The two SetSat() routines in the specs look different, but they're logically equivalent. |
| 46 | // Both map the minimum channel to 0, maximum to s, and scale the middle proportionately. |
| 47 | // The KHR version has done a better job at simplifying its math, so we use it here. |
| 48 | static void set_sat(float* r, float* g, float* b, float s) { |
| 49 | auto channel = [&](float c) { |
| 50 | float mn = min(*r,*g,*b), |
| 51 | mx = max(*r,*g,*b); |
| 52 | return mx == mn ? 0 |
| 53 | : (c - mn) * s / (mx - mn); |
| 54 | }; |
| 55 | *r = channel(*r); |
| 56 | *g = channel(*g); |
| 57 | *b = channel(*b); |
| 58 | } |
| 59 | |
| 60 | static void set_lum(float* r, float* g, float* b, float l) { |
| 61 | float diff = l - lum(*r,*g,*b); |
| 62 | *r += diff; |
| 63 | *g += diff; |
| 64 | *b += diff; |
| 65 | // We do clip_color() here as specified, just moved from set_lum() to blend(). |
| 66 | } |
| 67 | |
| 68 | static void clip_color_web(float* r, float* g, float* b) { |
| 69 | auto clip = [&](float c) { |
| 70 | float l = lum(*r,*g,*b), |
| 71 | mn = min(*r,*g,*b), |
| 72 | mx = max(*r,*g,*b); |
| 73 | if (mn < 0) { c = l + (c - l) * ( l) / (l - mn); } |
| 74 | if (mx > 1) { c = l + (c - l) * (1 - l) / (mx - l); } // <--- notice "1-l" |
Mike Klein | f484ac6 | 2017-05-06 15:44:38 -0400 | [diff] [blame^] | 75 | //SkASSERT(0 <= c); // This may end up very slightly negative... |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 76 | SkASSERT(c <= 1); |
| 77 | return c; |
| 78 | }; |
| 79 | *r = clip(*r); |
| 80 | *g = clip(*g); |
| 81 | *b = clip(*b); |
| 82 | } |
| 83 | static void clip_color_KHR(float* r, float* g, float* b) { |
| 84 | auto clip = [&](float c) { |
| 85 | float l = lum(*r,*g,*b), |
| 86 | mn = min(*r,*g,*b), |
| 87 | mx = max(*r,*g,*b); |
| 88 | if (mn < 0) { c = l + (c - l) * l / (l - mn); } |
| 89 | if (mx > 1) { c = l + (c - l) * l / (mx - l); } // <--- notice "l" |
Mike Klein | f484ac6 | 2017-05-06 15:44:38 -0400 | [diff] [blame^] | 90 | //SkASSERT(0 <= c); // This may end up very slightly negative... |
| 91 | //SkASSERT(c <= 1); // I think the mx > 1 logic is incorrect... |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 92 | return c; |
| 93 | }; |
| 94 | *r = clip(*r); |
| 95 | *g = clip(*g); |
| 96 | *b = clip(*b); |
| 97 | } |
| 98 | |
| 99 | |
| 100 | static void hue(float dr, float dg, float db, |
| 101 | float* sr, float* sg, float* sb) { |
| 102 | // Hue of Src, Saturation and Luminosity of Dst. |
| 103 | float R = *sr, |
| 104 | G = *sg, |
| 105 | B = *sb; |
| 106 | set_sat(&R,&G,&B, sat(dr,dg,db)); |
| 107 | set_lum(&R,&G,&B, lum(dr,dg,db)); |
| 108 | *sr = R; |
| 109 | *sg = G; |
| 110 | *sb = B; |
| 111 | } |
| 112 | |
| 113 | static void saturation(float dr, float dg, float db, |
| 114 | float* sr, float* sg, float* sb) { |
| 115 | // Saturation of Src, Hue and Luminosity of Dst |
| 116 | float R = dr, |
| 117 | G = dg, |
| 118 | B = db; |
| 119 | set_sat(&R,&G,&B, sat(*sr,*sg,*sb)); |
| 120 | set_lum(&R,&G,&B, lum( dr, dg, db)); // This may seem redundant. TODO: is it? |
| 121 | *sr = R; |
| 122 | *sg = G; |
| 123 | *sb = B; |
| 124 | } |
| 125 | |
| 126 | static void color(float dr, float dg, float db, |
| 127 | float* sr, float* sg, float* sb) { |
| 128 | // Hue and Saturation of Src, Luminosity of Dst. |
| 129 | float R = *sr, |
| 130 | G = *sg, |
| 131 | B = *sb; |
| 132 | set_lum(&R,&G,&B, lum(dr,dg,db)); |
| 133 | *sr = R; |
| 134 | *sg = G; |
| 135 | *sb = B; |
| 136 | } |
| 137 | |
| 138 | static void luminosity(float dr, float dg, float db, |
| 139 | float* sr, float* sg, float* sb) { |
| 140 | // Luminosity of Src, Hue and Saturation of Dst. |
| 141 | float R = dr, |
| 142 | G = dg, |
| 143 | B = db; |
| 144 | set_lum(&R,&G,&B, lum(*sr,*sg,*sb)); |
| 145 | *sr = R; |
| 146 | *sg = G; |
| 147 | *sb = B; |
| 148 | } |
| 149 | |
| 150 | static SkColor blend(SkColor dst, SkColor src, |
| 151 | void (*mode)(float,float,float, float*,float*,float*), |
Mike Klein | f484ac6 | 2017-05-06 15:44:38 -0400 | [diff] [blame^] | 152 | void (*clip_color)(float*,float*,float*), |
| 153 | bool legacy) { |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 154 | |
| 155 | SkASSERT(SkColorGetA(dst) == 0xff |
| 156 | && SkColorGetA(src) == 0xff); // Not fundamental, just simplifying for this GM. |
| 157 | |
Mike Klein | f484ac6 | 2017-05-06 15:44:38 -0400 | [diff] [blame^] | 158 | auto to_float = [&](SkColor c) { |
| 159 | if (legacy) { |
| 160 | return SkColor4f{ |
| 161 | SkColorGetR(c) * (1/255.0f), |
| 162 | SkColorGetG(c) * (1/255.0f), |
| 163 | SkColorGetB(c) * (1/255.0f), |
| 164 | 1.0f, |
| 165 | }; |
| 166 | } |
| 167 | return SkColor4f::FromColor(c); |
| 168 | }; |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 169 | |
Mike Klein | f484ac6 | 2017-05-06 15:44:38 -0400 | [diff] [blame^] | 170 | SkColor4f d = to_float(dst), |
| 171 | s = to_float(src); |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 172 | |
Mike Klein | f484ac6 | 2017-05-06 15:44:38 -0400 | [diff] [blame^] | 173 | mode( d.fR, d.fG, d.fB, |
| 174 | &s.fR, &s.fG, &s.fB); |
| 175 | clip_color(&s.fR, &s.fG, &s.fB); |
| 176 | |
| 177 | if (legacy) { |
| 178 | // We need to be a little careful here to show off clip_color_KHR()'s overflow |
| 179 | // while avoiding SkASSERTs inside SkColorSetRGB(). |
| 180 | return SkColorSetRGB((int)(s.fR * 255.0f + 0.5f) & 0xff, |
| 181 | (int)(s.fG * 255.0f + 0.5f) & 0xff, |
| 182 | (int)(s.fB * 255.0f + 0.5f) & 0xff); |
| 183 | } |
| 184 | return s.toSkColor(); |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 185 | } |
Mike Klein | 6bfe3f5 | 2017-05-05 13:49:00 -0400 | [diff] [blame] | 186 | |
| 187 | DEF_SIMPLE_GM(hsl, canvas, 600, 100) { |
| 188 | SkPaint label; |
| 189 | sk_tool_utils::set_portable_typeface(&label); |
| 190 | label.setAntiAlias(true); |
| 191 | |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 192 | const char* comment = "HSL blend modes are correct when you see no circles in the squares. " |
| 193 | "2 circles means the standards disagree."; |
Mike Klein | 6bfe3f5 | 2017-05-05 13:49:00 -0400 | [diff] [blame] | 194 | canvas->drawText(comment, strlen(comment), 10,10, label); |
| 195 | |
| 196 | // Just to keep things reaaaal simple, we'll only use opaque colors. |
| 197 | SkPaint bg, fg; |
| 198 | bg.setColor(0xff00ff00); // Fully-saturated bright green, H = 120°, S = 100%, L = 50%. |
| 199 | fg.setColor(0xff7f3f7f); // Partly-saturated dim magenta, H = 300°, S = ~33%, L = ~37%. |
| 200 | |
| 201 | struct { |
| 202 | SkBlendMode mode; |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 203 | void (*reference)(float,float,float, float*,float*,float*); |
Mike Klein | 6bfe3f5 | 2017-05-05 13:49:00 -0400 | [diff] [blame] | 204 | } tests[] = { |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 205 | { SkBlendMode::kSrc, nullptr }, |
| 206 | { SkBlendMode::kDst, nullptr }, |
| 207 | { SkBlendMode::kHue, hue }, |
| 208 | { SkBlendMode::kSaturation, saturation }, |
| 209 | { SkBlendMode::kColor, color }, |
| 210 | { SkBlendMode::kLuminosity, luminosity }, |
Mike Klein | 6bfe3f5 | 2017-05-05 13:49:00 -0400 | [diff] [blame] | 211 | }; |
Mike Klein | f484ac6 | 2017-05-06 15:44:38 -0400 | [diff] [blame^] | 212 | bool legacy = !canvas->imageInfo().colorSpace(); |
Mike Klein | 6bfe3f5 | 2017-05-05 13:49:00 -0400 | [diff] [blame] | 213 | for (auto test : tests) { |
| 214 | canvas->drawRect({20,20,80,80}, bg); |
| 215 | |
| 216 | fg.setBlendMode(test.mode); |
| 217 | canvas->drawRect({20,20,80,80}, fg); |
| 218 | |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 219 | if (test.reference) { |
| 220 | SkPaint web,KHR; |
Mike Klein | f484ac6 | 2017-05-06 15:44:38 -0400 | [diff] [blame^] | 221 | auto dst = bg.getColor(), |
| 222 | src = fg.getColor(); |
| 223 | web.setColor(blend(dst, src, test.reference, clip_color_web, legacy)); |
| 224 | KHR.setColor(blend(dst, src, test.reference, clip_color_KHR, legacy)); |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 225 | canvas->drawCircle(50,50, 20, web); |
| 226 | canvas->drawCircle(50,50, 10, KHR); // This circle may be very wrong. |
| 227 | } |
Mike Klein | 6bfe3f5 | 2017-05-05 13:49:00 -0400 | [diff] [blame] | 228 | |
| 229 | const char* name = SkBlendMode_Name(test.mode); |
| 230 | canvas->drawText(name, strlen(name), 20,90, label); |
| 231 | |
| 232 | canvas->translate(100,0); |
| 233 | } |
| 234 | } |