Skip to content

Instantly share code, notes, and snippets.

Revisions

  1. alexwal revised this gist Mar 5, 2021. 1 changed file with 14 additions and 21 deletions.
    35 changes: 14 additions & 21 deletions generateRandomPastelColor.swift
    Original file line number Diff line number Diff line change
    @@ -1,25 +1,18 @@
    // Adapted from Stack Overflow answer by David Crow http://stackoverflow.com/a/43235
    // Question: Algorithm to randomly generate an aesthetically-pleasing color palette by Brian Gianforcaro
    // Method randomly generates a pastel color, and optionally mixes it with another color
    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)

    import UIKit

    func randomPastelColor(mixedWith mixColor: UIColor? = nil) -> UIColor {
    // Mix the color
    let mixColor: UIColor = mixColor ?? .lightGray
    var mixRed: CGFloat = 0, mixGreen: CGFloat = 0, mixBlue: CGFloat = 0;
    mixColor.getRed(&mixRed, green: &mixGreen, blue: &mixBlue, alpha: nil)
    // Randomly generate number in closure
    let randomColor: () -> CGFloat = { CGFloat(arc4random() % 256 ) / 256 }
    let red = (randomColor() + mixRed) / 2;
    let green = (randomColor() + mixGreen) / 2;
    let blue = (randomColor() + mixBlue) / 2;
    return UIColor(red: red, green: green, blue: blue, alpha: 1)
    }
  2. @JohnCoates JohnCoates revised this gist Jul 6, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion generateRandomPastelColor.swift
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    // Adapted from Stack Overflow answer by David Crow http://stackoverflow.com/a/43235
    // Question: Algorithm to randomly generate an aesthetically-pleasing color palette by Brian Gianforcaro
    // Method generates a pastel color, and optionally mixes it with another color
    // Method randomly generates a pastel color, and optionally mixes it with another color
    func generateRandomPastelColor(withMixedColor mixColor: UIColor?) -> UIColor {
    // Randomly generate number in closure
    let randomColorGenerator = { ()-> CGFloat in
  3. @JohnCoates JohnCoates revised this gist Jul 6, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions generateRandomPastelColor.swift
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    // Adapted from Stack Overflow answer by David Crow http://stackoverflow.com/a/43235
    // Question: Algorithm to randomly generate an aesthetically-pleasing color palette by Brian Gianforcaro
    // Method generates a pastel color, and optionally mixes it with another color
    func generateRandomPastelColor(withMixedColor mixColor: UIColor?) -> UIColor {
    // Randomly generate number in closure
    let randomColorGenerator = { ()-> CGFloat in
  4. @JohnCoates JohnCoates revised this gist Jul 6, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion generateRandomPastelColor.swift
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    // 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
    // Question: Algorithm to randomly generate an aesthetically-pleasing color palette by Brian Gianforcaro
    func generateRandomPastelColor(withMixedColor mixColor: UIColor?) -> UIColor {
    // Randomly generate number in closure
    let randomColorGenerator = { ()-> CGFloat in
  5. @JohnCoates JohnCoates renamed this gist Jul 6, 2015. 1 changed file with 0 additions and 0 deletions.
  6. @JohnCoates JohnCoates revised this gist Jul 6, 2015. 1 changed file with 21 additions and 21 deletions.
    42 changes: 21 additions & 21 deletions generateRandomPastelColor
    Original file line number Diff line number Diff line change
    @@ -1,24 +1,24 @@
    // 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
    }
    // 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()
    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;
    }
    // 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)

    return UIColor(red: red, green: green, blue: blue, alpha: 1)
    }
    red = (red + mixRed) / 2;
    green = (green + mixGreen) / 2;
    blue = (blue + mixBlue) / 2;
    }

    return UIColor(red: red, green: green, blue: blue, alpha: 1)
    }
  7. @JohnCoates JohnCoates created this gist Jul 6, 2015.
    24 changes: 24 additions & 0 deletions generateRandomPastelColor
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    // 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)
    }