Advertisement
infinite_ammo

ColorFromHex.cs

Mar 16th, 2015
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.87 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class Util
  4. {
  5.     public static Color ColorFromHex(string hex)
  6.     {
  7.         hex = hex.Trim();
  8.         if (hex[0] == '#')
  9.             hex = hex.Substring(1, hex.Length-1);
  10.         byte r=0, g=0, b=0;
  11.         if (hex.Length == 6)
  12.         {
  13.             r = byte.Parse(hex.Substring(0,2), System.Globalization.NumberStyles.HexNumber);
  14.             g = byte.Parse(hex.Substring(2,2), System.Globalization.NumberStyles.HexNumber);
  15.             b = byte.Parse(hex.Substring(4,2), System.Globalization.NumberStyles.HexNumber);
  16.         }
  17.         else if (hex.Length == 3)
  18.         {
  19.             r = byte.Parse(hex.Substring(0, 1) + hex.Substring(0, 1), System.Globalization.NumberStyles.HexNumber);
  20.             g = byte.Parse(hex.Substring(1, 1) + hex.Substring(1, 1), System.Globalization.NumberStyles.HexNumber);
  21.             b = byte.Parse(hex.Substring(2, 1) + hex.Substring(2, 1), System.Globalization.NumberStyles.HexNumber);
  22.         }
  23.         return new Color32(r,g,b, 255);
  24.     }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement