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

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

这是我的代码:

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

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

值在0.01.0范围内。

例如除以255.但记住小数点,所以你得到浮点除法而不是整数除法。

喜欢

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

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) } } 

你有几个select

 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的颜色select器插件

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

Xcode插件

这一个给你一个select颜色的GUI: http : //www.youtube.com/watch?v=eblRfDQM0Go

豆荚和图书馆

有一个很好的pod,命名为MPColorTools : https : //github.com/marzapower/MPColorTools

这里是一个方法,你可以很容易地转换成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]; 

在Swift中,你可以很容易地设置你的背景颜色,例如像这样的单元格:

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

🙂

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