forked from yannickl/DynamicColor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewController.swift
More file actions
79 lines (60 loc) · 2.67 KB
/
Copy pathViewController.swift
File metadata and controls
79 lines (60 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//
// ViewController.swift
// DynamicColorExample
//
// Created by Yannick LORIOT on 01/06/15.
// Copyright (c) 2015 Yannick LORIOT. All rights reserved.
//
import UIKit
class ViewController: UIViewController, UICollectionViewDataSource {
private let ColorCellIdentifier = "ColorCell"
@IBOutlet weak var colorCollectionView: UICollectionView!
private lazy var colors: [(String, UIColor)] = {
let mainColor = UIColor(hexString: "#c0392b")
return [
("Original", mainColor),
("Lighter", mainColor.lighter()),
("Darkered", mainColor.darkened()),
("Saturated", mainColor.saturated()),
("Desaturated", mainColor.desaturated()),
("Grayscaled", mainColor.grayscaled()),
("Adjusted", mainColor.adjustedHue(amount: 45)),
("Complemented", mainColor.complemented()),
("Inverted", mainColor.inverted()),
("Mix Blue", mainColor.mixed(withColor: .blue)),
("Mix Green", mainColor.mixed(withColor: .green)),
("Mix Yellow", mainColor.mixed(withColor: .yellow)),
("Tinted", mainColor.tinted()),
("Shaded", mainColor.shaded())
]
}()
private lazy var gradients: [(String, UIColor)] = {
return [UIColor(hex: 0x3498db), UIColor(hex: 0xe74c3c) , UIColor(hex: 0xf1c40f)].gradient.colorPalette(amount: 15).map { ($0.toHexString(), $0) }
}()
override func viewDidLoad() {
super.viewDidLoad()
colorCollectionView.reloadData()
}
func collection(inSection section: Int) -> [(String, UIColor)] {
return section == 0 ? colors : gradients
}
// MARK: - UICollectionView DataSource Methods
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 2
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return collection(inSection: section).count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ColorCellIdentifier, for: indexPath) as! ColorCellView
let (title, color) = collection(inSection: indexPath.section)[indexPath.row]
cell.titleLabel?.text = title
cell.colorView?.backgroundColor = color
return cell
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let supplementaryView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "HeaderView", for: indexPath) as! HeaderView
supplementaryView.titleLabel.text = indexPath.section == 0 ? "Colors" : "Gradients"
return supplementaryView
}
}