Is there any way to generate a color from any String in Java / Android like an Encrypt / Hash function?
Example:
The String "Home" generates a color like "#FF1234".
The String "Sky" generates a color like "#00CC33"
...
Without randomize. So, the system will always calculate the same colors for that strings
Thanks
EDIT: The Strings are freely defined by the user
the String.hashCode()
will return an int value, so then it's just a matter of turning that into into a hex value.
String s = "Home";
String color = String.format("#%X", s.hashCode());
With consistent opacity:
String opacity = "#99"; //opacity between 00-ff
String hexColor = String.format(
opacity + "%06X", (0xFFFFFF & anyString.hashCode()));
Or using the new material design android Palette:
https://gist.github.com/odedhb/79d9ea471c10c040245e
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With