MaterialColors

public class MaterialColors


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

Summary

Constants

static final float
static final float
static final float
ALPHA_FULL = 1.0f
static final float
ALPHA_LOW = 0.32f
static final float
ALPHA_MEDIUM = 0.54f

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(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, 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(
    Context context,
    int colorAttributeResId,
    String errorMessageComponent
)

Returns the color int for the provided theme color attribute.

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 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(int backgroundColor, int overlayColor)

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

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.

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

Convenience method that calculates layer without an overlayAlpha value by passing in 1f for the alpha value.

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

Convenience method that wraps layer for layering colors from theme attributes.

Constants

ALPHA_DISABLED

public static final float ALPHA_DISABLED = 0.38f

ALPHA_DISABLED_LOW

public static final float ALPHA_DISABLED_LOW = 0.12f

ALPHA_FULL

public static final float ALPHA_FULL = 1.0f

ALPHA_LOW

public static final float ALPHA_LOW = 0.32f

ALPHA_MEDIUM

public static final float ALPHA_MEDIUM = 0.54f

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
int originalARGB

The original color.

int alpha

The additional alpha [0-255].

Returns
int

The blended color.

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.

Throws
java.lang.IllegalArgumentException

if the attribute is not set in the current theme.

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.

getColor

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

Returns the color int for the provided theme color attribute.

Throws
java.lang.IllegalArgumentException

if the attribute is not set in the current theme.

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.

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.

getColorRoles

public static ColorRoles getColorRoles(int color, boolean isLightTheme)

Returns the ColorRoles object generated from the provided input color.

Parameters
int color

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

boolean isLightTheme

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

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.

int color

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

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.

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.

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
int colorToHarmonize

The color to harmonize.

int colorToHarmonizeWith

The primary color selected for harmonization.

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.

int colorToHarmonize

The color to harmonize.

isColorLight

public static boolean isColorLight(int color)

Determines if a color should be considered light or dark.

layer

public static int layer(int backgroundColor, int overlayColor)

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

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.

layer

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

Convenience method that calculates layer without an overlayAlpha value by passing in 1f for the alpha value.

layer

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

Convenience method that wraps layer for layering colors from theme attributes.