An attractive color generator for Swift. Ported from randomColor.js.

Overview

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.

Install

This framework supports Swift 4.0/4.2/5.0 and above.

Swift Package Manager

Just like using any other Swift Package, add this repo to the dependencies section and depend it in your target:

let package = Package(
    name: "MyApp",
    //...
    dependencies: [
        .package(url: "https://github.com/onevcat/RandomColorSwift.git", .upToNextMajor(from: "2.0.0")),
    ],
    targets: [
        .target(
            name: "MyApp",
            dependencies: ["RandomColor"]),
    ]

Or, use the Swift Package Manager integrated in Xcode 11 or above to add this package as a dependency to your app.

CocoaPods

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

Carthage is also supported:

github "onevcat/RandomColorSwift"

Manually

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.

Example

import RandomColor

// 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.

Options

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 .monochrome, .red, .orange, .yellow, .green, .blue, .purple, .pink, .random or .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 .bright, .light, .dark or .random.

Count – An Int which specifies the number of colors to generate.

Acknowledgements

Thanks for David Merfield bringing us randomColor.js, which is a great utility.

The demo project is using Chirag Mehta's Name the Color JavaScript library to extract name of color.

License

This project is licensed under the terms of the MIT license.

You might also like...
UIGradient - A simple and powerful library for using gradient layer, image, color
UIGradient - A simple and powerful library for using gradient layer, image, color

UIGradient is now available on CocoaPods. Simply add the following to your project Podfile, and you'll be good to go.

A tool to calculate the color ratio of UIImage in iOS.
A tool to calculate the color ratio of UIImage in iOS.

UIImageColorRatio A tool to calculate the color ratio of UIImage in iOS. How to use UIImageColorRatio Get the color ratio of UIImage. let image = ...

A UIColor extension with CSS3 Color names.
A UIColor extension with CSS3 Color names.

CSS3ColorsSwift Overview CSS3ColorsSwift provides a UIColor extension with Web Color names. Demo Run the demo project in the Demo directory without ca

A beautiful set of predefined colors and a set of color methods to make your iOS/OSX development life easier.
A beautiful set of predefined colors and a set of color methods to make your iOS/OSX development life easier.

Installation Drag the included Colours.h and Colours.m files into your project. They are located in the top-level directory. You can see a demo of how

Generate color based on the given string.
Generate color based on the given string.

PFColorHash Swift 4.0 SUPPORT! Generate color based on the given string. Thanks to color-hash. Usage Basic let colorHash = PFColorHash() // in HSL, H

A simple UIColor category to get color with hex code.
A simple UIColor category to get color with hex code.

TFTColor A simple UIColor library to get UIColor object from RGB hex string/value, CMYK hex string/value or CMYK base component values. You can also r

Overrides macOS external display EDIDs to force RGB color

edidiotic edidiotic creates EDID overrides for external displays on macOS that set the display type to RGB color and removes all extension blocks. Thi

HEX color handling as an extension for UIColor

HEX color handling as an extension for UIColor. Written in Swift

ColorSlider is a SwiftUI view that displays a color slider
ColorSlider is a SwiftUI view that displays a color slider

ColorSlider is a SwiftUI view that displays a color slider. It is used to dynamically select a color from a range of colors or grayscale.

Comments
  • SWIFT_VERSION '3.0' is unsupported

    SWIFT_VERSION '3.0' is unsupported

    SWIFT_VERSION '3.0' is unsupported, supported versions are: 4.0, 4.2, 5.0.

    Also unable to convert to Swift 5.0, because Xcode 11 no longer supports this conversion. Does this mean this library is officially deprecated? I see previous issues and fixes, but I'm still getting this error. Installing manually didn't work, then installing with CocoaPods also did not work.

    opened by Coliinnn 1
  • How can I make the same colors?

    How can I make the same colors?

    Hello. I'm making a colorBlind game. How can I make the same colors in array with one excess (which is lighter than others in array, for example), using your library? Thank you!

    opened by IKorabel 1
Owner
Wei Wang
Developer, creator, proud father.
Wei Wang
ColorWheel Test - An attempt at creating a Color Wheel to be utilized for color picking in SwiftUI utlizing various tutuorials on youtube

This code focuses on creating a Color Wheel in which a user will be able to select a desired color to then be implemented in another project that will display that color in an LED connected to an arduino

Gerardo Cerpa 0 Jan 15, 2022
ChromaColorPicker - An intuitive HSB color picker built in Swift

An intuitive HSB color picker built in Swift. Supports multiple selection handles and is customizable to your needs.

Jonathan Cardasis 536 Dec 29, 2022
HEX color handling as an extension for UIColor. Written in Swift.

SwiftHEXColors HEX color handling as an extension for UIColor. Written in Swift.

Thi Doãn 692 Dec 22, 2022
Color framework for Swift & Objective-C (Gradient colors, hexcode support, colors from images & more).

Swift 3 To use the Swift 3 version, add this to your Podfile (until 2.2 or higher is released): pod 'ChameleonFramework/Swift', :git => 'https://githu

Vicc Alexander 12.5k Dec 27, 2022
Flat UI color palette helpers written in Swift.

FlatUIColors (swift) install Make sure you have the latest version of CocoaPods (gem install cocoapods) that has Swift support. At the time of this wr

Bryn Bellomy 172 Dec 6, 2022
Google Material Color Palette in Swift

Google Material Color in Swift Defined Google Material Color value ready to use in Swift refer to Google Material Design in Style/Color section ###How

Todsaporn Banjerdkit 48 Oct 30, 2019
A Lightweight But Powerful Color Kit (Swift)

BCColor A lightweight but powerful color kit (Swift) Features Pick Colors From Image Generate Monochrome Image Support Hex Color Style Lighten / Darke

Xiaobo Zhang 418 Nov 25, 2022
Aesthetic color-scheme generation written in Swift

Lorikeet Lightweight framework for generating visually aesthetic color-schemes in Swift Requirements UIKit Features What can Lorikeet do for you Calcu

Þorvaldur Rúnarsson 31 Jun 21, 2022
SheetyColors is an action sheet styled color picker for iOS

?? Based on UIAlertController: The SheetyColors API is based on UIKit's UIAlertController. Simply add buttons to it as you would for any other Action Sheet by defining UIAlertAction instances. Therefore, it nicely integrates with the look & feel of all other native system dialogs. However, you can also chose to use the color picker it self without an action sheet.

Christoph Wendt 102 Nov 10, 2022
Convenience methods for creating color using RGBA hex string.

UIColor+Hex, now Swift. Convenience method for creating autoreleased color using RGBA hex string.

R0CKSTAR 1.2k Dec 23, 2022