Skip to content

Instantly share code, notes, and snippets.

@odedhb
Created January 6, 2015 11:24
Show Gist options
  • Save odedhb/79d9ea471c10c040245e to your computer and use it in GitHub Desktop.
Save odedhb/79d9ea471c10c040245e to your computer and use it in GitHub Desktop.
Generate android Palette color from String
public int calculateColorBase() {
String opacity = "#ff"; //opacity between 00-ff
String hexColor = String.format(
opacity + "%06X", (0xeeeeee & name.hashCode()));
return Color.parseColor(hexColor);
}
public int calculateColor() {
ShapeDrawable drawable = new ShapeDrawable(new RectShape());
drawable.getPaint().setColor(calculateColorBase());
drawable.setIntrinsicHeight(2);
drawable.setIntrinsicWidth(2);
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
Palette palette = Palette.generate(bitmap);
return palette.getVibrantColor(0xff00bcd4);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment