开发者

Objective-c HTML->RGB color not correct in UIColor

开发者 https://www.devze.com 2023-04-03 14:35 出处:网络
I need to use custom colors in my app. I\'ve found some nice HTML colors and converted them to RGB values then apply them with:

I need to use custom colors in my app. I've found some nice HTML colors and converted them to RGB values then apply them with:

[UIColor colorWithRed:235 green:242 blue:212 alpha:1]

The problem is the resulting color in my app isn't co开发者_开发技巧rrect. Sometime it's just white and other times it's just way off. Red could be yellow etc.

Has anybody come across this before? Am I converting the colors incorrectly or something.


Simply divide all you color values by 255:

[UIColor colorWithRed:235/255.0f green:242/255.0f blue:212/255.0f alpha:1]


The range of valid values for the parameters to +colorWithRed:green:blue:alpha: is 0 to 1. Assuming your RGB values have the range 0 to 255, your example would be:

[UIColor colorWithRed:0.92f green:0.95f blue:0.83f alpha:1.f];


Simply use this method in .m

-(float)getColorValue:(float)colorVal
{
    return colorVal/255;
}

declare definition at .h

-(float)getColorValue:(float)colorVal

Example:

[UIColor colorWithRed:[self getColorValue:227] green:[self getColorValue:227] blue:[self getColorValue:227] alpha:1.0];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号