Florin Malita | a6e30f7 | 2018-03-23 13:41:58 -0400 | [diff] [blame^] | 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 "SkottieValue.h" |
| 9 | |
| 10 | #include "SkColor.h" |
| 11 | #include "SkPoint.h" |
| 12 | #include "SkSize.h" |
| 13 | |
| 14 | namespace skottie { |
| 15 | |
| 16 | template <> |
| 17 | size_t ValueTraits<ScalarValue>::Cardinality(const ScalarValue&) { |
| 18 | return 1; |
| 19 | } |
| 20 | |
| 21 | template <> |
| 22 | template <> |
| 23 | SkScalar ValueTraits<ScalarValue>::As<SkScalar>(const ScalarValue& v) { |
| 24 | return v; |
| 25 | } |
| 26 | |
| 27 | template <> |
| 28 | size_t ValueTraits<VectorValue>::Cardinality(const VectorValue& vec) { |
| 29 | return vec.size(); |
| 30 | } |
| 31 | |
| 32 | template <> |
| 33 | template <> |
| 34 | SkColor ValueTraits<VectorValue>::As<SkColor>(const VectorValue& v) { |
| 35 | // best effort to turn this into a color |
| 36 | const auto r = v.size() > 0 ? v[0] : 0, |
| 37 | g = v.size() > 1 ? v[1] : 0, |
| 38 | b = v.size() > 2 ? v[2] : 0, |
| 39 | a = v.size() > 3 ? v[3] : 1; |
| 40 | |
| 41 | return SkColorSetARGB(SkTPin<SkScalar>(a, 0, 1) * 255, |
| 42 | SkTPin<SkScalar>(r, 0, 1) * 255, |
| 43 | SkTPin<SkScalar>(g, 0, 1) * 255, |
| 44 | SkTPin<SkScalar>(b, 0, 1) * 255); |
| 45 | } |
| 46 | |
| 47 | template <> |
| 48 | template <> |
| 49 | SkPoint ValueTraits<VectorValue>::As<SkPoint>(const VectorValue& vec) { |
| 50 | // best effort to turn this into a point |
| 51 | const auto x = vec.size() > 0 ? vec[0] : 0, |
| 52 | y = vec.size() > 1 ? vec[1] : 0; |
| 53 | return SkPoint::Make(x, y); |
| 54 | } |
| 55 | |
| 56 | template <> |
| 57 | template <> |
| 58 | SkSize ValueTraits<VectorValue>::As<SkSize>(const VectorValue& vec) { |
| 59 | const auto pt = ValueTraits::As<SkPoint>(vec); |
| 60 | return SkSize::Make(pt.x(), pt.y()); |
| 61 | } |
| 62 | |
| 63 | template <> |
| 64 | size_t ValueTraits<ShapeValue>::Cardinality(const ShapeValue& path) { |
| 65 | return SkTo<size_t>(path.countVerbs()); |
| 66 | } |
| 67 | |
| 68 | template <> |
| 69 | template <> |
| 70 | SkPath ValueTraits<ShapeValue>::As<SkPath>(const ShapeValue& path) { |
| 71 | return path; |
| 72 | } |
| 73 | |
| 74 | } // namespace skottie |