Where is the CGColorSpace for RGBA?

I am trying to draw a circle shaded with a gradient from white to transparent. I am using Core Graphics.

Here is what I have which draws a gradient from white to black:

let colorSpace = CGColorSpaceCreateDeviceRGB();
let colors = [UIColor.white.cgColor, UIColor.black.cgColor] as CFArray;
let locations : [CGFloat] = [0.0, 1.0];
let glowGradient : CGGradient = CGGradient.init(colorsSpace: colorSpace, colors: colors, locations: locations)!;
let ctx = UIGraphicsGetCurrentContext()!;
ctx.drawRadialGradient(glowGradient, startCenter: rectCenter, startRadius: 0, endCenter: rectCenter, endRadius: imageWidthPts/2, options: []);

However, I do not want to draw white-to-black; I want to draw white-to-transparent.

To do so, I first tried changing the end color to UIColor.white.cgColor.copy(alpha: 0.0) (ie, transparent white). However, this failed with:

fatal error: unexpectedly found nil while unwrapping an Optional value

I assume this error is due to the color being outside the specified RGB color space ( CGColorSpaceCreateDeviceRGB() ).

The fix would seem to be to change the specified color space to one with an alpha component, such as RGBA. However, such color spaces do not appear to exist! There are only CGColorSpaceCreateDeviceRGB , CGColorSpaceCreateDeviceCMYK , and CGColorSpaceCreateDeviceGray .

But it makes no sense for there to be no available color spaces with an alpha component. The documentation explicitly describes support for alpha in gradients. The documentation for CGGradient.init says:

For example, if the color space is an RGBA color space and you want to use two colors in the gradient (one for a starting location and another for an ending location), then you need to provide 8 values in components —red, green, blue, and alpha values for the first color, followed by red, green, blue, and alpha values for the second color.

This RGBA encoding makes perfect sense, but it's impossible to tell Core Graphics that I'm using such an RGBA encoding, because there is no RGBA color space!

Where is the CGColorSpace for RGBA?


A slightly unsatisfactory answer: you can get the color space of a context with .colorSpace . In my case, it seems to give me an RGBA space, but I can't see any guarantee of this.

Here's the gradient using .colorSpace :

let ctx = UIGraphicsGetCurrentContext()!;
let colorSpace = ctx.colorSpace!;
let colorComponents : [CGFloat] = [
 // R    G    B    A
    1.0, 1.0, 1.0, 1.0,
    1.0, 1.0, 1.0, 0.0,
    ];
let locations : [CGFloat] = [0.0, 1.0];
let glowGradient : CGGradient = CGGradient.init(
    colorSpace: colorSpace,
    colorComponents: colorComponents,
    locations: locations,
    count: locations.count
    )!;
ctx.drawRadialGradient(glowGradient, startCenter: rectCenter, startRadius: 0, endCenter: rectCenter, endRadius: imageWidthPts/2, options: []);

It's particularly confusing that in my case, the colorSpace.numberOfComponents evaluates to 3, ie not RGBA, and yet it still correctly interprets the alpha component in the gradient. ¯_(ツ)_/¯


You don't need the RGBA color space to draw transparent radial/linear gradients. The RGB color space is enough. If it's not drawing it transparently, you probably have the background color of the view or the context misconfigured.

If you're creating a context you want to make sure that you pass in false for opaque: Iphone How to make context background transparent?

If you're using a CALayer on a UIView, you need to make sure that the UIView's background color is set to UIColor.clear. If it's set to nil, you'll end up with the gradient blending with black instead.

链接地址: http://www.djcxy.com/p/87546.html

上一篇: 将ARGB颜色转换为RGB

下一篇: RGBC的CGColorSpace在哪里?