Random Color Swift
Inspired by David Merfield's randomColor.js. It is a ported version to Swift. You can use the library to generate attractive random colors on iOS or macOS.
See the demo and site to know why does this exist.
This framework supports Swift 3.0 / Xcode 8 and above. If you need an earlier version for Swift 2, use version 0.1.0 instead.
The easiest way to use
RandomColor is installing it by CocoaPods. Add these lines to your Podfile:
source 'https://github.com/CocoaPods/Specs.git' platform :ios, '8.0' # platform :osx, '10.10' use_frameworks! pod 'RandomColorSwift'
Carthage is also supported:
If you need to support iOS 7.x, you will need to add the library manually into your project since dynamic framework is not supported for iOS 7.
Clone this repo and throw the source files under
RandomColor folder into your project to use it.
import RandomColorSwift // Returns a UIColor or NSColor object for an attractive color let color = randomColor() // Returns an array of ten green colors let greenColors = randomColors(count: 10, hue: .Green) // Returns a color for light blue let lightBlurColor = randomColor(hue: .Blue, luminosity: .Light) // Returns a color for a 'truly random' color let randomColor = randomColor(hue: .Random, luminosity: .Random) // Returns an array of ten dark pink colors let darkPinkColors = randomColors(count: 10, hue: .Pink, luminosity: .Dark) // Returns an array of twenty colors at hue of 120 let colors = randomColors(count: 20, hue: .Value(120), luminosity: .Random)
There is also a demo project in this repo.
You can pass an options object to influence the type of color it produces. The options object accepts the following properties:
Hue – Controls the hue of the generated color. Possible hue values for colors are
.value(Int). If you use
.value(Int), you should pass an
Int between 0 and 360.
Luminosity – Controls the luminosity of the generated color. You can pass
Count – An
Int which specifies the number of colors to generate.
Thanks for David Merfield bringing us randomColor.js, which is a great utility.
This project is licensed under the terms of the MIT license.