using System; namespace Prism.Glass { /// /// The state behind a color picker. Every part of the picker edits Current, so they can't disagree. Current is an /// Hsv, which keeps the hue of a gray and the hue and saturation of a black that a Color can't. /// public class ColorPickerModel { private Hsv current; /// /// Starts editing a color, which stays as for comparing against. /// public ColorPickerModel(Color original) { Original = original; current = Hsv.FromColor(original); } /// /// Gets the color the picker started with. /// public Color Original { get; private set; } /// /// Raised with the new color each time Current changes, and each time the model is restarted. /// public event Action Changed; /// /// Gets or sets the color being edited. Setting the color it already is raises nothing. /// public Hsv Current { get => current; set { if (value == current) { return; } current = value; Changed?.Invoke(current); } } /// /// Starts editing another color, which becomes both and . Raises /// even when the color is the one being edited, since the original may differ. /// public void Restart(Hsv color) { Original = color.ToColor(); current = color; Changed?.Invoke(current); } /// /// Gets the color being edited, rounded to bytes. /// public Color CurrentColor => current.ToColor(); /// /// Gets the color as hex text, with the alpha only when the color isn't opaque. /// public string Hex => HexColor.Format(CurrentColor, includeAlpha: CurrentColor.Alpha != byte.MaxValue); /// /// Gets the gray that, tinting a disc drawn at full value, gives the disc at the current value. /// public Color DiscTint => new Hsv(0, 0, current.Value).ToColor(); /// /// Gets the color that, tinting a ramp from black to white, gives the value strip at the current hue and /// saturation. /// public Color ValueStripTint => new Hsv(current.Hue, current.Saturation, 1).ToColor(); /// /// Gets the opaque color that tints the alpha strip. /// public Color AlphaStripTint => new Hsv(current.Hue, current.Saturation, current.Value).ToColor(); private static double Clamp(double fraction) { return Math.Max(0, Math.Min(1, fraction)); } /// /// Moves to the hue and saturation of a point on the disc, keeping the value and alpha. The point is in the /// unit-radius coordinates HueDisc uses. /// public void PickDisc(double x, double y) { Current = HueDisc.Pick(x, y, current); } /// /// Moves to this hue in turns, which wraps around, keeping the saturation, value and alpha. /// public void PickHue(double turns) { Current = new Hsv(turns, current.Saturation, current.Value, current.Alpha); } /// /// Moves to this saturation, from 0 to 1 and held to that range, keeping the hue, value and alpha. /// public void PickSaturation(double fraction) { Current = new Hsv(current.Hue, Clamp(fraction), current.Value, current.Alpha); } /// /// Moves to this value, from 0 to 1 and held to that range, keeping the hue, saturation and alpha. /// public void PickValue(double fraction) { Current = new Hsv(current.Hue, current.Saturation, Clamp(fraction), current.Alpha); } /// /// Moves to this alpha, from 0 to 1 and held to that range, keeping the hue, saturation and value. /// public void PickAlpha(double fraction) { Current = new Hsv(current.Hue, current.Saturation, current.Value, Clamp(fraction)); } /// /// Moves to a color. A gray keeps the current hue, and a black keeps the current hue and saturation. The /// color it already is changes nothing, so retyping it doesn't lose what rounding to bytes can't hold. /// public void SetColor(Color color) { if (color == CurrentColor) { return; } Hsv picked = Hsv.FromColor(color); double hue = picked.Hue; double saturation = picked.Saturation; if (picked.Value == 0) { hue = current.Hue; saturation = current.Saturation; } else if (picked.Saturation == 0) { hue = current.Hue; } Current = new Hsv(hue, saturation, picked.Value, picked.Alpha); } /// /// Moves to the color hex text names, as reads it. Returns false, and changes /// nothing, if the text isn't a color. /// public bool TryParseHex(string text) { if (!HexColor.TryParse(text, out Color color)) { return false; } SetColor(color); return true; } } }