3 public struct PatternView: View {
5 @Binding public var design: TileDesign
6 public var pixelSize: CGFloat
7 public var foregroundColor: Color
8 public var backgroundColor: Color
10 private let image: CGImage
11 private var patternSize: CGFloat {
15 public init(design: Binding<TileDesign>, pixelSize: CGFloat = 2.0, foregroundColor: Color = .black, backgroundColor: Color = .white) {
17 self.pixelSize = pixelSize
18 self.foregroundColor = foregroundColor
19 self.backgroundColor = backgroundColor
21 #if os(iOS) || os(watchOS) || os(tvOS)
22 let foregroundCGColor = UIColor(foregroundColor).cgColor
23 let backgroundCGColor = UIColor(backgroundColor).cgColor
25 let foregroundCGColor = NSColor(foregroundColor).cgColor
26 let backgroundCGColor = NSColor(backgroundColor).cgColor
29 self.image = TileImage.image(design.wrappedValue, pixelSize: pixelSize, foregroundColor: foregroundCGColor, backgroundColor: backgroundCGColor)
32 public var body: some View {
33 GeometryReader { gr in
34 Image(image, scale: 1, label: Text("Test")).resizable(resizingMode: .tile)
39 struct Pattern_Previews: PreviewProvider {
40 static var previews: some View {
43 PatternView(design: .constant(TileDesign.grid))
44 Text("Color override")
45 PatternView(design: .constant(TileDesign.balls), foregroundColor: .pink, backgroundColor: .cyan)
46 Text("Pixel size override")
47 PatternView(design: .constant(TileDesign.shingles), pixelSize: 8.0)