// // GradientFill.swift // lottie-swift // // Created by Brandon Withrow on 1/8/19. // import Foundation enum GradientType: Int, Codable { case none case linear case radial } /// An item that define a gradient fill final class GradientFill: ShapeItem { /// The opacity of the fill let opacity: KeyframeGroup /// The start of the gradient let startPoint: KeyframeGroup /// The end of the gradient let endPoint: KeyframeGroup /// The type of gradient let gradientType: GradientType /// Gradient Highlight Length. Only if type is Radial let highlightLength: KeyframeGroup? /// Highlight Angle. Only if type is Radial let highlightAngle: KeyframeGroup? /// The number of color points in the gradient let numberOfColors: Int /// The Colors of the gradient. let colors: KeyframeGroup<[Double]> private enum CodingKeys : String, CodingKey { case opacity = "o" case startPoint = "s" case endPoint = "e" case gradientType = "t" case highlightLength = "h" case highlightAngle = "a" case colors = "g" } private enum GradientDataKeys : String, CodingKey { case numberOfColors = "p" case colors = "k" } required init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: GradientFill.CodingKeys.self) self.opacity = try container.decode(KeyframeGroup.self, forKey: .opacity) self.startPoint = try container.decode(KeyframeGroup.self, forKey: .startPoint) self.endPoint = try container.decode(KeyframeGroup.self, forKey: .endPoint) self.gradientType = try container.decode(GradientType.self, forKey: .gradientType) self.highlightLength = try container.decodeIfPresent(KeyframeGroup.self, forKey: .highlightLength) self.highlightAngle = try container.decodeIfPresent(KeyframeGroup.self, forKey: .highlightAngle) let colorsContainer = try container.nestedContainer(keyedBy: GradientDataKeys.self, forKey: .colors) self.colors = try colorsContainer.decode(KeyframeGroup<[Double]>.self, forKey: .colors) self.numberOfColors = try colorsContainer.decode(Int.self, forKey: .numberOfColors) try super.init(from: decoder) } override func encode(to encoder: Encoder) throws { try super.encode(to: encoder) var container = encoder.container(keyedBy: CodingKeys.self) try container.encode(opacity, forKey: .opacity) try container.encode(startPoint, forKey: .startPoint) try container.encode(endPoint, forKey: .endPoint) try container.encode(gradientType, forKey: .gradientType) try container.encodeIfPresent(highlightLength, forKey: .highlightLength) try container.encodeIfPresent(highlightAngle, forKey: .highlightAngle) var colorsContainer = container.nestedContainer(keyedBy: GradientDataKeys.self, forKey: .colors) try colorsContainer.encode(numberOfColors, forKey: .numberOfColors) try colorsContainer.encode(colors, forKey: .colors) } }