69

I am working on an iPhone application which uses various colors. When user selects the particular color button I set drawing color accordingly. I am getting the color for some but in the most of the cases I am getting white color.

Here is my code:

-(IBAction)colorSelected:(UIButton *)sender
{

switch (sender.tag) 
{
    case 1:
        self.drawcolor= [UIColor colorWithRed:200 green:191 blue:231 alpha:1];
        break;            
    case 2:
        self.drawcolor= [UIColor colorWithRed:163 green:73 blue:164 alpha:1];
        break;  
    case 3:
        self.drawcolor= [UIColor colorWithRed:112 green:146 blue:76 alpha:1];
        break;  
    case 4:
        self.drawcolor= [UIColor colorWithRed:63 green:72 blue:204 alpha:1];
        break;  
    case 5:
        self.drawcolor= [UIColor colorWithRed:153 green:217 blue:234 alpha:1];
        break;  
    case 6:
        self.drawcolor= [UIColor colorWithRed:0 green:162 blue:232 alpha:1];
        break;          
    case 7:
        self.drawcolor= [UIColor colorWithRed:181 green:230 blue:29 alpha:1];
        break;  
    case 8:
        self.drawcolor= [UIColor colorWithRed:34 green:177 blue:76 alpha:1];
        break;          
    case 9:
        self.drawcolor= [UIColor colorWithRed:239 green:228 blue:176 alpha:1];
        break;          
    case 10:
        self.drawcolor= [UIColor colorWithRed:255 green:201 blue:0 alpha:1];
        break;          
    case 11:
        self.drawcolor= [UIColor colorWithRed:255 green:201 blue:14 alpha:1];
        break;          
    case 12:
        self.drawcolor= [UIColor colorWithRed:237 green:28 blue:36 alpha:1];
        break;          
    case 13:
        self.drawcolor= [UIColor colorWithRed:255 green:127 blue:39 alpha:1];
        break;          
    case 14:
        self.drawcolor= [UIColor colorWithRed:255 green:174 blue:201 alpha:1];
        break;          
    case 15:
        self.drawcolor= [UIColor colorWithRed:185 green:122 blue:87 alpha:1];
        break;          
    case 16:
        self.drawcolor= [UIColor colorWithRed:136 green:0 blue:21 alpha:1];
        break;          
    case 17:
        self.drawcolor= [UIColor colorWithRed:195 green:195 blue:195 alpha:1];
        break;          
    case 18:
        self.drawcolor= [UIColor colorWithRed:127 green:127 blue:127 alpha:1];
        break;          
    case 19:
        self.drawcolor= [UIColor colorWithRed:255 green:255 blue:255 alpha:1];
        break;
    case 20:
        self.drawcolor= [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
        break;  
    default:
        break;
}

self.preColor=self.drawcolor;
self.lineWidth=self.prelineWidth;
}

Can any one tell me what I am doing wrong ? Sumit

Ashwini Shahapurkar
  • 6,586
  • 6
  • 26
  • 35
slonkar
  • 4,055
  • 8
  • 39
  • 63
  • 1
    I stumbled upon a nice utility http://colorcode.globalmaverick.com/ it gives u Objecive-c and swift usable code directly from HEX color code or RGB. – amar Oct 05 '15 at 07:14

6 Answers6

200

The values are in the 0.0 to 1.0 range.

E.g. divide by 255., but remember the decimal dot so you get floating point division and not integer division.

Like

selectedColor = [UIColor colorWithRed:14.0/255.0 green:114.0/255.0 blue:199.0/255.0 alpha:1];
pkamb
  • 33,281
  • 23
  • 160
  • 191
Mats
  • 8,528
  • 1
  • 29
  • 35
  • 19
    To expand on Mats answer, here it is with an example: self.drawcolor =[UIColor colorWithRed:14.0/255.0 green:114.0/255.0 blue:199.0/255.0 alpha:1]; – Flea Feb 13 '13 at 15:57
14

Pragmatic approach with Swift.

extension UIColor {

   convenience init(rgbColorCodeRed red: Int, green: Int, blue: Int, alpha: CGFloat) {

     let redPart: CGFloat = CGFloat(red) / 255
     let greenPart: CGFloat = CGFloat(green) / 255
     let bluePart: CGFloat = CGFloat(blue) / 255

     self.init(red: redPart, green: greenPart, blue: bluePart, alpha: alpha)

   }
}
Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
user3378170
  • 2,371
  • 22
  • 11
8

You got several options

Code

CGFloat red = 16.0;
CGFloat green = 97.0;
CGFloat blue = 5.0;
CGFloat alpha = 255.0;
UIColor *color = [UIColor colorWithRed:(red/255.0) green:(green/255.0) blue:(blue/255.0) alpha:(alpha/255.0)];

Color picker plugin for Interface Builder

There's a nice color picker from Panic which works well with IB: http://panic.com/~wade/picker/

Xcode plugin

This one gives you a GUI for choosing colors: http://www.youtube.com/watch?v=eblRfDQM0Go

Pods and libraries

There's a nice pod named MPColorTools: https://github.com/marzapower/MPColorTools

hfossli
  • 22,616
  • 10
  • 116
  • 130
1

Here is a method that you can easily turn into a category on UIColor that will allow you to specify a color based on RGBA values of 0 - 255.

+ (UIColor *)colorFromRGBAWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha {
    return [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:alpha/255.0];
}

If you make it a UIColor category extension, you can use it like so:

UIColor *myAwesomeColor = [UIColor colorFromRGBAWithRed:48 green:119 blue:167 alpha:255];
Alex Zavatone
  • 4,106
  • 36
  • 54
1

In Swift you can easily set your Background-Color for example of a cell like this:

cell.backgroundColor = UIColor.init(red: 14.0/255.0, green: 114.0/255.0, blue: 199.0/255.0, alpha: 1)

:-)

Pixki
  • 51
  • 3
0
(ContentView.Layer.BorderColor) //CG Object
   = UIColor.LightGray.CGColor; 
Faheem
  • 930
  • 10
  • 7
  • 1
    Please edit with more information. Code-only and "try this" answers are discouraged, because they contain no searchable content, and don't explain why someone should "try this". – abarisone Jul 12 '16 at 11:52