Skip to content

Instantly share code, notes, and snippets.

@CrandellWS
Created March 13, 2017 10:24
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 CrandellWS/a061b029fec961ed35ab89ce74b73b67 to your computer and use it in GitHub Desktop.
Save CrandellWS/a061b029fec961ed35ab89ce74b73b67 to your computer and use it in GitHub Desktop.
Code Sample
public Drawable applyRandomColorToDrawable(Drawable image) {
if (image != null) {
// PorterDuffColorFilter porterDuffColorFilter = new PorterDuffColorFilter(randomColorInt(),
PorterDuffColorFilter porterDuffColorFilter = new PorterDuffColorFilter(getRandomAlphaColorInt(),
PorterDuff.Mode.SRC_ATOP);
image.setColorFilter(porterDuffColorFilter);
}
return image;
}
public static int randomColorInt() {
float[] TEMP_HSL = new float[]{0, 0, 0};
float[] hsl = TEMP_HSL;
hsl[0] = (float) (Math.random() * 360);
hsl[1] = (float) (40 + (Math.random() * 60));
hsl[2] = (float) (40 + (Math.random() * 60));
return ColorUtils.HSLToColor(hsl);
}
public static String[] getRandomColor() {
Random random = new Random();
int RGB = 0xff + 1;
String[] colors = new String[2];
int a = 256;
int r1 = (int) Math.floor(Math.random() * RGB);
int r2 = (int) Math.floor(Math.random() * RGB);
int r3 = (int) Math.floor(Math.random() * RGB);
colors[0] = String.format("#%08X", (0xFFFFFFFF & Color.rgb(r1, r2, r3)));
if ((r1 + r2 + r3) > 450) {
colors[1] = "#222222";
} else {
colors[1] = "#ffffff";
}
return colors;
}
// public static int getRandomAlphaColorInt(int alpha){
public static int getRandomAlphaColorInt() {
int r = (int) (0xff * Math.random());
int g = (int) (0xff * Math.random());
int b = (int) (0xff * Math.random());
// return Color.argb(alpha, r, g, b);
return Color.argb(0xff, r, g, b);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment