MaterialColors

public class MaterialColors
extends Object

java.lang.Object
   ↳ com.google.android.material.color.MaterialColors


A utility class for common color variants used in Material themes.

Summary

Constants

float ALPHA_DISABLED

float ALPHA_DISABLED_LOW

float ALPHA_FULL

float ALPHA_LOW

float ALPHA_MEDIUM

Public methods

static int compositeARGBWithAlpha(int originalARGB, int alpha)

Calculates a new color by multiplying an additional alpha int value to the alpha channel of a color in integer type.

static int getColor(Context context, int colorAttributeResId, int defaultValue)

Returns the color int for the provided theme color attribute, or the default value if the attribute is not set in the current theme.

static int getColor(View view, int colorAttributeResId, int defaultValue)

Returns the color int for the provided theme color attribute, or the default value if the attribute is not set in the current theme, using the view's Context.

static int getColor(View view, int colorAttributeResId)

Returns the color int for the provided theme color attribute, using the Context of the provided view.

static int getColor(Context context, int colorAttributeResId, String errorMessageComponent)

Returns the color int for the provided theme color attribute.

static Integer getColorOrNull(Context context, int colorAttributeResId)

Returns the color int for the provided theme color attribute, or null if the attribute is not set in the current theme.

static ColorRoles getColorRoles(int color, boolean isLightTheme)

Returns the ColorRoles object generated from the provided input color.

static ColorRoles getColorRoles(Context context, int color)

Returns the ColorRoles object generated from the provided input color.

static ColorStateList getColorStateList(Context context, int colorAttributeResId, ColorStateList defaultValue)

Returns the color state list for the provided theme color attribute, or the default value if the attribute is not set in the current theme.

static ColorStateList getColorStateListOrNull(Context context, int colorAttributeResId)

Returns the color state list for the provided theme color attribute, or null if the attribute is not set in the current theme.

static int harmonize(int colorToHarmonize, int colorToHarmonizeWith)

A convenience function to harmonize any two colors provided, returns the color int of the harmonized color, or the original design color value if color harmonization is not available.

static int harmonizeWithPrimary(Context context, int colorToHarmonize)

Returns the color int of the given color harmonized with the context theme's colorPrimary.

static boolean isColorLight(int color)

Determines if a color should be considered light or dark.

static int layer(View view, int backgroundColorAttributeResId, int overlayColorAttributeResId, float overlayAlpha)

Convenience method that wraps layer(int, int, float) for layering colors from theme attributes.

static int layer(int backgroundColor, int overlayColor)

Calculates a color that represents the layering of the overlayColor on top of the backgroundColor.

static int layer(View view, int backgroundColorAttributeResId, int overlayColorAttributeResId)

Convenience method that calculates layer(View, int, int, float) without an overlayAlpha value by passing in 1f for the alpha value.

static int layer(int backgroundColor, int overlayColor, float overlayAlpha)

Calculates a color that represents the layering of the overlayColor (with overlayAlpha applied) on top of the backgroundColor.

Inherited methods

Constants

ALPHA_DISABLED

public static final float ALPHA_DISABLED

Constant Value: 0.38

ALPHA_DISABLED_LOW

public static final float ALPHA_DISABLED_LOW

Constant Value: 0.12

ALPHA_FULL

public static final float ALPHA_FULL

Constant Value: 1.0

ALPHA_LOW

public static final float ALPHA_LOW

Constant Value: 0.32

ALPHA_MEDIUM

public static final float ALPHA_MEDIUM

Constant Value: 0.54

Public methods

compositeARGBWithAlpha

public static int compositeARGBWithAlpha (int originalARGB, 
                int alpha)

Calculates a new color by multiplying an additional alpha int value to the alpha channel of a color in integer type.

Parameters
originalARGB int: The original color.

alpha int: The additional alpha [0-255].

Returns
int The blended color.

getColor

public static int getColor (Context context, 
                int colorAttributeResId, 
                int defaultValue)

Returns the color int for the provided theme color attribute, or the default value if the attribute is not set in the current theme.

Parameters
context Context

colorAttributeResId int

defaultValue int

Returns
int

getColor

public static int getColor (View view, 
                int colorAttributeResId, 
                int defaultValue)

