Created
July 15, 2020 03:29
-
-
Save ponnamkarthik/7c59c7ced3d96549e181411e9dea03fd to your computer and use it in GitHub Desktop.
Dart Code to genreate hex code from random string
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
getRandomColor(String name) { | |
var hash = 0; | |
if (name.length == 0) return "#000000"; | |
for (var i = 0; i < name.length; i++) { | |
hash = name.codeUnitAt(i) + ((hash << 5) - hash); | |
hash = hash & hash; | |
} | |
var color = '#'; | |
for (var i = 0; i < 3; i++) { | |
var value = (hash >> (i * 8)) & 255; | |
var c = ('00' + value.toRadixString(16)); | |
print(c); | |
color += c.substring(c.length - 2); | |
} | |
return color; | |
} | |
void main() { | |
print(getRandomColor("Karthik")); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment