One Logo Pool Ball
SwiftUICore · IOS API REFERENCE

Color

struct · iOS 13.0+ · SDK 27.1

@frozen struct Color

A representation of a color that adapts to a given context. You can create a color in one of several ways: * Load a color from an Asset Catalog: ``` let aqua = Color("aqua") // Looks in your app's main bundle by default. ``` * Specify component values, like red, green, and blue; hue, saturation, and brightness; or white level: ``` let skyBlue = Color(red: 0.4627, green: 0.8392, blue: 1.0) let lemonYellow = Color(hue: 0.1639, saturation: 1, brightness: 1) let steelGray = Color(white: 0.4745) ``` * Create a color instance from another color, like a <doc://com.apple.documentation/documentation/UIKit/UIColor> or an <doc://com.apple.documentation/documentation/AppKit/NSColor>: ``` #if os(iOS) let linkColor = Color(uiColor: .link) #elseif os(macOS) let linkColor = Color(nsColor: .linkColor) #endif ``` * Use one of a palette of predefined colors, like ``ShapeStyle/black``, ``ShapeStyle/green``, and ``ShapeStyle/purple``. Some view modifiers can take a color as an argument. For example, ``View/foregroundStyle(_:)`` uses the color you provide to set the foreground color for view elements, like text or <doc://com.apple.documentation/design/human-interface-guidelines/sf-symbols>: Image(systemName: "leaf.fill") .foregroundStyle(Color.green) ![A screenshot of a green leaf.](Color-1) Because SwiftUI treats colors as ``View`` instances, you can also directly add them to a view hierarchy. For example, you can layer a rectangle beneath a sun image using colors defined above: ZStack { skyBlue Image(systemName: "sun.max.fill") .foregroundStyle(lemonYellow) } .frame(width: 200, height: 100) A color used as a view expands to fill all the space it's given, as defined by the frame of the enclosing ``ZStack`` in the above example: ![A screenshot of a yellow sun on a blue background.](Color-2) SwiftUI only resolves a color to a concrete value just before using it in a given environment. This enables a context-dependent appearance for system defined colors, or those that you load from an Asset Catalog. For example, a color can have distinct light and dark variants that the system chooses from at render time.

Members (37)

init · iOS 15.0+

init

TypeScript
static initCgColor(cgColor: _CoreGraphics_CGColor): Color
Swift declaration
init(cgColor: CGColor)

Creates a color from a Core Graphics color. - Parameter cgColor: A <doc://com.apple.documentation/documentation/CoreGraphics/CGColor> instance from which to create a color.

get · iOS 14.0+ · Deprecated in iOS 100000.0

cgColor

TypeScript
get cgColor(): (_CoreGraphics_CGColor) | null
Swift declaration
var cgColor: CGColor? { get }

A Core Graphics representation of the color, if available. You can get a <doc://com.apple.documentation/documentation/CoreGraphics/CGColor> instance from a constant SwiftUI color. This includes colors you create from a Core Graphics color, from RGB or HSB components, or from constant UIKit and AppKit colors. For a dynamic color, like one you load from an Asset Catalog using ``init(_:bundle:)``, or one you create from a dynamic UIKit or AppKit color, this property is `nil`. To evaluate all types of colors, use the `resolve(in:)` method.

get · iOS 13.0+

accentColor

TypeScript
static get accentColor(): Color
Swift declaration
static var accentColor: Color { get }

A color that reflects the accent color of the system or app. The accent color is a broad theme color applied to views and controls. You can set it at the application level by specifying an accent color in your app's asset catalog. > Note: In macOS, SwiftUI applies customization of the accent color only if the user chooses Multicolor under General > Accent color in System Preferences. The following code renders a ``Text`` view using the app's accent color: Text("Accent Color") .foregroundStyle(Color.accentColor)

method · iOS 26.0+

resolveHDR

TypeScript
resolveHDR(environment: EnvironmentValues): Color_ResolvedHDR
Swift declaration
func resolveHDR(in environment: EnvironmentValues) -> Color.ResolvedHDR

Evaluates this color to a resolved color with content headroom, given a set of environment values. - Parameter environment: the environment of the view displaying the color. - Returns: the color's value in the sRGB color space.

