diff options
author | Jim Van Verth <jvanverth@google.com> | 2017-06-09 11:09:03 -0400 |
---|---|---|
committer | Skia Commit-Bot <skia-commit-bot@chromium.org> | 2017-06-09 17:18:58 +0000 |
commit | 34d6e4b09f81a7fa6a4fe3c667d4663e55af97bc (patch) | |
tree | 0c2c9c199ed19d2e401739020abb6c0fbcf99848 /include/utils/SkShadowUtils.h | |
parent | febbffad1c24136f041d7fc2d5ffcd50e47a047f (diff) |
Add tonal color support for shadows.
Bug: skia:
Change-Id: Ib9bd9083da1d8a9fa90ae7c710386e6903541fd5
Reviewed-on: https://skia-review.googlesource.com/18148
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Jim Van Verth <jvanverth@google.com>
Diffstat (limited to 'include/utils/SkShadowUtils.h')
-rw-r--r-- | include/utils/SkShadowUtils.h | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/include/utils/SkShadowUtils.h b/include/utils/SkShadowUtils.h index 838a12bba8..0ac2f3708d 100644 --- a/include/utils/SkShadowUtils.h +++ b/include/utils/SkShadowUtils.h @@ -69,6 +69,42 @@ public: DrawShadow(canvas, path, zPlane, lightPos, lightRadius, ambientAlpha, spotAlpha, color, flags); } + + /** + * Helper routine to compute scale alpha values for one-pass tonal alpha. + * + * The final color we want to emulate is generated by rendering a color shadow (C_rgb) using an + * alpha computed from the color's luminance (C_a), and then a black shadow with alpha (S_a) + * which is an adjusted value of 'a'. Assuming SrcOver, a background color of B_rgb, and + * ignoring edge falloff, this becomes + * + * (C_a - S_a*C_a)*C_rgb + (1 - (S_a + C_a - S_a*C_a))*B_rgb + * + * Since we use premultiplied alpha, this means we can scale the color by (C_a - S_a*C_a) and + * 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 colorScale Factor to scale color values by + * @param tonalAlpha Value to set alpha to + */ + static inline void ComputeTonalColorParams(SkScalar r, SkScalar g, SkScalar b, SkScalar a, + SkScalar* colorScale, SkScalar* tonalAlpha) { + SkScalar max = SkTMax(SkTMax(r, g), b); + 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); + + *colorScale = adjustedLuminance*(SK_Scalar1 - a); + *tonalAlpha = *colorScale + a; + } }; #endif |