DynamicColor

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.

Constructors

Link copied to clipboard
constructor(@NonNull name: String, @NonNull palette: (DynamicScheme) -> TonalPalette, @NonNull tone: (DynamicScheme) -> Double, isBackground: Boolean, @Nullable background: (DynamicScheme) -> DynamicColor, @Nullable secondBackground: (DynamicScheme) -> DynamicColor, @Nullable contrastCurve: ContrastCurve, @Nullable toneDeltaPair: (DynamicScheme) -> ToneDeltaPair)
A constructor for DynamicColor.
constructor(@NonNull name: String, @NonNull palette: (DynamicScheme) -> TonalPalette, @NonNull tone: (DynamicScheme) -> Double, isBackground: Boolean, @Nullable background: (DynamicScheme) -> DynamicColor, @Nullable secondBackground: (DynamicScheme) -> DynamicColor, @Nullable contrastCurve: ContrastCurve, @Nullable toneDeltaPair: (DynamicScheme) -> ToneDeltaPair, @Nullable opacity: (DynamicScheme) -> Double)

Properties

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard

Functions

Link copied to clipboard
Adjust a tone down such that white has 4.5 contrast, if the tone is reasonably close to supporting it.
Link copied to clipboard
open fun foregroundTone(bgTone: Double, ratio: Double): Double
Given a background tone, find a foreground tone, while ensuring they reach a contrast ratio that is as close to ratio as possible.
Link copied to clipboard
open fun fromArgb(@NonNull name: String, argb: Int): DynamicColor
Create a DynamicColor from a hex code.
Link copied to clipboard
open fun fromPalette(@NonNull name: String, @NonNull palette: (DynamicScheme) -> TonalPalette, @NonNull tone: (DynamicScheme) -> Double, isBackground: Boolean): DynamicColor
A convenience constructor for DynamicColor.
Link copied to clipboard
open fun getArgb(@NonNull scheme: DynamicScheme): Int
Returns an ARGB integer (i.e.
Link copied to clipboard
open fun getHct(@NonNull scheme: DynamicScheme): Hct
Returns an HCT object.
Link copied to clipboard
open fun getTone(@NonNull scheme: DynamicScheme): Double
Returns the tone in HCT, ranging from 0 to 100, of the resolved color given scheme.
Link copied to clipboard
Tones less than ~T50 always permit white at 4.5 contrast.
Link copied to clipboard
People prefer white foregrounds on ~T60-70.