using System;
using UnityEngine;
using UnityEngine.UIElements;
///
/// class to handle the oklch colour picker ui
///
public class OklchColourPickerUI : MonoBehaviour
{
///
/// perceptual lightness value of the colour (0-100)
///
public double lightness;
///
/// chroma value of the colour (0-0.5)
///
public double chroma;
///
/// hue value of the colour (0-360)
///
public double hue;
///
/// slider for the chroma value
///
protected Slider ChromaSlider;
///
/// slider for the hue value
///
protected Slider HueSlider;
///
/// slider for the lightness value
///
protected Slider LightnessSlider;
///
/// visual element for the response colour preview
///
protected VisualElement ResponseColour;
///
/// modify state of initialised variables
///
private void Start()
{
LightnessSlider.value = 74.61f;
ChromaSlider.value = 0.0868f;
HueSlider.value = 335.72f;
}
///
/// initialise the ui elements and register change event callbacks functions
///
public void OnEnable()
{
var ui = GetComponent().rootVisualElement;
LightnessSlider = ui.Q("ResponseLightnessSlider");
LightnessSlider.RegisterCallback>(OnLightnessChange);
ChromaSlider = ui.Q("ResponseChromaSlider");
ChromaSlider.RegisterCallback>(OnChromaChange);
HueSlider = ui.Q("ResponseHueSlider");
HueSlider.RegisterCallback>(OnHueChange);
ResponseColour = ui.Q("ResponseColour");
}
///
/// handle lightness slider change
///
/// change event
protected void OnLightnessChange(ChangeEvent evt)
{
lightness = Math.Clamp(evt.newValue, 0d, 100d);
ResponseColour.style.backgroundColor = Colorimetry.RawLchToColor(lightness, chroma, hue);
}
///
/// handle chroma slider change
///
/// change event
protected void OnChromaChange(ChangeEvent evt)
{
chroma = Math.Clamp(evt.newValue, 0d, 0.5d);
ResponseColour.style.backgroundColor = Colorimetry.RawLchToColor(lightness, chroma, hue);
}
///
/// handle hue slider change
///
/// change event
protected void OnHueChange(ChangeEvent evt)
{
hue = Math.Clamp(evt.newValue, 0d, 360d);
ResponseColour.style.backgroundColor = Colorimetry.RawLchToColor(lightness, chroma, hue);
}
}