Color utilities
Background colors
To all but ensure proper contrast of text to background, we recommend using our pre-defined shades of background colors based on the EuiTheme brand colors. You can also use EuiPanel for the same effect plus more options like padding and rounded corners.
useEuiBackgroundColorCSS()[color]
style hook
Returns an object of the available color keys containing the css string of the computed background version for the given color
.
This is best used to map component prop styles to padding output.
color: 'transparent' | 'plain' | 'subdued' | 'accent' | 'primary' | 'success' | 'warning' | 'danger';
useEuiBackgroundColor(color, method?)
hook
Returns just the computed background color for the given color
.
color: 'transparent' | 'plain' | 'subdued' | 'accent' | 'primary' | 'success' | 'warning' | 'danger';
method? 'opaque' | 'transparent';
Different colors for different contexts
While the hook accepts rendering the value as opaque or transparent, we highly suggest reserving transparent for use only during interactive states like hover and focus.
Sample | Token | Value |
---|---|---|
useEuiBackgroundColor('transparent') | Value transparent | |
useEuiBackgroundColor('plain') | Value #FFF | |
useEuiBackgroundColor('subdued') | Value #fff | |
useEuiBackgroundColor('accent') | Value #feedf5 | |
useEuiBackgroundColor('primary') | Value #e6f1fa | |
useEuiBackgroundColor('success') | Value #e6f9f7 | |
useEuiBackgroundColor('warning') | Value #fff9e8 | |
useEuiBackgroundColor('danger') | Value #f8e9e9 |
Utilities
Contrast
Note that color contrast cannot be accurately detected when using transparency (colors with alpha channels).
makeHighContrastColor(foreground, ratio)(background)
function
Use this function to calculate the appropriate foreground color (usually text) based on a background color.
foreground: string;
ratio?: number = 4.5;
background?: string = euiTheme.colors.body;
If you want to use the same background color that the EUI theme uses for all of its contrast calculations, you can pass in the euiTheme
as the background.
isColorDark(red, green, blue)
function
Use this function to determine whether or not to use light or dark text against the given background color. It requires the values to be passed in as rgb integers and will return a boolean
if the color is dark based on luminance.
If the function returns true
, use euiTheme.colors.ghost
otherwise use euiTheme.colors.ink
as the text color.
red: 0-255;
green: 0-255;
blue: 0-255;
Transparency
transparentize(color, alpha)
function
Use this function to convert any color to rgba()
with the provided alpha value.
color: string;
alpha: decimal = 0-1;