blob: 120eebffe4d549149379eeae948719036909390e [file] [log] [blame]
Mike Klein6bfe3f52017-05-05 13:49:00 -04001/*
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 Klein5b4e2772017-05-06 14:01:22 -040015// 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 Klein6bfe3f52017-05-05 13:49:00 -040019// 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 Klein5b4e2772017-05-06 14:01:22 -040023// 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 Klein6bfe3f52017-05-05 13:49:00 -040027// We have had many inconsistent implementations of these modes.
28// This GM tries to demonstrate unambigously how they should work.
29//
Mike Klein5b4e2772017-05-06 14:01:22 -040030// 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
39static float min(float r, float g, float b) { return SkTMin(r, SkTMin(g, b)); }
40static float max(float r, float g, float b) { return SkTMax(r, SkTMax(g, b)); }
41
42static float sat(float r, float g, float b) { return max(r,g,b) - min(r,g,b); }
43static 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.
48static 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
60static 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
68static 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 Kleinf484ac62017-05-06 15:44:38 -040075 //SkASSERT(0 <= c); // This may end up very slightly negative...
Mike Klein5b4e2772017-05-06 14:01:22 -040076 SkASSERT(c <= 1);
77 return c;
78 };
79 *r = clip(*r);
80 *g = clip(*g);
81 *b = clip(*b);
82}
83static 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 Kleinf484ac62017-05-06 15:44:38 -040090 //SkASSERT(0 <= c); // This may end up very slightly negative...
91 //SkASSERT(c <= 1); // I think the mx > 1 logic is incorrect...
Mike Klein5b4e2772017-05-06 14:01:22 -040092 return c;
93 };
94 *r = clip(*r);
95 *g = clip(*g);
96 *b = clip(*b);
97}
98
99
100static 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
113static 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
126static 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
138static 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
150static SkColor blend(SkColor dst, SkColor src,
151 void (*mode)(float,float,float, float*,float*,float*),
Mike Kleinf484ac62017-05-06 15:44:38 -0400152 void (*clip_color)(float*,float*,float*),
153 bool legacy) {
Mike Klein5b4e2772017-05-06 14:01:22 -0400154
155 SkASSERT(SkColorGetA(dst) == 0xff
156 && SkColorGetA(src) == 0xff); // Not fundamental, just simplifying for this GM.
157
Mike Kleinf484ac62017-05-06 15:44:38 -0400158 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 Klein5b4e2772017-05-06 14:01:22 -0400169
Mike Kleinf484ac62017-05-06 15:44:38 -0400170 SkColor4f d = to_float(dst),
171 s = to_float(src);
Mike Klein5b4e2772017-05-06 14:01:22 -0400172
Mike Kleinf484ac62017-05-06 15:44:38 -0400173 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 Klein5b4e2772017-05-06 14:01:22 -0400185}
Mike Klein6bfe3f52017-05-05 13:49:00 -0400186
187DEF_SIMPLE_GM(hsl, canvas, 600, 100) {
188 SkPaint label;
189 sk_tool_utils::set_portable_typeface(&label);
190 label.setAntiAlias(true);
191
Mike Klein5b4e2772017-05-06 14:01:22 -0400192 const char* comment = "HSL blend modes are correct when you see no circles in the squares. "
193 "2 circles means the standards disagree.";
Mike Klein6bfe3f52017-05-05 13:49:00 -0400194 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 Klein5b4e2772017-05-06 14:01:22 -0400203 void (*reference)(float,float,float, float*,float*,float*);
Mike Klein6bfe3f52017-05-05 13:49:00 -0400204 } tests[] = {
Mike Klein5b4e2772017-05-06 14:01:22 -0400205 { SkBlendMode::kSrc, nullptr },
206 { SkBlendMode::kDst, nullptr },
207 { SkBlendMode::kHue, hue },
208 { SkBlendMode::kSaturation, saturation },
209 { SkBlendMode::kColor, color },
210 { SkBlendMode::kLuminosity, luminosity },
Mike Klein6bfe3f52017-05-05 13:49:00 -0400211 };
Mike Kleinf484ac62017-05-06 15:44:38 -0400212 bool legacy = !canvas->imageInfo().colorSpace();
Mike Klein6bfe3f52017-05-05 13:49:00 -0400213 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 Klein5b4e2772017-05-06 14:01:22 -0400219 if (test.reference) {
220 SkPaint web,KHR;
Mike Kleinf484ac62017-05-06 15:44:38 -0400221 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 Klein5b4e2772017-05-06 14:01:22 -0400225 canvas->drawCircle(50,50, 20, web);
226 canvas->drawCircle(50,50, 10, KHR); // This circle may be very wrong.
227 }
Mike Klein6bfe3f52017-05-05 13:49:00 -0400228
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}