Returns the color int for the provided theme color attribute, or the default value if the attribute is not set in the current theme, using the view's Context.

Parameters
view View

colorAttributeResId int

defaultValue int

Returns
int

getColor

public static int getColor (View view, 
                int colorAttributeResId)

Returns the color int for the provided theme color attribute, using the Context of the provided view.

Parameters
view View

colorAttributeResId int

Returns
int

Throws
IllegalArgumentException if the attribute is not set in the current theme.

getColor

public static int getColor (Context context, 
                int colorAttributeResId, 
                String errorMessageComponent)

Returns the color int for the provided theme color attribute.

Parameters
context Context

colorAttributeResId int

errorMessageComponent String

Returns
int

Throws
IllegalArgumentException if the attribute is not set in the current theme.

getColorOrNull

public static Integer getColorOrNull (Context context, 
                int colorAttributeResId)

Returns the color int for the provided theme color attribute, or null if the attribute is not set in the current theme.

Parameters
context Context

colorAttributeResId int

Returns
Integer

getColorRoles

public static ColorRoles getColorRoles (int color, 
                boolean isLightTheme)

Returns the ColorRoles object generated from the provided input color.

Parameters
color int: The input color provided for generating its associated four color roles.

isLightTheme boolean: Whether the input is light themed or not, true if light theme is enabled.

Returns
ColorRoles

getColorRoles

public static ColorRoles getColorRoles (Context context, 
                int color)

Returns the ColorRoles object generated from the provided input color.

Parameters
context Context: The target context.

color int: The input color provided for generating its associated four color roles.

Returns
ColorRoles

getColorStateList

public static ColorStateList getColorStateList (Context context, 
                int colorAttributeResId, 
                ColorStateList defaultValue)

Returns the color state list for the provided theme color attribute, or the default value if the attribute is not set in the current theme.

Parameters
context Context

colorAttributeResId int

defaultValue ColorStateList

Returns
ColorStateList

getColorStateListOrNull

public static ColorStateList getColorStateListOrNull (Context context, 
                int colorAttributeResId)

Returns the color state list for the provided theme color attribute, or null if the attribute is not set in the current theme.

Parameters
context Context

colorAttributeResId int

Returns
ColorStateList

harmonize

public static int harmonize (int colorToHarmonize, 
                int colorToHarmonizeWith)

A convenience function to harmonize any two colors provided, returns the color int of the harmonized color, or the original design color value if color harmonization is not available.

Parameters
colorToHarmonize int: The color to harmonize.

colorToHarmonizeWith int: The primary color selected for harmonization.

Returns
int

harmonizeWithPrimary

public static int harmonizeWithPrimary (Context context, 
                int colorToHarmonize)

Returns the color int of the given color harmonized with the context theme's colorPrimary.

Parameters
context Context: The target context.

colorToHarmonize int: The color to harmonize.

Returns
int

isColorLight

public static boolean isColorLight (int color)

Determines if a color should be considered light or dark.

Parameters
color int

Returns
boolean

layer

public static int layer (View view, 
                int backgroundColorAttributeResId, 
                int overlayColorAttributeResId, 
                float overlayAlpha)

Convenience method that wraps layer(int, int, float) for layering colors from theme attributes.

Parameters
view View

backgroundColorAttributeResId int

overlayColorAttributeResId int

overlayAlpha float

Returns
int

layer

public static int layer (int backgroundColor, 
                int overlayColor)

Calculates a color that represents the layering of the overlayColor on top of the backgroundColor.

Parameters
backgroundColor int

overlayColor int

Returns
int

layer

public static int layer (View view, 
                int backgroundColorAttributeResId, 
                int overlayColorAttributeResId)

Convenience method that calculates layer(View, int, int, float) without an overlayAlpha value by passing in 1f for the alpha value.

Parameters
view View

backgroundColorAttributeResId int

overlayColorAttributeResId int

Returns
int

layer

public static int layer (int backgroundColor, 
                int overlayColor, 
                float overlayAlpha)

Calculates a color that represents the layering of the overlayColor (with overlayAlpha applied) on top of the backgroundColor.

Parameters
backgroundColor int

overlayColor int

overlayAlpha float

Returns
int