private void OnGUI() { PrepareStyles(); UpdatePickerCoordinates(); EditorGUI.BeginChangeCheck(); EditorGUI.BeginChangeCheck(); colourModel = (Colour.Model)EditorGUILayout.Popup("Colour Space", (int)colourModel, System.Enum.GetNames(typeof(Colour.Model))); sliderAxis = (SliderAxis)EditorGUILayout.Popup("Slider Axis", (int)sliderAxis, System.Enum.GetNames(typeof(SliderAxis))); workingSpace = (Colour.RGBSpace)EditorGUILayout.Popup("Working Colour Space", (int)workingSpace, System.Enum.GetNames(typeof(Colour.RGBSpace))); if (EditorGUI.EndChangeCheck()) { Double3 newPickFrac = GetPickerCoords(currentColourXYZ, colourModel, workingSpace); Double3 pick = newPickFrac * maxValue; pickerCoords = new Int4(Mathf.RoundToInt((float)pick.x), Mathf.RoundToInt((float)pick.y), Mathf.RoundToInt((float)pick.z), pickerCoords.w); } limitingSpace = (Colour.RGBSpace)EditorGUILayout.Popup("Limiting Colour Space", (int)limitingSpace, System.Enum.GetNames(typeof(Colour.RGBSpace))); if (EditorGUI.EndChangeCheck()) { pickerOutOfDate = sliderOutOfDate = true; } EditorGUILayout.Space(); EditorGUILayout.ColorField(currentColourRGB); minSize = maxSize = new Vector2(textureSize + 60, textureSize + 280); UpdateTextures(); GUI.DrawTexture(pickerRect, pickerTexture, ScaleMode.StretchToFill); GUI.DrawTexture(sliderRect, sliderTexture, ScaleMode.StretchToFill); for (int i = 0; i < altSliders; ++i) GUI.DrawTexture(sliderGroupRect[i], sliderGroupTexture[i], ScaleMode.StretchToFill); int circleSize = 8; int halfCircleSize = 4; if (Event.current.type == EventType.Repaint) { pickerBox.Draw(pickerRect, GUIContent.none, 0); pickerBox.Draw(sliderRect, GUIContent.none, 0); pickerCircle.Draw(new Rect(pickerCoords.x + 10 - halfCircleSize, ((textureSize - 1) - (pickerCoords.y)) + 120 - halfCircleSize, circleSize, circleSize), GUIContent.none, 0); sliderVertArrow.Draw(new Rect(textureSize + 18, (textureSize - 1) - (pickerCoords.z) + 117, 34, 8), GUIContent.none, 0); for (int i = 0; i < altSliders; ++i) { sliderHorizArrow.Draw(new Rect(pickerCoords[i] + 7, sliderGroupRect[i].yMin - 4, 16, 28), GUIContent.none, 0); pickerBox.Draw(sliderGroupRect[i], GUIContent.none, 0); } } Double4 pickerPosFraction = pickerCoords / maxValue; if (pickerOutOfDate == true || sliderOutOfDate == true) { Double4 arrangement = GetPickerArrangement(sliderAxis, pickerPosFraction); Colour pickedColour = GetWorkingSpaceColour(colourModel, workingSpace, arrangement); if (Colour.IsInsideGamut(pickedColour, limitingSpace)) { currentColourXYZ = pickedColour; currentColourRGB = (Color)Colour.To_RGB(pickedColour, workingSpace); } else { currentColourXYZ = new Colour(Double3.zero); currentColourRGB = Color.black; } } currentColourRGB.a = (float)pickerPosFraction.w; Repaint(); pickerOutOfDate = sliderOutOfDate = false; }
private void UpdatePickerCoordinates() { Int2 mousePosition = new Int2(); if (Event.current.type == EventType.mouseDown) { if (pickerRect.Contains(Event.current.mousePosition)) { sliderOutOfDate = true; selectionMode = SelectionMode.Picker; } if (sliderRect.Contains(Event.current.mousePosition)) { pickerOutOfDate = true; selectionMode = SelectionMode.Slider; } if (sliderRect.Contains(Event.current.mousePosition)) { pickerOutOfDate = true; selectionMode = SelectionMode.Slider; } for (int i = 0; i < altSliders; ++i) { if (sliderGroupRect[i].Contains(Event.current.mousePosition)) { if (i < 3) { sliderOutOfDate = true; if (i == (int)sliderAxis) pickerOutOfDate = true; } selectionMode = (SelectionMode)i; } } } if ((Event.current.type == EventType.mouseDown || Event.current.type == EventType.mouseDrag) && selectionMode != SelectionMode.None) { mousePosition = (Int2)Event.current.mousePosition; if (selectionMode == SelectionMode.Picker) { mousePosition -= new Int2((int)pickerRect.xMin, (int)pickerRect.yMin); pickerCoords.x = mousePosition.x; pickerCoords.y = (textureSize - 1) - mousePosition.y; sliderOutOfDate = true; } else if (selectionMode == SelectionMode.Slider) { mousePosition -= new Int2((int)sliderRect.xMin, (int)sliderRect.yMin); pickerCoords.z = textureSize - mousePosition.y; pickerOutOfDate = true; } else { int sliderIndex = ((int)selectionMode); mousePosition -= new Int2((int)sliderGroupRect[sliderIndex].xMin, (int)sliderGroupRect[sliderIndex].yMin); pickerCoords[sliderIndex] = mousePosition.x; if (sliderIndex < 3) { sliderOutOfDate = true; if (sliderIndex == (int)sliderAxis) pickerOutOfDate = true; } } pickerCoords = MathfExt.Clamp(pickerCoords, 0, textureSize - 1); } if (Event.current.type == EventType.mouseUp) selectionMode = SelectionMode.None; }
public static Int4 Clamp(Int4 v, int min, int max) { return new Int4(Mathf.Clamp(v.x, min, max), Mathf.Clamp(v.y, min, max), Mathf.Clamp(v.z, min, max), Mathf.Clamp(v.w, min, max)); }