Removed old HSV picker and replaced with initial version of the new COlorPicker.

Includes some other new controls such as Box Slider and Color Slider.
Still needs some cleaning up as this is the initial check-in

--HG--
branch : develop_5.3
pull/413/head
Simon Jackson 2016-11-25 09:27:29 +00:00
parent c97b4f8a99
commit c46453655e
50 changed files with 11813 additions and 4992 deletions

67
Editor/BoxSliderEditor.cs Normal file
View File

@ -0,0 +1,67 @@
///Credit judah4
///Sourced from - http://forum.unity3d.com/threads/color-picker.267043/
using UnityEditor;
using UnityEditor.UI;
namespace UnityEngine.UI.Extensions
{
[CustomEditor(typeof(BoxSlider), true)]
[CanEditMultipleObjects]
public class BoxSliderEditor : SelectableEditor
{
SerializedProperty m_HandleRect;
SerializedProperty m_MinValue;
SerializedProperty m_MaxValue;
SerializedProperty m_WholeNumbers;
SerializedProperty m_ValueX;
SerializedProperty m_ValueY;
SerializedProperty m_OnValueChanged;
protected override void OnEnable()
{
base.OnEnable();
m_HandleRect = serializedObject.FindProperty("m_HandleRect");
m_MinValue = serializedObject.FindProperty("m_MinValue");
m_MaxValue = serializedObject.FindProperty("m_MaxValue");
m_WholeNumbers = serializedObject.FindProperty("m_WholeNumbers");
m_ValueX = serializedObject.FindProperty("m_ValueX");
m_ValueY = serializedObject.FindProperty("m_ValueY");
m_OnValueChanged = serializedObject.FindProperty("m_OnValueChanged");
}
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
EditorGUILayout.Space();
serializedObject.Update();
EditorGUILayout.PropertyField(m_HandleRect);
if (m_HandleRect.objectReferenceValue != null)
{
EditorGUI.BeginChangeCheck();
EditorGUILayout.PropertyField(m_MinValue);
EditorGUILayout.PropertyField(m_MaxValue);
EditorGUILayout.PropertyField(m_WholeNumbers);
EditorGUILayout.Slider(m_ValueX, m_MinValue.floatValue, m_MaxValue.floatValue);
EditorGUILayout.Slider(m_ValueY, m_MinValue.floatValue, m_MaxValue.floatValue);
// Draw the event notification options
EditorGUILayout.Space();
EditorGUILayout.PropertyField(m_OnValueChanged);
}
else
{
EditorGUILayout.HelpBox("Specify a RectTransform for the slider fill or the slider handle or both. Each must have a parent RectTransform that it can slide within.", MessageType.Info);
}
serializedObject.ApplyModifiedProperties();
}
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 8701e045b26e51f4eb345f2ccb3c13f5
timeCreated: 1426804458
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,7 +1,7 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: 9025de4c28a4f224e942989ab0d17137 guid: a45157cd1cb5ccd439f3cf178dd2c359
folderAsset: yes folderAsset: yes
timeCreated: 1473357182 timeCreated: 1480012406
licenseType: Pro licenseType: Pro
DefaultImporter: DefaultImporter:
userData: userData:

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 53b194f66900add419869c57ffbe7b85
timeCreated: 1480025159
licenseType: Pro
NativeFormatImporter:
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: d2365a7a72fdea14f9eaba13cf14f570
timeCreated: 1480025159
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +0,0 @@
fileFormatVersion: 2
guid: be7385871d213f844981bd24e601eb98
NativeFormatImporter:
userData:

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +0,0 @@
fileFormatVersion: 2
guid: ce46c07f0028e314ab7767577ab5e7a6
DefaultImporter:
userData:

View File

@ -0,0 +1,437 @@
///Credit judah4
///Sourced from - http://forum.unity3d.com/threads/color-picker.267043/
using System;
using UnityEngine.Events;
using UnityEngine.EventSystems;
namespace UnityEngine.UI.Extensions
{
[AddComponentMenu("UI/BoxSlider", 35)]
[RequireComponent(typeof(RectTransform))]
public class BoxSlider : Selectable, IDragHandler, IInitializePotentialDragHandler, ICanvasElement
{
public enum Direction
{
LeftToRight,
RightToLeft,
BottomToTop,
TopToBottom,
}
[Serializable]
public class BoxSliderEvent : UnityEvent<float, float> { }
[SerializeField]
private RectTransform m_HandleRect;
public RectTransform HandleRect { get { return m_HandleRect; } set { if (SetClass(ref m_HandleRect, value)) { UpdateCachedReferences(); UpdateVisuals(); } } }
[Space(6)]
[SerializeField]
private float m_MinValue = 0;
public float MinValue { get { return m_MinValue; } set { if (SetStruct(ref m_MinValue, value)) { Set(m_ValueX); SetY(m_ValueY); UpdateVisuals(); } } }
[SerializeField]
private float m_MaxValue = 1;
public float MaxValue { get { return m_MaxValue; } set { if (SetStruct(ref m_MaxValue, value)) { Set(m_ValueX); SetY(m_ValueY); UpdateVisuals(); } } }
[SerializeField]
private bool m_WholeNumbers = false;
public bool WholeNumbers { get { return m_WholeNumbers; } set { if (SetStruct(ref m_WholeNumbers, value)) { Set(m_ValueX); SetY(m_ValueY); UpdateVisuals(); } } }
[SerializeField]
private float m_ValueX = 1f;
public float ValueX
{
get
{
if (WholeNumbers)
return Mathf.Round(m_ValueX);
return m_ValueX;
}
set
{
Set(value);
}
}
public float NormalizedValueX
{
get
{
if (Mathf.Approximately(MinValue, MaxValue))
return 0;
return Mathf.InverseLerp(MinValue, MaxValue, ValueX);
}
set
{
this.ValueX = Mathf.Lerp(MinValue, MaxValue, value);
}
}
[SerializeField]
private float m_ValueY = 1f;
public float ValueY
{
get
{
if (WholeNumbers)
return Mathf.Round(m_ValueY);
return m_ValueY;
}
set
{
SetY(value);
}
}
public float NormalizedValueY
{
get
{
if (Mathf.Approximately(MinValue, MaxValue))
return 0;
return Mathf.InverseLerp(MinValue, MaxValue, ValueY);
}
set
{
this.ValueY = Mathf.Lerp(MinValue, MaxValue, value);
}
}
[Space(6)]
// Allow for delegate-based subscriptions for faster events than 'eventReceiver', and allowing for multiple receivers.
[SerializeField]
private BoxSliderEvent m_OnValueChanged = new BoxSliderEvent();
public BoxSliderEvent OnValueChanged { get { return m_OnValueChanged; } set { m_OnValueChanged = value; } }
// Private fields
//private Image m_FillImage;
//private Transform m_FillTransform;
//private RectTransform m_FillContainerRect;
private Transform m_HandleTransform;
private RectTransform m_HandleContainerRect;
// The offset from handle position to mouse down position
private Vector2 m_Offset = Vector2.zero;
private DrivenRectTransformTracker m_Tracker;
// Size of each step.
float StepSize { get { return WholeNumbers ? 1 : (MaxValue - MinValue) * 0.1f; } }
protected BoxSlider()
{ }
#if UNITY_EDITOR
protected override void OnValidate()
{
base.OnValidate();
if (WholeNumbers)
{
m_MinValue = Mathf.Round(m_MinValue);
m_MaxValue = Mathf.Round(m_MaxValue);
}
UpdateCachedReferences();
Set(m_ValueX, false);
SetY(m_ValueY, false);
// Update rects since other things might affect them even if value didn't change.
UpdateVisuals();
var prefabType = UnityEditor.PrefabUtility.GetPrefabType(this);
if (prefabType != UnityEditor.PrefabType.Prefab && !Application.isPlaying)
CanvasUpdateRegistry.RegisterCanvasElementForLayoutRebuild(this);
}
#endif // if UNITY_EDITOR
public virtual void Rebuild(CanvasUpdate executing)
{
#if UNITY_EDITOR
if (executing == CanvasUpdate.Prelayout)
OnValueChanged.Invoke(ValueX, ValueY);
#endif
}
public void LayoutComplete()
{
}
public void GraphicUpdateComplete()
{
}
public static bool SetClass<T>(ref T currentValue, T newValue) where T : class
{
if ((currentValue == null && newValue == null) || (currentValue != null && currentValue.Equals(newValue)))
return false;
currentValue = newValue;
return true;
}
public static bool SetStruct<T>(ref T currentValue, T newValue) where T : struct
{
if (currentValue.Equals(newValue))
return false;
currentValue = newValue;
return true;
}
protected override void OnEnable()
{
base.OnEnable();
UpdateCachedReferences();
Set(m_ValueX, false);
SetY(m_ValueY, false);
// Update rects since they need to be initialized correctly.
UpdateVisuals();
}
protected override void OnDisable()
{
m_Tracker.Clear();
base.OnDisable();
}
void UpdateCachedReferences()
{
if (m_HandleRect)
{
m_HandleTransform = m_HandleRect.transform;
if (m_HandleTransform.parent != null)
m_HandleContainerRect = m_HandleTransform.parent.GetComponent<RectTransform>();
}
else
{
m_HandleContainerRect = null;
}
}
// Set the valueUpdate the visible Image.
void Set(float input)
{
Set(input, true);
}
void Set(float input, bool sendCallback)
{
// Clamp the input
float newValue = Mathf.Clamp(input, MinValue, MaxValue);
if (WholeNumbers)
newValue = Mathf.Round(newValue);
// If the stepped value doesn't match the last one, it's time to update
if (m_ValueX == newValue)
return;
m_ValueX = newValue;
UpdateVisuals();
if (sendCallback)
m_OnValueChanged.Invoke(newValue, ValueY);
}
void SetY(float input)
{
SetY(input, true);
}
void SetY(float input, bool sendCallback)
{
// Clamp the input
float newValue = Mathf.Clamp(input, MinValue, MaxValue);
if (WholeNumbers)
newValue = Mathf.Round(newValue);
// If the stepped value doesn't match the last one, it's time to update
if (m_ValueY == newValue)
return;
m_ValueY = newValue;
UpdateVisuals();
if (sendCallback)
m_OnValueChanged.Invoke(ValueX, newValue);
}
protected override void OnRectTransformDimensionsChange()
{
base.OnRectTransformDimensionsChange();
UpdateVisuals();
}
enum Axis
{
Horizontal = 0,
Vertical = 1
}
// Force-update the slider. Useful if you've changed the properties and want it to update visually.
private void UpdateVisuals()
{
#if UNITY_EDITOR
if (!Application.isPlaying)
UpdateCachedReferences();
#endif
m_Tracker.Clear();
//to business!
if (m_HandleContainerRect != null)
{
m_Tracker.Add(this, m_HandleRect, DrivenTransformProperties.Anchors);
Vector2 anchorMin = Vector2.zero;
Vector2 anchorMax = Vector2.one;
anchorMin[0] = anchorMax[0] = (NormalizedValueX);
anchorMin[1] = anchorMax[1] = (NormalizedValueY);
m_HandleRect.anchorMin = anchorMin;
m_HandleRect.anchorMax = anchorMax;
}
}
// Update the slider's position based on the mouse.
void UpdateDrag(PointerEventData eventData, Camera cam)
{
RectTransform clickRect = m_HandleContainerRect;
if (clickRect != null && clickRect.rect.size[0] > 0)
{
Vector2 localCursor;
if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(clickRect, eventData.position, cam, out localCursor))
return;
localCursor -= clickRect.rect.position;
float val = Mathf.Clamp01((localCursor - m_Offset)[0] / clickRect.rect.size[0]);
NormalizedValueX = (val);
float valY = Mathf.Clamp01((localCursor - m_Offset)[1] / clickRect.rect.size[1]);
NormalizedValueY = (valY);
}
}
private bool MayDrag(PointerEventData eventData)
{
return IsActive() && IsInteractable() && eventData.button == PointerEventData.InputButton.Left;
}
public override void OnPointerDown(PointerEventData eventData)
{
if (!MayDrag(eventData))
return;
base.OnPointerDown(eventData);
m_Offset = Vector2.zero;
if (m_HandleContainerRect != null && RectTransformUtility.RectangleContainsScreenPoint(m_HandleRect, eventData.position, eventData.enterEventCamera))
{
Vector2 localMousePos;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(m_HandleRect, eventData.position, eventData.pressEventCamera, out localMousePos))
m_Offset = localMousePos;
m_Offset.y = -m_Offset.y;
}
else
{
// Outside the slider handle - jump to this point instead
UpdateDrag(eventData, eventData.pressEventCamera);
}
}
public virtual void OnDrag(PointerEventData eventData)
{
if (!MayDrag(eventData))
return;
UpdateDrag(eventData, eventData.pressEventCamera);
}
//public override void OnMove(AxisEventData eventData)
//{
// if (!IsActive() || !IsInteractable())
// {
// base.OnMove(eventData);
// return;
// }
// switch (eventData.moveDir)
// {
// case MoveDirection.Left:
// if (axis == Axis.Horizontal && FindSelectableOnLeft() == null) {
// Set(reverseValue ? value + stepSize : value - stepSize);
// SetY (reverseValue ? valueY + stepSize : valueY - stepSize);
// }
// else
// base.OnMove(eventData);
// break;
// case MoveDirection.Right:
// if (axis == Axis.Horizontal && FindSelectableOnRight() == null) {
// Set(reverseValue ? value - stepSize : value + stepSize);
// SetY(reverseValue ? valueY - stepSize : valueY + stepSize);
// }
// else
// base.OnMove(eventData);
// break;
// case MoveDirection.Up:
// if (axis == Axis.Vertical && FindSelectableOnUp() == null) {
// Set(reverseValue ? value - stepSize : value + stepSize);
// SetY(reverseValue ? valueY - stepSize : valueY + stepSize);
// }
// else
// base.OnMove(eventData);
// break;
// case MoveDirection.Down:
// if (axis == Axis.Vertical && FindSelectableOnDown() == null) {
// Set(reverseValue ? value + stepSize : value - stepSize);
// SetY(reverseValue ? valueY + stepSize : valueY - stepSize);
// }
// else
// base.OnMove(eventData);
// break;
// }
//}
//public override Selectable FindSelectableOnLeft()
//{
// if (navigation.mode == Navigation.Mode.Automatic && axis == Axis.Horizontal)
// return null;
// return base.FindSelectableOnLeft();
//}
//public override Selectable FindSelectableOnRight()
//{
// if (navigation.mode == Navigation.Mode.Automatic && axis == Axis.Horizontal)
// return null;
// return base.FindSelectableOnRight();
//}
//public override Selectable FindSelectableOnUp()
//{
// if (navigation.mode == Navigation.Mode.Automatic && axis == Axis.Vertical)
// return null;
// return base.FindSelectableOnUp();
//}
//public override Selectable FindSelectableOnDown()
//{
// if (navigation.mode == Navigation.Mode.Automatic && axis == Axis.Vertical)
// return null;
// return base.FindSelectableOnDown();
//}
public virtual void OnInitializePotentialDrag(PointerEventData eventData)
{
eventData.useDragThreshold = false;
}
}
}

