From dcbb2db6d9ceb7b38a8842121a9163ff86cfa8b5 Mon Sep 17 00:00:00 2001 From: Florin Malita Date: Tue, 9 Jan 2018 13:11:56 -0500 Subject: [skotty] Color opacity support TBR= Change-Id: I21dd6dda211d17c5de1b815fd43eac713a8e8ccc Reviewed-on: https://skia-review.googlesource.com/92840 Reviewed-by: Florin Malita Commit-Queue: Florin Malita --- experimental/skotty/Skotty.cpp | 11 ++++++++--- experimental/skotty/SkottyProperties.cpp | 15 +++++++++++++++ experimental/skotty/SkottyProperties.h | 17 +++++++++++++++++ 3 files changed, 40 insertions(+), 3 deletions(-) (limited to 'experimental') diff --git a/experimental/skotty/Skotty.cpp b/experimental/skotty/Skotty.cpp index d118516d16..8d43d46b45 100644 --- a/experimental/skotty/Skotty.cpp +++ b/experimental/skotty/Skotty.cpp @@ -297,12 +297,17 @@ sk_sp AttachColorPaint(const Json::Value& obj, AttachContext* ctx) auto color_node = sksg::Color::Make(SK_ColorBLACK); color_node->setAntiAlias(true); - auto color_attached = BindProperty(obj["c"], ctx, color_node, - [](sksg::Color* node, const VectorValue& c) { + auto composite = sk_make_sp(color_node); + auto color_attached = BindProperty(obj["c"], ctx, composite, + [](CompositeColor* node, const VectorValue& c) { node->setColor(ValueTraits::As(c)); }); + auto opacity_attached = BindProperty(obj["o"], ctx, composite, + [](CompositeColor* node, const ScalarValue& o) { + node->setOpacity(o); + }); - return color_attached ? color_node : nullptr; + return (color_attached || opacity_attached) ? color_node : nullptr; } sk_sp AttachFillPaint(const Json::Value& jfill, AttachContext* ctx) { diff --git a/experimental/skotty/SkottyProperties.cpp b/experimental/skotty/SkottyProperties.cpp index 3bd95d8969..579823b24c 100644 --- a/experimental/skotty/SkottyProperties.cpp +++ b/experimental/skotty/SkottyProperties.cpp @@ -10,6 +10,7 @@ #include "SkColor.h" #include "SkottyPriv.h" #include "SkPath.h" +#include "SkSGColor.h" #include "SkSGPath.h" #include "SkSGRect.h" #include "SkSGTransform.h" @@ -177,6 +178,20 @@ SkPath ValueTraits::As(const ShapeValue& path) { return path; } +CompositeColor::CompositeColor(sk_sp wrapped_node) + : fColorNode(std::move(wrapped_node)) { + SkASSERT(fColorNode); +} + +void CompositeColor::apply() { + // 'opacity' is [0..100] + const auto a = SkScalarRoundToInt(SkTPin(fOpacity * .01f, 0, 1) * SkColorGetA(fColor)); + fColorNode->setColor(SkColorSetARGB(a, + SkColorGetR(fColor), + SkColorGetG(fColor), + SkColorGetB(fColor))); +} + CompositeRRect::CompositeRRect(sk_sp wrapped_node) : fRRectNode(std::move(wrapped_node)) {} diff --git a/experimental/skotty/SkottyProperties.h b/experimental/skotty/SkottyProperties.h index 4357367b3f..cb64ed748b 100644 --- a/experimental/skotty/SkottyProperties.h +++ b/experimental/skotty/SkottyProperties.h @@ -8,6 +8,7 @@ #ifndef SkottyProperties_DEFINED #define SkottyProperties_DEFINED +#include "SkColor.h" #include "SkPath.h" #include "SkPoint.h" #include "SkSize.h" @@ -20,6 +21,7 @@ #include namespace sksg { +class Color; class Matrix; class Path; class RRect; @@ -53,6 +55,21 @@ using ShapeValue = SkPath; p_type f##p_name = p_default; \ public: +class CompositeColor final : public SkRefCnt { +public: + explicit CompositeColor(sk_sp); + + COMPOSITE_PROPERTY(Color , SkColor , SK_ColorBLACK) + COMPOSITE_PROPERTY(Opacity, SkScalar, 100 ) + +private: + void apply(); + + sk_sp fColorNode; + + using INHERITED = SkRefCnt; +}; + class CompositeRRect final : public SkRefCnt { public: explicit CompositeRRect(sk_sp); -- cgit v1.2.3