Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Assets/HSVPicker/ColorPickerTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ public class ColorPickerTester : MonoBehaviour
// Use this for initialization
void Start ()
{
picker.onValueChanged.AddListener(color =>
/* picker.onValueChanged.AddListener(color =>
{
renderer.material.color = color;
});
}
*/ }

// Update is called once per frame
void Update () {
Expand Down
75 changes: 38 additions & 37 deletions Assets/HSVPicker/HSVPicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ public class HSVPicker : MonoBehaviour {

public static Color currentColor;
public Image colorImage;
public Image pointer;
public Image cursor;
public RawImage hsvSlider;
public RawImage hsvImage;
Expand All @@ -18,6 +17,7 @@ public class HSVPicker : MonoBehaviour {
//public InputField inputG;
//public InputField inputB;

public Slider sliderRGB;
public Slider sliderR;
public Slider sliderG;
public Slider sliderB;
Expand All @@ -31,18 +31,28 @@ public class HSVPicker : MonoBehaviour {
public float cursorY = 0;


public HSVSliderEvent onValueChanged = new HSVSliderEvent();
// 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);

sliderRGB.onValueChanged.AddListener(newValue =>
{
Color color=((Texture2D)hsvSlider.texture).GetPixel((int)(hsvSlider.rectTransform.rect.width*.5f),(int)(hsvSlider.rectTransform.rect.height*(newValue)));
currentColor=color;
if (!dontAssignUpdate)
{
AssignColor(currentColor);
}
hexrgb.ManipulateViaRGB2Hex();
});
sliderR.onValueChanged.AddListener(newValue =>
{
currentColor.r = newValue;
if (dontAssignUpdate == false)
if (!dontAssignUpdate)
{
AssignColor(currentColor);
}
Expand All @@ -52,7 +62,7 @@ void Awake()
sliderG.onValueChanged.AddListener(newValue =>
{
currentColor.g = newValue;
if (dontAssignUpdate == false)
if (!dontAssignUpdate)
{
AssignColor(currentColor);
}
Expand All @@ -62,7 +72,7 @@ void Awake()
sliderB.onValueChanged.AddListener(newValue =>
{
currentColor.b = newValue;
if (dontAssignUpdate == false)
if (!dontAssignUpdate)
{
AssignColor(currentColor);
}
Expand All @@ -75,26 +85,14 @@ void Awake()
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());
dontAssignUpdate=true;
HsvColor hsv = HSVUtil.ConvertRgbToHsv(color);

// Debug.Log(hsv.ToString());

float hOffset = (float)(hsv.H / 360);

Expand All @@ -109,18 +107,19 @@ public void AssignColor(Color color)

MovePointer(hOffset, false);
MoveCursor((float)hsv.S, (float)hsv.V, false);

currentColor = color;
UpdateInputs();
colorImage.color = currentColor;

onValueChanged.Invoke(currentColor);

// onValueChanged.Invoke(currentColor);
//z hsvImage.texture = HSVUtil.GenerateColorTexture((int)hsvImage.rectTransform.rect.width, (int)hsvImage.rectTransform.rect.height, currentColor);
dontAssignUpdate=false;
}


public Color MoveCursor(float posX, float posY, bool updateInputs=true)
{
dontAssignUpdate = updateInputs;
// dontAssignUpdate = updateInputs;
if (posX > 1)
{
posX %= 1;
Expand All @@ -144,9 +143,9 @@ public Color MoveCursor(float posX, float posY, bool updateInputs=true)
if (updateInputs)
{
UpdateInputs();
onValueChanged.Invoke(currentColor);
// onValueChanged.Invoke(currentColor);
}
dontAssignUpdate = false;
// dontAssignUpdate = false;
return currentColor;
}

Expand All @@ -158,20 +157,20 @@ public Color GetColor(float posX, float posY)

public Color MovePointer(float newPos, bool updateInputs = true)
{
dontAssignUpdate = updateInputs;
// dontAssignUpdate = updateInputs;
if (newPos > 1)
{
newPos %= 1f;//hsv
}
pointerPos = newPos;

var mainColor =((Texture2D)hsvSlider.texture).GetPixelBilinear(0, pointerPos);
Color 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;
// Destroy(hsvImage.texture);
// hsvImage.texture = null;

hsvImage.texture = HSVUtil.GenerateColorTexture((int)hsvImage.rectTransform.rect.width, (int)hsvImage.rectTransform.rect.height, mainColor);
}
Expand All @@ -185,30 +184,32 @@ public Color MovePointer(float newPos, bool updateInputs = true)

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);
sliderRGB.value = 1f-pointerPos;

currentColor = GetColor(cursorX, cursorY);
colorImage.color = currentColor;

if (updateInputs)
{
UpdateInputs();
onValueChanged.Invoke(currentColor);
// onValueChanged.Invoke(currentColor);
}
dontAssignUpdate = false;
// dontAssignUpdate = false;
return currentColor;
}

public void UpdateInputs()
{

dontAssignUpdate=true;
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);
dontAssignUpdate=false;

}

void OnDestroy()
Expand Down
Binary file modified Assets/HSVPicker/PickerTestVertical.unity
Binary file not shown.