Created
January 6, 2015 11:24
-
-
Save odedhb/79d9ea471c10c040245e to your computer and use it in GitHub Desktop.
Generate android Palette color from String
This file contains 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 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