Skip to content

Instantly share code, notes, and snippets.

@Mrteller
Mrteller / UIColorFromHexString
Created March 25, 2019 17:17
Create UIColor from different kinds String representing a valid hexadecimal number.
import UIKit
extension UIColor {
/**
The rgba string representation of color with alpha of the form #RRGGBBAA/#RRGGBB.
- parameter rgba: String value, which may or may not start from # sign.
*/
public convenience init?(rgba: String, defaultAlpha: CGFloat = 1) {
// We migth want to trim the string first from spaces, etc.
let rgbaStripped = rgba.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
@Mrteller
Mrteller / MinMaxOfTuple
Created March 21, 2019 19:47
Min, Max, Enclosing rectangle on array of XY pairs of data
func minsOf(pairs: [(Int, Int)]) -> (Int, Int) {
return pairs.reduce((Int.max, Int.max)) {
(($0.0 < $1.0) ? $0.0 : $1.0, (($0.1 < $1.1) ? $0.1 : $1.1))
}
}
func maxsOf(pairs: [(Int, Int)]) -> (Int, Int) {
return pairs.reduce((Int.min, Int.min)) {
(($0.0 > $1.0) ? $0.0 : $1.0, (($0.1 > $1.1) ? $0.1 : $1.1))
}