get · iOS 13.0+

description

TypeScript
get description(): string
Swift declaration
var description: String { get }

A textual representation of the color. Use this method to get a string that represents the color. The <doc://com.apple.documentation/documentation/Swift/print(_:separator:terminator:)> function uses this property to get a string representing an instance: print(Color.red) // Prints "red"

method · iOS 26.0+

exposureAdjust

TypeScript
exposureAdjust(stops: number): Color
Swift declaration
@export(implementation) func exposureAdjust(_ stops: Double) -> Color

Returns a new color with an exposure adjustment applied. This function adjusts the exposure of a color by multipling its linear-light representation by `pow(2, stops)` and adjusting its HDR content headroom. For example the system yellow color could have its brightness increased by two exposure levels: Color.yellow.exposureAdjust(2) - Parameter stops: the number of exposure levels to adjust by. - Returns: a new color with the exposure adjustment applied.

init · iOS 13.0+

init

TypeScript
static initHueSaturationBrightnessOpacity(hue: number, saturation: number, brightness: number, opacity?: number): Color
Swift declaration
init(hue: Double, saturation: Double, brightness: Double, opacity: Double = 1)

Creates a constant color from hue, saturation, and brightness values. This initializer creates a constant color that doesn't change based on context. For example, it doesn't have distinct light and dark appearances, unlike various system-defined colors, or a color that you load from an Asset Catalog with ``init(_:bundle:)``. - Parameters: - hue: A value in the range `0` to `1` that maps to an angle from 0° to 360° to represent a shade on the color wheel. - saturation: A value in the range `0` to `1` that indicates how strongly the hue affects the color. A value of `0` removes the effect of the hue, resulting in gray. As the value increases, the hue becomes more prominent. - brightness: A value in the range `0` to `1` that indicates how bright a color is. A value of `0` results in black, regardless of the other components. The color lightens as you increase this component. - opacity: An optional degree of opacity, given in the range `0` to `1`. A value of `0` means 100% transparency, while a value of `1` means 100% opacity. The default is `1`.

method · iOS 18.0+

mix

TypeScript
mix(rhs: Color, fraction: number, colorSpace?: Gradient_ColorSpace): Color
Swift declaration
func mix(with rhs: Color, by fraction: Double, in colorSpace: Gradient.ColorSpace = .perceptual) -> Color

Returns a version of self mixed with `rhs` by the amount specified by `fraction`. - Parameters: - rhs: The color to mix `self` with. - fraction: The amount of blending, `0.5` means `self` is mixed in equal parts with `rhs`. - colorSpace: The color space used to mix the colors. - Returns: A new `Color` based on `self` and `rhs`.

get · iOS 13.0+

red

TypeScript
static get red(): Color
Swift declaration
static let red: Color

A context-dependent red color suitable for use in UI elements.

get · iOS 13.0+

blue

TypeScript
static get blue(): Color
Swift declaration
static let blue: Color

A context-dependent blue color suitable for use in UI elements.

get · iOS 15.0+

cyan

TypeScript
static get cyan(): Color
Swift declaration
static let cyan: Color

A context-dependent cyan color suitable for use in UI elements.

get · iOS 13.0+

gray

TypeScript
static get gray(): Color
Swift declaration
static let gray: Color

A context-dependent gray color suitable for use in UI elements.

get · iOS 15.0+

mint

TypeScript
static get mint(): Color
Swift declaration
static let mint: Color

A context-dependent mint color suitable for use in UI elements.

get · iOS 13.0+

pink

TypeScript
static get pink(): Color
Swift declaration
static let pink: Color

A context-dependent pink color suitable for use in UI elements.

get · iOS 15.0+

teal

TypeScript
static get teal(): Color
Swift declaration
static let teal: Color

A context-dependent teal color suitable for use in UI elements.

get · iOS 13.0+

black

TypeScript
static get black(): Color
Swift declaration
static let black: Color

A black color suitable for use in UI elements.

get · iOS 15.0+

brown

TypeScript
static get brown(): Color
Swift declaration
static let brown: Color

A context-dependent brown color suitable for use in UI elements.

