UI Toolbox logoUI Toolbox
Support

ColorPicker

A ColorPicker is a BaseField<Color>. It has a saturation/brightness square or a color wheel, a hue bar, an alpha bar, a preview, a hex input, RGBA inputs (0 to 255) and HSV inputs (H 0 to 360, S and V 0 to 100). Parts says which of them show.

A Banner color picker with a saturation and brightness square, hue and alpha bars, a color preview with an eyedropper button, hex and RGBA fields, and six preset swatchesA Banner color picker with a saturation and brightness square, hue and alpha bars, a color preview with an eyedropper button, hex and RGBA fields, and six preset swatches

<UIToolbox.ColorPicker
    label="Theme"
    value="#FF6B35FF"
    presets="#FF6B35,#004E89,#009639" />
<UIToolbox.ColorPicker show-alpha="false" recent-count="0" />
<UIToolbox.ColorPicker
    label="Accent"
    compact="true"
    show-eyedropper="false" />
<UIToolbox.ColorPicker shape="Wheel" harmony="Triadic" />
<UIToolbox.ColorPicker parts="Area, Bars" />
Member Description
value The color. Listen with RegisterValueChangedCallback.
Committed An event for an edit that is done: a drag ended, an input was edited, a swatch was clicked or the eyedropper picked. The argument is the value. A value set in code does not raise it.
ShowAlpha (show-alpha) Shows the alpha bar and the A input (true). Off, the value is always opaque.
Presets (presets) The preset swatches. In UXML, a comma-separated list of HTML colors.
RecentCount (recent-count) The most colors the recent row keeps (8). 0 hides the row.
RecentColors The recent colors, newest first.
Hex The value as #RRGGBBAA (#RRGGBB without alpha). Set it with any HTML color, for example #F80 or orange.
Hsv Hue, saturation and brightness, each from 0 to 1.
Compact (compact), IsOpen, Open, Close, Toggle Shows only a swatch in the field. A click on it, Enter or Space opens the full picker in an AnchoredPopup; Escape or a click outside closes it.
ShowEyedropper (show-eyedropper), StartEyedropper, CancelEyedropper, IsPicking, PickPreview The Pick button (on by default), and the eyedropper: the next left click on the panel sets the value to the color on the screen under the pointer. See below.
EyedropperIcon (eyedropper-icon) The Pick button shows a dropper icon (on by default), or the text "Pick". The tooltip names the button in both.
Parts (parts) The parts that show, a set of ColorPickerParts (All). See Parts.
Shape (shape) A ColorPickerShape: Square (default): the square and a hue bar. Wheel: a color wheel and a brightness bar.
Harmony (harmony) The rule of HarmonyColors: None (default), Complementary, Analogous, Triadic, SplitComplementary, Tetradic.
HarmonyColors The value and the colors that go with it by Harmony; the value is the first.
KEY_STEP The step of an arrow key or a gamepad move in the area or a bar: 0.02 of the range.

Parts

Parts is a set of flags. A part that is not in the set does not show and takes no space. In UXML, write the names with commas; in C#, join them with |.

Part Shows
Area The square, or the wheel.
Bars The bars next to the area: hue (brightness next to the wheel) and alpha.
Preview The preview of the color, with the Pick button.
Hex The hex input.
Rgb The R, G, B and A inputs.
Hsv The H, S and V inputs.
Presets The row of preset swatches.
Recent The row of recent colors.
Harmonies The row of harmony swatches.
<!-- Only the square and the hue bar -->
<UIToolbox.ColorPicker parts="Area, Bars" show-alpha="false" />
<!-- A swatch in the field; its popup has only the hex input -->
<UIToolbox.ColorPicker parts="Hex" compact="true" />
picker.Parts =
    ColorPickerParts.Area | ColorPickerParts.Bars | ColorPickerParts.Hex;
picker.Parts &= ~ColorPickerParts.Recent;   // takes one part away

Wheel and harmonies

With shape="Wheel", the area is a disc: the hue is on the angle (red at the right, then against the clock) and the saturation is on the radius, with white in the center. The bar next to it is the brightness. A drag, the keys and a gamepad work as in the square; a drag that leaves the disc stays on its edge.

Harmony gives the colors that go with the value. Each has the saturation, the brightness and the alpha of the value, and a hue at a fixed angle from it:

Harmony Hues, in degrees from the value
Complementary 180
Analogous 30, 330
Triadic 120, 240
SplitComplementary 150, 210
Tetradic 90, 180, 270

