DynamicColor

class DynamicColor(val name: String, val palette: (DynamicScheme) -> TonalPalette, val tone: (DynamicScheme) -> Double, val isBackground: Boolean, val background: (DynamicScheme) -> DynamicColor?, val secondBackground: (DynamicScheme) -> DynamicColor?, val contrastCurve: ContrastCurve?, val toneDeltaPair: (DynamicScheme) -> ToneDeltaPair?, val opacity: (DynamicScheme) -> Double? = null)

A color that adjusts itself based on UI state, represented by DynamicScheme.

This color automatically adjusts to accommodate a desired contrast level, or other adjustments such as differing in light mode versus dark mode, or what the theme is, or what the color that produced the theme is, etc.

Colors without backgrounds do not change tone when contrast changes. Colors with backgrounds become closer to their background as contrast lowers, and further when contrast increases.

Prefer the static constructors. They provide a much more simple interface, such as requiring just a hexcode, or just a hexcode and a background.

Ultimately, each component necessary for calculating a color, adjusting it for a desired contrast level, and ensuring it has a certain lightness/tone difference from another color, is provided by a function that takes a DynamicScheme and returns a value. This ensures ultimate flexibility, any desired behavior of a color for any design system, but it usually unnecessary. See the default constructor for more information.

Parameters

name

The name of the dynamic color.

palette

Function that provides a TonalPalette given DynamicScheme. A TonalPalette is defined by a hue and chroma, so this replaces the need to specify hue/chroma. By providing a tonal palette, when contrast adjustments are made, intended chroma can be preserved.

tone

Function that provides a tone, given a DynamicScheme.

isBackground

Whether this dynamic color is a background, with some other color as the foreground.

background

The background of the dynamic color (as a function of a DynamicScheme), if it exists.

secondBackground

A second background of the dynamic color (as a function of a DynamicScheme), if it exists.

contrastCurve

A ContrastCurve object specifying how its contrast against its background should behave in various contrast levels options.

toneDeltaPair

A ToneDeltaPair object specifying a tone delta constraint between two colors. One of them must be the color being constructed.

opacity

A function returning the opacity of a color, as a number between 0 and 1.

Constructors

Link copied to clipboard
constructor(name: String, palette: (DynamicScheme) -> TonalPalette, tone: (DynamicScheme) -> Double, isBackground: Boolean, background: (DynamicScheme) -> DynamicColor?, secondBackground: (DynamicScheme) -> DynamicColor?, contrastCurve: ContrastCurve?, toneDeltaPair: (DynamicScheme) -> ToneDeltaPair?, opacity: (DynamicScheme) -> Double? = null)

Strongly prefer using one of the convenience constructors. This class is arguably too flexible to ensure it can support any scenario. Functional arguments allow overriding without risks that come with subclasses.

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
val opacity: (DynamicScheme) -> Double? = null
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard

Functions

Link copied to clipboard
fun getArgb(scheme: DynamicScheme): Int

Returns an ARGB integer (i.e. a hex code).

Link copied to clipboard
fun getHct(scheme: DynamicScheme): Hct

Returns an HCT object.

Link copied to clipboard

Returns the tone in HCT, ranging from 0 to 100, of the resolved color given scheme.