View File

@ -1,8 +1,12 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: 4b2932ecb1276c447863e4d540fc693a guid: a3fd655d1aa9c684d88cdfdd0da9aa34
timeCreated: 1480011174
licenseType: Pro
MonoImporter: MonoImporter:
serializedVersion: 2 serializedVersion: 2
defaultReferences: [] defaultReferences: []
executionOrder: 0 executionOrder: 0
icon: {instanceID: 0} icon: {instanceID: 0}
userData: userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,7 +1,7 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: 3928670b50ad9a842ab163d5236d4c03 guid: 7ab63fe1b000c254ebed3937038bb01b
folderAsset: yes folderAsset: yes
timeCreated: 1473357182 timeCreated: 1480011199
licenseType: Pro licenseType: Pro
DefaultImporter: DefaultImporter:
userData: userData:

View File

@ -0,0 +1,31 @@
///Credit judah4
///Sourced from - http://forum.unity3d.com/threads/color-picker.267043/
using System.Globalization;
namespace UnityEngine.UI.Extensions.ColorPicker
{
[RequireComponent(typeof(Image))]
public class ColorImage : MonoBehaviour
{
public ColorPickerControl 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;
}
}
}

View File

@ -1,8 +1,12 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: 95a3081947ad463418f853d27e477017 guid: 4210a64d2ce72e9488cf2ad174e9df5b
timeCreated: 1480011173
licenseType: Pro
MonoImporter: MonoImporter:
serializedVersion: 2 serializedVersion: 2
defaultReferences: [] defaultReferences: []
executionOrder: 0 executionOrder: 0
icon: {instanceID: 0} icon: {instanceID: 0}
userData: userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,88 @@
///Credit judah4
///Sourced from - http://forum.unity3d.com/threads/color-picker.267043/
using System.Globalization;
namespace UnityEngine.UI.Extensions.ColorPicker
{
[RequireComponent(typeof(Text))]
public class ColorLabel : MonoBehaviour
{
public ColorPickerControl picker;
public ColorValues type;
public string prefix = "R: ";
public float minValue = 0;
public float maxValue = 255;
public int precision = 0;
private Text label;
private void Awake()
{
label = GetComponent<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<Text>();
UpdateValue();
}
#endif
private void ColorChanged(Color color)
{
UpdateValue();
}
private void HSVChanged(float hue, float sateration, float value)
{
UpdateValue();
}
private void UpdateValue()
{
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();
}
}
}