The wheel shows a small dot at each harmony color, and the Harmonies part is a row with a swatch for the value and each harmony color. A click on a swatch, or Enter on it, makes that color the value. The row also shows with the square.

<UIToolbox.ColorPicker
    name="paint"
    shape="Wheel"
    harmony="Triadic"
    value="#E4572E" />
var picker = root.Q<ColorPicker>("paint");
picker.RegisterValueChangedCallback(evt =>
{
    // The value first, then the harmony colors
    IReadOnlyList<Color> colors = picker.HarmonyColors;
    body.style.backgroundColor = colors[0];
    trim.style.backgroundColor = colors[1];
    glow.style.backgroundColor = colors[2];
});

// The same colors without a picker
// red, green, blue
Color[] triad = ColorHarmonies.Get(Color.red, ColorHarmony.Triadic);
// 0.5, 0
float[] hues = ColorHarmonies.Hues(0.5f, ColorHarmony.Complementary);

Cost of a drag

A drag in the area or a bar refreshes the picker one time for each pointer move. The hex input, the RGBA and HSV inputs, the harmony row and the markers of the harmonies are left alone while their part does not show. The tooltips of the harmony swatches wait for the end of the drag. With the hex and number inputs hidden, a drag move makes no garbage. The eyedropper reads one pixel of the screen for each pointer move.

Sizes

The picker takes the width that the layout or its style gives it. With all parts it needs 358 px (338 px without the alpha bar) for the area and the inputs side by side. In a narrower picker the inputs go under the area, and the area fills the width up to 220 px and stays square. There are no size classes: set a width.

With a label, the label takes 150 px beside the picker. In a host that is too narrow for the label and the picker side by side, the label goes above the picker, so the area and the inputs keep the whole width. In a row host the picker shrinks with the space that its siblings leave, down to 160 px. A compact picker is as wide as its swatch and does not have this floor. Its label stays beside the swatch, also while the popup is open, because the full picker is in the popup.

<UIToolbox.ColorPicker style="width: 260px;" />

For a bigger area in a wide picker, set both sizes of the area in USS. The bars follow its height.

.big-picker .tb-color-picker__sv,
.big-picker .tb-color-picker__wheel {
    width: 220px;
    height: 220px;
}

Styling

The square, the wheel, the bars and the preview are drawn in code; the markers of the wheel and the bars, the inputs and the swatches are elements.

Class Element
tb-color-picker The field.
tb-color-picker__picker The row with the areas and the side column.
tb-color-picker__areas The row with the square or the wheel, and the bars.
tb-color-picker__sv The saturation/brightness square (140 px), an XYPad. Its pad has tb-xypad__pad, and its ring of the value is drawn with the --tb-xypad-handle-border and --tb-xypad-handle-outline colors.
tb-color-picker__wheel, tb-color-picker__wheel-rim The wheel (140 px), and the ring over its edge.
tb-color-picker__hue, tb-color-picker__brightness, tb-color-picker__alpha The bars (14 px wide). They take the height of the area.
tb-color-picker__marker The ring on the wheel. Its child is the dark inner ring. With the focus on the wheel, it has the accent color.
tb-color-picker__bar-marker The line on a bar. With the focus on the bar, it has the accent color.
tb-color-picker__harmony-marker A dot on the wheel at a harmony color (8 px).
tb-color-picker__harmonies The row of harmony swatches. Each swatch has tb-color-picker__swatch.
tb-color-picker--stacked On the body while the picker is too narrow for the area and the inputs side by side.
tb-color-picker--label-above On the picker while the host is too narrow for the label beside the body. The label is above the body. A compact picker never has it.
tb-color-picker--compact On the picker while Compact is on. The picker is as wide as its swatch; without it the picker is at least 160 px wide.
tb-color-picker__side The column with the preview and the inputs.
tb-color-picker__preview The current color over a checker.
tb-color-picker__hex The hex TextField.
tb-color-picker__channels, tb-color-picker__channel The RGBA and HSV columns and each IntegerField.
tb-color-picker__swatches, tb-color-picker__swatch The preset and recent rows and each swatch.
tb-color-picker__body The picker and the swatch rows. In compact mode it moves into the popup.
tb-color-picker__eyedropper The Pick button next to the preview. It takes the accent color while the picker is picking.
tb-color-picker--picking On the field and on its body while the eyedropper waits for a click.
tb-color-picker__compact-swatch The swatch of compact mode (40 x 20 px).
tb-color-picker__popup The popup of compact mode (8 px padding).