Skip to content

Commit 3da65d0

Browse files
authored
Merge pull request #56 from wbm1113/master
string.ToColor()
2 parents 75e47ac + a856fdc commit 3da65d0

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using UnityEngine;
2+
using System.Globalization;
3+
4+
// e.g. "#FFFFFF".ToColor()
5+
6+
namespace UnityLibrary
7+
{
8+
public static class StringExtensions
9+
{
10+
public static Color ToColor(this string hex) {
11+
hex = hex.Replace("0x", "");
12+
hex = hex.Replace("#", "");
13+
14+
byte a = 255;
15+
byte r = byte.Parse(hex.Substring(0,2), NumberStyles.HexNumber);
16+
byte g = byte.Parse(hex.Substring(2,2), NumberStyles.HexNumber);
17+
byte b = byte.Parse(hex.Substring(4,2), NumberStyles.HexNumber);
18+
19+
if (hex.Length == 8)
20+
a = byte.Parse(hex.Substring(6,2), NumberStyles.HexNumber);
21+
22+
return new Color32(r,g,b,a);
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)