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.

FreeAsInBeer commented Feb 13, 2013

Thanks!

@HeshamMegid

This comment has been minimized.

HeshamMegid commented Mar 6, 2013

Awesome, thanks.

@leejunkit

This comment has been minimized.

leejunkit commented Mar 18, 2013

thank you so much for this.

@pixind

This comment has been minimized.

pixind commented Mar 20, 2013

Thanks a lot!

@khjdot156

This comment has been minimized.

khjdot156 commented May 14, 2013

thanks you for your share

@Vostro162

This comment has been minimized.

Vostro162 commented May 24, 2013

thanks you :D

@joslinm

This comment has been minimized.

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.

joyjding commented Jul 21, 2013

Thank you!

@xlsmearlx

This comment has been minimized.

xlsmearlx commented Jul 24, 2013

Thanks! its great for debugging.

@yomajkel

This comment has been minimized.

yomajkel commented Jul 25, 2013

Quite a snippet there :)

@richlu1018

This comment has been minimized.

richlu1018 commented Jul 29, 2013

Thx a lot for ur share~

@jaiversin

This comment has been minimized.

jaiversin commented Jul 30, 2013

Great, thanks

@emarashliev

This comment has been minimized.

emarashliev commented Aug 16, 2013

Great 1 ! Thx

@itayadler

This comment has been minimized.

itayadler commented Oct 16, 2013

Thanks man :)

@taufikobet

This comment has been minimized.

taufikobet commented Nov 17, 2013

Awesome :)

@YOwatari

This comment has been minimized.

YOwatari commented Dec 6, 2013

Awesome!

@dwilliamscode

This comment has been minimized.

dwilliamscode commented Dec 19, 2013

Thanks!

@nitriques

This comment has been minimized.

nitriques commented Jan 4, 2014

Thanks!

@sleroux

This comment has been minimized.

sleroux commented Feb 13, 2014

Thanks!

@blacklee

This comment has been minimized.

blacklee commented Feb 15, 2014

Thanks!

@ariauakbar

This comment has been minimized.

ariauakbar commented Feb 21, 2014

Thanks!

@0oneo

This comment has been minimized.

0oneo commented Mar 12, 2014

thanks!!!

@anvarazizov

This comment has been minimized.

anvarazizov commented Apr 18, 2014

Thanx, this is a great solution! :)

@guarani

This comment has been minimized.

guarani commented May 17, 2014

Cheers!

@jwyjoy

This comment has been minimized.

jwyjoy commented May 27, 2014

Thanks!

@markawil

This comment has been minimized.

markawil commented May 30, 2014

Boom, what I needed, thanks.

@LyahovchukS

This comment has been minimized.

LyahovchukS commented Aug 19, 2014

Thanks!

@MangeshVyas

This comment has been minimized.

MangeshVyas commented Aug 20, 2014

Thanks!

@aleclarson

This comment has been minimized.

aleclarson commented Sep 14, 2014

short and sweet. thanks for saving me some time!

@KanybekMomukeyev

This comment has been minimized.

KanybekMomukeyev commented Sep 19, 2014

Great!
Thnks

@fobos000

This comment has been minimized.

fobos000 commented Sep 27, 2014

Thank you!

@awedeebawe

This comment has been minimized.

awedeebawe commented Oct 8, 2014

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

@emadhegab

This comment has been minimized.

emadhegab commented Nov 25, 2014

ho hooo

@alexpell00

This comment has been minimized.

alexpell00 commented Dec 18, 2014

Thanks

@romantomjak

This comment has been minimized.

romantomjak commented Jan 16, 2015

Thanks! This is great for debugging!

@vladborovtsov

This comment has been minimized.

vladborovtsov commented Feb 2, 2015

thanks so much!

@wagppinto

This comment has been minimized.

wagppinto commented Feb 19, 2015

Obrigado

@vguerra

This comment has been minimized.

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.

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.

ghost commented Apr 6, 2015

Beauty!

@colasbd

This comment has been minimized.

colasbd commented Apr 30, 2015

Thanks !

@JayachandraA

This comment has been minimized.

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.

michelgoni commented Jun 8, 2015

Thank you very much!

@Pastez

This comment has been minimized.

Pastez commented Jun 24, 2015

Thanks

@jamescmartinez

This comment has been minimized.

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.

jxdwinter commented Jul 22, 2015

Thanks!

@ardarda

This comment has been minimized.

ardarda commented Aug 4, 2015

Thx!

@karonator

This comment has been minimized.

karonator commented Aug 31, 2015

Nice, thank you!

@domagojb

This comment has been minimized.

domagojb commented Sep 1, 2015

Nice thanks

@VladKorzun

This comment has been minimized.

VladKorzun commented Nov 7, 2015

Thanks a lot

@manishios

This comment has been minimized.

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.

SudoKel commented Feb 19, 2017

Thank you! 👍

@enigmatic7earth

This comment has been minimized.

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.

JAGATBARAIYA commented Apr 4, 2017

Great...!

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