get · iOS 13.0+

clear

TypeScript
static get clear(): Color
Swift declaration
static let clear: Color

A clear color suitable for use in UI elements.

get · iOS 13.0+

green

TypeScript
static get green(): Color
Swift declaration
static let green: Color

A context-dependent green color suitable for use in UI elements.

get · iOS 13.0+

white

TypeScript
static get white(): Color
Swift declaration
static let white: Color

A white color suitable for use in UI elements.

get · iOS 15.0+

indigo

TypeScript
static get indigo(): Color
Swift declaration
static let indigo: Color

A context-dependent indigo color suitable for use in UI elements.

get · iOS 13.0+

orange

TypeScript
static get orange(): Color
Swift declaration
static let orange: Color

A context-dependent orange color suitable for use in UI elements.

get · iOS 13.0+

purple

TypeScript
static get purple(): Color
Swift declaration
static let purple: Color

A context-dependent purple color suitable for use in UI elements.

get · iOS 13.0+

yellow

TypeScript
static get yellow(): Color
Swift declaration
static let yellow: Color

A context-dependent yellow color suitable for use in UI elements.

method · iOS 13.0+

opacity

TypeScript
opacity(opacity: number): Color
Swift declaration
func opacity(_ opacity: Double) -> Color

Multiplies the opacity of the color by the given amount. - Parameter opacity: The amount by which to multiply the opacity of the color. - Returns: A view with modified opacity.

get · iOS 13.0+

primary

TypeScript
static get primary(): Color
Swift declaration
static let primary: Color

The color to use for primary content.

method · iOS 17.0+

resolve

TypeScript
resolve(environment: EnvironmentValues): Color_Resolved
Swift declaration
func resolve(in environment: EnvironmentValues) -> Color.Resolved

Evaluates this color to a resolved color given the current `context`.

get · iOS 16.0+

gradient

TypeScript
get gradient(): AnyGradient
Swift declaration
var gradient: AnyGradient { get }

Returns the standard gradient for the color `self`. For example, filling a rectangle with a gradient derived from the standard blue color: Rectangle().fill(.blue.gradient)

method · iOS 26.0+

headroom

TypeScript
headroom(headroom?: (number) | null): Color
Swift declaration
func headroom(_ headroom: Double?) -> Color

Creates a new color with specified HDR content headroom. High Dynamic Range colors (those with RGB components outside the standard [0, 1] range) should be annotated with their headroom to ensure that they are displayed correctly. Knowing content headroom allows the rendering system to automatically increase display headroom when the color is displayed and to tone map the color when the available display headroom is insufficient to render the color as intended. For example a custom yellow color whose brightness has been increased by two exposure levels: Color(.sRGB, red: 1.83, green: 1.47, blue: 0) .headroom(4) note that headroom is a linear quantity, and as such any color adjustments should typically be made in a linear color space. - Parameter headroom: the headroom value to associate with the new color. - Returns: a new color with the specified content headroom.

get · iOS 13.0+

secondary

TypeScript
static get secondary(): Color
Swift declaration
static let secondary: Color

The color to use for secondary content.

init · iOS 13.0+

init

TypeScript
static initRedGreenBlueOpacity(colorSpace: Color_RGBColorSpace, red: number, green: number, blue: number, opacity?: number): Color
Swift declaration
init(_ colorSpace: Color.RGBColorSpace = .sRGB, red: Double, green: Double, blue: Double, opacity: Double = 1)

Creates a constant color from red, green, and blue component values. This initializer creates a constant color that doesn't change based on context. For example, it doesn't have distinct light and dark appearances, unlike various system-defined colors, or a color that you load from an Asset Catalog with ``init(_:bundle:)``. A standard sRGB color space clamps each color component — `red`, `green`, and `blue` — to a range of `0` to `1`, but SwiftUI colors use an extended sRGB color space, so you can use component values outside that range. This makes it possible to create colors using the ``RGBColorSpace/sRGB`` or ``RGBColorSpace/sRGBLinear`` color space that make full use of the wider gamut of a diplay that supports ``RGBColorSpace/displayP3``. - Parameters: - colorSpace: The profile that specifies how to interpret the color for display. The default is ``RGBColorSpace/sRGB``. - red: The amount of red in the color. - green: The amount of green in the color. - blue: The amount of blue in the color. - opacity: An optional degree of opacity, given in the range `0` to `1`. A value of `0` means 100% transparency, while a value of `1` means 100% opacity. The default is `1`.

