可以在CGContextSetRGBFillColor中给出不同的颜色吗?

时间:2021-12-02 12:05:29

How can I give the color HEXCOLOR(0xe3f3fbff) in CGContextSetRGBFillColor?

如何在CGContextSetRGBFillColor中提供HEXCOLOR(0xe3f3fbff)颜色?

2 个解决方案

#1


Try

CGContextSetRGBFillColor(context, 0xe3 / 255.0, 0xf3 / 255.0, 0xfb / 255.0, 0xff / 255.0);

#2


CGContextSetRGBFillColor requires CGFloat values for R,G,B and alpha (from 0.0 to 1.0), so you'll have to convert every component of the hex color to values between 0.0 and 1.0.

CGContextSetRGBFillColor需要R,G,B和alpha的CGFloat值(从0.0到1.0),因此您必须将十六进制颜色的每个组件转换为0.0到1.0之间的值。

In your case:

在你的情况下:

// R = 0xe3 / 0xff = 0.890
// G = 0xf3 / 0xff = 0.953
// B = 0xfb / 0xff = 0.984
// A = 0xff / 0xff = 1.000

CGContextSetRGBFillColor(context,0.89,0.953,0.984,1.0);

You can convert your hex color string to r,g,b,a values like this:

您可以将十六进制颜色字符串转换为r,g,b,这样的值:

NSString  *color = @"0xe3f3fbff";

unsigned r,g,b,a;

[[NSScanner scannerWithString:[color substringWithRange:NSMakeRange(2,2)]] scanHexInt:&r];
[[NSScanner scannerWithString:[color substringWithRange:NSMakeRange(4,2)]] scanHexInt:&g];
[[NSScanner scannerWithString:[color substringWithRange:NSMakeRange(6,2)]] scanHexInt:&b];
[[NSScanner scannerWithString:[color substringWithRange:NSMakeRange(7,2)]] scanHexInt:&a];

CGContextSetRGBFillColor(context,r/255.0,g/255.0,b/255.0,a/255.0);

#1


Try

CGContextSetRGBFillColor(context, 0xe3 / 255.0, 0xf3 / 255.0, 0xfb / 255.0, 0xff / 255.0);

#2


CGContextSetRGBFillColor requires CGFloat values for R,G,B and alpha (from 0.0 to 1.0), so you'll have to convert every component of the hex color to values between 0.0 and 1.0.

CGContextSetRGBFillColor需要R,G,B和alpha的CGFloat值(从0.0到1.0),因此您必须将十六进制颜色的每个组件转换为0.0到1.0之间的值。

In your case:

在你的情况下:

// R = 0xe3 / 0xff = 0.890
// G = 0xf3 / 0xff = 0.953
// B = 0xfb / 0xff = 0.984
// A = 0xff / 0xff = 1.000

CGContextSetRGBFillColor(context,0.89,0.953,0.984,1.0);

You can convert your hex color string to r,g,b,a values like this:

您可以将十六进制颜色字符串转换为r,g,b,这样的值:

NSString  *color = @"0xe3f3fbff";

unsigned r,g,b,a;

[[NSScanner scannerWithString:[color substringWithRange:NSMakeRange(2,2)]] scanHexInt:&r];
[[NSScanner scannerWithString:[color substringWithRange:NSMakeRange(4,2)]] scanHexInt:&g];
[[NSScanner scannerWithString:[color substringWithRange:NSMakeRange(6,2)]] scanHexInt:&b];
[[NSScanner scannerWithString:[color substringWithRange:NSMakeRange(7,2)]] scanHexInt:&a];

CGContextSetRGBFillColor(context,r/255.0,g/255.0,b/255.0,a/255.0);