Created
February 9, 2019 17:00
-
-
Save brucevang/8035bc68d70786dcab6ebfad40403d0d to your computer and use it in GitHub Desktop.
Method to get Hex Value and Return to Flutter Color
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
int getColorHexFromStr(String colorStr) { | |
colorStr = "FF" + colorStr; | |
colorStr = colorStr.replaceAll("#", ""); | |
int val = 0; | |
int len = colorStr.length; | |
for (int i = 0; i < len; i++) { | |
int hexDigit = colorStr.codeUnitAt(i); | |
if (hexDigit >= 48 && hexDigit <= 57) { | |
val += (hexDigit - 48) * (1 << (4 * (len - 1 - i))); | |
} else if (hexDigit >= 65 && hexDigit <= 70) { | |
// A..F | |
val += (hexDigit - 55) * (1 << (4 * (len - 1 - i))); | |
} else if (hexDigit >= 97 && hexDigit <= 102) { | |
// a..f | |
val += (hexDigit - 87) * (1 << (4 * (len - 1 - i))); | |
} else { | |
throw new FormatException("An error occurred when converting a color"); | |
} | |
} | |
return val; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment