<!--
{
  "availability" : [
    "iOS: 2.0.0 -",
    "iPadOS: 2.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.5.0 -",
    "tvOS: -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "CoreGraphics",
  "identifier" : "/documentation/CoreGraphics/CGGradient/init(colorSpace:colorComponents:locations:count:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "Core Graphics",
      "CoreGraphics"
    ],
    "preciseIdentifier" : "c:@F@CGGradientCreateWithColorComponents"
  },
  "title" : "init(colorSpace:colorComponents:locations:count:)"
}
-->

# init(colorSpace:colorComponents:locations:count:)

Creates a CGGradient object from a color space and the provided color components and locations.

```
init?(colorSpace space: CGColorSpace, colorComponents components: UnsafePointer<CGFloat>, locations: UnsafePointer<CGFloat>?, count: Int)
```

## Parameters

`space`

The color space to use for the gradient. You cannot use a pattern or indexed color space.

`components`

The color components for each color that defines the gradient. The components should be in the color space specified by `space`. If you are unsure of the number of components, you can call the function [`numberOfComponents`](/documentation/CoreGraphics/CGColorSpace/numberOfComponents).

    The number of items in this array should be the product of     `count`     and the number of components in the color space. 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.

`locations`

The location for each color provided in `components`. Each location must be a `CGFloat` value in the range of 0 to 1, inclusive. If 0 and 1 are not in the `locations` array, Quartz uses the colors provided that are closest to 0 and 1 for those locations.

    If     `locations`      is     `NULL`    , the first color in     `colors`      is assigned to location     `0`    , the last color in    `colors`      is assigned to location     `1`    , and intervening colors are assigned locations that are at equal intervals in between.

`count`

The number of locations provided in the `locations` parameters.

## Return Value

A CGGradient object.

## See Also

[`drawRadialGradient(_:startCenter:startRadius:endCenter:endRadius:options:)`](/documentation/CoreGraphics/CGContext/drawRadialGradient(_:startCenter:startRadius:endCenter:endRadius:options:))

Paints a gradient fill that varies along the area defined by the provided starting and ending circles.

  [Quartz 2D Programming Guide](https://developer.apple.com/library/archive/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/Introduction/Introduction.html#//apple_ref/doc/uid/TP30001066)

[`drawLinearGradient(_:start:end:options:)`](/documentation/CoreGraphics/CGContext/drawLinearGradient(_:start:end:options:))

Paints a gradient fill that varies along the line defined by the provided starting and ending points.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)