iOS Developer Library

Developer

Core Image Reference Collection CIColor Class Reference

Options
Deployment Target:

On This Page
Language:

CIColor

The CIColor class contains color values and the color space for which the color values are valid. You use CIColor objects in conjunction with other Core Image classes, such as CIFilter, CIContext,and CIImage, to take advantage of the built-in Core Image filters when processing images.

A color space defines a one-, two-, three-, or four-dimensional environment whose color components represent intensity values. A color component is also referred to as a color channel. An RGB color space, for example, is a three-dimensional color space whose stimuli are the red, green, and blue intensities that make up a given color. Regardless of the color space, in Core Image, color values range from 0.0 to 1.0, with 0.0 representing an absence of that component (0 percent) and 1.0 representing 100 percent.

Colors also have an alpha component that represents the opacity of the color, with 0.0 meaning completely transparent and 1.0 meaning completely opaque. If a color does not have an explicit alpha component, Core Image paints the color as if the alpha component equals 1.0. You always provide unpremultiplied color components to Core Image and Core Image provides unpremultiplied color components to you. Core Image premultiplies each color component with the alpha value in order to optimize calculations. For more information on premultiplied alpha values see Core Image Programming Guide.

  • Initializes a Core Image color object with a Core Graphics color.

    Declaration

    Swift

    init(CGColor c: CGColor)

    Objective-C

    - (instancetype)initWithCGColor:(CGColorRef)c

    Parameters

    c

    A Core Graphics color value.

    Return Value

    The resulting Core Image color.

    Discussion

    A CGColorRef object is the fundamental opaque data type used internally by Core Graphics to represent colors. For more information on Core Graphics color and color spaces, see Quartz 2D Programming Guide.

    You can pass a CGColorRef object that represents any color space, including CMYK, but Core Image converts all color spaces to the Core Image working color space before it passes the color space to the filter kernel. The Core Image working color space uses three color components plus alpha.

    Availability

    Available in iOS 5.0 and later.

  • Initializes a Core Image color object using a UIKit color object.

    Declaration

    Swift

    convenience init(color color: UIColor)

    Objective-C

    - (instancetype)initWithColor:(UIColor *)color

    Parameters

    color

    The initial color value, which can belong to any available colorspace.

    Return Value

    The resulting Core Image color.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 5.0 and later.

  • Initializes a Core Image color object with the specified red, green, and blue component values.

    Declaration

    Objective-C

    - (instancetype)initWithRed:(CGFloat)r green:(CGFloat)g blue:(CGFloat)b

    Parameters

    r

    The unpremultiplied red component of the color.

    g

    The unpremultiplied green component of the color.

    b

    The unpremultiplied blue component of the color.

    Return Value

    The resulting Core Image color.

    Availability

    Available in iOS 9.0 and later.

  • Initializes a Core Image color object with the specified red, green, blue, and alpha component values.

    Declaration

    Swift

    convenience init(red r: CGFloat, green g: CGFloat, blue b: CGFloat, alpha a: CGFloat)

    Objective-C

    - (instancetype)initWithRed:(CGFloat)r green:(CGFloat)g blue:(CGFloat)b alpha:(CGFloat)a

    Parameters

    r

    The unpremultiplied red component of the color.

    g

    The unpremultiplied green component of the color.

    b

    The unpremultiplied red component of the color.

    a

    The alpha (opacity) component of the color.

    Return Value

    The resulting Core Image color.

    Availability

    Available in iOS 9.0 and later.

  • The Quartz 2D color space associated with the color. (read-only)

    Declaration

    Swift

    var colorSpace: CGColorSpace { get }

    Objective-C

    @property(readonly) CGColorSpaceRef colorSpace

    Availability

    Available in iOS 5.0 and later.

  • The color components of the color. (read-only)

    Declaration

    Swift

    var components: UnsafePointer<CGFloat> { get }

    Objective-C

    @property(readonly) const CGFloat *components

    Discussion

    This property’s value is an array of color components, specified as floating-point values in the range of 0.0 through 1.0. This array includes an alpha component if there is one.

    Availability

    Available in iOS 5.0 and later.

  • Returns the number of color components in the color. (read-only)

    Declaration

    Swift

    var numberOfComponents: Int { get }

    Objective-C

    @property(readonly) size_t numberOfComponents

    Discussion

    This number includes the alpha component if the color contains one.

    Availability

    Available in iOS 5.0 and later.

  • red red Property

    The unpremultiplied red component of the color. (read-only)

    Declaration

    Swift

    var red: CGFloat { get }

    Objective-C

    @property(readonly) CGFloat red

    Availability

    Available in iOS 5.0 and later.

  • The unpremultiplied green component of the color. (read-only)

    Declaration

    Swift

    var green: CGFloat { get }

    Objective-C

    @property(readonly) CGFloat green

    Availability

    Available in iOS 5.0 and later.

  • blue blue Property

    The unpremultiplied blue component of the color. (read-only)

    Declaration

    Swift

    var blue: CGFloat { get }

    Objective-C

    @property(readonly) CGFloat blue

    Availability

    Available in iOS 5.0 and later.

  • The alpha value of the color. (read-only)

    Declaration

    Swift

    var alpha: CGFloat { get }

    Objective-C

    @property(readonly) CGFloat alpha

    Discussion

    A color created without an explicit alpha value has an alpha of 1.0 by default.

    Availability

    Available in iOS 5.0 and later.

  • A formatted string that specifies the components of the color. (read-only)

    Declaration

    Swift

    var stringRepresentation: String { get }

    Objective-C

    @property(readonly) NSString *stringRepresentation

    Discussion

    The string representation always has four components—red, green, blue, and alpha. The default value for the alpha component is 1.0. For example, this string:

    @"0.5 0.7 0.3 1.0"

    indicates an RGB color whose components are 50% red, 70% green, 30% blue, and 100% opaque (alpha value of 1.0).

    Availability

    Available in iOS 5.0 and later.