Incorrect Hue returned by getHue function

試著忘記壹切 提交于 2019-12-24 12:41:20

问题


I am converting RGB to HSV using built in function of UIColour: - (BOOL)getHue:(CGFloat *)hue saturation:(CGFloat *)saturation brightness:(CGFloat *)brightness alpha:(CGFloat *)alpha but for some RGB it is not giving correct output.

CGFloat h,s,v,a;
UIColor *tColor=[UIColor colorWithRed:(230.0/255.0) green:(226.0/255.0) blue:(226.0/255.0) alpha:1.0];
BOOL success=[tColor getHue:&h saturation:&s brightness:&v alpha:&a];
NSLog(@"Output-> Success:%d, hue:%f, sat:%f, value:%f, alpha:%f",success,h,s,v,a);

Actual output:
Output-> Success:1, hue:1.000000, sat:0.017391, value:0.901961, alpha:1.000000

Expected output:
hue:0, sat:0.017391, value:0.901961, alpha:1.000000

Note: I have derived this expected output from some online conversion tools(ex: http://www.rapidtables.com/convert/color/rgb-to-hsv.htm)

You can see clearly difference in Hue? So, why such difference in Hue and how can I get my expected output?


回答1:


Hue in HSB or HSL is measured as degrees around a colour circle, starting with red at 0° and wrapping back around to red at 360°; so a hue of 0° is the same as a hue of 360°. UIColor maps this range (0–360°) to the values 0–1 (0°=0.0, 360°=1.0).




回答2:


While most of the rest of the world defines "hue" as going from 0 to 360 degrees, for UIColor it's 0.0 to 1.0.

Actually, just as 360.000 degrees is identical to 0.000 in geometry or someone else's hue, so we have with UIColor's hue that 1.000 is identical to 0.000 (hallucinate as many zero digits after the decimal point as you like, and in binary if you prefer.)

So, your "actual output" and "expected output" are identical. In coding, keep hue in the range 0.000 to 0.999, and change 1.000 to 0.000 to avoid trouble.



来源:https://stackoverflow.com/questions/15917495/incorrect-hue-returned-by-gethue-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!