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