Quote:
Originally Posted by me0007
Is there any simple way to convert an RGB UIColor to a grayscale 'color'?
Thanks in advance!
|
You need to convert to a luminance value. I found this on Stack Overflow with a quick Google search:
Luminance (standard, objective): (0.2126*R) + (0.7152*G) + (0.0722*B)
Luminance (perceived option 1): (0.299*R + 0.587*G + 0.114*B)
Luminance (perceived option 2, slower to calculate): sqrt( 0.241*R^2 + 0.691*G^2 + 0.068*B^2 )
The bottom 2 are probably the best choices.
Or this one:
You'd then just set the r,g, and b values to that amount.
If you're working with 0-255 values, convert them to floats first (value/255), then convert the result back to a 0-255 value by multiplying it by 255.