]> git.r.bdr.sh - rbdr/patterns/blobdiff - Sources/Patterns/Tile.swift
Add explicit initializers
[rbdr/patterns] / Sources / Patterns / Tile.swift
index c27fcf9782215de890f4e191b2d4390a2efee5a8..9c39640b8ab4dbfdae2ab29988ac9851a8310079 100644 (file)
@@ -1,16 +1,24 @@
 import SwiftUI
 
-struct Tile: View {
+public struct Tile: View {
   
-  let pixelSize: CGFloat = 2.0;
-  
-  let design: TileDesign
+  public let design: TileDesign
+  public var pixelSize: CGFloat = 2.0;
+  public var foregroundColor: Color = .black
+  public var backgroundColor: Color = .white
   
   private var pixels: [Int] {
     design.pixels()
   }
   
-    var body: some View {
+  public init(design: TileDesign, pixelSize: CGFloat = 2.0, foregroundColor: Color = .black, backgroundColor: Color = .white) {
+    self.design = design
+    self.pixelSize = pixelSize
+    self.foregroundColor = foregroundColor
+    self.backgroundColor = backgroundColor
+  }
+  
+    public var body: some View {
       VStack(spacing: 0) {
         ForEach(0 ..< 8) { i in
           HStack(spacing: 0) {
@@ -18,8 +26,8 @@ struct Tile: View {
               Rectangle()
                 .frame(width: pixelSize, height: pixelSize)
                 .foregroundColor(pixels[(i % 8) * 8 + j % 8] == 0
-                                  ? .black
-                                  : .white
+                                  ? foregroundColor
+                                  : backgroundColor
                 )
             }
           }
@@ -30,6 +38,13 @@ struct Tile: View {
 
 struct Tile_Previews: PreviewProvider {
     static var previews: some View {
-      Tile(design: .grid)
+      VStack {
+        Text("Default")
+        Tile(design: .grid)
+        Text("Color override")
+        Tile(design: .balls, foregroundColor: .pink, backgroundColor: .cyan)
+        Text("Pixel size override")
+        Tile(design: .shingles, pixelSize: 8.0)
+      }
     }
 }