Skip to content

Commit

Permalink
Add hex textbox to the color picker
Browse files Browse the repository at this point in the history
  • Loading branch information
ManlyMarco committed Dec 30, 2023
1 parent 29c1f40 commit d6f3dba
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions Shared/BetterColorPicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,44 @@ public static void AddPickerButton(PickerSliderInput __instance)
b.onClick.AddListener(() => Capturing = !Capturing);

_setColor = color => __instance.color = color;

// Spawn a HEX textbox next to the HSV/RGB toggles
var textboxCopy = GameObject.Instantiate(__instance.inputR.gameObject, __instance.transform.Find("ColorMode"), false);
textboxCopy.name = "HexTextbox";

var textboxRt = textboxCopy.GetComponent<RectTransform>();
textboxRt.localPosition = new Vector3(195, 4, 0);
textboxRt.sizeDelta = new Vector2(150, 28);

var textbox = textboxCopy.GetComponent<TMP_InputField>();
textbox.characterLimit = 9;
textbox.characterValidation = TMP_InputField.CharacterValidation.None;
textbox.contentType = TMP_InputField.ContentType.Standard;
textbox.onValueChanged.ActuallyRemoveAllListeners();
textbox.onValueChanged.AddListener(OnTextboxTextChanged);

__instance.updateColorAction += UpdateTextboxHexText;

// Probably unnecessary, but just in case
UpdateTextboxHexText(__instance.color);

void OnTextboxTextChanged(string hexStr)
{
if (ColorUtility.TryParseHtmlString(hexStr, out var resultColor))
{
if (resultColor != __instance.color) _setColor(resultColor);

textbox.targetGraphic.color = Color.white;
}
else
{
textbox.targetGraphic.color = Color.red;
}
}
void UpdateTextboxHexText(Color newColor)
{
textbox.text = "#" + ColorUtility.ToHtmlStringRGBA(newColor);
}
}

/// <summary>
Expand Down

0 comments on commit d6f3dba

Please sign in to comment.