diff options
author | Jim Van Verth <jvanverth@google.com> | 2017-12-19 13:02:38 -0500 |
---|---|---|
committer | Skia Commit-Bot <skia-commit-bot@chromium.org> | 2017-12-19 19:27:06 +0000 |
commit | 744cbb388870bc1ae0f6b3ac0fbeaa136f55917b (patch) | |
tree | a8929b482248d8fd9cd0e57880e7696dff7784ad /include/utils | |
parent | faa095e9842b924c20de84dce1bcc1adad7fe2e4 (diff) |
Fix alpha contribution to tonal color.
Also adds a tonal color GM, a grayscale mode to shadowutils GM,
and animated alpha to SampleAndroidShadows.
Bug: skia:
Change-Id: I1dcb5cab7e53ffa7a3bf1a07b6ebfed38df1a9ed
Reviewed-on: https://skia-review.googlesource.com/85002
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Jim Van Verth <jvanverth@google.com>
Diffstat (limited to 'include/utils')
-rw-r--r-- | include/utils/SkShadowUtils.h | 34 |
1 files changed, 24 insertions, 10 deletions
diff --git a/include/utils/SkShadowUtils.h b/include/utils/SkShadowUtils.h index c362488f15..9550a925b9 100644 --- a/include/utils/SkShadowUtils.h +++ b/include/utils/SkShadowUtils.h @@ -84,9 +84,9 @@ public: * set the alpha to (S_a + C_a - S_a*C_a). * * @param r Red value of color - * @param g Red value of color - * @param b Red value of color - * @param a Red value of color + * @param g Green value of color + * @param b Blue value of color + * @param a Alpha value of color * @param colorScale Factor to scale color values by * @param tonalAlpha Value to set alpha to */ @@ -96,14 +96,28 @@ public: SkScalar min = SkTMin(SkTMin(r, g), b); SkScalar luminance = 0.5f*(max + min); - // We get best results with a luminance between 0.3 and 0.5, with smoothstep applied - SkScalar adjustedLuminance = (0.6f - 0.4f*luminance)*luminance*luminance + 0.3f; - // Similarly, we need to tone down the given greyscale alpha depending on how - // much color we're applying. - a -= (0.5f*adjustedLuminance - 0.15f); + // We compute a color alpha value based on the luminance of the color, scaled by an + // adjusted alpha value. We want the following properties to match the UX examples + // (assuming a = 0.25) and to ensure that we have reasonable results when the color + // is black and/or the alpha is 0: + // f(0, a) = 0 + // f(luminance, 0) = 0 + // f(1, 0.25) = .5 + // f(0.5, 0.25) = .4 + // f(1, 1) = 1 + // The following functions match this as closely as possible. + SkScalar alphaAdjust = (2.6f + (-2.66667f + 1.06667f*a)*a)*a; + SkScalar colorAlpha = (3.544762f + (-4.891428f + 2.3466f*luminance)*luminance)*luminance; + colorAlpha = SkTPin(alphaAdjust*colorAlpha, 0.0f, 1.0f); - *colorScale = adjustedLuminance*(SK_Scalar1 - a); - *tonalAlpha = *colorScale + a; + // Similarly, we set the greyscale alpha based on luminance and alpha so that + // f(0, a) = a + // f(luminance, 0) = 0 + // f(1, 0.25) = 0.15 + SkScalar greyscaleAlpha = SkTPin(a*(1 - 0.4f*luminance), 0.0f, 1.0f); + + *colorScale = colorAlpha*(SK_Scalar1 - greyscaleAlpha); + *tonalAlpha = *colorScale + greyscaleAlpha; } }; |