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.
<UIToolbox.ColorPicker label="Theme" value="#FF6B35FF" presets="#FF6B35,#004E89,#009639" />
<UIToolbox.ColorPicker show-alpha="false" recent-count="0" />
<UIToolbox.ColorPicker label="Accent" compact="true" 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. |
Eyedropper (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) |
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. |
- A drag in the square, the wheel or a bar changes the value on each move, as a
Sliderdoes.Committedcomes one time, at the release: use it for work that must not run on each move, such as a save. - The area and each bar take the focus; the marker has the accent color then. The arrow keys, or the stick or D-pad of a gamepad, move the value by
KEY_STEP.Committedcomes one time, when the focus leaves. A move past the edge of the area or the bar moves the focus on, so a gamepad can leave it. - A color goes to the recent row when a drag ends, an input is edited, or a swatch is clicked. A color already in the row moves to the front.
- A swatch takes the focus. Enter, Space or the Submit button of a gamepad picks its color, as a click does.
- The picker keeps the hue and saturation apart from the value. A gray keeps its hue and black keeps its saturation, so a move back to a bright color does not reset them.
- The hex input also takes hex without
#. A text that is not a color is ignored, and the input shows the value again. - The inputs commit on Enter or when they lose focus. Their own change events stop in the picker; only
ChangeEvent<Color>leaves it. - The eyedropper reads the rendered frame with
ElementImage, so it works in Play mode on runtime panels only;StartEyedropperreturns false elsewhere. The pick keeps the alpha of the value and adds the color to the recent row. The click goes to nothing else. Another mouse button or Escape cancels. - While the eyedropper waits for the click, the preview shows the color under the pointer and follows each move.
PickPreviewgives that color, or null when the picker is not picking. The value changes only on the click. - In compact mode, a pick outside the popup also closes the popup.
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
ShowAlpha,EyedropperandCompactwork as before, together withParts: the alpha bar needsBarsandShowAlpha, the A input needsRgbandShowAlpha, and the Pick button needsPreviewandEyedropper.- The preset row also needs
Presetswith a color, the recent row a recent color, and the harmony row aHarmony. Partschanges only what shows. The picker still records the recent colors without theRecentpart;RecentCount0 turns that off.
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
Color[] triad = ColorHarmonies.Get(Color.red, ColorHarmony.Triadic); // red, green, blue
float[] hues = ColorHarmonies.Hues(0.5f, ColorHarmony.Complementary); // 0.5, 0
HarmonyColorschanges with the value and withHarmonyonly, so there is no event of its own: read it in the change event of the picker, and after you setHarmony.- The picker uses its kept hue, so the harmony colors of a gray keep their places on the wheel.
ColorHarmonies.Getsees only the color: for a gray, each harmony color is the same gray.
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 and stays square. There are no size classes: set a width.
<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, inputs and 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). |
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 in the square and on the wheel. Its child is the dark inner ring. With the focus on the area, 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__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). |