Skip to content

Commit

Permalink
Only convert solid fills to colors
Browse files Browse the repository at this point in the history
  • Loading branch information
alldne authored and subdan committed Dec 30, 2020
1 parent 5f82164 commit 7f02bc3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
29 changes: 28 additions & 1 deletion Sources/FigmaAPI/Model/Node.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,37 @@ public struct Document: Decodable {
public let style: TypeStyle?
}

// https://www.figma.com/plugin-docs/api/Paint/
public struct Paint: Decodable {
public let type: String
public let type: PaintType
public let opacity: Double?
public let color: PaintColor?

public var asSolid: SolidPaint? {
return SolidPaint(self)
}
}

public enum PaintType: String, Decodable {
case solid = "SOLID"
case image = "IMAGE"
case rectangle = "RECTANGLE"
case gradientLinear = "GRADIENT_LINEAR"
case gradientRadial = "GRADIENT_RADIAL"
case gradientAngular = "GRADIENT_ANGULAR"
case gradientDiamond = "GRADIENT_DIAMOND"
}

public struct SolidPaint: Decodable {
public let opacity: Double?
public let color: PaintColor

public init?(_ paint: Paint) {
guard paint.type == .solid else { return nil }
guard let color = paint.color else { return nil }
self.opacity = paint.opacity
self.color = color
}
}

public struct PaintColor: Decodable {
Expand Down
4 changes: 2 additions & 2 deletions Sources/FigmaExport/Loaders/ColorsLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ final class ColorsLoader {
/// Соотносит массив Style и Node чтобы получит массив Color
private func nodesAndStylesToColors(nodes: [NodeId: Node], styles: [Style]) -> [Color] {
return styles.compactMap { style -> Color? in
guard let node = nodes[style.nodeId] else { return nil}
guard let fill = node.document.fills.first else { return nil }
guard let node = nodes[style.nodeId] else { return nil }
guard let fill = node.document.fills.first?.asSolid else { return nil }
let alpha: Double = fill.opacity ?? fill.color.a
let platform = Platform(rawValue: style.description)

Expand Down

0 comments on commit 7f02bc3

Please sign in to comment.