[英]CGColor - CGColorGetComponents doesn't seem to work properly?
I am trying to get the rgb values from a CGColor I use the followig code.我正在尝试从使用以下代码的 CGColor 获取 rgb 值。 The problem is that all returned values for red, green, and blue always return as a float and they are always between 0 and 1 for example:
rgb(0.000000, 0.847059, 0.945098)
问题是红色、绿色和蓝色的所有返回值始终以浮点数形式返回,并且它们始终介于 0 和 1 之间,例如:
rgb(0.000000, 0.847059, 0.945098)
How do i convert these val;ues to real rgb values?我如何将这些值转换为真正的 rgb 值?
int numComponents = CGColorGetNumberOfComponents(color);
if (numComponents == 4)
{
const CGFloat *components = CGColorGetComponents(color);
CGFloat red = components[0];
CGFloat green = components[1];
CGFloat blue = components[2];
CGFloat alpha = components[3];
}
It returns the correct rgb values represented as a percentage.它返回以百分比表示的正确 rgb 值。 So if your Red =.8 it's 80% of 255 which is 204
因此,如果您的红色 =.8,则为 255 的 80%,即 204
so your code could look like this:所以您的代码可能如下所示:
int numComponents = CGColorGetNumberOfComponents(color);
if (numComponents == 4)
{
const CGFloat *components = CGColorGetComponents(color);
NSInteger red = (NSInteger) components[0] * 255;
NSInteger green = (NSInteger) components[1] * 255;
NSInteger blue = (NSInteger) components[2] * 255;
CGFloat alpha = components[3];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.