Created
July 16, 2016 21:23
-
-
Save arashrasoulzadeh/dfc329c26a1277d1eff2e2529578278c to your computer and use it in GitHub Desktop.
Hex Code to Color Variable in unity
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 static Color hexToColor(string hex) | |
{ | |
hex = hex.Replace("0x", "");//in case the string is formatted 0xFFFFFF | |
hex = hex.Replace("#", "");//in case the string is formatted #FFFFFF | |
byte a = 255;//assume fully visible unless specified in hex | |
byte r = byte.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber); | |
byte g = byte.Parse(hex.Substring(2, 2), System.Globalization.NumberStyles.HexNumber); | |
byte b = byte.Parse(hex.Substring(4, 2), System.Globalization.NumberStyles.HexNumber); | |
//Only use alpha if the string has enough characters | |
if (hex.Length == 8) | |
{ | |
a = byte.Parse(hex.Substring(4, 2), System.Globalization.NumberStyles.HexNumber); | |
} | |
return new Color32(r, g, b, a); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment