如何正确地从RGB值初始化UIColor?

时间:2011-11-05 22:56:35

标签: uibutton rgb uicolor

我正在使用各种颜色的iPhone应用程序。当用户选择特定颜色按钮时,我相应地设置了绘图颜色。我得到了一些颜色,但在大多数情况下我会变成白色。

这是我的代码:

-(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;
}

任何人都可以告诉我我做错了什么吗? 萨米特

6 个答案:

答案 0 :(得分:184)

值在0.01.0范围内。

E.g。除以255.,但要记住小数点,以便得到浮点除法而不是整数除法。

selectedColor = [UIColor colorWithRed:14.0/255.0 green:114.0/255.0 blue:199.0/255.0 alpha:1];

答案 1 :(得分:12)

使用 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)

   }
}

答案 2 :(得分:6)

你有几个选择

<强>代码

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

Interface Builder的颜色选择器插件

Panic有一个很好的颜色选择器,可以与IB一起使用:http://panic.com/~wade/picker/

Xcode插件

这个为您提供了一个用于选择颜色的GUI:http://www.youtube.com/watch?v=eblRfDQM0Go

广告库和图书馆

有一个名为MPColorTools的漂亮广告连播:https://github.com/marzapower/MPColorTools

答案 3 :(得分:1)

这是一种方法,您可以轻松地在UIColor上变成一个类别,允许您根据RGBA值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];
}

如果你把它作为UIColor类别扩展,你可以这样使用它:

UIColor *myAwesomeColor = [UIColor colorFromRGBAWithRed:48 green:119 blue:167 alpha:255];

答案 4 :(得分:1)

在Swift中,您可以轻松设置背景颜色,例如像这样的单元格:

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

:-)

答案 5 :(得分:0)

(ContentView.Layer.BorderColor) //CG Object
   = UIColor.LightGray.CGColor;