Besides Color, SwiftUI has the ShapeStyle protocol, which can be used with many UI elements. Here are some of the styles available:
Color
HierarchicalShapeStyle
Gradients
Materials
ImagePaint
Semantic styles (foreground, background, etc.)
as well as modifiers for things like opacity and shadow.
A custom Elixir type that represents AnyShapeStyle that can be JSON encoded could be a good solution.
Here is what the encoded values could look like given the Swift representation below:
This is the type that would get decoded. It can then be mapped to AnyShapeStyle.
indirect enum CodableShapeStyle: Codable {
case color(CodableColor)
case hierarchical(level: HierarchyLevel)
case angularGradient(stops: [GradientStop], center: CodableUnitPoint, startAngle: Double, endAngle: Double)
case conicGradient(stops: [GradientStop], center: CodableUnitPoint, angle: Double)
case ellipticalGradient(stops: [GradientStop], center: CodableUnitPoint, startRadiusFraction: Double, endRadiusFraction: Double)
case linearGradient(stops: [GradientStop], startPoint: CodableUnitPoint, endPoint: CodableUnitPoint)
case radialGradient(stops: [GradientStop], center: CodableUnitPoint, startRadius: Double, endRadius: Double)
case material(CodableMaterial)
case image(name: String, sourceRect: Rect, scale: Double)
case foreground
case background
case selection
case separator
case tint
case blendMode(root: Self, blendMode: CodableBlendMode)
case opacity(root: Self, opacity: Double)
case shadow(root: Self, style: CodableShadowStyle, color: CodableColor, radius: Double, x: Double, y: Double)
enum CodableColor: Codable {
case system(String)
case hex(String)
}
enum HierarchyLevel: String, Codable {
case primary
case secondary
case tertiary
case quaternary
}
struct GradientStop: Codable {
let color: CodableColor
let location: Double
}
struct CodableUnitPoint: Codable {
let x: Double
let y: Double
}
enum CodableMaterial: String, Codable {
case ultraThin
case thin
case regular
case thick
case ultraThick
case bar
}
struct Rect: Codable {
let x: Double
let y: Double
let width: Double
let height: Double
}
enum CodableBlendMode: String, Codable {
case normal
case darken
case multiply
case colorBurn
case plusDarker
case lighten
case screen
case colorDodge
case plusLighter
case overlay
case softLight
case hardLight
case difference
case exclusion
case hue
case saturation
case color
case luminosity
case sourceAtop
case destinationOver
case destinationOut
}
enum CodableShadowStyle: String, Codable {
case drop
case inner
}
}
Besides
Color
, SwiftUI has theShapeStyle
protocol, which can be used with many UI elements. Here are some of the styles available:Color
HierarchicalShapeStyle
ImagePaint
foreground
,background
, etc.)as well as modifiers for things like
opacity
andshadow
.A custom Elixir type that represents
AnyShapeStyle
that can be JSON encoded could be a good solution. Here is what the encoded values could look like given the Swift representation below:Swift Representation
This is the type that would get decoded. It can then be mapped to
AnyShapeStyle
.