DynamicColor class

A color that adjusts itself based on UI state provided 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

DynamicColor({required String name, required TonalPalette palette(DynamicScheme), required double tone(DynamicScheme), required bool isBackground, required DynamicColor background(DynamicScheme)?, required DynamicColor secondBackground(DynamicScheme)?, required ContrastCurve? contrastCurve, required ToneDeltaPair toneDeltaPair(DynamicScheme)?})
The base (explicit) constructor for DynamicColor.
DynamicColor.fromPalette({String name = '', required TonalPalette palette(DynamicScheme), required double tone(DynamicScheme), bool isBackground = false, DynamicColor background(DynamicScheme)?, DynamicColor secondBackground(DynamicScheme)?, ContrastCurve? contrastCurve, ToneDeltaPair toneDeltaPair(DynamicScheme)?})
The convenience constructor for DynamicColor.
factory

Properties

background → (DynamicColor Function(DynamicScheme)?)
final
contrastCurve ContrastCurve?
final
hashCode int
The hash code for this object.
no setterinherited
isBackground bool
final
name String
final
palette TonalPalette Function(DynamicScheme)
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
secondBackground → (DynamicColor Function(DynamicScheme)?)
final
tone double Function(DynamicScheme)
final
toneDeltaPair → (ToneDeltaPair Function(DynamicScheme)?)
final

Methods

getArgb(DynamicScheme scheme) int
Return a ARGB integer (i.e. a hex code). scheme Defines the conditions of the user interface, for example, whether or not it is dark mode or light mode, and what the desired contrast level is.
getHct(DynamicScheme scheme) Hct
Return a color, expressed in the HCT color space, that this DynamicColor is under the conditions in scheme.
getTone(DynamicScheme scheme) double
Return a tone, T in the HCT color space, that this DynamicColor is under the conditions in scheme.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

enableLightForeground(double tone) double
Adjust a tone such that white has 4.5 contrast, if the tone is reasonably close to supporting it.
foregroundTone(double bgTone, double ratio) double
Given a background tone, find a foreground tone, while ensuring they reach a contrast ratio that is as close to ratio as possible.
toneAllowsLightForeground(double tone) bool
Returns whether tone can reach a contrast ratio of 4.5 with a lighter color.
tonePrefersLightForeground(double tone) bool
Returns whether tone prefers a light foreground.