Skip to content

Instantly share code, notes, and snippets.

Avatar

Kevin Peng aoenth

  • Toronto, Canada
View GitHub Profile
@aoenth
aoenth / UIColor+Extensions.swift
Created May 28, 2020
UIColor convenience initializer, taking hex as an argument
View UIColor+Extensions.swift
import UIKit
extension UIColor {
convenience init(hex: Int) {
let red = CGFloat((hex & 0xFF0000) >> (4 * 4))/0xFF
let green = CGFloat((hex & 0x00FF00) >> (4 * 2))/0xFF
let blue = CGFloat(hex & 0x0000FF)/0xFF
self.init(red: red, green: green, blue: blue, alpha: 1)
}
}
@aoenth
aoenth / DynamicColors.swift
Created May 27, 2020
Return a color based on percentage. 0% is Red, and 100% is Green
View DynamicColors.swift
extension UIColor {
static func colorForPercent(_ percentage: Double) -> UIColor {
switch percentage {
case 0...0.5:
return firstHalf(percentage: percentage * 2)
case 0.5...1:
return secondHalf(percentage: (percentage - 0.5) * 2)
default:
fatalError("Bad Input: percentage must satisfy 0 <= percentage <= 1")
}
@aoenth
aoenth / FixedColors.swift
Last active May 27, 2020
Return a color based on percentage. 0% is Red and 100% is Green
View FixedColors.swift
static func colorForPercent(_ percentage: Double) -> UIColor {
switch percentage {
case 0..<0.33:
return UIColor(hex: 0xE02020)
case 0.33..<0.66:
return UIColor(hex: 0xFA6400)
case 0.66..<1:
return UIColor(hex: 0xF7B500)
case 1:
return UIColor(hex: 0x6DD400)
View CZ010.swift
let anotherDate = normalDate(fromYear: 1991, month: 1, day: 20)
let anotherZodiac = zodiacFrom(date: anotherDate)
print(anotherZodiac)
View CZ009.swift
let date = normalDate(fromYear: 1991, month: 6, day: 22)
let zodiac = zodiacFrom(date: date)
print(zodiac)
View CZ008.swift
func normalDate(fromYear year: Int, month: Int, day: Int) -> Date {
var normalCalendarDateComponents = DateComponents()
normalCalendarDateComponents.year = year
normalCalendarDateComponents.month = month
normalCalendarDateComponents.day = day
let normalCalendar = Calendar(identifier: .gregorian)
let normalDate = normalCalendar.date(from: normalCalendarDateComponents)!
return normalDate
}
View CZ007.swift
guard let hyphen = chineseDate.firstIndex(of: "-") else {
fatalError("\(chineseDate) is not correctly formatted, use DateFormatter.Style.full")
}
let startIndex = chineseDate.index(after: hyphen)
let endIndex = chineseDate.index(chineseDate.endIndex, offsetBy: -2)
let branch = chineseDate[startIndex ... endIndex]
print(branch)
View CZ006.swift
let dict = [
"zi": "rat",
"chou": "ox",
"yin": "tiger",
"mao": "rabbit",
"chen": "dragon",
"si": "snake",
"wu": "horse",
"wei": "goat",
"shen": "monkey",
View CZ004.swift
let formatter = DateFormatter()
formatter.dateFormat = "MMM d, yyyy"
formatter.calendar = .init(identifier: .chinese)
formatter.dateStyle = .full
print(formatter.string(from: normalDate))
View CZ003.swift
let formatter = DateFormatter()
formatter.dateFormat = "MMM d, yyyy"
print(formatter.string(from: normalDate))
You can’t perform that action at this time.