Instantly share code, notes, and snippets.

Embed
What would you like to do?
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 commented Feb 13, 2013

Thanks!

@HeshamMegid

This comment has been minimized.

Show comment
Hide comment
@HeshamMegid

HeshamMegid Mar 6, 2013

Awesome, thanks.

HeshamMegid commented Mar 6, 2013

Awesome, thanks.

@leejunkit

This comment has been minimized.

Show comment
Hide comment
@leejunkit

leejunkit Mar 18, 2013

thank you so much for this.

leejunkit commented Mar 18, 2013

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

khjdot156 commented May 14, 2013

thanks you for your share

@Vostro162

This comment has been minimized.

Show comment
Hide comment
@Vostro162

Vostro162 May 24, 2013

thanks you :D

Vostro162 commented May 24, 2013

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!

joyjding commented Jul 21, 2013

Thank you!

@xlsmearlx

This comment has been minimized.

Show comment
Hide comment
@xlsmearlx

xlsmearlx Jul 24, 2013

Thanks! its great for debugging.

xlsmearlx commented Jul 24, 2013

Thanks! its great for debugging.

@yomajkel

This comment has been minimized.

Show comment
Hide comment
@yomajkel

yomajkel Jul 25, 2013

Quite a snippet there :)

yomajkel commented Jul 25, 2013

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~

richlu1018 commented Jul 29, 2013

Thx a lot for ur share~

@jaiversin

This comment has been minimized.

Show comment
Hide comment
@jaiversin

jaiversin Jul 30, 2013

Great, thanks

jaiversin commented Jul 30, 2013

Great, thanks

@emarashliev

This comment has been minimized.

Show comment
Hide comment
@emarashliev

emarashliev Aug 16, 2013

Great 1 ! Thx

emarashliev commented Aug 16, 2013

Great 1 ! Thx

@itayadler

This comment has been minimized.

Show comment
Hide comment
@itayadler

itayadler Oct 16, 2013

Thanks man :)

itayadler commented Oct 16, 2013

Thanks man :)

@taufikobet

This comment has been minimized.

Show comment
Hide comment
@taufikobet

taufikobet commented Nov 17, 2013

Awesome :)

@YOwatari

This comment has been minimized.

Show comment
Hide comment
@YOwatari

YOwatari commented Dec 6, 2013

Awesome!

@dwilliamscode

This comment has been minimized.

Show comment
Hide comment
@dwilliamscode

dwilliamscode commented Dec 19, 2013

Thanks!

@nitriques

This comment has been minimized.

Show comment
Hide comment
@nitriques

nitriques commented Jan 4, 2014

Thanks!

@sleroux

This comment has been minimized.

Show comment
Hide comment
@sleroux

sleroux commented Feb 13, 2014

Thanks!

@BlackLee

This comment has been minimized.

Show comment
Hide comment
@BlackLee

BlackLee commented Feb 15, 2014

Thanks!

@ariauakbar

This comment has been minimized.

Show comment
Hide comment
@ariauakbar

ariauakbar commented Feb 21, 2014

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! :)

anvarazizov commented Apr 18, 2014

Thanx, this is a great solution! :)

@guarani

This comment has been minimized.

Show comment
Hide comment
@guarani

guarani commented May 17, 2014

Cheers!

@jwyjoy

This comment has been minimized.

Show comment
Hide comment
@jwyjoy

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.

markawil commented May 30, 2014

Boom, what I needed, thanks.

@LyahovchukS

This comment has been minimized.

Show comment
Hide comment
@LyahovchukS

LyahovchukS commented Aug 19, 2014

Thanks!

@MangeshVyas

This comment has been minimized.

Show comment
Hide comment
@MangeshVyas

MangeshVyas commented Aug 20, 2014

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!

aleclarson commented Sep 14, 2014

short and sweet. thanks for saving me some time!

@KanybekMomukeyev

This comment has been minimized.

Show comment
Hide comment
@KanybekMomukeyev

KanybekMomukeyev commented Sep 19, 2014

Great!
Thnks

@fobos000

This comment has been minimized.

Show comment
Hide comment
@fobos000

fobos000 Sep 27, 2014

Thank you!

fobos000 commented Sep 27, 2014

Thank you!

@awedeebawe

This comment has been minimized.

Show comment
Hide comment
@awedeebawe

awedeebawe Oct 8, 2014

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

awedeebawe commented Oct 8, 2014

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

@emadhegab

This comment has been minimized.

Show comment
Hide comment
@emadhegab

emadhegab commented Nov 25, 2014

ho hooo

@alexpell00

This comment has been minimized.

Show comment
Hide comment
@alexpell00

alexpell00 commented Dec 18, 2014

Thanks

@romantomjak

This comment has been minimized.

Show comment
Hide comment
@romantomjak

romantomjak Jan 16, 2015

Thanks! This is great for debugging!

romantomjak commented Jan 16, 2015

Thanks! This is great for debugging!

@vladborovtsov

This comment has been minimized.

Show comment
Hide comment
@vladborovtsov

vladborovtsov Feb 2, 2015

thanks so much!

vladborovtsov commented Feb 2, 2015

thanks so much!

@wagppinto

This comment has been minimized.

Show comment
Hide comment
@wagppinto

wagppinto commented Feb 19, 2015

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 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];

JayachandraA commented 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];
@michelgoni

This comment has been minimized.

Show comment
Hide comment
@michelgoni

michelgoni Jun 8, 2015

Thank you very much!

michelgoni commented Jun 8, 2015

Thank you very much!

@Pastez

This comment has been minimized.

Show comment
Hide comment
@Pastez

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)

jamescmartinez commented 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)
@jxdwinter

This comment has been minimized.

Show comment
Hide comment
@jxdwinter

jxdwinter commented Jul 22, 2015

Thanks!

@ardarda

This comment has been minimized.

Show comment
Hide comment
@ardarda

ardarda commented Aug 4, 2015

Thx!

@karonator

This comment has been minimized.

Show comment
Hide comment
@karonator

karonator Aug 31, 2015

Nice, thank you!

karonator commented Aug 31, 2015

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

VladKorzun commented Nov 7, 2015

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?

manishios commented Mar 18, 2016

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

enigmatic7earth commented Mar 29, 2017

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 commented Apr 4, 2017

Great...!

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