Skip to main content

ColorPicker

color-picker

See the ColorPickerDemo.cs.

using Gridrand.Contracts;
using Gridrand.RimGui.Manual;
using System;

namespace Gridrand.RimGui.Extensions.Manual
{
/// <summary>
/// Demonstrates the color picker.
/// </summary>
class ColorPickerDemo : ManualBase, IManual
{
HsvColor hsv = new(240d, 0.8d, 0.8d, 0.8d);

public ColorPickerDemo(ManualBaseResource p) : base(p)
{
}

public void Draw()
{
using (ExStyle.RectInnerPointHandle.Colors.Begin(new Color32(220, 220, 220, 255)))
using (ExStyle.VerticalBarHandle.Colors.Begin(new Color32(220, 220, 220, 255)))
using (Style.SpacingXs.Begin(5f))
using (var rects = LayoutBuilder
.Fixed(200f)
.Fixed(20f)
.Fixed(20f)
.Fit(1f)
.BuildAllocatedHorizontal(height: 200f))
{
var isHandledSVPanel = DrawSVPanel(rects.R0);
DrawHue(rects.R1);
DrawAlpha(rects.R2);

// color preview
Gui.NextRect(rects.R3).ColorPreview(hsv.ToRgb());

var color = (Color32)hsv.ToRgb();
var r = color.r;
var g = color.g;
var b = color.b;
var a = color.a;
var changedRgba = false;
// Allow direct editing of R, G, B, A color components
if (Gui.LabelNext("R").SliderInputNumeric<byte>(ref r, 0, 255)) changedRgba = true;
if (Gui.LabelNext("G").SliderInputNumeric<byte>(ref g, 0, 255)) changedRgba = true;
if (Gui.LabelNext("B").SliderInputNumeric<byte>(ref b, 0, 255)) changedRgba = true;
if (Gui.LabelNext("A").SliderInputNumeric<byte>(ref a, 0, 255)) changedRgba = true;
if (changedRgba)
hsv = new(new Color32(r, g, b, a));

Gui.SmallSpacing();

var h = hsv.H;
var s = hsv.S;
var v = hsv.V;
var changedHsv = false;
// Allow direct editing of R, G, B, A color components
if (Gui.LabelNext("H").SliderInputNumeric(ref h, 0d, HsvColor.HMax)) changedHsv = true;
if (Gui.LabelNext("S").SliderInputNumeric(ref s, 0d, 1d)) changedHsv = true;
if (Gui.LabelNext("V").SliderInputNumeric(ref v, 0d, 1d)) changedHsv = true;
if (changedHsv)
hsv = new HsvColor(h, s, v, hsv.A);
}
}

bool DrawSVPanel(Rect svPanelRect)
{
// Draw SV panel and handle user interaction
Gui.NextRect(svPanelRect).CustomDelegate(hsv.H, (shaper, widget) => shaper.AddSVPanel(widget.Rect, widget.State));

// Update HSV values even when any part of the SV panel is clicked
var v01 = Gui.SnapRectInnerPointHandle(svPanelRect, new Vector2((float)hsv.S, (float)hsv.V));
hsv = new HsvColor(hsv.H, v01.x, v01.y, hsv.A);
return Ctx.IsLastPressed();
}

// Draw Hue bar and handle vertical slider input
void DrawHue(Rect barHandleRect)
{
// Draw Hue bar and handle vertical slider input
Gui.NextRect(barHandleRect).CustomDelegate((shaper, widget) => shaper.AddHueBar(widget.Rect));
var hue01 = Gui.SnapVerticalBarHandle(barHandleRect, (float)hsv.H01);
hsv = new HsvColor(Math.Min(hue01 * HsvColor.HMax, HsvColor.HMax), hsv.S, hsv.V, hsv.A);
}

// Show RGB preview with alpha gradient
void DrawAlpha(Rect rect)
{
// Show RGB preview with alpha gradient
Gui.NextRect(rect).ColorPreview(new());
var rgb0 = hsv.ToRgb();
var bottom = rgb0.ChangeAlpha(0);
var top = rgb0.ChangeAlpha(255);
Gui.NextRect(rect).Gradient(bottom, bottom, top, top);

// Handle alpha channel adjustment
var alpha = Gui.SnapVerticalBarHandle(rect, (float)hsv.A);
hsv = new HsvColor(hsv.H, hsv.S, hsv.V, alpha);
}

}
}