Skip to content

Instantly share code, notes, and snippets.

@BCsl
Forked from martintreurnicht/ColorUtil
Created December 29, 2016 11:10
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 BCsl/08dac5effcb4d1d0180003e5b9b6619d to your computer and use it in GitHub Desktop.
Save BCsl/08dac5effcb4d1d0180003e5b9b6619d 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