Created
June 29, 2012 12:01
-
-
Save AngryAnt/3017576 to your computer and use it in GitHub Desktop.
Handy utility functions for creating solid colour textures for GUI use.
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
// Usage: | |
m_SideBarTexture = CreateTexture (CreateColor ("#30433C")); | |
// The goodness: | |
static Color CreateColor (string hexCode, float alpha = 1.0f) | |
{ | |
if (hexCode.Length == 7 && hexCode[0] == '#') | |
{ | |
hexCode = hexCode.Substring (1, 6); | |
} | |
else if (hexCode.Length != 6) | |
{ | |
throw new System.ArgumentException ("Malformed hex code"); | |
} | |
return new Color ( | |
((float)int.Parse (hexCode.Substring (0, 2), NumberStyles.HexNumber)) / 255.0f, | |
((float)int.Parse (hexCode.Substring (2, 2), NumberStyles.HexNumber)) / 255.0f, | |
((float)int.Parse (hexCode.Substring (4, 2), NumberStyles.HexNumber)) / 255.0f, | |
alpha | |
); | |
} | |
static Texture2D CreateTexture (Color c) | |
{ | |
Texture2D texture = new Texture2D (2, 2); | |
texture.SetPixels (new Color[] {c, c, c, c}); | |
texture.Apply (); | |
return texture; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment