Color

Class

Common
value class Color(val value: ULong)

The Color class contains color information to be used while painting in Canvas. Color supports ColorSpaces with 3 components, plus one for alpha.

Creating

Color can be created with one of these methods: // from 4 separate Float components. Alpha and ColorSpace are optional val rgbaWhiteFloat = Color(red = 1f, green = 1f, blue = 1f, alpha = 1f, ColorSpace.get(ColorSpaces.Srgb)) // from a 32-bit SRGB color integer val fromIntWhite = Color(android.graphics.Color.WHITE) val fromLongBlue = Color(0xFF0000FF) // from SRGB integer component values. Alpha is optional val rgbaWhiteInt = Color(red = 0xFF, green = 0xFF, blue = 0xFF, alpha = 0xFF)

Representation

A Color always defines a color using 4 components packed in a single 64 bit long value. One of these components is always alpha while the other three components depend on the color space's color model. The most common color model is the RGB model in which the components represent red, green, and blue values.

Component ranges: the ranges defined in the tables below indicate the ranges that can be encoded in a color long. They do not represent the actual ranges as they may differ per color space. For instance, the RGB components of a color in the Display P3 color space use the 0..1 range. Please refer to the documentation of the various color spaces to find their respective ranges.

Alpha range: while alpha is encoded in a color long using a 10 bit integer (thus using a range of 0..1023), it is converted to and from 0..1 float values when decoding and encoding color longs.

sRGB color space: for compatibility reasons and ease of use, Color encoded sRGB colors do not use the same encoding as other color longs.

| Component | Name        | Size    | Range                 |
|-----------|-------------|---------|-----------------------|
| `RGB` color model                   |
| R         | Red         | 16 bits | ``-65504.0, 65504.0`` |
| G         | Green       | 16 bits | ``-65504.0, 65504.0`` |
| B         | Blue        | 16 bits | ``-65504.0, 65504.0`` |
| A         | Alpha       | 10 bits | ``0..1023``           |
|           | Color space | 6 bits  | ``0..63``             |
| `SRGB` color space                      |
| A         | Alpha       | 8 bits  | ``0..255``            |
| R         | Red         | 8 bits  | ``0..255``            |
| G         | Green       | 8 bits  | ``0..255``            |
| B         | Blue        | 8 bits  | ``0..255``            |
| X         | Unused      | 32 bits | ``0``                 |
| `XYZ` color model                   |
| X         | X           | 16 bits | ``-65504.0, 65504.0`` |
| Y         | Y           | 16 bits | ``-65504.0, 65504.0`` |
| Z         | Z           | 16 bits | ``-65504.0, 65504.0`` |
| A         | Alpha       | 10 bits | ``0..1023``           |
|           | Color space | 6 bits  | ``0..63``             |
| `Lab` color model                   |
| L         | L           | 16 bits | ``-65504.0, 65504.0`` |
| a         | a           | 16 bits | ``-65504.0, 65504.0`` |
| b         | b           | 16 bits | ``-65504.0, 65504.0`` |
| A         | Alpha       | 10 bits | ``0..1023``           |
|           | Color space | 6 bits  | ``0..63``             |

The components in this table are listed in encoding order, which is why color longs in the RGB model are called RGBA colors (even if this doesn't quite hold for the special case of sRGB colors).

The color encoding relies on half-precision float values (fp16). If you wish to know more about the limitations of half-precision float values, please refer to the documentation of the Float16 class.

The values returned by these methods depend on the color space encoded in the color long. The values are however typically in the 0..1 range for RGB colors. Please refer to the documentation of the various color spaces for the exact ranges.

Properties

Common
val colorSpace: ColorSpace

Returns this color's color space.

Returns

A non-null instance of ColorSpace
Common
val red: Float

Returns the value of the red component in the range defined by this color's color space (see ColorSpace.getMinValue and ColorSpace.getMaxValue).

If this color's color model is not RGB, calling this is the first component of the ColorSpace.

Common
val green: Float

Returns the value of the green component in the range defined by this color's color space (see ColorSpace.getMinValue and ColorSpace.getMaxValue).

If this color's color model is not RGB, calling this is the second component of the ColorSpace.

Common
val blue: Float

Returns the value of the blue component in the range defined by this color's color space (see ColorSpace.getMinValue and ColorSpace.getMaxValue).

If this color's color model is not RGB, calling this is the third component of the ColorSpace.

Common
val alpha: Float

Returns the value of the alpha component in the range 0..1.

Functions

fun convert(colorSpace: ColorSpace): Color

Converts this color from its color space to the specified color space. The conversion is done using the default rendering intent as specified by ColorSpace.connect.

Parameters

colorSpaceThe destination color space, cannot be null

Returns

A non-null color instance in the specified color space
inline operator fun component1(): Float
inline operator fun component2(): Float
inline operator fun component3(): Float
inline operator fun component4(): Float
inline operator fun component5(): ColorSpace
fun copy(
        alpha: Float = this.alpha,
        red: Float = this.red,
        green: Float = this.green,
        blue: Float = this.blue,
    ): Color

Copies the existing color, changing only the provided values. The ColorSpace of the returned Color is the same as this colorSpace.

Companion Object

Properties

Common
val Black = Color(0xFF000000)
Common
val DarkGray = Color(0xFF444444)
Common
val Gray = Color(0xFF888888)
Common
val LightGray = Color(0xFFCCCCCC)
Common
val White = Color(0xFFFFFFFF)
Common
val Red = Color(0xFFFF0000)
Common
val Green = Color(0xFF00FF00)
Common
val Blue = Color(0xFF0000FF)
Common
val Yellow = Color(0xFFFFFF00)
Common
val Cyan = Color(0xFF00FFFF)
Common
val Magenta = Color(0xFFFF00FF)
Common
val Transparent = Color(0x00000000)
Common
val Unspecified = Color(0f, 0f, 0f, 0f, ColorSpaces.Unspecified)

Because Color is an inline class, this represents an unset value without having to box the Color. It will be treated as Transparent when drawn. A Color can compare with Unspecified for equality or use isUnspecified to check for the unset value or isSpecified for any color that isn't Unspecified.

Methods

Common
fun hsv(
            hue: Float,
            saturation: Float,
            value: Float,
            alpha: Float = 1f,
            colorSpace: Rgb = ColorSpaces.Srgb,
        ): Color

Return a Color from hue, saturation, and value (HSV representation).

Parameters

hueThe color value in the range (0..360), where 0 is red, 120 is green, and 240 is blue
saturationThe amount of hue represented in the color in the range (0..1), where 0 has no color and 1 is fully saturated.
alphaAlpha channel to apply to the computed color
valueThe strength of the color, where 0 is black.
colorSpaceThe RGB color space used to calculate the Color from the HSV values.
Common
fun hsl(
            hue: Float,
            saturation: Float,
            lightness: Float,
            alpha: Float = 1f,
            colorSpace: Rgb = ColorSpaces.Srgb,
        ): Color

Return a Color from hue, saturation, and lightness (HSL representation).

Parameters

hueThe color value in the range (0..360), where 0 is red, 120 is green, and 240 is blue
saturationThe amount of hue represented in the color in the range (0..1), where 0 has no color and 1 is fully saturated.
lightnessA range of (0..1) where 0 is black, 0.5 is fully colored, and 1 is white.
alphaAlpha channel to apply to the computed color
colorSpaceThe RGB color space used to calculate the Color from the HSL values.