跳转至
Structure

Color

An environment-dependent color.

Declaration

@frozen struct Color : Hashable, CustomStringConvertible

Overview

Color represents an environment-dependent color that conforms to View. Colors conformance to View means that a color can be used as a view itself.

For example:

struct ExampleView: View {
    var body: some View {
        Color.yellow
    }
}
A yellow view in which the color conforms to view and hence is used as a view itself.

Color also conforms to ShapeStyle which allows it to serve as a fill or stroke on a shape.

For example, as a fill:

struct ExampleView: View {
    var body: some View {
        Circle()
            .fill(Color.yellow)
    }
}
A view containing a large yellow-filled circle in which the color conforms to ShapeStyle and serves as the fill on the circle shape.

And as a stroke:

struct ExampleView: View {
    var body: some View {
        Circle()
            .stroke(Color.yellow)
    }
}
A view containing a large circle with a yellow outline in which the color conforms to ShapeStyle and serves as the stroke on the circle shape.

  • Note: A Color is a late-binding token: SwiftUI only resolves it to a concrete value just before using it in a given environment.

Availability

iOS 13.0+

macOS 10.15+

tvOS 13.0+

watchOS 6.0+

Topics


Type Property

accentColor A color that represents the system or application accent color.

black A true black color View.

blue A stylized blue color View.

clear A clear color View, equivalent to an empty view.

gray A gray color View.

green A stylized green color View.

orange An orange color View.

pink A pink color View.

primary The default color of text, based on the environment.

purple A purple color View.

red A stylized red color View.

secondary The default color of text, with a little bit of transparency.

white A true white color View.

yellow A stylized yellow color View.


Type Method

==(lhs:rhs:) Returns a Boolean value indicating whether two values are equal.


Initializer

init(_:) Creates a color from an instance of CGColor.

init(_:) Creates a color from an instance of UIColor.

init(_:bundle:) Creates a named color.

init(_:red:green:blue:opacity:) Create a Color from RGB and opacity values along with an optional colorspace.

init(_:white:opacity:) Create a Color from grayscale and opacity.

init(hue:saturation:brightness:opacity:) Creates a Color from hue, saturation, brightness, and opacity values.


Type Alias

Body The type of view representing the body of this view.


Instance Method

hash(into:) Hashes the essential components of this value by feeding them into the given hasher.

opacity(_:) Updates the transparency channel of a Color, returning a Color back.


Enumeration

RGBColorSpace The common ways of organizing RGB colors for a screen.


Instance Property

cgColor Returns a CGColor that represents this color if one can be constructed that accurately represents this color.

description A textual representation of this instance.

hashValue The hash value.