Class DynamicColorUtils

java.lang.Object
com.pranavpandey.android.dynamic.util.DynamicColorUtils

public class DynamicColorUtils extends Object
Helper class to change colors dynamically.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    adjustAlpha(int color, float factor)
    Adjust alpha of a color according to the given parameter.
    static float
    calculateContrast(int color1, int color2)
    Calculate color contrast difference between two colors based on the luminance value according to XYZ color space.
    static int
    CMYKToRGB(float cyan, float magenta, float yellow, float black)
    Generate the color integer from the CMYK color space.
    static void
    colorToCMYK(int color, float[] cmyk)
    Generate the CMYK color space from the color integer.
    static int
    getAccentColor(int color)
    Calculate accent color based on the given color for android theme generation.
    static double
    getColorDarkness(int color)
    Calculate darkness of a color.
    static String
    getColorString(int color, boolean includeAlpha, boolean includeHash)
    Get hexadecimal string from the color integer.
    static int
    getContrastColor(int color, int contrastWith)
    Calculate contrast of a color based on the given base color so that it will always be visible on top of the base color.
    static int
    getContrastColor(int color, int contrastWith, float visibleContrast)
    Calculate contrast of a color based on the given base color so that it will always be visible on top of the base color.
    static int
    getContrastColor(int color, int contrastWith, float visibleContrast, boolean recursive)
    Calculate contrast of a color based on the given base color so that it will always be visible on top of the base color.
    static int
    getDarkerColor(int color, float amount)
    Darkens a color by a given amount.
    static int
    getDarkerColor(int color, float amount, boolean adjust)
    Darkens a color by a given amount.
    static int
    Calculate less visible color of a given color.
    static int
    getLighterColor(int color, float amount)
    Lightens a color by a given amount.
    static int
    getLighterColor(int color, float amount, boolean adjust)
    Lightens a color by a given amount.
    static int
    Generate a random rgb color.
    static int
    getRandomColor(int color)
    Generate a random rgb color by comparing a given color.
    static int
    getStateColor(int color, float lightenBy, float darkenBy)
    Lightens or darkens a color by a given amount.
    static int
    getTintColor(int color)
    Calculate tint based on a given color for better readability.
    static int
    getTintColor(int color, float visibleContrast)
    Calculate tint based on a given color for better readability.
    static boolean
    isAlpha(int color)
    Checks whether the color has alpha component.
    static boolean
    isColorDark(int color)
    Detect light or dark color.
    static boolean
    isTranslucent(int color)
    Checks whether the color is translucent.
    static int
    removeAlpha(int color)
    Remove alpha from a color.
    static int
    replaceColor(int color, int with)
    Replace HSV components of a color integer.
    static int
    setAlpha(int color, int alpha)
    Set alpha to a color.
    static int
    shiftColor(int color, float by)
    Shift a color according to the given parameter.
    static int
    shiftColor(int color, float shiftLightBy, float shiftDarkBy)
    Shift a color according to the supplied parameters.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • DynamicColorUtils

      public DynamicColorUtils()
  • Method Details

    • getRandomColor

      @ColorInt public static int getRandomColor()
      Generate a random rgb color.
      Returns:
      The randomly generated color.
      See Also:
    • getRandomColor

      @ColorInt public static int getRandomColor(@ColorInt int color)
      Generate a random rgb color by comparing a given color.
      Parameters:
      color - The color to compare.
      Returns:
      The randomly generated color.
    • adjustAlpha

      @ColorInt public static int adjustAlpha(@ColorInt int color, float factor)
      Adjust alpha of a color according to the given parameter.
      Parameters:
      color - The color to adjust the alpha.
      factor - The factor in float to be set.
      Returns:
      The color with adjusted alpha.
    • isAlpha

      public static boolean isAlpha(@ColorInt int color)
      Checks whether the color has alpha component.
      Parameters:
      color - The color to be checked for alpha component.
      Returns:
      true if the color has alpha component.
    • setAlpha

      @ColorInt public static int setAlpha(@ColorInt int color, @IntRange(from=0L,to=255L) int alpha)
      Set alpha to a color.
      Parameters:
      color - The color to set the alpha.
      alpha - The alpha to be set.
      Returns:
      The color with alpha.
    • removeAlpha

      @ColorInt public static int removeAlpha(@ColorInt int color)
      Remove alpha from a color.
      Parameters:
      color - The color to remove the alpha.
      Returns:
      The color without alpha.
    • isTranslucent

      public static boolean isTranslucent(@ColorInt int color)
      Checks whether the color is translucent.
      Parameters:
      color - The color to be checked for transparency.
      Returns:
      true if the color is translucent.
    • getColorDarkness

      public static double getColorDarkness(@ColorInt int color)
      Calculate darkness of a color.
      Parameters:
      color - The color whose darkness to be calculated.
      Returns:
      The darkness of color (less than or equal to 1).

      0 for white and 1 for black.

    • isColorDark

      public static boolean isColorDark(@ColorInt int color)
      Detect light or dark color.
      Parameters:
      color - The color whose darkness to be calculated.
      Returns:
      true if color is dark.
    • getDarkerColor

      @ColorInt public static int getDarkerColor(@ColorInt int color, @FloatRange(from=0.0,to=1.0) float amount, boolean adjust)
      Darkens a color by a given amount.
      Parameters:
      color - The color to be darkened.
      amount - The amount to darken the color.

      0 will leave the color unchanged.

      1 will make the color completely black.

      adjust - true to adjust the visible contrast.
      Returns:
      The darker color.
    • getDarkerColor

      @ColorInt public static int getDarkerColor(@ColorInt int color, @FloatRange(from=0.0,to=1.0) float amount)
      Darkens a color by a given amount.
      Parameters:
      color - The color to be darkened.
      amount - The amount to darken the color.

      0 will leave the color unchanged.

      1 will make the color completely black.

      Returns:
      The darker color.
    • getLighterColor

      @ColorInt public static int getLighterColor(@ColorInt int color, @FloatRange(from=0.0,to=1.0) float amount, boolean adjust)
      Lightens a color by a given amount.
      Parameters:
      color - The color to be lightened.
      amount - The amount to lighten the color.

      0 will leave the color unchanged.

      1 will make the color completely white.

      adjust - true to adjust the visible contrast.
      Returns:
      The lighter color.
    • getLighterColor

      @ColorInt public static int getLighterColor(@ColorInt int color, @FloatRange(from=0.0,to=1.0) float amount)
      Lightens a color by a given amount.
      Parameters:
      color - The color to be lightened.
      amount - The amount to lighten the color.

      0 will leave the color unchanged.

      1 will make the color completely white.

      Returns:
      The lighter color.
    • shiftColor

      @ColorInt public static int shiftColor(@ColorInt int color, @FloatRange(from=0.0,to=2.0) float by)
      Shift a color according to the given parameter.

      Useful to create different color states.

      Parameters:
      color - The color to be shifted.
      by - The factor in float by which shift the color.
      Returns:
      The shifted color.
    • shiftColor

      @ColorInt public static int shiftColor(@ColorInt int color, @FloatRange(from=0.0,to=2.0) float shiftLightBy, @FloatRange(from=0.0,to=2.0) float shiftDarkBy)
      Shift a color according to the supplied parameters.

      The shifted color will be lighter for a dark color and vice versa.

      Parameters:
      color - The color to be shifted.
      shiftLightBy - The factor in float by which shift the light color.
      shiftDarkBy - The factor in float by which shift the dark color.
      Returns:
      The shifted color.
    • getAccentColor

      @ColorInt public static int getAccentColor(@ColorInt int color)
      Calculate accent color based on the given color for android theme generation.

      Still in beta, so sometimes may produce inaccurate colors.

      Parameters:
      color - The color whose accent color to be calculated.
      Returns:
      The accent color based on the given color.
    • calculateContrast

      public static float calculateContrast(@ColorInt int color1, @ColorInt int color2)
      Calculate color contrast difference between two colors based on the luminance value according to XYZ color space.
      Parameters:
      color1 - The first color to calculate the contrast difference.
      color2 - The second color to calculate the contrast difference.
      Returns:
      The color contrast between the two colors.
      See Also:
      • calculateXyzLuma(int)
    • getTintColor

      @ColorInt public static int getTintColor(@ColorInt int color, @FloatRange(from=0.0,to=1.0) float visibleContrast)
      Calculate tint based on a given color for better readability.
      Parameters:
      color - The color whose tint to be calculated.
      visibleContrast - The acceptable contrast between the two colors.
      Returns:
      The tint of the given color.
      See Also:
    • getTintColor

      @ColorInt public static int getTintColor(@ColorInt int color)
      Calculate tint based on a given color for better readability.
      Parameters:
      color - The color whose tint to be calculated.
      Returns:
      The tint of the given color.
      See Also:
    • getStateColor

      @ColorInt public static int getStateColor(@ColorInt int color, @FloatRange(from=0.0,to=1.0) float lightenBy, @FloatRange(from=0.0,to=1.0) float darkenBy)
      Lightens or darkens a color by a given amount.
      Parameters:
      color - The color to be lighten or darken.
      lightenBy - The amount to lighten the color.

      0 will leave the color unchanged.

      1 will make the color completely white.

      darkenBy - The amount to darken the color.

      0 will leave the color unchanged.

      1 will make the color completely black.

      Returns:
      The state color.
    • getContrastColor

      @ColorInt public static int getContrastColor(@ColorInt int color, @ColorInt int contrastWith, @FloatRange(from=0.0,to=1.0) float visibleContrast, boolean recursive)
      Calculate contrast of a color based on the given base color so that it will always be visible on top of the base color.
      Parameters:
      color - The color whose contrast to be calculated.
      contrastWith - The background color to calculate the contrast.
      visibleContrast - The acceptable contrast between the two colors.
      recursive - true to improve contrast by recursion.

      It must be used with caution to avoid infinite loop.

      Returns:
      The contrast of the given color according to the base color.
    • getContrastColor

      @ColorInt public static int getContrastColor(@ColorInt int color, @ColorInt int contrastWith, @FloatRange(from=0.0,to=1.0) float visibleContrast)
      Calculate contrast of a color based on the given base color so that it will always be visible on top of the base color.
      Parameters:
      color - The color whose contrast to be calculated.
      contrastWith - The background color to calculate the contrast.
      visibleContrast - The acceptable contrast between the two colors.
      Returns:
      The contrast of the given color according to the base color.
      See Also:
    • getContrastColor

      @ColorInt public static int getContrastColor(@ColorInt int color, @ColorInt int contrastWith)
      Calculate contrast of a color based on the given base color so that it will always be visible on top of the base color.
      Parameters:
      color - The color whose contrast to be calculated.
      contrastWith - The background color to calculate the contrast.
      Returns:
      The contrast of the given color according to the base color.
      See Also:
    • getLessVisibleColor

      @ColorInt public static int getLessVisibleColor(@ColorInt int color)
      Calculate less visible color of a given color.

      Useful to create unselected or disabled color states.

      Parameters:
      color - The color whose less visible color to be calculated.
      Returns:
      The less visible color by shifting the color.
    • getColorString

      public static String getColorString(@ColorInt int color, boolean includeAlpha, boolean includeHash)
      Get hexadecimal string from the color integer.
      Parameters:
      color - The color to get the hex code.
      includeAlpha - true to include alpha in the string.
      includeHash - true to include # in the string.
      Returns:
      The hexadecimal string equivalent of the supplied color integer.
    • CMYKToRGB

      @ColorInt public static int CMYKToRGB(@FloatRange(from=0.0,to=100.0) float cyan, @FloatRange(from=0.0,to=100.0) float magenta, @FloatRange(from=0.0,to=100.0) float yellow, @FloatRange(from=0.0,to=100.0) float black)
      Generate the color integer from the CMYK color space.
      Parameters:
      cyan - The cyan color component of the CMYK color space.
      magenta - The magenta color component of the CMYK color space.
      yellow - The yellow color component of the CMYK color space.
      black - The black color component of the CMYK color space.
      Returns:
      The generated color integer from the CMYK color space.
    • colorToCMYK

      public static void colorToCMYK(@ColorInt int color, @NonNull @Size(4L) float[] cmyk)
      Generate the CMYK color space from the color integer.
      Parameters:
      color - The color to generate the CMYK color space.
      cmyk - The array to store the CMYK color space.
    • replaceColor

      @ColorInt public static int replaceColor(@ColorInt int color, @ColorInt int with)
      Replace HSV components of a color integer.
      Parameters:
      color - The color to be replaced.
      with - The color with replacements.
      Returns:
      The color with replaced HSV components.