View File

@ -1,8 +1,12 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: 8262e4a8322117f4da079921eaa72834 guid: b32edf53f1ae84f479c9439b4b5f5b91
timeCreated: 1480011174
licenseType: Pro
MonoImporter: MonoImporter:
serializedVersion: 2 serializedVersion: 2
defaultReferences: [] defaultReferences: []
executionOrder: 0 executionOrder: 0
icon: {instanceID: 0} icon: {instanceID: 0}
userData: userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,257 @@
///Credit judah4
///Sourced from - http://forum.unity3d.com/threads/color-picker.267043/
namespace UnityEngine.UI.Extensions.ColorPicker
{
public class ColorPickerControl : MonoBehaviour
{
private float _hue = 0;
private float _saturation = 0;
private float _brightness = 0;
private float _red = 0;
private float _green = 0;
private float _blue = 0;
private float _alpha = 1;
public ColorChangedEvent onValueChanged = new ColorChangedEvent();
public HSVChangedEvent onHSVChanged = new HSVChangedEvent();
public Color CurrentColor
{
get
{
return new Color(_red, _green, _blue, _alpha);
}
set
{
if (CurrentColor == value)
return;
_red = value.r;
_green = value.g;
_blue = value.b;
_alpha = value.a;
RGBChanged();
SendChangedEvent();
}
}
private void Start()
{
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 _red;
}
set
{
if (_red == value)
return;
_red = value;
RGBChanged();
SendChangedEvent();
}
}
public float G
{
get
{
return _green;
}
set
{
if (_green == value)
return;
_green = value;
RGBChanged();
SendChangedEvent();
}
}
public float B
{
get
{
return _blue;
}
set
{
if (_blue == value)
return;
_blue = value;
RGBChanged();
SendChangedEvent();
}
}
private float A
{
get
{
return _alpha;
}
set
{
if (_alpha == value)
return;
_alpha = 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, _alpha);
_red = color.r;
_green = color.g;
_blue = color.b;
}
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 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("");
}
}
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 062e39c1ab27d894bb43a964baff69d2
timeCreated: 1480011173
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,49 @@
///Credit judah4
///Sourced from - http://forum.unity3d.com/threads/color-picker.267043/
using System.Globalization;
namespace UnityEngine.UI.Extensions.ColorPicker
{
public class ColorPickerPresets : MonoBehaviour
{
public ColorPickerControl picker;
public GameObject[] presets;
public Image createPresetImage;
void Awake()
{
// picker.onHSVChanged.AddListener(HSVChanged);
picker.onValueChanged.AddListener(ColorChanged);
}
public void CreatePresetButton()
{
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;
}
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 502205da447ca7a479ce5ae45e5c19d2
timeCreated: 1480011173
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,12 +1,12 @@
///Credit judah4 ///Credit judah4
///Sourced from - http://forum.unity3d.com/threads/color-picker.267043/ ///Sourced from - http://forum.unity3d.com/threads/color-picker.267043/
namespace UnityEngine.UI.Extensions namespace UnityEngine.UI.Extensions.ColorPicker
{ {
public class ColorPickerTester : MonoBehaviour public class ColorPickerTester : MonoBehaviour
{ {
public Renderer pickerRenderer; public Renderer pickerRenderer;
public HSVPicker picker; public ColorPickerControl picker;
void Awake() void Awake()
{ {

View File

@ -0,0 +1,93 @@
///Credit judah4
///Sourced from - http://forum.unity3d.com/threads/color-picker.267043/
namespace UnityEngine.UI.Extensions.ColorPicker
{
/// <summary>
/// Displays one of the color values of aColorPicker
/// </summary>
[RequireComponent(typeof(Slider))]
public class ColorSlider : MonoBehaviour
{
public ColorPickerControl ColorPicker;
/// <summary>
/// Which value this slider can edit.
/// </summary>
public ColorValues type;
private Slider slider;
private bool listen = true;
private void Awake()
{
slider = GetComponent<Slider>();
ColorPicker.onValueChanged.AddListener(ColorChanged);
ColorPicker.onHSVChanged.AddListener(HSVChanged);
slider.onValueChanged.AddListener(SliderChanged);
}
private void OnDestroy()
{
ColorPicker.onValueChanged.RemoveListener(ColorChanged);
ColorPicker.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;
ColorPicker.AssignColor(type, newValue);
}
listen = true;
}
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 91558da930270ac4a960ba03b81c836a
timeCreated: 1480011173
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,231 @@
///Credit judah4
///Sourced from - http://forum.unity3d.com/threads/color-picker.267043/
namespace UnityEngine.UI.Extensions.ColorPicker
{
[RequireComponent(typeof(RawImage)), ExecuteInEditMode()]
public class ColorSliderImage : MonoBehaviour
{
public ColorPickerControl 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 (image)
{
RegenerateTexture();
}
else
{
Debug.LogWarning("Missing RawImage on object [" + name + "]");
}
}
private void OnEnable()
{
if (picker != null && Application.isPlaying)
{
picker.onValueChanged.AddListener(ColorChanged);
picker.onHSVChanged.AddListener(ColorChanged);
}
}
private void OnDisable()
{
if (picker != null)
{
picker.onValueChanged.RemoveListener(ColorChanged);
picker.onHSVChanged.RemoveListener(ColorChanged);
}
}
private void OnDestroy()
{
if (image.texture != null)
DestroyImmediate(image.texture);
}
#if UNITY_EDITOR
private void OnValidate()
{
image = GetComponent<RawImage>();
if (image)
{
RegenerateTexture();
}
else
{
Debug.LogWarning("Missing RawImage on object [" + name + "]");
}
}
#endif
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 ColorChanged(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()
{
if (!picker)
{
Debug.LogWarning("Missing Picker on object [" + name + "]");
}
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;
}
}
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: cd3cce5a27f2db94fa394c4719bddecd
timeCreated: 1480011174
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,17 @@
///Credit judah4
///Sourced from - http://forum.unity3d.com/threads/color-picker.267043/
namespace UnityEngine.UI.Extensions.ColorPicker
{
public enum ColorValues
{
R,
G,
B,
A,
Hue,
Saturation,
Value
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 0f473590aed5e3f4ab5a0157b2a53dbd
timeCreated: 1480011173
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: cbbc689694061e6439664eda55449513
folderAsset: yes
timeCreated: 1480011173
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,9 @@
using UnityEngine;
using System;
using UnityEngine.Events;
[Serializable]
public class ColorChangedEvent : UnityEvent<Color>
{
}

View File

@ -1,8 +1,8 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: ff46fbecea7739f4690e4285c88f53c5 guid: ff46fbecea7739f4690e4285c88f53c5
MonoImporter: MonoImporter:
serializedVersion: 2 serializedVersion: 2
defaultReferences: [] defaultReferences: []
executionOrder: 0 executionOrder: 0
icon: {instanceID: 0} icon: {instanceID: 0}
userData: userData:

View File

@ -0,0 +1,7 @@
using UnityEngine;
using UnityEngine.Events;
public class HSVChangedEvent : UnityEvent<float, float, float>
{
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 3d95ce8fba3dbbf4eb14411412169b88
timeCreated: 1442747317
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,206 @@
///Credit judah4
///Sourced from - http://forum.unity3d.com/threads/color-picker.267043/
using System;
namespace UnityEngine.UI.Extensions.ColorPicker
{
#region ColorUtilities
public static class HSVUtil
{
public static HsvColor ConvertRgbToHsv(Color color)
{
return ConvertRgbToHsv((int)(color.r * 255), (int)(color.g * 255), (int)(color.b * 255));
}
//Converts an RGB color to an HSV color.
public static HsvColor ConvertRgbToHsv(double r, double b, double g)
{
double delta, min;
double h = 0, s, v;
min = Math.Min(Math.Min(r, g), b);
v = Math.Max(Math.Max(r, g), b);
delta = v - min;
if (v == 0.0)
s = 0;
else
s = delta / v;
if (s == 0)
h = 360;
else
{
if (r == v)
h = (g - b) / delta;
else if (g == v)
h = 2 + (b - r) / delta;
else if (b == v)
h = 4 + (r - g) / delta;
h *= 60;
if (h <= 0.0)
h += 360;
}
HsvColor hsvColor = new HsvColor()
{
H = 360 - h,
S = s,
V = v / 255
};
return hsvColor;
}
// Converts an HSV color to an RGB color.
public static Color ConvertHsvToRgb(double h, double s, double v, float alpha)
{
double r = 0, g = 0, b = 0;
if (s == 0)
{
r = v;
g = v;
b = v;
}
else
{
int i;
double f, p, q, t;
if (h == 360)
h = 0;
else
h = h / 60;
i = (int)(h);
f = h - i;
p = v * (1.0 - s);
q = v * (1.0 - (s * f));
t = v * (1.0 - (s * (1.0f - f)));
switch (i)
{
case 0:
r = v;
g = t;
b = p;
break;
case 1:
r = q;
g = v;
b = p;
break;
case 2:
r = p;
g = v;
b = t;
break;
case 3:
r = p;
g = q;
b = v;
break;
case 4:
r = t;
g = p;
b = v;
break;
default:
r = v;
g = p;
b = q;
break;
}
}
return new Color((float)r, (float)g, (float)b, alpha);
}
}
#endregion ColorUtilities
// Describes a color in terms of
// Hue, Saturation, and Value (brightness)
#region HsvColor
public struct HsvColor
{
/// <summary>
/// The Hue, ranges between 0 and 360
/// </summary>
public double H;
/// <summary>
/// The saturation, ranges between 0 and 1
/// </summary>
public double S;
// The value (brightness), ranges between 0 and 1
public double V;
public float NormalizedH
{
get
{
return (float)H / 360f;
}
set
{
H = (double)value * 360;
}
}
public float NormalizedS
{
get
{
return (float)S;
}
set
{
S = value;
}
}
public float NormalizedV
{
get
{
return (float)V;
}
set
{
V = (double)value;
}
}
public HsvColor(double h, double s, double v)
{
this.H = h;
this.S = s;
this.V = v;
}
public override string ToString()
{
return "{" + H.ToString("f2") + "," + S.ToString("f2") + "," + V.ToString("f2") + "}";
}
}
#endregion HsvColor
}

View File

@ -0,0 +1,101 @@
///Credit judah4
///Sourced from - http://forum.unity3d.com/threads/color-picker.267043/
using System.Globalization;
namespace UnityEngine.UI.Extensions.ColorPicker
{
[RequireComponent(typeof(InputField))]
public class HexColorField : MonoBehaviour
{
public ColorPickerControl ColorPicker;
public bool displayAlpha;
private InputField hexInputField;
private const string hexRegex = "^#?(?:[0-9a-fA-F]{3,4}){1,2}$";
private void Awake()
{
hexInputField = GetComponent<InputField>();
// Add listeners to keep text (and color) up to date
hexInputField.onEndEdit.AddListener(UpdateColor);
ColorPicker.onValueChanged.AddListener(UpdateHex);
}
private void OnDestroy()
{
hexInputField.onValueChanged.RemoveListener(UpdateColor);
ColorPicker.onValueChanged.RemoveListener(UpdateHex);
}
private void UpdateHex(Color newColor)
{
hexInputField.text = ColorToHex(newColor);
}
private void UpdateColor(string newHex)
{
Color32 color;
if (HexToColor(newHex, out color))
ColorPicker.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)
{
if (displayAlpha)
return string.Format("#{0:X2}{1:X2}{2:X2}{3:X2}", color.r, color.g, color.b, color.a);
else
return string.Format("#{0:X2}{1:X2}{2:X2}", color.r, color.g, color.b);
}
public static bool HexToColor(string hex, out Color32 color)
{
// Check if this is a valid hex string (# is optional)
if (System.Text.RegularExpressions.Regex.IsMatch(hex, hexRegex))
{
int startIndex = hex.StartsWith("#") ? 1 : 0;
if (hex.Length == startIndex + 8) //#RRGGBBAA
{
color = new Color32(byte.Parse(hex.Substring(startIndex, 2), NumberStyles.AllowHexSpecifier),
byte.Parse(hex.Substring(startIndex + 2, 2), NumberStyles.AllowHexSpecifier),
byte.Parse(hex.Substring(startIndex + 4, 2), NumberStyles.AllowHexSpecifier),
byte.Parse(hex.Substring(startIndex + 6, 2), NumberStyles.AllowHexSpecifier));
}
else if (hex.Length == startIndex + 6) //#RRGGBB
{
color = new Color32(byte.Parse(hex.Substring(startIndex, 2), NumberStyles.AllowHexSpecifier),
byte.Parse(hex.Substring(startIndex + 2, 2), NumberStyles.AllowHexSpecifier),
byte.Parse(hex.Substring(startIndex + 4, 2), NumberStyles.AllowHexSpecifier),
255);
}
else if (hex.Length == startIndex + 4) //#RGBA
{
color = new Color32(byte.Parse("" + hex[startIndex] + hex[startIndex], NumberStyles.AllowHexSpecifier),
byte.Parse("" + hex[startIndex + 1] + hex[startIndex + 1], NumberStyles.AllowHexSpecifier),
byte.Parse("" + hex[startIndex + 2] + hex[startIndex + 2], NumberStyles.AllowHexSpecifier),
byte.Parse("" + hex[startIndex + 3] + hex[startIndex + 3], NumberStyles.AllowHexSpecifier));
}
else //#RGB
{
color = new Color32(byte.Parse("" + hex[startIndex] + hex[startIndex], NumberStyles.AllowHexSpecifier),
byte.Parse("" + hex[startIndex + 1] + hex[startIndex + 1], NumberStyles.AllowHexSpecifier),
byte.Parse("" + hex[startIndex + 2] + hex[startIndex + 2], NumberStyles.AllowHexSpecifier),
255);
}
return true;
}
else
{
color = new Color32();
return false;
}
}
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 7343402602909bd4f928d58433c5c87f
timeCreated: 1480011173
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,123 @@
///Credit judah4
///Sourced from - http://forum.unity3d.com/threads/color-picker.267043/
using System.Globalization;
namespace UnityEngine.UI.Extensions.ColorPicker
{
[RequireComponent(typeof(BoxSlider), typeof(RawImage)), ExecuteInEditMode()]
public class SVBoxSlider : MonoBehaviour
{
public ColorPickerControl picker;
private BoxSlider slider;
private RawImage image;
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>();
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);
}
#if UNITY_EDITOR
private void OnValidate()
{
image = GetComponent<RawImage>();
RegenerateSVTexture();
}
#endif
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 != h)
{
lastH = h;
RegenerateSVTexture();
}
if (s != slider.NormalizedValueX)
{
listen = false;
slider.NormalizedValueX = s;
}
if (v != 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);
Texture2D texture = new Texture2D(100, 100)
{
hideFlags = HideFlags.DontSave
};
for (int s = 0; s < 100; s++)
{
Color32[] colors = new Color32[100];
for (int v = 0; v < 100; v++)
{
colors[v] = HSVUtil.ConvertHsvToRgb(h, (float)s / 100, (float)v / 100, 1);
}
texture.SetPixels32(s, 0, 1, 100, colors);
}
texture.Apply();
image.texture = texture;
}
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 7a0647785ed421e449239dbd6512e156
timeCreated: 1480011173
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,35 @@
///Credit judah4
///Sourced from - http://forum.unity3d.com/threads/color-picker.267043/
using System.Globalization;
namespace UnityEngine.UI.Extensions
{
public class TiltWindow : MonoBehaviour
{
public Vector2 range = new Vector2(5f, 3f);
Transform mTrans;
Quaternion mStart;
Vector2 mRot = Vector2.zero;
void Start ()
{
mTrans = transform;
mStart = mTrans.localRotation;
}
void Update ()
{
Vector3 pos = Input.mousePosition;
float halfWidth = Screen.width * 0.5f;
float halfHeight = Screen.height * 0.5f;
float x = Mathf.Clamp((pos.x - halfWidth) / halfWidth, -1f, 1f);
float y = Mathf.Clamp((pos.y - halfHeight) / halfHeight, -1f, 1f);
mRot = Vector2.Lerp(mRot, new Vector2(x, y), Time.deltaTime * 5f);
mTrans.localRotation = mStart * Quaternion.Euler(-mRot.y * range.y, mRot.x * range.x, 0f);
}
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: c7be5109ea5b91e4b856621023b15168
timeCreated: 1480011174
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,228 +0,0 @@
///Credit judah4
///Sourced from - http://forum.unity3d.com/threads/color-picker.267043/
namespace UnityEngine.UI.Extensions
{
public class HSVPicker : MonoBehaviour
{
public HexRGB hexrgb;
public Color currentColor;
public Image colorImage;
public Image pointer;
public Image cursor;
public RawImage hsvSlider;
public RawImage hsvImage;
//public InputField inputR;
//public InputField inputG;
//public InputField inputB;
public Slider sliderR;
public Slider sliderG;
public Slider sliderB;
public Text sliderRText;
public Text sliderGText;
public Text sliderBText;
public float pointerPos = 0;
public float cursorX = 0;
public float cursorY = 0;
public HSVSliderEvent onValueChanged = new HSVSliderEvent();
private bool dontAssignUpdate = false;
void Awake()
{
hsvSlider.texture = HSVUtil.GenerateHSVTexture((int)hsvSlider.rectTransform.rect.width, (int)hsvSlider.rectTransform.rect.height);
sliderR.onValueChanged.AddListener(newValue =>
{
currentColor.r = newValue;
if (dontAssignUpdate == false)
{
AssignColor(currentColor);
}
sliderRText.text = "R:" + (int)(currentColor.r * 255f);
hexrgb.ManipulateViaRGB2Hex();
});
sliderG.onValueChanged.AddListener(newValue =>
{
currentColor.g = newValue;
if (dontAssignUpdate == false)
{
AssignColor(currentColor);
}
sliderGText.text = "G:" + (int)(currentColor.g * 255f);
hexrgb.ManipulateViaRGB2Hex();
});
sliderB.onValueChanged.AddListener(newValue =>
{
currentColor.b = newValue;
if (dontAssignUpdate == false)
{
AssignColor(currentColor);
}
sliderBText.text = "B:" + (int)(currentColor.b * 255f);
hexrgb.ManipulateViaRGB2Hex();
});
hsvImage.texture = HSVUtil.GenerateColorTexture((int)hsvImage.rectTransform.rect.width, (int)hsvImage.rectTransform.rect.height, ((Texture2D)hsvSlider.texture).GetPixelBilinear(0, 0));
MoveCursor(cursorX, cursorY);
}
// Update is called once per frame
void Update()
{
//if (Input.GetKeyDown(KeyCode.R))
//{
// var color = new Color(45f / 255, 200f / 255, 255f / 255);
// Debug.Log(color);
// AssignColor(color);
// }
}
public void AssignColor(Color color)
{
var hsv = HSVUtil.ConvertRgbToHsv(color);
// Debug.Log(hsv.ToString());
float hOffset = (float)(hsv.H / 360);
//if (hsv.S > 1)
//{
// hsv.S %= 1f;
//}
//if (hsv.V > 1)
//{
// hsv.V %= 1f;
//}
MovePointer(hOffset, false);
MoveCursor((float)hsv.S, (float)hsv.V, false);
currentColor = color;
colorImage.color = currentColor;
onValueChanged.Invoke(currentColor);
}
public Color MoveCursor(float posX, float posY, bool updateInputs = true)
{
dontAssignUpdate = updateInputs;
if (posX > 1)
{
posX %= 1;
}
if (posY > 1)
{
posY %= 1;
}
posY = Mathf.Clamp(posY, 0, .9999f);
posX = Mathf.Clamp(posX, 0, .9999f);
cursorX = posX;
cursorY = posY;
cursor.rectTransform.anchoredPosition = new Vector2(posX * hsvImage.rectTransform.rect.width, posY * hsvImage.rectTransform.rect.height - hsvImage.rectTransform.rect.height);
currentColor = GetColor(cursorX, cursorY);
colorImage.color = currentColor;
if (updateInputs)
{
UpdateInputs();
onValueChanged.Invoke(currentColor);
}
dontAssignUpdate = false;
return currentColor;
}
public Color GetColor(float posX, float posY)
{
//Debug.Log(posX + " " + posY);
return ((Texture2D)hsvImage.texture).GetPixel((int)(cursorX * hsvImage.texture.width), (int)(cursorY * hsvImage.texture.height));
}
public Color MovePointer(float newPos, bool updateInputs = true)
{
dontAssignUpdate = updateInputs;
if (newPos > 1)
{
newPos %= 1f;//hsv
}
pointerPos = newPos;
var mainColor = ((Texture2D)hsvSlider.texture).GetPixelBilinear(0, pointerPos);
if (hsvImage.texture != null)
{
if ((int)hsvImage.rectTransform.rect.width != hsvImage.texture.width || (int)hsvImage.rectTransform.rect.height != hsvImage.texture.height)
{
Destroy(hsvImage.texture);
hsvImage.texture = null;
hsvImage.texture = HSVUtil.GenerateColorTexture((int)hsvImage.rectTransform.rect.width, (int)hsvImage.rectTransform.rect.height, mainColor);
}
else
{
HSVUtil.GenerateColorTexture(mainColor, (Texture2D)hsvImage.texture);
}
}
else
{
hsvImage.texture = HSVUtil.GenerateColorTexture((int)hsvImage.rectTransform.rect.width, (int)hsvImage.rectTransform.rect.height, mainColor);
}
pointer.rectTransform.anchoredPosition = new Vector2(0, -pointerPos * hsvSlider.rectTransform.rect.height);
currentColor = GetColor(cursorX, cursorY);
colorImage.color = currentColor;
if (updateInputs)
{
UpdateInputs();
onValueChanged.Invoke(currentColor);
}
dontAssignUpdate = false;
return currentColor;
}
public void UpdateInputs()
{
sliderR.value = currentColor.r;
sliderG.value = currentColor.g;
sliderB.value = currentColor.b;
sliderRText.text = "R:" + (currentColor.r * 255f);
sliderGText.text = "G:" + (currentColor.g * 255f);
sliderBText.text = "B:" + (currentColor.b * 255f);
}
void OnDestroy()
{
if (hsvSlider.texture != null)
{
Destroy(hsvSlider.texture);
}
if (hsvImage.texture != null)
{
Destroy(hsvImage.texture);
}
}
}
}

View File

@ -1,11 +0,0 @@
///Credit judah4
///Sourced from - http://forum.unity3d.com/threads/color-picker.267043/
using UnityEngine.Events;
namespace UnityEngine.UI.Extensions
{
public class HSVSliderEvent : UnityEvent<Color>
{
}
}

View File

@ -1,278 +0,0 @@
///Credit judah4
///Sourced from - http://forum.unity3d.com/threads/color-picker.267043/
using System;
using System.Collections.Generic;
namespace UnityEngine.UI.Extensions
{
#region ColorUtilities
public static class HSVUtil
{
public static HsvColor ConvertRgbToHsv(Color color)
{
return ConvertRgbToHsv((int)(color.r * 255), (int)(color.g * 255), (int)(color.b * 255));
}
// Converts an RGB color to an HSV color.
public static HsvColor ConvertRgbToHsv(double r, double b, double g)
{
double delta, min;
double h = 0, s, v;
min = Math.Min(Math.Min(r, g), b);
v = Math.Max(Math.Max(r, g), b);
delta = v - min;
if (v == 0.0)
{
s = 0;
}
else
s = delta / v;
if (s == 0)
h = 0.0f;
else
{
if (r == v)
h = (g - b) / delta;
else if (g == v)
h = 2 + (b - r) / delta;
else if (b == v)
h = 4 + (r - g) / delta;
h *= 60;
if (h < 0.0)
h = h + 360;
}
HsvColor hsvColor = new HsvColor();
hsvColor.H = h;
hsvColor.S = s;
hsvColor.V = v / 255;
return hsvColor;
}
public static Color ConvertHsvToRgb(HsvColor color)
{
return ConvertHsvToRgb(color.H, color.S, color.V);
}
// Converts an HSV color to an RGB color.
public static Color ConvertHsvToRgb(double h, double s, double v)
{
double r = 0, g = 0, b = 0;
if (s == 0)
{
r = v;
g = v;
b = v;
}
else
{
int i;
double f, p, q, t;
if (h == 360)
h = 0;
else
h = h / 60;
i = (int)(h);
f = h - i;
p = v * (1.0 - s);
q = v * (1.0 - (s * f));
t = v * (1.0 - (s * (1.0f - f)));
switch (i)
{
case 0:
r = v;
g = t;
b = p;
break;
case 1:
r = q;
g = v;
b = p;
break;
case 2:
r = p;
g = v;
b = t;
break;
case 3:
r = p;
g = q;
b = v;
break;
case 4:
r = t;
g = p;
b = v;
break;
default:
r = v;
g = p;
b = q;
break;
}
}
return new Color((float)r, (float)g, (float)b, 1);//255, (byte)(r * 255), (byte)(g * 255), (byte)(b * 255));
}
// Generates a list of colors with hues ranging from 0 360
// and a saturation and value of 1.
public static List<Color> GenerateHsvSpectrum()
{
List<Color> colorsList = new List<Color>(8);
//for (int i = 0; i < 29; i++)
//{
// colorsList.Add(
// ConvertHsvToRgb(i * 12, 1, 1)
// );
//}
for (int i = 0; i < 359; i++)
{
colorsList.Add(
ConvertHsvToRgb(i, 1, 1)
);
}
colorsList.Add(ConvertHsvToRgb(0, 1, 1));
return colorsList;
}
public static Texture2D GenerateHSVTexture(int width, int height)
{
var list = GenerateHsvSpectrum();
float interval = 1;
if (list.Count > height)
{
interval = (float)list.Count / height;
}
var texture = new Texture2D(width, height);
int ySize = Mathf.Max(1, (int)(1f / (list.Count / interval) * height));
int colorH = 0;
Color color = Color.white;
for (float cnt = 0; cnt < list.Count; cnt += interval)
{
color = list[(int)cnt];
Color[] colors = new Color[width * ySize];
for (int i = 0; i < width * ySize; i++)
{
colors[i] = color;
}
if (colorH < height)
{
texture.SetPixels(0, colorH, width, ySize, colors);
}
colorH += ySize;
}
texture.Apply();
return texture;
}
public static Texture2D GenerateColorTexture(Color mainColor, Texture2D texture)
{
int width = texture.width;
int height = texture.height;
var hsvColor = ConvertRgbToHsv((int)(mainColor.r * 255), (int)(mainColor.g * 255), (int)(mainColor.b * 255));
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
var adjustedColor = hsvColor;
adjustedColor.V = (float)y / height;
adjustedColor.S = (float)x / width;
var color = ConvertHsvToRgb(adjustedColor.H, adjustedColor.S, adjustedColor.V);
texture.SetPixel(x, y, color);
}
}
texture.Apply();
return texture;
}
public static Texture2D GenerateColorTexture(int width, int height, Color mainColor)
{
return GenerateColorTexture(mainColor, new Texture2D(width, height));
}
}
#endregion ColorUtilities
// Describes a color in terms of
// Hue, Saturation, and Value (brightness)
#region HsvColor
public struct HsvColor
{
public double H;
public double S;
public double V;
public HsvColor(double h, double s, double v)
{
this.H = h;
this.S = s;
this.V = v;
}
public override string ToString()
{
return "{" + H + "," + S + "," + V + "}";
}
}
#endregion HsvColor
}

View File

@ -1,79 +0,0 @@
///Credit judah4
///Sourced from - http://forum.unity3d.com/threads/color-picker.267043/
using System.Globalization;
namespace UnityEngine.UI.Extensions
{
public class HexRGB : MonoBehaviour
{
// Unity 5.1/2 needs an InputFiled vs grabbing the text component
public InputField hexInput;
public HSVPicker hsvpicker;
public void ManipulateViaRGB2Hex()
{
Color color = hsvpicker.currentColor;
string hex = ColorToHex(color);
hexInput.text = hex;
}
public static string ColorToHex(Color color)
{
int r = (int)(color.r * 255);
int g = (int)(color.g * 255);
int b = (int)(color.b * 255);
return string.Format("#{0:X2}{1:X2}{2:X2}", r, g, b);
}
public void ManipulateViaHex2RGB()
{
string hex = hexInput.text;
Vector3 rgb = Hex2RGB(hex);
Color color = NormalizeVector4(rgb, 255f, 1f); print(rgb);
hsvpicker.AssignColor(color);
}
static Color NormalizeVector4(Vector3 v, float r, float a)
{
float red = v.x / r;
float green = v.y / r;
float blue = v.z / r;
return new Color(red, green, blue, a);
}
Vector3 Hex2RGB(string hexColor)
{
//Remove # if present
if (hexColor.IndexOf('#') != -1)
hexColor = hexColor.Replace("#", "");
int red = 0;
int green = 0;
int blue = 0;
if (hexColor.Length == 6)
{
//#RRGGBB
red = int.Parse(hexColor.Substring(0, 2), NumberStyles.AllowHexSpecifier);
green = int.Parse(hexColor.Substring(2, 2), NumberStyles.AllowHexSpecifier);
blue = int.Parse(hexColor.Substring(4, 2), NumberStyles.AllowHexSpecifier);
}
else if (hexColor.Length == 3)
{
//#RGB
red = int.Parse(hexColor[0].ToString() + hexColor[0].ToString(), NumberStyles.AllowHexSpecifier);
green = int.Parse(hexColor[1].ToString() + hexColor[1].ToString(), NumberStyles.AllowHexSpecifier);
blue = int.Parse(hexColor[2].ToString() + hexColor[2].ToString(), NumberStyles.AllowHexSpecifier);
}
return new Vector3(red, green, blue);
}
}
}

View File

@ -1,38 +0,0 @@
///Credit judah4
///Sourced from - http://forum.unity3d.com/threads/color-picker.267043/
using UnityEngine.EventSystems;
namespace UnityEngine.UI.Extensions
{
public class HsvBoxSelector : MonoBehaviour, IDragHandler, IPointerDownHandler
{
public HSVPicker picker;
void PlaceCursor(PointerEventData eventData)
{
var pos = new Vector2(eventData.position.x - picker.hsvImage.rectTransform.position.x, picker.hsvImage.rectTransform.rect.height * picker.hsvImage.transform.lossyScale.y - (picker.hsvImage.rectTransform.position.y - eventData.position.y));
// Debug.Log(pos);
pos.x /= picker.hsvImage.rectTransform.rect.width * picker.hsvImage.transform.lossyScale.x;
pos.y /= picker.hsvImage.rectTransform.rect.height * picker.hsvImage.transform.lossyScale.y;
pos.x = Mathf.Clamp(pos.x, 0, .9999f); //1 is the same as 0
pos.y = Mathf.Clamp(pos.y, 0, .9999f);
//Debug.Log(pos);
picker.MoveCursor(pos.x, pos.y);
}
public void OnDrag(PointerEventData eventData)
{
PlaceCursor(eventData);
}
public void OnPointerDown(PointerEventData eventData)
{
PlaceCursor(eventData);
}
}
}

View File

@ -1,37 +0,0 @@
///Credit judah4
///Sourced from - http://forum.unity3d.com/threads/color-picker.267043/
using UnityEngine.EventSystems;
namespace UnityEngine.UI.Extensions
{
public class HsvSliderPicker : MonoBehaviour, IDragHandler, IPointerDownHandler
{
public HSVPicker picker;
void PlacePointer(PointerEventData eventData)
{
var pos = new Vector2(eventData.position.x - picker.hsvSlider.rectTransform.position.x, picker.hsvSlider.rectTransform.position.y - eventData.position.y);
pos.y /= picker.hsvSlider.rectTransform.rect.height * picker.hsvSlider.canvas.transform.lossyScale.y;
//Debug.Log(eventData.position.ToString() + " " + picker.hsvSlider.rectTransform.position + " " + picker.hsvSlider.rectTransform.rect.height);
pos.y = Mathf.Clamp(pos.y, 0, 1f);
picker.MovePointer(pos.y);
}
public void OnDrag(PointerEventData eventData)
{
PlacePointer(eventData);
}
public void OnPointerDown(PointerEventData eventData)
{
PlacePointer(eventData);
}
}
}

View File

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 7d5b03a797859a94989cdbcb68e37fb1
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData: