Skip to content

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.

Copy link

@FreeAsInBeer FreeAsInBeer commented Feb 13, 2013

Thanks!

@HeshamMegid

This comment has been minimized.

Copy link

@HeshamMegid HeshamMegid commented Mar 6, 2013

Awesome, thanks.

@leejunkit

This comment has been minimized.

Copy link

@leejunkit leejunkit commented Mar 18, 2013

thank you so much for this.

@pixind

This comment has been minimized.

Copy link

@pixind pixind commented Mar 20, 2013

Thanks a lot!

@khjdot156

This comment has been minimized.

Copy link

@khjdot156 khjdot156 commented May 14, 2013

thanks you for your share

@mariushartig

This comment has been minimized.

Copy link

@mariushartig mariushartig commented May 24, 2013

thanks you :D

@joslinm

This comment has been minimized.

Copy link

@joslinm 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.

Copy link

@joyjding joyjding commented Jul 21, 2013

Thank you!

@xlsmearlx

This comment has been minimized.

Copy link

@xlsmearlx xlsmearlx commented Jul 24, 2013

Thanks! its great for debugging.

@yomajkel

This comment has been minimized.

Copy link

@yomajkel yomajkel commented Jul 25, 2013

Quite a snippet there :)

@richlu1018

This comment has been minimized.

Copy link

@richlu1018 richlu1018 commented Jul 29, 2013

Thx a lot for ur share~

@jaiversin

This comment has been minimized.

Copy link

@jaiversin jaiversin commented Jul 30, 2013

Great, thanks

@emarashliev

This comment has been minimized.

Copy link

@emarashliev emarashliev commented Aug 16, 2013

Great 1 ! Thx

@itayadler

This comment has been minimized.

Copy link

@itayadler itayadler commented Oct 16, 2013

Thanks man :)

@taufikobet

This comment has been minimized.

Copy link

@taufikobet taufikobet commented Nov 17, 2013

Awesome :)

@YOwatari

This comment has been minimized.

Copy link

@YOwatari YOwatari commented Dec 6, 2013

Awesome!

@dwilliamscode

This comment has been minimized.

Copy link

@dwilliamscode dwilliamscode commented Dec 19, 2013

Thanks!

@nitriques

This comment has been minimized.

Copy link

@nitriques nitriques commented Jan 4, 2014

Thanks!

@sleroux

This comment has been minimized.

Copy link

@sleroux sleroux commented Feb 13, 2014

Thanks!

@blacklee

This comment has been minimized.

Copy link

@blacklee blacklee commented Feb 15, 2014

Thanks!

@ariauakbar

This comment has been minimized.

Copy link

@ariauakbar ariauakbar commented Feb 21, 2014

Thanks!

@0oneo

This comment has been minimized.

Copy link

@0oneo 0oneo commented Mar 12, 2014

thanks!!!

@anvarazizov

This comment has been minimized.

Copy link

@anvarazizov anvarazizov commented Apr 18, 2014

Thanx, this is a great solution! :)

@guarani

This comment has been minimized.

Copy link

@guarani guarani commented May 17, 2014

Cheers!

@jwyjoy

This comment has been minimized.

Copy link

@jwyjoy jwyjoy commented May 27, 2014

Thanks!

@markawil

This comment has been minimized.

Copy link

@markawil markawil commented May 30, 2014

Boom, what I needed, thanks.

@LyahovchukS

This comment has been minimized.

Copy link

@LyahovchukS LyahovchukS commented Aug 19, 2014

Thanks!

@MangeshVyas

This comment has been minimized.

Copy link

@MangeshVyas MangeshVyas commented Aug 20, 2014

Thanks!

@aleclarson

This comment has been minimized.

Copy link

@aleclarson aleclarson commented Sep 14, 2014

short and sweet. thanks for saving me some time!

@KanybekMomukeyev

This comment has been minimized.

Copy link

@KanybekMomukeyev KanybekMomukeyev commented Sep 19, 2014

Great!
Thnks

@fobos000

This comment has been minimized.

Copy link

@fobos000 fobos000 commented Sep 27, 2014

Thank you!

@awedeebawe

This comment has been minimized.

Copy link

@awedeebawe awedeebawe commented Oct 8, 2014

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

@emadhegab

This comment has been minimized.

Copy link

@emadhegab emadhegab commented Nov 25, 2014

ho hooo

@alexpell00

This comment has been minimized.

Copy link

@alexpell00 alexpell00 commented Dec 18, 2014

Thanks

@romantomjak

This comment has been minimized.

Copy link

@romantomjak romantomjak commented Jan 16, 2015

Thanks! This is great for debugging!

@vladborovtsov

This comment has been minimized.

Copy link

@vladborovtsov vladborovtsov commented Feb 2, 2015

thanks so much!

@wagpinto

This comment has been minimized.

Copy link

@wagpinto wagpinto commented Feb 19, 2015

Obrigado

@vguerra

This comment has been minimized.

Copy link

@vguerra 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.

Copy link

@sapjjr 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.

Copy link

@ghost ghost commented Apr 6, 2015

Beauty!

@colasbd

This comment has been minimized.

Copy link

@colasbd colasbd commented Apr 30, 2015

Thanks !

@JayachandraA

This comment has been minimized.

Copy link

@JayachandraA 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.

Copy link

@michelgoni michelgoni commented Jun 8, 2015

Thank you very much!

@Pastez

This comment has been minimized.

Copy link

@Pastez Pastez commented Jun 24, 2015

Thanks

@jamescmartinez

This comment has been minimized.

Copy link

@jamescmartinez 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.

Copy link

@jxdwinter jxdwinter commented Jul 22, 2015

Thanks!

@ardarda

This comment has been minimized.

Copy link

@ardarda ardarda commented Aug 4, 2015

Thx!

@karonator

This comment has been minimized.

Copy link

@karonator karonator commented Aug 31, 2015

Nice, thank you!

@domagojb

This comment has been minimized.

Copy link

@domagojb domagojb commented Sep 1, 2015

Nice thanks

@VladKorzun

This comment has been minimized.

Copy link

@VladKorzun VladKorzun commented Nov 7, 2015

Thanks a lot

@manishios

This comment has been minimized.

Copy link

@manishios 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.

Copy link

@SudoKel SudoKel commented Feb 19, 2017

Thank you! 👍

@enigmatic7earth

This comment has been minimized.

Copy link

@enigmatic7earth 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.

Copy link

@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
You can’t perform that action at this time.