Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIColor display not exactly correct?

I input this in viewDidLoad

self.view.backgroundColor = [UIColor colorWithRed:231/255.0 green:77/255.0 blue:77/255.0 alpha:1.0];

and create label in xib , set color e74d4d(which convert to rgb is 231,77,77)

I want to show image on the website , but it tells me I need 10 reputation.

I debug the code,and find this,

(lldb) po self.view.backgroundColor
UIDeviceRGBColorSpace 0.905882 0.301961 0.301961 1

(lldb) po self.label.textColor
UIDeviceRGBColorSpace 0.87161 0.208926 0.237916 1

this is screenshot of the simulator

this is the code with UIColor

this color setup

i even set this to clear color,but didn't work

like image 949
Henson Fang Avatar asked Dec 05 '25 11:12

Henson Fang


1 Answers

while you are passing value for UIColor always use float value.

self.view.backgroundColor = [UIColor colorWithRed:231.0/255.0 green:77.0/255.0 blue:77.0/255.0 alpha:1.0];

also use the same float value while you are converting to other format.

Edit: From the comments. Dividing 231/255.0 and 231.0/255.0 are the same. Yes it is, but i have written that always pass float value means in for both values.

As user doesn't provided second conversation i thought function is using 255 for devision instead of 255.0 as a result it will be a integer value.

Try below code with RGBFromUIColor macro one by one, it will show different output.

self.titleLabel.backgroundColor = [UIColor colorWithRed:231/255 green:77/255 blue:77/255 alpha:1.0];
self.titleLabel2.backgroundColor = [UIColor colorWithRed:231.0/255.0 green:77.0/255.0 blue:77.0/255.0 alpha:1.0];
self.titleLabel3.backgroundColor = RGBFromUIColor(231, 77, 77);
self.titleLabel4.backgroundColor = RGBFromUIColor(231.0, 77.0, 77.0)

Now suppose macro for RGBFromUIColor is like below

#define RGBFromUIColor(r, g, b) \[UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1]

or like this

#define RGBFromUIColor(r, g, b) \[UIColor colorWithRed:(r)/255 green:(g)/255 blue:(b)/255 alpha:1]

The last RGBFromUIColor will not have same value in case it doen't devided by float i.e 255.0

like image 96
Samir Avatar answered Dec 08 '25 01:12

Samir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!