cfischer
cfischer

Reputation: 24902

Weird arguments in UIColor colorWithRed:green:blue:alpha:

When checking this method, I was expecting for red, green and blue to be in the 0-255 range. Instead, it's in 0-1.

Am I the only one who thinks this is weird?

Is there any reason not o use the more common 0-255 values for RGB, or even hex numbers (as in html)?

Upvotes: 2

Views: 621

Answers (2)

Pete
Pete

Reputation: 26

The reason sometimes RGB values are represented as float values rather than 0 to 255 is because 0 to 255 assumes you are using 8 bits to represent each colour component and hence have to use 24 bits for each colour in your frame buffers. This may not be the case if you are using displays that only support 256 colours in total or more than 16 million etc. In theory then can be an infinite number of shades of red, green or blue. The number of bits you use to represent them depends on how accurate you need to represent colour and how much memory you have on graphics cards to represent images etc. For many cases 0 to 255 is fine. But there is another world out there where it isn't fine, and for those devices / accurate rendering requirements, floating point numbers provide a much needed alternative.

Upvotes: 1

taskinoor
taskinoor

Reputation: 46027

In my opinion this is not weird. Both 0-255 and 0.0-1.0 levels are widely used in different platforms. You can always convert that by using something like this:

#define FLOAT_COLOR_VALUE(n) (n)/255.0

Upvotes: 3

Related Questions