Tanoda
unity-ui-extensions/Runtime/Scripts/Controls/ColorPicker/HexColorField.cs
Go to the documentation of this file.
1
3
4using System.Globalization;
5
7{
8
9 [RequireComponent(typeof(InputField))]
10public class HexColorField : MonoBehaviour
11{
13
14 public bool displayAlpha;
15
16 private InputField hexInputField;
17
18 private const string hexRegex = "^#?(?:[0-9a-fA-F]{3,4}){1,2}$";
19
20 private void Awake()
21 {
22 hexInputField = GetComponent<InputField>();
23
24 // Add listeners to keep text (and color) up to date
25 hexInputField.onEndEdit.AddListener(UpdateColor);
26 ColorPicker.onValueChanged.AddListener(UpdateHex);
27 }
28
29 private void OnDestroy()
30 {
31 hexInputField.onValueChanged.RemoveListener(UpdateColor);
32 ColorPicker.onValueChanged.RemoveListener(UpdateHex);
33 }
34
35 private void UpdateHex(Color newColor)
36 {
37 hexInputField.text = ColorToHex(newColor);
38 }
39
40 private void UpdateColor(string newHex)
41 {
42 Color32 color;
43 if (HexToColor(newHex, out color))
45 else
46 Debug.Log("hex value is in the wrong format, valid formats are: #RGB, #RGBA, #RRGGBB and #RRGGBBAA (# is optional)");
47 }
48
49 private string ColorToHex(Color32 color)
50 {
51 if (displayAlpha)
52 return string.Format("#{0:X2}{1:X2}{2:X2}{3:X2}", color.r, color.g, color.b, color.a);
53 else
54 return string.Format("#{0:X2}{1:X2}{2:X2}", color.r, color.g, color.b);
55 }
56
57 public static bool HexToColor(string hex, out Color32 color)
58 {
59 // Check if this is a valid hex string (# is optional)
60 if (System.Text.RegularExpressions.Regex.IsMatch(hex, hexRegex))
61 {
62 int startIndex = hex.StartsWith("#") ? 1 : 0;
63
64 if (hex.Length == startIndex + 8) //#RRGGBBAA
65 {
66 color = new Color32(byte.Parse(hex.Substring(startIndex, 2), NumberStyles.AllowHexSpecifier),
67 byte.Parse(hex.Substring(startIndex + 2, 2), NumberStyles.AllowHexSpecifier),
68 byte.Parse(hex.Substring(startIndex + 4, 2), NumberStyles.AllowHexSpecifier),
69 byte.Parse(hex.Substring(startIndex + 6, 2), NumberStyles.AllowHexSpecifier));
70 }
71 else if (hex.Length == startIndex + 6) //#RRGGBB
72 {
73 color = new Color32(byte.Parse(hex.Substring(startIndex, 2), NumberStyles.AllowHexSpecifier),
74 byte.Parse(hex.Substring(startIndex + 2, 2), NumberStyles.AllowHexSpecifier),
75 byte.Parse(hex.Substring(startIndex + 4, 2), NumberStyles.AllowHexSpecifier),
76 255);
77 }
78 else if (hex.Length == startIndex + 4) //#RGBA
79 {
80 color = new Color32(byte.Parse("" + hex[startIndex] + hex[startIndex], NumberStyles.AllowHexSpecifier),
81 byte.Parse("" + hex[startIndex + 1] + hex[startIndex + 1], NumberStyles.AllowHexSpecifier),
82 byte.Parse("" + hex[startIndex + 2] + hex[startIndex + 2], NumberStyles.AllowHexSpecifier),
83 byte.Parse("" + hex[startIndex + 3] + hex[startIndex + 3], NumberStyles.AllowHexSpecifier));
84 }
85 else //#RGB
86 {
87 color = new Color32(byte.Parse("" + hex[startIndex] + hex[startIndex], NumberStyles.AllowHexSpecifier),
88 byte.Parse("" + hex[startIndex + 1] + hex[startIndex + 1], NumberStyles.AllowHexSpecifier),
89 byte.Parse("" + hex[startIndex + 2] + hex[startIndex + 2], NumberStyles.AllowHexSpecifier),
90 255);
91 }
92 return true;
93 }
94 else
95 {
96 color = new Color32();
97 return false;
98 }
99 }
100}
101}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
UnityEngine.Color Color
Definition: TestScript.cs:32
ColorChangedEvent onValueChanged
Definition: ColorPicker.cs:19
Color CurrentColor
Definition: ColorPicker.cs:23