<!--
{
  "availability" : [
    "macOS: 10.5.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSGradient",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSGradient"
  },
  "title" : "NSGradient"
}
-->

# NSGradient

An object that can draw gradient fill colors

```
class NSGradient
```

## Overview

This class provides convenience methods for drawing radial or linear (axial) gradients for rectangles and [`NSBezierPath`](/documentation/AppKit/NSBezierPath) objects. It also supports primitive methods that let you customize the shape of the gradient fill. A gradient consists of two or more color changes over the range of the gradient shape. When creating a gradient object, you specify the colors and their locations relative to the start and end of the gradient. This combination of color and location is known as a *color stop*. During drawing, the [`NSGradient`](/documentation/AppKit/NSGradient) object uses the color stop information to compute color changes for you and passes that information to the Quartz shading functions.

Because the [`NSGradient`](/documentation/AppKit/NSGradient) class uses Quartz shadings, drawing is handled by computing the colors at a given point mathematically. This technique results in smooth gradients regardless of the resolution of the target device.

For more information about gradients and their appearance, see [Gradients](https://developer.apple.com/library/archive/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_shadings/dq_shadings.html#//apple_ref/doc/uid/TP30001066-CH207) in [Quartz 2D Programming Guide](https://developer.apple.com/library/archive/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/Introduction/Introduction.html#//apple_ref/doc/uid/TP30001066).

## Topics

### Creating a Gradient

[`-  initWithStartingColor:endingColor:`](/documentation/AppKit/NSGradient/init(starting:ending:))

Initializes a newly allocated gradient object with two colors.

[`-  initWithColors:`](/documentation/AppKit/NSGradient/init(colors:))

Initializes a newly allocated gradient object with an array of colors.

[`init(colorsAndLocations:)`](/documentation/AppKit/NSGradient/init(colorsAndLocations:))

Initializes a newly allocated gradient object with a comma-separated list of arguments.

[`-  initWithColorsAndLocations:`](/documentation/AppKit/NSGradient/initWithColorsAndLocations:)

Initializes a newly allocated gradient object with a comma-separated list of arguments.

[`-  initWithColors:atLocations:colorSpace:`](/documentation/AppKit/NSGradient/init(colors:atLocations:colorSpace:))

Initializes a newly allocated gradient object with the specified colors, color locations, and color space.

[`-  initWithCoder:`](/documentation/AppKit/NSGradient/init(coder:))

Creates a gradient from data in an unarchiver.

### Drawing a Linear Gradient

[`-  drawFromPoint:toPoint:options:`](/documentation/AppKit/NSGradient/draw(from:to:options:))

Draws a linear gradient between the specified start and end points.

[`-  drawInRect:angle:`](/documentation/AppKit/NSGradient/draw(in:angle:)-7sdyh)

Fills the specified rectangle with a linear gradient.

[`-  drawInBezierPath:angle:`](/documentation/AppKit/NSGradient/draw(in:angle:)-68adz)

Fills the specified path with a linear gradient.

### Drawing a Radial Gradient

[`-  drawFromCenter:radius:toCenter:radius:options:`](/documentation/AppKit/NSGradient/draw(fromCenter:radius:toCenter:radius:options:))

Draws a radial gradient between the specified circles.

[`-  drawInRect:relativeCenterPosition:`](/documentation/AppKit/NSGradient/draw(in:relativeCenterPosition:)-3a83)

Draws a radial gradient starting at the center of the specified rectangle.

[`-  drawInBezierPath:relativeCenterPosition:`](/documentation/AppKit/NSGradient/draw(in:relativeCenterPosition:)-502cc)

Draws a radial gradient starting at the center point of the specified path.

### Getting Gradient Properties

[`colorSpace`](/documentation/AppKit/NSGradient/colorSpace)

The color space of the colors associated with the gradient.

[`numberOfColorStops`](/documentation/AppKit/NSGradient/numberOfColorStops)

The number of color stops associated with the gradient.

[`-  getColor:location:atIndex:`](/documentation/AppKit/NSGradient/getColor(_:location:at:))

Returns information about the color stop at the specified index in the receiver’s color array.

[`-  interpolatedColorAtLocation:`](/documentation/AppKit/NSGradient/interpolatedColor(atLocation:))

Returns the color of the rendered gradient at the specified relative location.

### Constants

[`DrawingOptions`](/documentation/AppKit/NSGradient/DrawingOptions)

Constants that specify gradient drawing options.

## Relationships

### Conforms To

[`Sendable`](/documentation/Swift/Sendable)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

[`NSObjectProtocol`](/documentation/ObjectiveC/NSObjectProtocol)

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

[`CVarArg`](/documentation/Swift/CVarArg)

[`NSCopying`](/documentation/Foundation/NSCopying)

[`Hashable`](/documentation/Swift/Hashable)

[`Equatable`](/documentation/Swift/Equatable)

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

[`NSSecureCoding`](/documentation/Foundation/NSSecureCoding)

[`NSCoding`](/documentation/Foundation/NSCoding)

### Inherits From

[`NSObject-swift.class`](/documentation/ObjectiveC/NSObject-swift.class)

---

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)