Forked from JohnCoates/generateRandomPastelColor.swift
Last active
March 5, 2021 21:40
-
-
Save alexwal/51cb721d299ae5fb204abfa8faef8e11 to your computer and use it in GitHub Desktop.
Randomly generate pastel UIColor in Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Adapted from Stack Overflow answer by David Crow http://stackoverflow.com/a/43235 | |
// Question: Algorithm to randomly generate an aesthetically-pleasing color palette Brian Gianforcaro | |
func generateRandomPastelColor(withMixedColor mixColor: UIColor?) -> UIColor { | |
// Randomly generate number in closure | |
let randomColorGenerator = { ()-> CGFloat in | |
CGFloat(arc4random() % 256 ) / 256 | |
} | |
var red: CGFloat = randomColorGenerator() | |
var green: CGFloat = randomColorGenerator() | |
var blue: CGFloat = randomColorGenerator() | |
// Mix the color | |
if let mixColor = mixColor { | |
var mixRed: CGFloat = 0, mixGreen: CGFloat = 0, mixBlue: CGFloat = 0; | |
mixColor.getRed(&mixRed, green: &mixGreen, blue: &mixBlue, alpha: nil) | |
red = (red + mixRed) / 2; | |
green = (green + mixGreen) / 2; | |
blue = (blue + mixBlue) / 2; | |
} | |
return UIColor(red: red, green: green, blue: blue, alpha: 1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment