-
Notifications
You must be signed in to change notification settings - Fork 96
/
ColorInput.cs
76 lines (62 loc) · 1.32 KB
/
ColorInput.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using UnityEngine;
using UnityEngine.UI;
using System;
using SCANsat.Unity.HSVPicker.Enum;
namespace SCANsat.Unity.HSVPicker.UI
{
/// <summary>
/// Displays one of the color values of aColorPicker
/// </summary>
[RequireComponent(typeof(InputField))]
public class ColorInput : MonoBehaviour
{
public ColorPicker hsvpicker;
/// <summary>
/// Which value this slider can edit.
/// </summary>
public ColorValues type;
private InputField inputField;
private void Awake()
{
inputField = GetComponent<InputField>();
inputField.onValueChanged.AddListener(InputChanged);
}
private void OnDestroy()
{
inputField.onValueChanged.RemoveListener(InputChanged);
}
private void InputChanged(string input)
{
if (string.IsNullOrEmpty(input))
return;
float original = 0;
switch(type)
{
case ColorValues.R:
original = hsvpicker.R;
break;
case ColorValues.G:
original = hsvpicker.G;
break;
case ColorValues.B:
original = hsvpicker.B;
break;
}
float f = original;
if (!float.TryParse(input, out f))
return;
if (f < 0)
return;
else if (input.StartsWith("1."))
f = 1;
else if (f >= 1 && f <= 255)
{
f = Mathf.RoundToInt(f);
f /= 255;
}
else if (f > 255)
return;
hsvpicker.AssignColor(type, f);
}
}
}