init · iOS 13.0+

init

TypeScript
static initWhiteOpacity(colorSpace: Color_RGBColorSpace, white: number, opacity?: number): Color
Swift declaration
init(_ colorSpace: Color.RGBColorSpace = .sRGB, white: Double, opacity: Double = 1)

Creates a constant grayscale color. This initializer creates a constant color that doesn't change based on context. For example, it doesn't have distinct light and dark appearances, unlike various system-defined colors, or a color that you load from an Asset Catalog with ``init(_:bundle:)``. A standard sRGB color space clamps the `white` component to a range of `0` to `1`, but SwiftUI colors use an extended sRGB color space, so you can use component values outside that range. This makes it possible to create colors using the ``RGBColorSpace/sRGB`` or ``RGBColorSpace/sRGBLinear`` color space that make full use of the wider gamut of a diplay that supports ``RGBColorSpace/displayP3``. - Parameters: - colorSpace: The profile that specifies how to interpret the color for display. The default is ``RGBColorSpace/sRGB``. - white: A value that indicates how white the color is, with higher values closer to 100% white, and lower values closer to 100% black. - opacity: An optional degree of opacity, given in the range `0` to `1`. A value of `0` means 100% transparency, while a value of `1` means 100% opacity. The default is `1`.

init · iOS 13.0+

init

TypeScript
static initBundle(name: string, bundle?: (_Foundation_Bundle) | null): Color
Swift declaration
init(_ name: String, bundle: Bundle? = nil)

Creates a color from a color set that you indicate by name. Use this initializer to load a color from a color set stored in an Asset Catalog. The system determines which color within the set to use based on the environment at render time. For example, you can provide light and dark versions for background and foreground colors: ![A screenshot of color sets for foreground and background colors, each with light and dark variants, in an Asset Catalog.](Color-init-1) You can then instantiate colors by referencing the names of the assets: struct Hello: View { var body: some View { ZStack { Color("background") Text("Hello, world!") .foregroundStyle(Color("foreground")) } .frame(width: 200, height: 100) } } SwiftUI renders the appropriate colors for each appearance: ![A side by side comparison of light and dark appearance screenshots of the same content. The light variant shows dark text on a light background, while the dark variant shows light text on a dark background.](Color-init-2) - Parameters: - name: The name of the color resource to look up. - bundle: The bundle in which to search for the color resource. If you don't indicate a bundle, the initializer looks in your app's main bundle by default.

init · iOS 26.0+

init

TypeScript
static init(resolved: Color_ResolvedHDR): Color
Swift declaration
init(_ resolved: Color.ResolvedHDR)

Creates a constant color with the values specified by the resolved color. - Parameter resolved: the resolved color to initialize as the color's value.

init · iOS 17.0+

init

TypeScript
static init85145(resolved: Color_Resolved): Color
Swift declaration
init(_ resolved: Color.Resolved)

Creates a constant color with the values specified by the resolved color.

init · iOS 17.0+

init

TypeScript
static init50051(resource: _DeveloperToolsSupport_ColorResource): Color
Swift declaration
init(_ resource: ColorResource)

Initialize a `Color` with a color resource.

init · iOS 14.0+ · Deprecated in iOS 100000.0

init

TypeScript
static init51359(cgColor: _CoreGraphics_CGColor): Color
Swift declaration
init(_ cgColor: CGColor)

Creates a color from a Core Graphics color. - Parameter cgColor: A <doc://com.apple.documentation/documentation/CoreGraphics/CGColor> instance from which to create a color.

Skipped declarations

2 declarations were excluded from these bindings. Each reason comes from the generator or the iOS SDK compiler.

Color.init(_:)

The iOS SDK compiler rejected this binding: no exact matches in call to initializer

Color.init(_:)

The iOS SDK compiler rejected this binding: no exact matches in call to initializer

All SwiftUICore types →