Created
July 18, 2016 06:26
-
-
Save dp-singh/bb29459d0eb73e980783fcc343009f8d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Drawables { | |
@NonNull | |
public static Drawable getSelectableDrawableFor(int color) { | |
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { | |
StateListDrawable stateListDrawable = new StateListDrawable(); | |
stateListDrawable.addState( | |
new int[]{android.R.attr.state_pressed}, | |
new ColorDrawable(lightenOrDarken(color, 0.20D)) | |
); | |
stateListDrawable.addState( | |
new int[]{android.R.attr.state_focused}, | |
new ColorDrawable(lightenOrDarken(color, 0.40D)) | |
); | |
stateListDrawable.addState( | |
new int[]{}, | |
new ColorDrawable(color) | |
); | |
return stateListDrawable; | |
} else { | |
ColorStateList pressedColor = ColorStateList.valueOf(lightenOrDarken(color, 0.2D)); | |
ColorDrawable defaultColor = new ColorDrawable(color); | |
Drawable rippleColor = getRippleColor(color); | |
return new RippleDrawable( | |
pressedColor, | |
defaultColor, | |
rippleColor | |
); | |
} | |
} | |
@NonNull | |
private static Drawable getRippleColor(int color) { | |
float[] outerRadii = new float[8]; | |
Arrays.fill(outerRadii, 3); | |
RoundRectShape r = new RoundRectShape(outerRadii, null, null); | |
ShapeDrawable shapeDrawable = new ShapeDrawable(r); | |
shapeDrawable.getPaint().setColor(color); | |
return shapeDrawable; | |
} | |
private static int lightenOrDarken(int color, double fraction) { | |
if (canLighten(color, fraction)) { | |
return lighten(color, fraction); | |
} else { | |
return darken(color, fraction); | |
} | |
} | |
private 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); | |
} | |
private 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 boolean canLighten(int color, double fraction) { | |
int red = Color.red(color); | |
int green = Color.green(color); | |
int blue = Color.blue(color); | |
return canLightenComponent(red, fraction) | |
&& canLightenComponent(green, fraction) | |
&& canLightenComponent(blue, fraction); | |
} | |
private static boolean canLightenComponent(int colorComponent, double fraction) { | |
int red = Color.red(colorComponent); | |
int green = Color.green(colorComponent); | |
int blue = Color.blue(colorComponent); | |
return red + (red * fraction) < 255 | |
&& green + (green * fraction) < 255 | |
&& blue + (blue * fraction) < 255; | |
} | |
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