Class DynamicColorUtils
- java.lang.Object
-
- com.pranavpandey.android.dynamic.util.DynamicColorUtils
-
public class DynamicColorUtils extends java.lang.ObjectHelper class to change colors dynamically.
-
-
Constructor Summary
Constructors Constructor Description DynamicColorUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static intadjustAlpha(int color, float factor)Adjust alpha of a color according to the given parameter.static floatcalculateContrast(int color1, int color2)Calculate color contrast difference between two colors based on the luminance value according to XYZ color space.static intCMYKToRGB(float cyan, float magenta, float yellow, float black)Generate the color integer from the CMYK color space.static voidcolorToCMYK(int color, float[] cmyk)Generate the CMYK color space from the color integer.static intgetAccentColor(int color)Calculate accent color based on the given color for android theme generation.static doublegetColorDarkness(int color)Calculate darkness of a color.static java.lang.StringgetColorString(int color, boolean includeAlpha, boolean includeHash)Get hexadecimal string from the color integer.static intgetContrastColor(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 intgetContrastColor(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 intgetContrastColor(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 intgetDarkerColor(int color, float amount)Darkens a color by a given amount.static intgetDarkerColor(int color, float amount, boolean adjust)Darkens a color by a given amount.static intgetLessVisibleColor(int color)Calculate less visible color of a given color.static intgetLighterColor(int color, float amount)Lightens a color by a given amount.static intgetLighterColor(int color, float amount, boolean adjust)Lightens a color by a given amount.static intgetRandomColor()Generate a random rgb color.static intgetRandomColor(int color)Generate a random rgb color by comparing a given color.static intgetStateColor(int color, float lightenBy, float darkenBy)Lightens or darkens a color by a given amount.static intgetTintColor(int color)Calculate tint based on a given color for better readability.static intgetTintColor(int color, float visibleContrast)Calculate tint based on a given color for better readability.static booleanisAlpha(int color)Checks whether the color has alpha component.static booleanisColorDark(int color)Detect light or dark color.static booleanisTranslucent(int color)Checks whether the color is translucent.static intremoveAlpha(int color)Remove alpha from a color.static intsetAlpha(int color, int alpha)Set alpha to a color.static intshiftColor(int color, float by)Shift a color according to the given parameter.static intshiftColor(int color, float shiftLightBy, float shiftDarkBy)Shift a color according to the supplied parameters.
-
-
-
Method Detail
-
getRandomColor
@ColorInt public static int getRandomColor()
Generate a random rgb color.- Returns:
- The randomly generated color.
- See Also:
Random,Color.HSVToColor(float[])
-
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:
trueif 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:
trueif 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).
0for white and1for black.
-
isColorDark
public static boolean isColorDark(@ColorInt int color)Detect light or dark color.- Parameters:
color- The color whose darkness to be calculated.- Returns:
trueif 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.0will leave the color unchanged.1will make the color completely black.adjust-trueto 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.0will leave the color unchanged.1will 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.0will leave the color unchanged.1will make the color completely white.adjust-trueto 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.0will leave the color unchanged.1will 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:
getContrastColor(int, int, float)
-
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:
getContrastColor(int, int)
-
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.0will leave the color unchanged.1will make the color completely white.darkenBy- The amount to darken the color.0will leave the color unchanged.1will 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-trueto 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(int, int, float, boolean)
-
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:
getContrastColor(int, int, float)
-
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 java.lang.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-trueto include alpha in the string.includeHash-trueto 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.
-
-