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 "modules/skottie/src/SkottieValue.h"
9
10 #include "include/core/SkColor.h"
11 #include "include/core/SkM44.h"
12 #include "include/core/SkPoint.h"
13 #include "include/core/SkSize.h"
14 #include "include/private/SkNx.h"
15 #include "modules/skottie/src/SkottieJson.h"
16 #include "modules/skottie/src/SkottiePriv.h"
17
18 #include <algorithm>
19
20 namespace skottie {
21
22 template <>
FromJSON(const skjson::Value & jv,const internal::AnimationBuilder *,ScalarValue * v)23 bool ValueTraits<ScalarValue>::FromJSON(const skjson::Value& jv, const internal::AnimationBuilder*,
24 ScalarValue* v) {
25 return Parse(jv, v);
26 }
27
28 template <>
FromJSON(const skjson::Value & jv,const internal::AnimationBuilder *,Vec2Value * v)29 bool ValueTraits<Vec2Value>::FromJSON(const skjson::Value& jv, const internal::AnimationBuilder*,
30 Vec2Value* v) {
31 return Parse(jv, v);
32 }
33
34 template <>
35 template <>
As(const ScalarValue & v)36 SkScalar ValueTraits<ScalarValue>::As<SkScalar>(const ScalarValue& v) {
37 return v;
38 }
39
40 // DEPRECATED: remove after converting everything to SkColor4f
41 template <>
42 template <>
As(const VectorValue & v)43 SkColor ValueTraits<VectorValue>::As<SkColor>(const VectorValue& v) {
44 // best effort to turn this into a color
45 const auto r = v.size() > 0 ? v[0] : 0,
46 g = v.size() > 1 ? v[1] : 0,
47 b = v.size() > 2 ? v[2] : 0,
48 a = v.size() > 3 ? v[3] : 1;
49
50 return SkColorSetARGB(SkScalarRoundToInt(SkTPin(a, 0.0f, 1.0f) * 255),
51 SkScalarRoundToInt(SkTPin(r, 0.0f, 1.0f) * 255),
52 SkScalarRoundToInt(SkTPin(g, 0.0f, 1.0f) * 255),
53 SkScalarRoundToInt(SkTPin(b, 0.0f, 1.0f) * 255));
54 }
55
56 template <>
57 template <>
As(const VectorValue & v)58 SkColor4f ValueTraits<VectorValue>::As<SkColor4f>(const VectorValue& v) {
59 // best effort to turn a vector into a color
60 const auto r = v.size() > 0 ? SkTPin(v[0], 0.0f, 1.0f) : 0,
61 g = v.size() > 1 ? SkTPin(v[1], 0.0f, 1.0f) : 0,
62 b = v.size() > 2 ? SkTPin(v[2], 0.0f, 1.0f) : 0,
63 a = v.size() > 3 ? SkTPin(v[3], 0.0f, 1.0f) : 1;
64
65 return { r, g, b, a };
66 }
67
68 template <>
69 template <>
As(const VectorValue & vec)70 SkPoint ValueTraits<VectorValue>::As<SkPoint>(const VectorValue& vec) {
71 // best effort to turn this into a 2D point
72 return SkPoint {
73 vec.size() > 0 ? vec[0] : 0,
74 vec.size() > 1 ? vec[1] : 0,
75 };
76 }
77
78 template <>
79 template <>
As(const VectorValue & vec)80 SkV3 ValueTraits<VectorValue>::As<SkV3>(const VectorValue& vec) {
81 // best effort to turn this into a 3D point
82 return SkV3 {
83 vec.size() > 0 ? vec[0] : 0,
84 vec.size() > 1 ? vec[1] : 0,
85 vec.size() > 2 ? vec[2] : 0,
86 };
87 }
88
89 template <>
90 template <>
As(const VectorValue & vec)91 SkSize ValueTraits<VectorValue>::As<SkSize>(const VectorValue& vec) {
92 const auto pt = ValueTraits::As<SkPoint>(vec);
93 return SkSize::Make(pt.x(), pt.y());
94 }
95
96 } // namespace skottie
97