Skip to content

Instantly share code, notes, and snippets.

@moulichandu
Forked from martintreurnicht/ColorUtil
Created July 29, 2016 12:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moulichandu/72732c583f9d8ef6b95d708819893b0f to your computer and use it in GitHub Desktop.
Save moulichandu/72732c583f9d8ef6b95d708819893b0f to your computer and use it in GitHub Desktop.
Lighten and darken colors in android
public static int lighten(int color, double fraction) {
int red = Color.red(color);
int green = Color.green(color);
int blue = Color.blue(color);
red = lightenColor(red, fraction);
green = lightenColor(green, fraction);
blue = lightenColor(blue, fraction);
int alpha = Color.alpha(color);
return Color.argb(alpha, red, green, blue);
}
public static int darken(int color, double fraction) {
int red = Color.red(color);
int green = Color.green(color);
int blue = Color.blue(color);
red = darkenColor(red, fraction);
green = darkenColor(green, fraction);
blue = darkenColor(blue, fraction);
int alpha = Color.alpha(color);
return Color.argb(alpha, red, green, blue);
}
private static int darkenColor(int color, double fraction) {
return (int)Math.max(color - (color * fraction), 0);
}
private static int lightenColor(int color, double fraction) {
return (int) Math.min(color + (color * fraction), 255);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment