namespace UnityEngine.UI.Extensions { public class HSVPicker : MonoBehaviour { public UI_Ladder ui_ladder; public Color currentColor; public Image colorImage; public Image pointer; public Image cursor; public RawImage hsvSlider; public RawImage hsvImage; public Color Low_Color_Picker; public Color Mid_Color_Picker; public Color High_Color_Picker; private int idx_in_Edit_Mode = 0; public Movable_Panel_Interface moveable_Panel_Interface; public Color_Swatches color_Swatches; public Swatch_Button_Animated sba_Low_Picker; public Swatch_Button_Animated sba_Mid_Picker; public Swatch_Button_Animated sba_High_Picker; public Slider slide_Vert_Hue; float vert_Hue; public Slider slide_HueBox_Vert; float hueBox_Vert; public Slider slide_HueBox_Horz; float hueBox_Horz; public float pointerPos = 0; public float cursorX = 0; public float cursorY = 0; public HSVSliderEvent onValueChanged = new HSVSliderEvent(); private bool dontAssignUpdate = false; void Start() { hsvSlider.texture = HSVUtil.GenerateHSVTexture((int)hsvSlider.rectTransform.rect.width, (int)hsvSlider.rectTransform.rect.height); idx_in_Edit_Mode = 0; slide_Vert_Hue.onValueChanged.AddListener(newValue => { vert_Hue = newValue; MovePointer(vert_Hue); }); slide_HueBox_Vert.onValueChanged.AddListener(newValue => { hueBox_Vert = newValue; MoveCursor(hueBox_Horz, hueBox_Vert); }); slide_HueBox_Horz.onValueChanged.AddListener(newValue => { hueBox_Horz = newValue; MoveCursor(hueBox_Horz, hueBox_Vert); }); hsvImage.texture = HSVUtil.GenerateColorTexture((int)hsvImage.rectTransform.rect.width, (int)hsvImage.rectTransform.rect.height, ((Texture2D)hsvSlider.texture).GetPixelBilinear(0, 0)); MoveCursor(cursorX, cursorY); } void Update() { //ui_ladder.Info_Box(1, "cIdx = " + idx_in_Edit_Mode.ToString()); } public void Select_Color_to_Edit(int col_Idx) { idx_in_Edit_Mode = col_Idx; AssignColor(color_Swatches.getColor_Swatch(col_Idx)); } 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) { return ((Texture2D)hsvImage.texture).GetPixel((int)(cursorX * hsvImage.texture.width), (int)(cursorY * hsvImage.texture.height)); } int count = 0; 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 && ++count > 2) { count = 0; 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() { } void OnDestroy() { if (hsvSlider.texture != null) { Destroy(hsvSlider.texture); } if (hsvImage.texture != null) { Destroy(hsvImage.texture); } } } }
using UnityEngine; using System.Collections; public class Color_Swatches : MonoBehaviour { public UI_Ladder ui_ladder; public Color Low_Color; public Color Mid_Color; public Color High_Color; public Movable_Panel_Interface moveable_Panel_Interface; public Swatch_Button_Animated sba_Low; public Swatch_Button_Animated sba_Mid; public Swatch_Button_Animated sba_High; public enum cols { LOW, MID, HIGH, } void Start () { sba_Low.myIdx = 0; sba_Mid.myIdx = 1; sba_High.myIdx = 2; } public void Launch_Color_Picker(int colID) { ui_ladder.Info_Box(1, "swatches = " + colID.ToString()); moveable_Panel_Interface.Select_COLOR_PICKER(colID); } public void setColor_Swatch(int idx, Color col) { if(idx == (int) cols.LOW) { Low_Color = col; sba_Low.setColor(col); } else if(idx == (int)cols.MID) { Mid_Color = col; sba_Mid.setColor(col); } else { High_Color = col; sba_High.setColor(col); } } public Color getColor_Swatch(int idx) { if (idx == (int)cols.LOW) { return Low_Color; } else if (idx == (int)cols.MID) { return Mid_Color; } else { return High_Color; } } void Update () { } }