Class DynamicBimodalPaletteColor
- java.lang.Object
-
- org.pushingpixels.ephemeral.chroma.dynamiccolor.DynamicBimodalPaletteColor
-
public final class DynamicBimodalPaletteColor extends java.lang.Object
A color that adjusts itself based on UI state, represented by DynamicBimodalPalette.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 DynamicBimodalPalette 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.
-
-
Field Summary
Fields Modifier and Type Field Description java.util.function.Function<DynamicBimodalPalette,DynamicBimodalPaletteColor>
background
ContrastCurve
contrastCurve
private java.util.HashMap<DynamicBimodalPalette,Hct>
hctCache
boolean
isBackground
boolean
isInverse
java.lang.String
name
java.util.function.Function<DynamicBimodalPalette,java.lang.Double>
opacity
java.util.function.Function<DynamicBimodalPalette,java.lang.Double>
tone
-
Constructor Summary
Constructors Constructor Description DynamicBimodalPaletteColor(java.lang.String name, java.util.function.Function<DynamicBimodalPalette,java.lang.Double> tone, boolean isBackground, boolean isInverse, java.util.function.Function<DynamicBimodalPalette,DynamicBimodalPaletteColor> background, ContrastCurve contrastCurve)
A constructor for DynamicColor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static double
enableLightForeground(double tone)
Adjust a tone down such that white has 4.5 contrast, if the tone is reasonably close to supporting it.static double
foregroundTone(double bgTone, double ratio, boolean allowDynamicPreference, boolean isDark)
Given a background tone, find a foreground tone, while ensuring they reach a contrast ratio that is as close to ratio as possible.int
getArgb(DynamicBimodalPalette palette)
Returns an ARGB integer (i.e.Hct
getHct(DynamicBimodalPalette palette)
Returns an HCT object.double
getTone(DynamicBimodalPalette palette)
Returns the tone in HCT, ranging from 0 to 100, of the resolved color given palette.static boolean
toneAllowsLightForeground(double tone)
Tones less than ~T50 always permit white at 4.5 contrast.static boolean
tonePrefersLightForeground(double tone)
People prefer white foregrounds on ~T60-70.
-
-
-
Field Detail
-
name
public final java.lang.String name
-
tone
public final java.util.function.Function<DynamicBimodalPalette,java.lang.Double> tone
-
isBackground
public final boolean isBackground
-
isInverse
public final boolean isInverse
-
background
public final java.util.function.Function<DynamicBimodalPalette,DynamicBimodalPaletteColor> background
-
contrastCurve
public final ContrastCurve contrastCurve
-
opacity
public final java.util.function.Function<DynamicBimodalPalette,java.lang.Double> opacity
-
hctCache
private final java.util.HashMap<DynamicBimodalPalette,Hct> hctCache
-
-
Constructor Detail
-
DynamicBimodalPaletteColor
public DynamicBimodalPaletteColor(java.lang.String name, java.util.function.Function<DynamicBimodalPalette,java.lang.Double> tone, boolean isBackground, boolean isInverse, java.util.function.Function<DynamicBimodalPalette,DynamicBimodalPaletteColor> background, ContrastCurve contrastCurve)
A constructor for DynamicColor._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.
For example, the default behavior of adjust tone at max contrast to be at a 7.0 ratio with its background is principled and matches accessibility guidance. That does not mean it's the desired approach for _every_ design system, and every color pairing, always, in every case.
For opaque colors (colors with alpha = 100%).
- Parameters:
name
- The name of the dynamic color.tone
- Function that provides a tone, given a DynamicBimodalPalette.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 `DynamicBimodalPalette`), if it exists.contrastCurve
- A `ContrastCurve` object specifying how its contrast against its background should behave in various contrast levels options.
-
-
Method Detail
-
getArgb
public int getArgb(DynamicBimodalPalette palette)
Returns an ARGB integer (i.e. a hex code).- Parameters:
palette
- 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
public Hct getHct(DynamicBimodalPalette palette)
Returns an HCT object.- Parameters:
palette
- 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.
-
getTone
public double getTone(DynamicBimodalPalette palette)
Returns the tone in HCT, ranging from 0 to 100, of the resolved color given palette.
-
foregroundTone
public static double foregroundTone(double bgTone, double ratio, boolean allowDynamicPreference, boolean isDark)
Given a background tone, find a foreground tone, while ensuring they reach a contrast ratio that is as close to ratio as possible.
-
enableLightForeground
public static double enableLightForeground(double tone)
Adjust a tone down such that white has 4.5 contrast, if the tone is reasonably close to supporting it.
-
tonePrefersLightForeground
public static boolean tonePrefersLightForeground(double tone)
People prefer white foregrounds on ~T60-70. Observed over time, and also by Andrew Somers during research for APCA.T60 used as to create the smallest discontinuity possible when skipping down to T49 in order to ensure light foregrounds.
Since `tertiaryContainer` in dark monochrome scheme requires a tone of 60, it should not be adjusted. Therefore, 60 is excluded here.
-
toneAllowsLightForeground
public static boolean toneAllowsLightForeground(double tone)
Tones less than ~T50 always permit white at 4.5 contrast.
-
-