Skip to content

Instantly share code, notes, and snippets.

@AngryAnt
Created June 29, 2012 12:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save AngryAnt/3017576 to your computer and use it in GitHub Desktop.
Save AngryAnt/3017576 to your computer and use it in GitHub Desktop.
Handy utility functions for creating solid colour textures for GUI use.
// 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