You've already forked UniversalViewer
Initial files
This commit is contained in:
30
Assets/Scripts/HSVPicker/UI/ColorImage.cs
Normal file
30
Assets/Scripts/HSVPicker/UI/ColorImage.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace HSVPicker
|
||||
{
|
||||
[RequireComponent(typeof(Image))]
|
||||
public class ColorImage : MonoBehaviour
|
||||
{
|
||||
public ColorPicker picker;
|
||||
|
||||
private Image image;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
image = GetComponent<Image>();
|
||||
picker.onValueChanged.AddListener(ColorChanged);
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
picker.onValueChanged.RemoveListener(ColorChanged);
|
||||
}
|
||||
|
||||
private void ColorChanged(Color newColor)
|
||||
{
|
||||
image.color = newColor;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
12
Assets/Scripts/HSVPicker/UI/ColorImage.cs.meta
Normal file
12
Assets/Scripts/HSVPicker/UI/ColorImage.cs.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6bca58eb07ad66b498a2f158bcb13225
|
||||
timeCreated: 1442675622
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
92
Assets/Scripts/HSVPicker/UI/ColorLabel.cs
Normal file
92
Assets/Scripts/HSVPicker/UI/ColorLabel.cs
Normal file
@@ -0,0 +1,92 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using TMPro;
|
||||
|
||||
namespace HSVPicker
|
||||
{
|
||||
|
||||
[RequireComponent(typeof(TMP_Text))]
|
||||
public class ColorLabel : MonoBehaviour
|
||||
{
|
||||
public ColorPicker picker;
|
||||
|
||||
public ColorValues type;
|
||||
|
||||
public string prefix = "R: ";
|
||||
public float minValue = 0;
|
||||
public float maxValue = 255;
|
||||
|
||||
public int precision = 0;
|
||||
|
||||
[SerializeField, HideInInspector]
|
||||
private TMP_Text label;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
label = GetComponent<TMP_Text>();
|
||||
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
if (Application.isPlaying && picker != null)
|
||||
{
|
||||
picker.onValueChanged.AddListener(ColorChanged);
|
||||
picker.onHSVChanged.AddListener(HSVChanged);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
if (picker != null)
|
||||
{
|
||||
picker.onValueChanged.RemoveListener(ColorChanged);
|
||||
picker.onHSVChanged.RemoveListener(HSVChanged);
|
||||
}
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
private void OnValidate()
|
||||
{
|
||||
label = GetComponent<TMP_Text>();
|
||||
UpdateValue();
|
||||
}
|
||||
#endif
|
||||
|
||||
private void ColorChanged(Color color)
|
||||
{
|
||||
UpdateValue();
|
||||
}
|
||||
|
||||
private void HSVChanged(float hue, float sateration, float value)
|
||||
{
|
||||
UpdateValue();
|
||||
}
|
||||
|
||||
private void UpdateValue()
|
||||
{
|
||||
if(label == null)
|
||||
return;
|
||||
|
||||
if (picker == null)
|
||||
{
|
||||
label.text = prefix + "-";
|
||||
}
|
||||
else
|
||||
{
|
||||
float value = minValue + (picker.GetValue(type) * (maxValue - minValue));
|
||||
|
||||
label.text = prefix + ConvertToDisplayString(value);
|
||||
}
|
||||
}
|
||||
|
||||
private string ConvertToDisplayString(float value)
|
||||
{
|
||||
if (precision > 0)
|
||||
return value.ToString("f " + precision);
|
||||
else
|
||||
return Mathf.FloorToInt(value).ToString();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
12
Assets/Scripts/HSVPicker/UI/ColorLabel.cs.meta
Normal file
12
Assets/Scripts/HSVPicker/UI/ColorLabel.cs.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6b10e832a32d2d14facd8a3f489ee8d6
|
||||
timeCreated: 1442587803
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
306
Assets/Scripts/HSVPicker/UI/ColorPicker.cs
Normal file
306
Assets/Scripts/HSVPicker/UI/ColorPicker.cs
Normal file
@@ -0,0 +1,306 @@
|
||||
using UnityEngine;
|
||||
namespace HSVPicker
|
||||
{
|
||||
public class ColorPicker : MonoBehaviour
|
||||
{
|
||||
|
||||
private float _hue = 0;
|
||||
private float _saturation = 0;
|
||||
private float _brightness = 0;
|
||||
|
||||
[SerializeField]
|
||||
private Color _color = Color.red;
|
||||
|
||||
[Header("Setup")]
|
||||
public ColorPickerSetup Setup;
|
||||
|
||||
[Header("Event")]
|
||||
public ColorChangedEvent onValueChanged = new ColorChangedEvent();
|
||||
public HSVChangedEvent onHSVChanged = new HSVChangedEvent();
|
||||
|
||||
public Color CurrentColor
|
||||
{
|
||||
get
|
||||
{
|
||||
return _color;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (CurrentColor == value)
|
||||
return;
|
||||
|
||||
_color = value;
|
||||
|
||||
RGBChanged();
|
||||
|
||||
SendChangedEvent();
|
||||
}
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
Setup.AlphaSlidiers.Toggle(Setup.ShowAlpha);
|
||||
Setup.ColorToggleElement.Toggle(Setup.ShowColorSliderToggle);
|
||||
Setup.RgbSliders.Toggle(Setup.ShowRgb);
|
||||
Setup.HsvSliders.Toggle(Setup.ShowHsv);
|
||||
Setup.ColorBox.Toggle(Setup.ShowColorBox);
|
||||
|
||||
HandleHeaderSetting(Setup.ShowHeader);
|
||||
UpdateColorToggleText();
|
||||
|
||||
RGBChanged();
|
||||
SendChangedEvent();
|
||||
}
|
||||
|
||||
public float H
|
||||
{
|
||||
get
|
||||
{
|
||||
return _hue;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (_hue == value)
|
||||
return;
|
||||
|
||||
_hue = value;
|
||||
|
||||
HSVChanged();
|
||||
|
||||
SendChangedEvent();
|
||||
}
|
||||
}
|
||||
|
||||
public float S
|
||||
{
|
||||
get
|
||||
{
|
||||
return _saturation;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (_saturation == value)
|
||||
return;
|
||||
|
||||
_saturation = value;
|
||||
|
||||
HSVChanged();
|
||||
|
||||
SendChangedEvent();
|
||||
}
|
||||
}
|
||||
|
||||
public float V
|
||||
{
|
||||
get
|
||||
{
|
||||
return _brightness;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (_brightness == value)
|
||||
return;
|
||||
|
||||
_brightness = value;
|
||||
|
||||
HSVChanged();
|
||||
|
||||
SendChangedEvent();
|
||||
}
|
||||
}
|
||||
|
||||
public float R
|
||||
{
|
||||
get
|
||||
{
|
||||
return _color.r;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (_color.r == value)
|
||||
return;
|
||||
|
||||
_color.r = value;
|
||||
|
||||
RGBChanged();
|
||||
|
||||
SendChangedEvent();
|
||||
}
|
||||
}
|
||||
|
||||
public float G
|
||||
{
|
||||
get
|
||||
{
|
||||
return _color.g;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (_color.g == value)
|
||||
return;
|
||||
|
||||
_color.g = value;
|
||||
|
||||
RGBChanged();
|
||||
|
||||
SendChangedEvent();
|
||||
}
|
||||
}
|
||||
|
||||
public float B
|
||||
{
|
||||
get
|
||||
{
|
||||
return _color.b;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (_color.b == value)
|
||||
return;
|
||||
|
||||
_color.b = value;
|
||||
|
||||
RGBChanged();
|
||||
|
||||
SendChangedEvent();
|
||||
}
|
||||
}
|
||||
|
||||
private float A
|
||||
{
|
||||
get
|
||||
{
|
||||
return _color.a;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (_color.a == value)
|
||||
return;
|
||||
|
||||
_color.a = value;
|
||||
|
||||
SendChangedEvent();
|
||||
}
|
||||
}
|
||||
|
||||
private void RGBChanged()
|
||||
{
|
||||
HsvColor color = HSVUtil.ConvertRgbToHsv(CurrentColor);
|
||||
|
||||
_hue = color.normalizedH;
|
||||
_saturation = color.normalizedS;
|
||||
_brightness = color.normalizedV;
|
||||
}
|
||||
|
||||
private void HSVChanged()
|
||||
{
|
||||
Color color = HSVUtil.ConvertHsvToRgb(_hue * 360, _saturation, _brightness, _color.a);
|
||||
|
||||
_color = color;
|
||||
}
|
||||
|
||||
private void SendChangedEvent()
|
||||
{
|
||||
onValueChanged.Invoke(CurrentColor);
|
||||
onHSVChanged.Invoke(_hue, _saturation, _brightness);
|
||||
}
|
||||
|
||||
public void AssignColor(ColorValues type, float value)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case ColorValues.R:
|
||||
R = value;
|
||||
break;
|
||||
case ColorValues.G:
|
||||
G = value;
|
||||
break;
|
||||
case ColorValues.B:
|
||||
B = value;
|
||||
break;
|
||||
case ColorValues.A:
|
||||
A = value;
|
||||
break;
|
||||
case ColorValues.Hue:
|
||||
H = value;
|
||||
break;
|
||||
case ColorValues.Saturation:
|
||||
S = value;
|
||||
break;
|
||||
case ColorValues.Value:
|
||||
V = value;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void AssignColor(Color color)
|
||||
{
|
||||
CurrentColor = color;
|
||||
}
|
||||
|
||||
public float GetValue(ColorValues type)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case ColorValues.R:
|
||||
return R;
|
||||
case ColorValues.G:
|
||||
return G;
|
||||
case ColorValues.B:
|
||||
return B;
|
||||
case ColorValues.A:
|
||||
return A;
|
||||
case ColorValues.Hue:
|
||||
return H;
|
||||
case ColorValues.Saturation:
|
||||
return S;
|
||||
case ColorValues.Value:
|
||||
return V;
|
||||
default:
|
||||
throw new System.NotImplementedException("");
|
||||
}
|
||||
}
|
||||
|
||||
public void ToggleColorSliders()
|
||||
{
|
||||
Setup.ShowHsv = !Setup.ShowHsv;
|
||||
Setup.ShowRgb = !Setup.ShowRgb;
|
||||
Setup.HsvSliders.Toggle(Setup.ShowHsv);
|
||||
Setup.RgbSliders.Toggle(Setup.ShowRgb);
|
||||
|
||||
|
||||
UpdateColorToggleText();
|
||||
}
|
||||
|
||||
void UpdateColorToggleText()
|
||||
{
|
||||
if (Setup.ShowRgb)
|
||||
{
|
||||
Setup.SliderToggleButtonText.text = "RGB";
|
||||
}
|
||||
|
||||
if (Setup.ShowHsv)
|
||||
{
|
||||
Setup.SliderToggleButtonText.text = "HSV";
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleHeaderSetting(ColorPickerSetup.ColorHeaderShowing setupShowHeader)
|
||||
{
|
||||
if (setupShowHeader == ColorPickerSetup.ColorHeaderShowing.Hide)
|
||||
{
|
||||
Setup.ColorHeader.Toggle(false);
|
||||
return;
|
||||
}
|
||||
|
||||
Setup.ColorHeader.Toggle(true);
|
||||
|
||||
Setup.ColorPreview.Toggle(setupShowHeader != ColorPickerSetup.ColorHeaderShowing.ShowColorCode);
|
||||
Setup.ColorCode.Toggle(setupShowHeader != ColorPickerSetup.ColorHeaderShowing.ShowColor);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
8
Assets/Scripts/HSVPicker/UI/ColorPicker.cs.meta
Normal file
8
Assets/Scripts/HSVPicker/UI/ColorPicker.cs.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8262e4a8322117f4da079921eaa72834
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
58
Assets/Scripts/HSVPicker/UI/ColorPickerSetup.cs
Normal file
58
Assets/Scripts/HSVPicker/UI/ColorPickerSetup.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
|
||||
using UnityEngine;
|
||||
using TMPro;
|
||||
|
||||
namespace HSVPicker
|
||||
{
|
||||
[System.Serializable]
|
||||
public class ColorPickerSetup
|
||||
{
|
||||
public enum ColorHeaderShowing
|
||||
{
|
||||
Hide,
|
||||
ShowColor,
|
||||
ShowColorCode,
|
||||
ShowAll,
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class UiElements
|
||||
{
|
||||
public RectTransform[] Elements;
|
||||
|
||||
|
||||
public void Toggle(bool active)
|
||||
{
|
||||
for (int cnt = 0; cnt < Elements.Length; cnt++)
|
||||
{
|
||||
Elements[cnt].gameObject.SetActive(active);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public bool ShowRgb = true;
|
||||
public bool ShowHsv;
|
||||
public bool ShowAlpha = true;
|
||||
public bool ShowColorBox = true;
|
||||
public bool ShowColorSliderToggle = true;
|
||||
|
||||
public ColorHeaderShowing ShowHeader = ColorHeaderShowing.ShowAll;
|
||||
|
||||
public UiElements RgbSliders;
|
||||
public UiElements HsvSliders;
|
||||
public UiElements ColorToggleElement;
|
||||
public UiElements AlphaSlidiers;
|
||||
|
||||
|
||||
public UiElements ColorHeader;
|
||||
public UiElements ColorCode;
|
||||
public UiElements ColorPreview;
|
||||
|
||||
public UiElements ColorBox;
|
||||
public TMP_Text SliderToggleButtonText;
|
||||
|
||||
public string PresetColorsId = "default";
|
||||
public Color[] DefaultPresetColors;
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/HSVPicker/UI/ColorPickerSetup.cs.meta
Normal file
11
Assets/Scripts/HSVPicker/UI/ColorPickerSetup.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5f1e0a31fe4cdb5458d5a88aa1268434
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
67
Assets/Scripts/HSVPicker/UI/ColorPresetManager.cs
Normal file
67
Assets/Scripts/HSVPicker/UI/ColorPresetManager.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
|
||||
namespace HSVPicker
|
||||
{
|
||||
public static class ColorPresetManager
|
||||
{
|
||||
private static Dictionary<string, ColorPresetList> _presets = new Dictionary<string, ColorPresetList>();
|
||||
|
||||
public static ColorPresetList Get(string listId = "default")
|
||||
{
|
||||
ColorPresetList preset;
|
||||
if (!_presets.TryGetValue(listId, out preset))
|
||||
{
|
||||
preset = new ColorPresetList(listId);
|
||||
_presets.Add(listId, preset);
|
||||
}
|
||||
|
||||
return preset;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public class ColorPresetList
|
||||
{
|
||||
public string ListId { get; private set; }
|
||||
public List<Color> Colors { get; private set; }
|
||||
|
||||
public event UnityAction<List<Color>> OnColorsUpdated;
|
||||
|
||||
public ColorPresetList(string listId, List<Color> colors = null)
|
||||
{
|
||||
if (colors == null)
|
||||
{
|
||||
colors = new List<Color>();
|
||||
}
|
||||
|
||||
Colors = colors;
|
||||
ListId = listId;
|
||||
}
|
||||
|
||||
public void AddColor(Color color)
|
||||
{
|
||||
Colors.Add(color);
|
||||
if (OnColorsUpdated != null)
|
||||
{
|
||||
OnColorsUpdated.Invoke(Colors);
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateList(IEnumerable<Color> colors)
|
||||
{
|
||||
Colors.Clear();
|
||||
Colors.AddRange(colors);
|
||||
|
||||
if (OnColorsUpdated != null)
|
||||
{
|
||||
OnColorsUpdated.Invoke(Colors);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/HSVPicker/UI/ColorPresetManager.cs.meta
Normal file
11
Assets/Scripts/HSVPicker/UI/ColorPresetManager.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 55b59bed8e892614e9397d8a20e36e0c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
86
Assets/Scripts/HSVPicker/UI/ColorPresets.cs
Normal file
86
Assets/Scripts/HSVPicker/UI/ColorPresets.cs
Normal file
@@ -0,0 +1,86 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace HSVPicker
|
||||
{
|
||||
public class ColorPresets : MonoBehaviour
|
||||
{
|
||||
public ColorPicker picker;
|
||||
public GameObject[] presets;
|
||||
public Image createPresetImage;
|
||||
|
||||
private ColorPresetList _colors;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
// picker.onHSVChanged.AddListener(HSVChanged);
|
||||
picker.onValueChanged.AddListener(ColorChanged);
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
_colors = ColorPresetManager.Get(picker.Setup.PresetColorsId);
|
||||
|
||||
if (_colors.Colors.Count < picker.Setup.DefaultPresetColors.Length)
|
||||
{
|
||||
_colors.UpdateList(picker.Setup.DefaultPresetColors);
|
||||
}
|
||||
|
||||
_colors.OnColorsUpdated += OnColorsUpdate;
|
||||
OnColorsUpdate(_colors.Colors);
|
||||
}
|
||||
|
||||
private void OnColorsUpdate(List<Color> colors)
|
||||
{
|
||||
for (int cnt = 0; cnt < presets.Length; cnt++)
|
||||
{
|
||||
if (colors.Count <= cnt)
|
||||
{
|
||||
presets[cnt].SetActive(false);
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
presets[cnt].SetActive(true);
|
||||
presets[cnt].GetComponent<Image>().color = colors[cnt];
|
||||
|
||||
}
|
||||
|
||||
createPresetImage.gameObject.SetActive(colors.Count < presets.Length);
|
||||
|
||||
}
|
||||
|
||||
public void CreatePresetButton()
|
||||
{
|
||||
_colors.AddColor(picker.CurrentColor);
|
||||
|
||||
// for (var i = 0; i < presets.Length; i++)
|
||||
//{
|
||||
// if (!presets[i].activeSelf)
|
||||
// {
|
||||
// presets[i].SetActive(true);
|
||||
// presets[i].GetComponent<Image>().color = picker.CurrentColor;
|
||||
// break;
|
||||
// }
|
||||
//}
|
||||
}
|
||||
|
||||
public void PresetSelect(Image sender)
|
||||
{
|
||||
picker.CurrentColor = sender.color;
|
||||
}
|
||||
|
||||
// Not working, it seems ConvertHsvToRgb() is broken. It doesn't work when fed
|
||||
// input h, s, v as shown below.
|
||||
// private void HSVChanged(float h, float s, float v)
|
||||
// {
|
||||
// createPresetImage.color = HSVUtil.ConvertHsvToRgb(h, s, v, 1);
|
||||
// }
|
||||
private void ColorChanged(Color color)
|
||||
{
|
||||
createPresetImage.color = color;
|
||||
}
|
||||
}
|
||||
}
|
||||
12
Assets/Scripts/HSVPicker/UI/ColorPresets.cs.meta
Normal file
12
Assets/Scripts/HSVPicker/UI/ColorPresets.cs.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0923373e76e77402c9c53a2f1250ad3e
|
||||
timeCreated: 1456875791
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
92
Assets/Scripts/HSVPicker/UI/ColorSlider.cs
Normal file
92
Assets/Scripts/HSVPicker/UI/ColorSlider.cs
Normal file
@@ -0,0 +1,92 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
namespace HSVPicker
|
||||
{
|
||||
/// <summary>
|
||||
/// Displays one of the color values of aColorPicker
|
||||
/// </summary>
|
||||
[RequireComponent(typeof(Slider))]
|
||||
public class ColorSlider : MonoBehaviour
|
||||
{
|
||||
public ColorPicker hsvpicker;
|
||||
|
||||
/// <summary>
|
||||
/// Which value this slider can edit.
|
||||
/// </summary>
|
||||
public ColorValues type;
|
||||
|
||||
private Slider slider;
|
||||
|
||||
private bool listen = true;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
slider = GetComponent<Slider>();
|
||||
|
||||
hsvpicker.onValueChanged.AddListener(ColorChanged);
|
||||
hsvpicker.onHSVChanged.AddListener(HSVChanged);
|
||||
slider.onValueChanged.AddListener(SliderChanged);
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
hsvpicker.onValueChanged.RemoveListener(ColorChanged);
|
||||
hsvpicker.onHSVChanged.RemoveListener(HSVChanged);
|
||||
slider.onValueChanged.RemoveListener(SliderChanged);
|
||||
}
|
||||
|
||||
private void ColorChanged(Color newColor)
|
||||
{
|
||||
listen = false;
|
||||
switch (type)
|
||||
{
|
||||
case ColorValues.R:
|
||||
slider.normalizedValue = newColor.r;
|
||||
break;
|
||||
case ColorValues.G:
|
||||
slider.normalizedValue = newColor.g;
|
||||
break;
|
||||
case ColorValues.B:
|
||||
slider.normalizedValue = newColor.b;
|
||||
break;
|
||||
case ColorValues.A:
|
||||
slider.normalizedValue = newColor.a;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void HSVChanged(float hue, float saturation, float value)
|
||||
{
|
||||
listen = false;
|
||||
switch (type)
|
||||
{
|
||||
case ColorValues.Hue:
|
||||
slider.normalizedValue = hue; //1 - hue;
|
||||
break;
|
||||
case ColorValues.Saturation:
|
||||
slider.normalizedValue = saturation;
|
||||
break;
|
||||
case ColorValues.Value:
|
||||
slider.normalizedValue = value;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void SliderChanged(float newValue)
|
||||
{
|
||||
if (listen)
|
||||
{
|
||||
newValue = slider.normalizedValue;
|
||||
//if (type == ColorValues.Hue)
|
||||
// newValue = 1 - newValue;
|
||||
|
||||
hsvpicker.AssignColor(type, newValue);
|
||||
}
|
||||
listen = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
12
Assets/Scripts/HSVPicker/UI/ColorSlider.cs.meta
Normal file
12
Assets/Scripts/HSVPicker/UI/ColorSlider.cs.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c600592efa0cf25479655321bf4fb08a
|
||||
timeCreated: 1442586558
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
206
Assets/Scripts/HSVPicker/UI/ColorSliderImage.cs
Normal file
206
Assets/Scripts/HSVPicker/UI/ColorSliderImage.cs
Normal file
@@ -0,0 +1,206 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
namespace HSVPicker
|
||||
{
|
||||
[RequireComponent(typeof(RawImage)), ExecuteInEditMode()]
|
||||
public class ColorSliderImage : MonoBehaviour
|
||||
{
|
||||
public ColorPicker picker;
|
||||
|
||||
/// <summary>
|
||||
/// Which value this slider can edit.
|
||||
/// </summary>
|
||||
public ColorValues type;
|
||||
|
||||
public Slider.Direction direction;
|
||||
|
||||
private RawImage image;
|
||||
|
||||
private RectTransform rectTransform
|
||||
{
|
||||
get
|
||||
{
|
||||
return transform as RectTransform;
|
||||
}
|
||||
}
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
image = GetComponent<RawImage>();
|
||||
|
||||
if(Application.isPlaying)
|
||||
RegenerateTexture();
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
if (picker != null && Application.isPlaying)
|
||||
{
|
||||
picker.onValueChanged.AddListener(ColorChanged);
|
||||
picker.onHSVChanged.AddListener(HSVChanged);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
if (picker != null)
|
||||
{
|
||||
picker.onValueChanged.RemoveListener(ColorChanged);
|
||||
picker.onHSVChanged.RemoveListener(HSVChanged);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
if (image.texture != null)
|
||||
DestroyImmediate(image.texture);
|
||||
}
|
||||
|
||||
private void ColorChanged(Color newColor)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case ColorValues.R:
|
||||
case ColorValues.G:
|
||||
case ColorValues.B:
|
||||
case ColorValues.Saturation:
|
||||
case ColorValues.Value:
|
||||
RegenerateTexture();
|
||||
break;
|
||||
case ColorValues.A:
|
||||
case ColorValues.Hue:
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void HSVChanged(float hue, float saturation, float value)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case ColorValues.R:
|
||||
case ColorValues.G:
|
||||
case ColorValues.B:
|
||||
case ColorValues.Saturation:
|
||||
case ColorValues.Value:
|
||||
RegenerateTexture();
|
||||
break;
|
||||
case ColorValues.A:
|
||||
case ColorValues.Hue:
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void RegenerateTexture()
|
||||
{
|
||||
Color32 baseColor = picker != null ? picker.CurrentColor : Color.black;
|
||||
|
||||
float h = picker != null ? picker.H : 0;
|
||||
float s = picker != null ? picker.S : 0;
|
||||
float v = picker != null ? picker.V : 0;
|
||||
|
||||
Texture2D texture;
|
||||
Color32[] colors;
|
||||
|
||||
bool vertical = direction == Slider.Direction.BottomToTop || direction == Slider.Direction.TopToBottom;
|
||||
bool inverted = direction == Slider.Direction.TopToBottom || direction == Slider.Direction.RightToLeft;
|
||||
|
||||
int size;
|
||||
switch (type)
|
||||
{
|
||||
case ColorValues.R:
|
||||
case ColorValues.G:
|
||||
case ColorValues.B:
|
||||
case ColorValues.A:
|
||||
size = 255;
|
||||
break;
|
||||
case ColorValues.Hue:
|
||||
size = 360;
|
||||
break;
|
||||
case ColorValues.Saturation:
|
||||
case ColorValues.Value:
|
||||
size = 100;
|
||||
break;
|
||||
default:
|
||||
throw new System.NotImplementedException("");
|
||||
}
|
||||
if (vertical)
|
||||
texture = new Texture2D(1, size);
|
||||
else
|
||||
texture = new Texture2D(size, 1);
|
||||
|
||||
texture.hideFlags = HideFlags.DontSave;
|
||||
colors = new Color32[size];
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case ColorValues.R:
|
||||
for (byte i = 0; i < size; i++)
|
||||
{
|
||||
colors[inverted ? size - 1 - i : i] = new Color32(i, baseColor.g, baseColor.b, 255);
|
||||
}
|
||||
break;
|
||||
case ColorValues.G:
|
||||
for (byte i = 0; i < size; i++)
|
||||
{
|
||||
colors[inverted ? size - 1 - i : i] = new Color32(baseColor.r, i, baseColor.b, 255);
|
||||
}
|
||||
break;
|
||||
case ColorValues.B:
|
||||
for (byte i = 0; i < size; i++)
|
||||
{
|
||||
colors[inverted ? size - 1 - i : i] = new Color32(baseColor.r, baseColor.g, i, 255);
|
||||
}
|
||||
break;
|
||||
case ColorValues.A:
|
||||
for (byte i = 0; i < size; i++)
|
||||
{
|
||||
colors[inverted ? size - 1 - i : i] = new Color32(i, i, i, 255);
|
||||
}
|
||||
break;
|
||||
case ColorValues.Hue:
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
colors[inverted ? size - 1 - i : i] = HSVUtil.ConvertHsvToRgb(i, 1, 1, 1);
|
||||
}
|
||||
break;
|
||||
case ColorValues.Saturation:
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
colors[inverted ? size - 1 - i : i] = HSVUtil.ConvertHsvToRgb(h * 360, (float)i / size, v, 1);
|
||||
}
|
||||
break;
|
||||
case ColorValues.Value:
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
colors[inverted ? size - 1 - i : i] = HSVUtil.ConvertHsvToRgb(h * 360, s, (float)i / size, 1);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
throw new System.NotImplementedException("");
|
||||
}
|
||||
texture.SetPixels32(colors);
|
||||
texture.Apply();
|
||||
|
||||
if (image.texture != null)
|
||||
DestroyImmediate(image.texture);
|
||||
image.texture = texture;
|
||||
|
||||
switch (direction)
|
||||
{
|
||||
case Slider.Direction.BottomToTop:
|
||||
case Slider.Direction.TopToBottom:
|
||||
image.uvRect = new Rect(0, 0, 2, 1);
|
||||
break;
|
||||
case Slider.Direction.LeftToRight:
|
||||
case Slider.Direction.RightToLeft:
|
||||
image.uvRect = new Rect(0, 0, 1, 2);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
12
Assets/Scripts/HSVPicker/UI/ColorSliderImage.cs.meta
Normal file
12
Assets/Scripts/HSVPicker/UI/ColorSliderImage.cs.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7ca76dd9ad6eb204c9b0481aece34497
|
||||
timeCreated: 1442682013
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
53
Assets/Scripts/HSVPicker/UI/HexColorField.cs
Normal file
53
Assets/Scripts/HSVPicker/UI/HexColorField.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using TMPro;
|
||||
namespace HSVPicker
|
||||
{
|
||||
[RequireComponent(typeof(TMP_InputField))]
|
||||
public class HexColorField : MonoBehaviour
|
||||
{
|
||||
public ColorPicker hsvpicker;
|
||||
|
||||
public bool displayAlpha;
|
||||
|
||||
private TMP_InputField hexInputField;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
hexInputField = GetComponent<TMP_InputField>();
|
||||
|
||||
// Add listeners to keep text (and color) up to date
|
||||
hexInputField.onEndEdit.AddListener(UpdateColor);
|
||||
hsvpicker.onValueChanged.AddListener(UpdateHex);
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
hexInputField.onValueChanged.RemoveListener(UpdateColor);
|
||||
hsvpicker.onValueChanged.RemoveListener(UpdateHex);
|
||||
}
|
||||
|
||||
private void UpdateHex(Color newColor)
|
||||
{
|
||||
hexInputField.text = ColorToHex(newColor);
|
||||
}
|
||||
|
||||
private void UpdateColor(string newHex)
|
||||
{
|
||||
Color color;
|
||||
if (!newHex.StartsWith("#"))
|
||||
newHex = "#"+newHex;
|
||||
if (ColorUtility.TryParseHtmlString(newHex, out color))
|
||||
hsvpicker.CurrentColor = color;
|
||||
else
|
||||
Debug.Log("hex value is in the wrong format, valid formats are: #RGB, #RGBA, #RRGGBB and #RRGGBBAA (# is optional)");
|
||||
}
|
||||
|
||||
private string ColorToHex(Color32 color)
|
||||
{
|
||||
return displayAlpha
|
||||
? string.Format("#{0:X2}{1:X2}{2:X2}{3:X2}", color.r, color.g, color.b, color.a)
|
||||
: string.Format("#{0:X2}{1:X2}{2:X2}", color.r, color.g, color.b);
|
||||
}
|
||||
}
|
||||
}
|
||||
8
Assets/Scripts/HSVPicker/UI/HexColorField.cs.meta
Normal file
8
Assets/Scripts/HSVPicker/UI/HexColorField.cs.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d85c534b3c1560544b09d0996dfeba84
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
121
Assets/Scripts/HSVPicker/UI/SVBoxSlider.cs
Normal file
121
Assets/Scripts/HSVPicker/UI/SVBoxSlider.cs
Normal file
@@ -0,0 +1,121 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace HSVPicker
|
||||
{
|
||||
[RequireComponent(typeof(BoxSlider), typeof(RawImage)), ExecuteInEditMode()]
|
||||
public class SVBoxSlider : MonoBehaviour
|
||||
{
|
||||
public ColorPicker picker;
|
||||
|
||||
private BoxSlider slider;
|
||||
private RawImage image;
|
||||
|
||||
private int textureWidth = 128;
|
||||
private int textureHeight = 128;
|
||||
|
||||
private float lastH = -1;
|
||||
private bool listen = true;
|
||||
|
||||
public RectTransform rectTransform
|
||||
{
|
||||
get
|
||||
{
|
||||
return transform as RectTransform;
|
||||
}
|
||||
}
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
slider = GetComponent<BoxSlider>();
|
||||
image = GetComponent<RawImage>();
|
||||
if(Application.isPlaying)
|
||||
{
|
||||
RegenerateSVTexture ();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
if (Application.isPlaying && picker != null)
|
||||
{
|
||||
slider.onValueChanged.AddListener(SliderChanged);
|
||||
picker.onHSVChanged.AddListener(HSVChanged);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
if (picker != null)
|
||||
{
|
||||
slider.onValueChanged.RemoveListener(SliderChanged);
|
||||
picker.onHSVChanged.RemoveListener(HSVChanged);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
if ( image.texture != null )
|
||||
{
|
||||
DestroyImmediate (image.texture);
|
||||
}
|
||||
}
|
||||
|
||||
private void SliderChanged(float saturation, float value)
|
||||
{
|
||||
if (listen)
|
||||
{
|
||||
picker.AssignColor(ColorValues.Saturation, saturation);
|
||||
picker.AssignColor(ColorValues.Value, value);
|
||||
}
|
||||
listen = true;
|
||||
}
|
||||
|
||||
private void HSVChanged(float h, float s, float v)
|
||||
{
|
||||
if (!lastH.Equals(h))
|
||||
{
|
||||
lastH = h;
|
||||
RegenerateSVTexture();
|
||||
}
|
||||
|
||||
if (!s.Equals(slider.normalizedValue))
|
||||
{
|
||||
listen = false;
|
||||
slider.normalizedValue = s;
|
||||
}
|
||||
|
||||
if (!v.Equals(slider.normalizedValueY))
|
||||
{
|
||||
listen = false;
|
||||
slider.normalizedValueY = v;
|
||||
}
|
||||
}
|
||||
|
||||
private void RegenerateSVTexture()
|
||||
{
|
||||
double h = picker != null ? picker.H * 360 : 0;
|
||||
|
||||
if ( image.texture != null )
|
||||
DestroyImmediate (image.texture);
|
||||
|
||||
var texture = new Texture2D (textureWidth, textureHeight);
|
||||
texture.wrapMode = TextureWrapMode.Clamp;
|
||||
texture.hideFlags = HideFlags.DontSave;
|
||||
|
||||
for ( int s = 0; s < textureWidth; s++ )
|
||||
{
|
||||
Color[] colors = new Color[textureHeight];
|
||||
for ( int v = 0; v < textureHeight; v++ )
|
||||
{
|
||||
colors[v] = HSVUtil.ConvertHsvToRgb (h, (float)s / textureWidth, (float)v / textureHeight, 1);
|
||||
}
|
||||
texture.SetPixels (s, 0, 1, textureHeight, colors);
|
||||
}
|
||||
texture.Apply();
|
||||
|
||||
image.texture = texture;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
12
Assets/Scripts/HSVPicker/UI/SVBoxSlider.cs.meta
Normal file
12
Assets/Scripts/HSVPicker/UI/SVBoxSlider.cs.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1e4240873631f724496efec97d7151b3
|
||||
timeCreated: 1442650713
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user