<!--
{
  "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(colorsSpace:colors:locations:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "Core Graphics",
      "CoreGraphics"
    ],
    "preciseIdentifier" : "c:@F@CGGradientCreateWithColors"
  },
  "title" : "init(colorsSpace:colors:locations:)"
}
-->

# init(colorsSpace:colors:locations:)

Creates a gradient object from a color space and the provided color objects and locations.

```
init?(colorsSpace space: CGColorSpace?, colors: CFArray, locations: UnsafePointer<CGFloat>?)
```

## Parameters

`space`

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

`colors`

A non-empty array of [`CGColor`](/documentation/CoreGraphics/CGColor) objects that should be in the color space specified by `space`. If `space` is not `NULL`, each color will be converted (if necessary) to that color space and the gradient will drawn in that color space. Otherwise, each color will be converted to and drawn in the GenericRGB color space.

`locations`

The location for each color provided in `colors`; each location must be a <doc://com.apple.documentation/documentation/CoreFoundation/CGFloat-swift.struct> 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.

    The     `locations`     array should contain the same number of items as the     `colors`     array.

## Return Value

A [`CGGradient`](/documentation/CoreGraphics/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.

[`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)