]> git.r.bdr.sh - rbdr/patterns/blob - Sources/Patterns/Tile.swift
389332428f29b8ebe9e22f56ba7e10e4f77a7810
[rbdr/patterns] / Sources / Patterns / Tile.swift
1 import SwiftUI
2
3 public struct Tile: View {
4
5 let design: TileDesign
6 var pixelSize: CGFloat = 2.0;
7 var foregroundColor: Color = .black
8 var backgroundColor: Color = .white
9
10 private var pixels: [Int] {
11 design.pixels()
12 }
13
14 public var body: some View {
15 VStack(spacing: 0) {
16 ForEach(0 ..< 8) { i in
17 HStack(spacing: 0) {
18 ForEach(0 ..< 8) { j in
19 Rectangle()
20 .frame(width: pixelSize, height: pixelSize)
21 .foregroundColor(pixels[(i % 8) * 8 + j % 8] == 0
22 ? foregroundColor
23 : backgroundColor
24 )
25 }
26 }
27 }
28 }
29 }
30 }
31
32 struct Tile_Previews: PreviewProvider {
33 static var previews: some View {
34 VStack {
35 Text("Default")
36 Tile(design: .grid)
37 Text("Color override")
38 Tile(design: .balls, foregroundColor: .pink, backgroundColor: .cyan)
39 Text("Pixel size override")
40 Tile(design: .shingles, pixelSize: 8.0)
41 }
42 }
43 }