Skip to content

Instantly share code, notes, and snippets.

View klein-artur's full-sized avatar

Artur Hellmann klein-artur

View GitHub Profile
@klein-artur
klein-artur / StringExtensionSubscript.swift
Created October 24, 2016 11:30
An extension of a string to provide subscription functionality.
extension String {
subscript (i: Int) -> Character {
get {
assert(i < self.characters.count && i >= 0, "Index out of range.")
return self[self.index(self.startIndex, offsetBy: String.IndexDistance(i))]
}
set {
assert(i < self.characters.count && i >= 0, "Index out of range.")
let index = self.index(self.startIndex, offsetBy: String.IndexDistance(i))
let endIndex = self.index(self.startIndex, offsetBy: String.IndexDistance(i+1))
@klein-artur
klein-artur / randomColor.swift
Last active July 5, 2016 13:24
get a random UIColor in Swift
func getRandomColor(minVal: Int = 0, maxVal: Int = 255) -> UIColor {
// get random values between the min value (0) and max value (255):
let randomRed: Int = Int(arc4random_uniform(UInt32(maxVal-minVal+1)))+minVal
let randomGreen: Int = Int(arc4random_uniform(UInt32(maxVal-minVal+1)))+minVal
let randomBlue: Int = Int(arc4random_uniform(UInt32(maxVal-minVal+1)))+minVal
return UIColor(red: CGFloat(Float(randomRed) / Float(255)), green: CGFloat(Float(randomGreen) / Float(255)), blue: CGFloat(Float(randomBlue) / Float(255)), alpha: 1.0)
}
@klein-artur
klein-artur / complementaryColor.swift
Last active May 29, 2022 21:10
get complementary color to UIColor in Swift
// get a complementary color to this color:
func getComplementaryForColor(color: UIColor) -> UIColor {
let ciColor = CIColor(color: color)
// get the current values and make the difference from white:
let compRed: CGFloat = 1.0 - ciColor.red
let compGreen: CGFloat = 1.0 - ciColor.green
let compBlue: CGFloat = 1.0 - ciColor.blue