Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //using System.Globalization;
- public static Color ParseColor(string hex) {
- int length = hex.Length;
- if(length != 6 && length != 8)
- throw new ArgumentException($"Color Hex code {hex} is not a valid hex code.");
- var color = new Color32();
- if(
- byte.TryParse(hex.Substring(0, 2), NumberStyles.AllowHexSpecifier, NumberFormatInfo.InvariantInfo, out byte r) &&
- byte.TryParse(hex.Substring(2, 2), NumberStyles.AllowHexSpecifier, NumberFormatInfo.InvariantInfo, out byte g) &&
- byte.TryParse(hex.Substring(4, 2), NumberStyles.AllowHexSpecifier, NumberFormatInfo.InvariantInfo, out byte b))
- {
- color.r = r;
- color.b = b;
- color.g = g;
- } else
- throw new ArgumentException($"Color Hex code {hex} is not a valid hex code.");
- if(length == 8)
- if(byte.TryParse(hex.Substring(6, 2), NumberStyles.AllowHexSpecifier, NumberFormatInfo.InvariantInfo, out byte a))
- color.a = a;
- else
- throw new ArgumentException($"Color Hex code {hex} is not a valid hex code.");
- else
- color.a = 0xFF;
- return color;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement