Create a gist now

Instantly share code, notes, and snippets.

Embed
Generate a random color (UIColor) in Objective-C
/*
Distributed under The MIT License:
http://opensource.org/licenses/mit-license.php
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
CGFloat hue = ( arc4random() % 256 / 256.0 ); // 0.0 to 1.0
CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from white
CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from black
UIColor *color = [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
@FreeAsInBeer

This comment has been minimized.

Show comment
Hide comment
@FreeAsInBeer

FreeAsInBeer Feb 13, 2013

Thanks!

Thanks!

@HeshamMegid

This comment has been minimized.

Show comment
Hide comment
@HeshamMegid

HeshamMegid Mar 6, 2013

Awesome, thanks.

Awesome, thanks.

@leejunkit

This comment has been minimized.

Show comment
Hide comment
@leejunkit

leejunkit Mar 18, 2013

thank you so much for this.

thank you so much for this.

@pixind

This comment has been minimized.

Show comment
Hide comment
@pixind

pixind Mar 20, 2013

Thanks a lot!

pixind commented Mar 20, 2013

Thanks a lot!

@khjdot156

This comment has been minimized.

Show comment
Hide comment
@khjdot156

khjdot156 May 14, 2013

thanks you for your share

thanks you for your share

@Vostro162

This comment has been minimized.

Show comment
Hide comment
@Vostro162

Vostro162 May 24, 2013

thanks you :D

thanks you :D

@joslinm

This comment has been minimized.

Show comment
Hide comment
@joslinm

joslinm Jul 1, 2013

thank you this is great for debugging. i set background color to random color when laying out many subviews

joslinm commented Jul 1, 2013

thank you this is great for debugging. i set background color to random color when laying out many subviews

@joyjding

This comment has been minimized.

Show comment
Hide comment
@joyjding

joyjding Jul 21, 2013

Thank you!

Thank you!

@xlsmearlx

This comment has been minimized.

Show comment
Hide comment
@xlsmearlx

xlsmearlx Jul 24, 2013

Thanks! its great for debugging.

Thanks! its great for debugging.

@yomajkel

This comment has been minimized.

Show comment
Hide comment
@yomajkel

yomajkel Jul 25, 2013

Quite a snippet there :)

Quite a snippet there :)

@richlu1018

This comment has been minimized.

Show comment
Hide comment
@richlu1018

richlu1018 Jul 29, 2013

Thx a lot for ur share~

Thx a lot for ur share~

@jaiversin

This comment has been minimized.

Show comment
Hide comment
@jaiversin

jaiversin Jul 30, 2013

Great, thanks

Great, thanks

@emarashliev

This comment has been minimized.

Show comment
Hide comment
@emarashliev

emarashliev Aug 16, 2013

Great 1 ! Thx

Great 1 ! Thx

@itayadler

This comment has been minimized.

Show comment
Hide comment
@itayadler

itayadler Oct 16, 2013

Thanks man :)

Thanks man :)

@taufikobet

This comment has been minimized.

Show comment
Hide comment
@taufikobet

taufikobet Nov 17, 2013

Awesome :)

Awesome :)

@YOwatari

This comment has been minimized.

Show comment
Hide comment
@YOwatari

YOwatari Dec 6, 2013

Awesome!

YOwatari commented Dec 6, 2013

Awesome!

@dwilliamscode

This comment has been minimized.

Show comment
Hide comment
@dwilliamscode

dwilliamscode Dec 19, 2013

Thanks!

Thanks!

@nitriques

This comment has been minimized.

Show comment
Hide comment
@nitriques

nitriques Jan 4, 2014

Thanks!

Thanks!

@sleroux

This comment has been minimized.

Show comment
Hide comment
@sleroux

sleroux Feb 13, 2014

Thanks!

sleroux commented Feb 13, 2014

Thanks!

@BlackLee

This comment has been minimized.

Show comment
Hide comment
@BlackLee

BlackLee Feb 15, 2014

Thanks!

Thanks!

@ariauakbar

This comment has been minimized.

Show comment
Hide comment
@ariauakbar

ariauakbar Feb 21, 2014

Thanks!

Thanks!

@0oneo

This comment has been minimized.

Show comment
Hide comment
@0oneo

0oneo Mar 12, 2014

thanks!!!

0oneo commented Mar 12, 2014

thanks!!!

@anvarazizov

This comment has been minimized.

Show comment
Hide comment
@anvarazizov

anvarazizov Apr 18, 2014

Thanx, this is a great solution! :)

Thanx, this is a great solution! :)

@guarani

This comment has been minimized.

Show comment
Hide comment
@guarani

guarani May 17, 2014

Cheers!

guarani commented May 17, 2014

Cheers!

@jwyjoy

This comment has been minimized.

Show comment
Hide comment
@jwyjoy

jwyjoy May 27, 2014

Thanks!

jwyjoy commented May 27, 2014

Thanks!

@markawil

This comment has been minimized.

Show comment
Hide comment
@markawil

markawil May 30, 2014

Boom, what I needed, thanks.

Boom, what I needed, thanks.

@LyahovchukS

This comment has been minimized.

Show comment
Hide comment
@LyahovchukS

LyahovchukS Aug 19, 2014

Thanks!

Thanks!

@MangeshVyas

This comment has been minimized.

Show comment
Hide comment
@MangeshVyas

MangeshVyas Aug 20, 2014

Thanks!

Thanks!

@aleclarson

This comment has been minimized.

Show comment
Hide comment
@aleclarson

aleclarson Sep 14, 2014

short and sweet. thanks for saving me some time!

short and sweet. thanks for saving me some time!

@KanybekMomukeyev

This comment has been minimized.

Show comment
Hide comment
@KanybekMomukeyev

KanybekMomukeyev Sep 19, 2014

Great!
Thnks

Great!
Thnks

@fobos000

This comment has been minimized.

Show comment
Hide comment
@fobos000

fobos000 Sep 27, 2014

Thank you!

Thank you!

@awedeebawe

This comment has been minimized.

Show comment
Hide comment
@awedeebawe

awedeebawe Oct 8, 2014

woo hoo :)))) Thank you very much :)

woo hoo :)))) Thank you very much :)

@emadhegab

This comment has been minimized.

Show comment
Hide comment
@emadhegab

emadhegab Nov 25, 2014

ho hooo

ho hooo

@alexpell00

This comment has been minimized.

Show comment
Hide comment
@alexpell00

alexpell00 Dec 18, 2014

Thanks

Thanks

@romantomjak

This comment has been minimized.

Show comment
Hide comment
@romantomjak

romantomjak Jan 16, 2015

Thanks! This is great for debugging!

Thanks! This is great for debugging!

@vladborovtsov

This comment has been minimized.

Show comment
Hide comment
@vladborovtsov

vladborovtsov Feb 2, 2015

thanks so much!

thanks so much!

@wagppinto

This comment has been minimized.

Show comment
Hide comment
@wagppinto

wagppinto Feb 19, 2015

Obrigado

Obrigado

@vguerra

This comment has been minimized.

Show comment
Hide comment
@vguerra

vguerra Mar 20, 2015

Thank you for the snippet @kylefox ... I think that on line #26 one should divide by 255 .. mod 256 yields a number between 0 and 255 otherwise you'll never get to 1.0.

vguerra commented Mar 20, 2015

Thank you for the snippet @kylefox ... I think that on line #26 one should divide by 255 .. mod 256 yields a number between 0 and 255 otherwise you'll never get to 1.0.

@sapjjr

This comment has been minimized.

Show comment
Hide comment
@sapjjr

sapjjr Mar 22, 2015

Mine was a bit more long but it work

func changeBackgroundColor() {
// this has been taken from stackoverflow.com/questions/27390234/changing-main-view-controller-background-color-from-value-in-nsuserdefaults
//which gave me idea on how the background color works
// initialise defaults
let defaults = NSUserDefaults.standardUserDefaults()
// Start color off with a default value
var color = UIColor.grayColor()
var backcolor = 0

    if let backColor = defaults.objectForKey("backColor") as? String {
        // accessing the variable
        switch backColor {
        case "1": color = UIColor.redColor()
        case "2": color = UIColor.greenColor()
        case "3": color = UIColor.cyanColor()
        case "4": color = UIColor.yellowColor()
        case "5": color = UIColor.grayColor()
        case "6": color = UIColor.orangeColor()
        default: break




        }
    }

    //this has been lifted from :
    //stackoverflow.com/questions/24007129/how-does-one-generate-a-random-number-in-apples-swift-language
    //to give ideas on how random numbers work
    // and also cast from Int to String via
    //  stackoverflow.com/questions/24161336/convert-int-to-string-in-swift
    //to give ideas on how random numbers work


    NSUserDefaults.standardUserDefaults().setObject( "\(String (Int(arc4random_uniform(UInt32(6)+1))))", forKey: "backColor") //saving a random number and calling it backcolor
    NSUserDefaults.standardUserDefaults().synchronize() //
    view.backgroundColor = color

}

sapjjr commented Mar 22, 2015

Mine was a bit more long but it work

func changeBackgroundColor() {
// this has been taken from stackoverflow.com/questions/27390234/changing-main-view-controller-background-color-from-value-in-nsuserdefaults
//which gave me idea on how the background color works
// initialise defaults
let defaults = NSUserDefaults.standardUserDefaults()
// Start color off with a default value
var color = UIColor.grayColor()
var backcolor = 0

    if let backColor = defaults.objectForKey("backColor") as? String {
        // accessing the variable
        switch backColor {
        case "1": color = UIColor.redColor()
        case "2": color = UIColor.greenColor()
        case "3": color = UIColor.cyanColor()
        case "4": color = UIColor.yellowColor()
        case "5": color = UIColor.grayColor()
        case "6": color = UIColor.orangeColor()
        default: break




        }
    }

    //this has been lifted from :
    //stackoverflow.com/questions/24007129/how-does-one-generate-a-random-number-in-apples-swift-language
    //to give ideas on how random numbers work
    // and also cast from Int to String via
    //  stackoverflow.com/questions/24161336/convert-int-to-string-in-swift
    //to give ideas on how random numbers work


    NSUserDefaults.standardUserDefaults().setObject( "\(String (Int(arc4random_uniform(UInt32(6)+1))))", forKey: "backColor") //saving a random number and calling it backcolor
    NSUserDefaults.standardUserDefaults().synchronize() //
    view.backgroundColor = color

}
@ghost

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost Apr 6, 2015

Beauty!

ghost commented Apr 6, 2015

Beauty!

@colasbd

This comment has been minimized.

Show comment
Hide comment
@colasbd

colasbd Apr 30, 2015

Thanks !

colasbd commented Apr 30, 2015

Thanks !

@JayachandraA

This comment has been minimized.

Show comment
Hide comment
@JayachandraA

JayachandraA May 26, 2015

Here is the snippet 
CGFloat redLevel    = rand() / (float) RAND_MAX;
CGFloat greenLevel  = rand() / (float) RAND_MAX;
CGFloat blueLevel   = rand() / (float) RAND_MAX;

self.view.backgroundColor = [UIColor colorWithRed: redLevel
                                            green: greenLevel
                                             blue: blueLevel
                                            alpha: 1.0];
Here is the snippet 
CGFloat redLevel    = rand() / (float) RAND_MAX;
CGFloat greenLevel  = rand() / (float) RAND_MAX;
CGFloat blueLevel   = rand() / (float) RAND_MAX;

self.view.backgroundColor = [UIColor colorWithRed: redLevel
                                            green: greenLevel
                                             blue: blueLevel
                                            alpha: 1.0];
@michelgoni

This comment has been minimized.

Show comment
Hide comment
@michelgoni

michelgoni Jun 8, 2015

Thank you very much!

Thank you very much!

@Pastez

This comment has been minimized.

Show comment
Hide comment
@Pastez

Pastez Jun 24, 2015

Thanks

Pastez commented Jun 24, 2015

Thanks

@jamescmartinez

This comment has been minimized.

Show comment
Hide comment
@jamescmartinez

jamescmartinez Jul 5, 2015

Swift:

    let hue = CGFloat(Double(arc4random() % 256) / 256.0) // 0.0 to 1.0
    let saturation = CGFloat(Double(arc4random() % 128) / 266.0 + 0.5) // 0.5 to 1.0, away from white
    let brightness = CGFloat(Double(arc4random() % 128) / 256.0 + 0.5) // 0.5 to 1.0, away from black
    return UIColor(hue: hue, saturation: saturation, brightness: brightness, alpha: 1.0)

Swift:

    let hue = CGFloat(Double(arc4random() % 256) / 256.0) // 0.0 to 1.0
    let saturation = CGFloat(Double(arc4random() % 128) / 266.0 + 0.5) // 0.5 to 1.0, away from white
    let brightness = CGFloat(Double(arc4random() % 128) / 256.0 + 0.5) // 0.5 to 1.0, away from black
    return UIColor(hue: hue, saturation: saturation, brightness: brightness, alpha: 1.0)
@jxdwinter

This comment has been minimized.

Show comment
Hide comment
@jxdwinter

jxdwinter Jul 22, 2015

Thanks!

Thanks!

@ardarda

This comment has been minimized.

Show comment
Hide comment
@ardarda

ardarda Aug 4, 2015

Thx!

ardarda commented Aug 4, 2015

Thx!

@karonator

This comment has been minimized.

Show comment
Hide comment
@karonator

karonator Aug 31, 2015

Nice, thank you!

Nice, thank you!

@domagojb

This comment has been minimized.

Show comment
Hide comment
@domagojb

domagojb Sep 1, 2015

Nice thanks

domagojb commented Sep 1, 2015

Nice thanks

@VladKorzun

This comment has been minimized.

Show comment
Hide comment
@VladKorzun

VladKorzun Nov 7, 2015

Thanks a lot

Thanks a lot

@manishios

This comment has been minimized.

Show comment
Hide comment
@manishios

manishios Mar 18, 2016

I have a color code like "15233421". How can I consider alpha value while using any of the solution?

I have a color code like "15233421". How can I consider alpha value while using any of the solution?

@SudoKel

This comment has been minimized.

Show comment
Hide comment
@SudoKel

SudoKel Feb 19, 2017

Thank you! 👍

SudoKel commented Feb 19, 2017

Thank you! 👍

@enigmatic7earth

This comment has been minimized.

Show comment
Hide comment
@enigmatic7earth

enigmatic7earth Mar 29, 2017

Thank you so much for this share. It helps people like me who are new to iOS programming in ObjC

Thank you so much for this share. It helps people like me who are new to iOS programming in ObjC

@JAGATBARAIYA

This comment has been minimized.

Show comment
Hide comment
@JAGATBARAIYA

JAGATBARAIYA Apr 4, 2017

Great...!

Great...!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment