ColorUtils.cs 864 B

1234567891011121314151617181920212223242526272829303132
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using UnityEngine;
  5. namespace WildAttack
  6. {
  7. /// <summary>
  8. /// 颜色转换工具
  9. /// </summary>
  10. public class ColorUtils : Singleton<ColorUtils>
  11. {
  12. /// <summary>
  13. /// hex码转换color
  14. /// </summary>
  15. /// <param name="hex"></param>
  16. /// <returns></returns>
  17. public static Color HexToColor(string hex)
  18. {
  19. hex = hex.Replace("0x", string.Empty);
  20. hex = hex.Replace("#", string.Empty);
  21. byte a = byte.MaxValue;
  22. byte r = byte.Parse(hex.Substring(0, 2), NumberStyles.HexNumber);
  23. byte g = byte.Parse(hex.Substring(2, 2), NumberStyles.HexNumber);
  24. byte b = byte.Parse(hex.Substring(4, 2), NumberStyles.HexNumber);
  25. if (hex.Length == 8)
  26. {
  27. a = byte.Parse(hex.Substring(6, 2), NumberStyles.HexNumber);
  28. }
  29. return new Color32(r, g, b, a);
  30. }
  31. }
  32. }