{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

ColorKt

public final class ColorKt


Summary

Public methods

static final @NonNull Color
Color(int color)

Creates a new Color instance from an ARGB color int.

static final @NonNull Color
Color(long color)

Creates a new Color instance from an ARGB color int.

static final @NonNull Color
Color(int red, int green, int blue, int alpha)

Creates a new Color instance from an ARGB color components.

static final @NonNull Color
Color(
    float red,
    float green,
    float blue,
    float alpha,
    @NonNull ColorSpace colorSpace
)

Create a Color by passing individual red, green, blue, alpha, and colorSpace components.

static final @NonNull Color
compositeOver(@NonNull Color receiver, @NonNull Color background)

Composites this color on top of background using the Porter-Duff 'source over' mode.

static final boolean

false when this is Color.Unspecified.

static final boolean

true when this is Color.Unspecified.

static final @NonNull Color
lerp(@NonNull Color start, @NonNull Color stop, float fraction)

Linear interpolate between two Colors, start and stop with fraction fraction between the two.

static final float
luminance(@NonNull Color receiver)

Returns the relative luminance of this color.

static final @NonNull Color
takeOrElse(@NonNull Color receiver, @NonNull Function0<@NonNull Color> block)

If this color isSpecified then this is returned, otherwise block is executed and its result is returned.

static final int
toArgb(@NonNull Color receiver)

Converts this color to an ARGB color int.

Public methods

Color

public static final @NonNull Color Color(int color)

Creates a new Color instance from an ARGB color int. The resulting color is in the sRGB color space.

Parameters
int color

The ARGB color int to create a Color from.

Returns
@NonNull Color

A non-null instance of {@link Color}

Color

public static final @NonNull Color Color(long color)

Creates a new Color instance from an ARGB color int. The resulting color is in the sRGB color space. This is useful for specifying colors with alpha greater than 0x80 in numeric form without using Long.toInt:

val color = Color(0xFF000080)
Parameters
long color

The 32-bit ARGB color int to create a Color from

Returns
@NonNull Color

A non-null instance of {@link Color}

Color

public static final @NonNull Color Color(int red, int green, int blue, int alpha)

Creates a new Color instance from an ARGB color components. The resulting color is in the sRGB color space. The default alpha value is 0xFF (opaque).

Parameters
int red

The red component of the color, between 0 and 255.

int green

The green component of the color, between 0 and 255.

int blue

The blue component of the color, between 0 and 255.

int alpha

The alpha component of the color, between 0 and 255.

Returns
@NonNull Color

A non-null instance of {@link Color}

Color

public static final @NonNull Color Color(
    float red,
    float green,
    float blue,
    float alpha,
    @NonNull ColorSpace colorSpace
)

Create a Color by passing individual red, green, blue, alpha, and colorSpace components. The default color space is SRGB and the default alpha is 1.0 (opaque). colorSpace must have a ColorSpace.componentCount of

compositeOver

public static final @NonNull Color compositeOver(@NonNull Color receiver, @NonNull Color background)

Composites this color on top of background using the Porter-Duff 'source over' mode.

Both this and background must not be pre-multiplied, and the resulting color will also not be pre-multiplied.

The ColorSpace of the result is always the ColorSpace of background.

Returns
@NonNull Color

the Color representing this composited on top of background, converted to the color space of background.

isSpecified

public static final boolean isSpecified(@NonNull Color receiver)

false when this is Color.Unspecified.

isUnspecified

public static final boolean isUnspecified(@NonNull Color receiver)

true when this is Color.Unspecified.

lerp

public static final @NonNull Color lerp(@NonNull Color start, @NonNull Color stop, float fraction)

Linear interpolate between two Colors, start and stop with fraction fraction between the two. The ColorSpace of the result is always the ColorSpace of stop. fraction should be between 0 and 1, inclusive. Interpolation is done in the ColorSpaces.Oklab color space.

luminance

public static final float luminance(@NonNull Color receiver)

Returns the relative luminance of this color.

Based on the formula for relative luminance defined in WCAG 2.0, W3C Recommendation 11 December 2008.

Returns
float

A value between 0 (darkest black) and 1 (lightest white)

Throws
kotlin.IllegalArgumentException

If the this color's color space does not use the RGB color model

takeOrElse

public static final @NonNull Color takeOrElse(@NonNull Color receiver, @NonNull Function0<@NonNull Color> block)

If this color isSpecified then this is returned, otherwise block is executed and its result is returned.

toArgb

public static final int toArgb(@NonNull Color receiver)

Converts this color to an ARGB color int. A color int is always in the sRGB color space. This implies a color space conversion is applied if needed.

Returns
int

An ARGB color in the sRGB color space