Color picker
Any color, chosen on a field of lightness and chroma, a hue and an opacity, or typed.
On this page
A color picker chooses any color: lightness and chroma on a field under one thumb, the hue and the opacity on two sliders, and the color itself as text in hex, rgb() or oklch(). It chooses in OKLCH, the space every color of the theme is written in, so a step of lightness looks like the same step at every hue. The field is a picture set into the surface; the thumb is the color in a pearl ring; the tracks are native range inputs. The color-picker behavior keeps the parts in step.
Color picker
<div class="sheet p-md"> <div class="color-picker" style="--color-picker-lightness: .62; --color-picker-chroma: .19; --color-picker-hue: 260"> <div class="color-picker-area"> <div class="color-picker-thumb" role="slider" tabindex="0" aria-label="Lightness and chroma" aria-valuemin="0" aria-valuemax="100" aria-valuenow="62" aria-valuetext="Lightness 62%, chroma 0.19"></div> </div> <span class="swatch swatch-lg color-picker-swatch"></span> <input type="range" class="slider color-picker-hue" min="0" max="360" value="260" aria-label="Hue"> <input type="range" class="slider color-picker-alpha" min="0" max="100" value="100" aria-label="Opacity"> <div class="color-picker-fields"> <select class="select select-sm color-picker-format" aria-label="Format"> <option value="hex">Hex</option> <option value="rgb">RGB</option> <option value="oklch" selected>OKLCH</option> </select> <input class="input input-sm color-picker-text" name="accent" value="oklch(62% 0.19 260)" aria-label="Accent color" spellcheck="false" autocomplete="off"> </div> </div></div><script type="module"> import { attach } from '@master/ui/behaviors' attach()</script>import { useEffect, useRef } from 'react'import { attach } from '@master/ui/behaviors'export function Example() { const root = useRef<HTMLDivElement>(null) useEffect(() => attach(root.current!), []) return ( <div ref={root} className="sheet p-md"> <div className="color-picker" style={{ '--color-picker-lightness': '.62', '--color-picker-chroma': '.19', '--color-picker-hue': '260' } as React.CSSProperties}> <div className="color-picker-area"> <div className="color-picker-thumb" role="slider" tabIndex={0} aria-label="Lightness and chroma" aria-valuemin={0} aria-valuemax={100} aria-valuenow={62} aria-valuetext="Lightness 62%, chroma 0.19"></div> </div> <span className="swatch swatch-lg color-picker-swatch"></span> <input type="range" className="slider color-picker-hue" min="0" max="360" defaultValue="260" aria-label="Hue" /> <input type="range" className="slider color-picker-alpha" min="0" max="100" defaultValue="100" aria-label="Opacity" /> <div className="color-picker-fields"> <select className="select select-sm color-picker-format" aria-label="Format" defaultValue="oklch"> <option value="hex">Hex</option> <option value="rgb">RGB</option> <option value="oklch">OKLCH</option> </select> <input className="input input-sm color-picker-text" name="accent" defaultValue="oklch(62% 0.19 260)" aria-label="Accent color" spellCheck="false" autoComplete="off" /> </div> </div> </div> )}<script> import { attach } from '@master/ui/behaviors'</script><div class="sheet p-md" {@attach attach}> <div class="color-picker" style="--color-picker-lightness: .62; --color-picker-chroma: .19; --color-picker-hue: 260"> <div class="color-picker-area"> <div class="color-picker-thumb" role="slider" tabindex="0" aria-label="Lightness and chroma" aria-valuemin="0" aria-valuemax="100" aria-valuenow="62" aria-valuetext="Lightness 62%, chroma 0.19"></div> </div> <span class="swatch swatch-lg color-picker-swatch"></span> <input type="range" class="slider color-picker-hue" min="0" max="360" value="260" aria-label="Hue"> <input type="range" class="slider color-picker-alpha" min="0" max="100" value="100" aria-label="Opacity"> <div class="color-picker-fields"> <select class="select select-sm color-picker-format" aria-label="Format"> <option value="hex">Hex</option> <option value="rgb">RGB</option> <option value="oklch" selected>OKLCH</option> </select> <input class="input input-sm color-picker-text" name="accent" value="oklch(62% 0.19 260)" aria-label="Accent color" spellcheck="false" autocomplete="off"> </div> </div></div><script setup>import { onMounted, onUnmounted, ref } from 'vue'import { attach } from '@master/ui/behaviors'const root = ref(null)let detachonMounted(() => { detach = attach(root.value) })onUnmounted(() => detach?.())</script><template> <div ref="root" class="sheet p-md"> <div class="color-picker" style="--color-picker-lightness: .62; --color-picker-chroma: .19; --color-picker-hue: 260"> <div class="color-picker-area"> <div class="color-picker-thumb" role="slider" tabindex="0" aria-label="Lightness and chroma" aria-valuemin="0" aria-valuemax="100" aria-valuenow="62" aria-valuetext="Lightness 62%, chroma 0.19"></div> </div> <span class="swatch swatch-lg color-picker-swatch"></span> <input type="range" class="slider color-picker-hue" min="0" max="360" value="260" aria-label="Hue"> <input type="range" class="slider color-picker-alpha" min="0" max="100" value="100" aria-label="Opacity"> <div class="color-picker-fields"> <select class="select select-sm color-picker-format" aria-label="Format"> <option value="hex">Hex</option> <option value="rgb">RGB</option> <option value="oklch" selected>OKLCH</option> </select> <input class="input input-sm color-picker-text" name="accent" value="oklch(62% 0.19 260)" aria-label="Accent color" spellcheck="false" autocomplete="off"> </div> </div> </div></template>#Opened from a button
Most of the time a color is one setting among others, and the picker opens from a button that shows it. Put the button and a popover with color-picker-popover inside the root: the popover becomes the column, and a swatch with color-picker-swatch in the button shows the color while the picker is closed, since both stand in the root that holds it. The browser opens, places and dismisses the popover; the behavior is the same.
Opened from a button
<div class="color-picker" style="--color-picker-lightness: .6256; --color-picker-chroma: .1933; --color-picker-hue: 23.03"> <button type="button" class="btn" popovertarget="brand-picker"> <span class="swatch swatch-sm color-picker-swatch"></span> Brand color </button> <div id="brand-picker" popover class="popover color-picker-popover"> <div class="color-picker-area"> <div class="color-picker-thumb" role="slider" tabindex="0" aria-label="Lightness and chroma" aria-valuemin="0" aria-valuemax="100" aria-valuenow="62.6" aria-valuetext="Lightness 62.6%, chroma 0.193"></div> </div> <span class="swatch swatch-lg color-picker-swatch"></span> <input type="range" class="slider color-picker-hue" min="0" max="360" value="23" aria-label="Hue"> <input type="range" class="slider color-picker-alpha" min="0" max="100" value="100" aria-label="Opacity"> <div class="color-picker-fields"> <select class="select select-sm color-picker-format" aria-label="Format"> <option value="hex" selected>Hex</option> <option value="rgb">RGB</option> <option value="oklch">OKLCH</option> </select> <input class="input input-sm color-picker-text" name="brand" value="#e5484d" aria-label="Brand color" spellcheck="false" autocomplete="off"> </div> </div></div><script type="module"> import { attach } from '@master/ui/behaviors' attach()</script>import { useEffect, useRef } from 'react'import { attach } from '@master/ui/behaviors'export function Example() { const root = useRef<HTMLDivElement>(null) useEffect(() => attach(root.current!), []) return ( <div ref={root} className="color-picker" style={{ '--color-picker-lightness': '.6256', '--color-picker-chroma': '.1933', '--color-picker-hue': '23.03' } as React.CSSProperties}> <button type="button" className="btn" popoverTarget="brand-picker"> <span className="swatch swatch-sm color-picker-swatch"></span> Brand color </button> <div id="brand-picker" popover="" className="popover color-picker-popover"> <div className="color-picker-area"> <div className="color-picker-thumb" role="slider" tabIndex={0} aria-label="Lightness and chroma" aria-valuemin={0} aria-valuemax={100} aria-valuenow={62.6} aria-valuetext="Lightness 62.6%, chroma 0.193"></div> </div> <span className="swatch swatch-lg color-picker-swatch"></span> <input type="range" className="slider color-picker-hue" min="0" max="360" defaultValue="23" aria-label="Hue" /> <input type="range" className="slider color-picker-alpha" min="0" max="100" defaultValue="100" aria-label="Opacity" /> <div className="color-picker-fields"> <select className="select select-sm color-picker-format" aria-label="Format" defaultValue="hex"> <option value="hex">Hex</option> <option value="rgb">RGB</option> <option value="oklch">OKLCH</option> </select> <input className="input input-sm color-picker-text" name="brand" defaultValue="#e5484d" aria-label="Brand color" spellCheck="false" autoComplete="off" /> </div> </div> </div> )}<script> import { attach } from '@master/ui/behaviors'</script><div class="color-picker" style="--color-picker-lightness: .6256; --color-picker-chroma: .1933; --color-picker-hue: 23.03" {@attach attach}> <button type="button" class="btn" popovertarget="brand-picker"> <span class="swatch swatch-sm color-picker-swatch"></span> Brand color </button> <div id="brand-picker" popover class="popover color-picker-popover"> <div class="color-picker-area"> <div class="color-picker-thumb" role="slider" tabindex="0" aria-label="Lightness and chroma" aria-valuemin="0" aria-valuemax="100" aria-valuenow="62.6" aria-valuetext="Lightness 62.6%, chroma 0.193"></div> </div> <span class="swatch swatch-lg color-picker-swatch"></span> <input type="range" class="slider color-picker-hue" min="0" max="360" value="23" aria-label="Hue"> <input type="range" class="slider color-picker-alpha" min="0" max="100" value="100" aria-label="Opacity"> <div class="color-picker-fields"> <select class="select select-sm color-picker-format" aria-label="Format"> <option value="hex" selected>Hex</option> <option value="rgb">RGB</option> <option value="oklch">OKLCH</option> </select> <input class="input input-sm color-picker-text" name="brand" value="#e5484d" aria-label="Brand color" spellcheck="false" autocomplete="off"> </div> </div></div><script setup>import { onMounted, onUnmounted, ref } from 'vue'import { attach } from '@master/ui/behaviors'const root = ref(null)let detachonMounted(() => { detach = attach(root.value) })onUnmounted(() => detach?.())</script><template> <div ref="root" class="color-picker" style="--color-picker-lightness: .6256; --color-picker-chroma: .1933; --color-picker-hue: 23.03"> <button type="button" class="btn" popovertarget="brand-picker"> <span class="swatch swatch-sm color-picker-swatch"></span> Brand color </button> <div id="brand-picker" popover class="popover color-picker-popover"> <div class="color-picker-area"> <div class="color-picker-thumb" role="slider" tabindex="0" aria-label="Lightness and chroma" aria-valuemin="0" aria-valuemax="100" aria-valuenow="62.6" aria-valuetext="Lightness 62.6%, chroma 0.193"></div> </div> <span class="swatch swatch-lg color-picker-swatch"></span> <input type="range" class="slider color-picker-hue" min="0" max="360" value="23" aria-label="Hue"> <input type="range" class="slider color-picker-alpha" min="0" max="100" value="100" aria-label="Opacity"> <div class="color-picker-fields"> <select class="select select-sm color-picker-format" aria-label="Format"> <option value="hex" selected>Hex</option> <option value="rgb">RGB</option> <option value="oklch">OKLCH</option> </select> <input class="input input-sm color-picker-text" name="brand" value="#e5484d" aria-label="Brand color" spellcheck="false" autocomplete="off"> </div> </div> </div></template>Leave out color-picker-alpha for a color that has to be opaque, such as a brand color that is printed: the swatch then stands beside the hue alone, and a color typed with an opacity is taken without it. Leave out the swatch too, and the tracks take the whole width.
#Why OKLCH
The field could be saturation and value, the square most pickers draw, which is easy to paint because it is only sRGB mixed with white and black. It is a poor field to choose a palette on: the same value is a dark blue and a glaring yellow, and a hue moved at the same saturation changes how light the color looks. OKLCH is built so that equal steps look equal, which is why the theme writes its colors in it, and why a picker for a theme should choose in it too.
- Lightness runs from 100% at the top of the field to 0 at the bottom, and is the same lightness at every hue.
- Chroma runs from none, a grey, at the start of the field to .37 at its end, as far as Display P3 reaches. A color is not always as vivid as its chroma asks: the screen shows the nearest one it can, and so do the field and the thumb, through the same rule.
- Hue runs round the circle on its own slider, painted at one lightness and chroma so every hue can be told apart.
The value is written in the format the select says. oklch() is written exactly as it is held. Hex and rgb() can hold only sRGB, so a color past it is fitted as CSS Color 4 fits a color to a screen: chroma is taken away, at the same lightness and hue, until the color is within a just noticeable difference of one sRGB can write. Switching the format never moves the color held, so a color goes from hex to oklch() and back to the same hex.
A gradient runs along one axis only, so the field is drawn in rows: each row is a gradient from grey to .37 at its own lightness, exact along the line, and the rows halfway between them lie over the others under a mask that fades from one row to the next. The colors are the theme's mixins, functions of the hue the behavior publishes, so the component writes no color of its own.
#The behavior
colorPicker takes the root, the element with the color-picker class, and returns the function that detaches it.
import { colorPicker } from '@master/ui/behaviors/color-picker.js'const accent = colorPicker(document.querySelector('.color-picker'))accent.value // 'oklch(62% 0.19 260)'accent.value = '#e5484d' // choose a color, and hex as its format- The color starts from the text field's value, in any of the three formats, and the format with it. The behavior writes it on the root as four numbers,
--color-picker-lightness,--color-picker-chroma,--color-picker-hueand--color-picker-alpha, which place the thumb and paint the field, the tracks and the swatches. Write the first color's numbers in the root'sstyletoo, so the field is right before the script runs. - The field. A press puts the thumb under the pointer and gives it focus; a drag follows, even past the edge of the field, where the color stops. The keys are in the table below.
- The text takes a color as soon as what is typed is one, and leaves the text as it is typed. When the field is committed, with Enter or by leaving it, the color is written again in the format chosen; text that is not a color is put back.
- Events. The thumb dispatches
inputwhile the color moves andchangewhen a drag ends or a key moved it; the sliders, the select and the text field dispatch their own. Whoever listens on the root reads the newvaluein each of them. - The thumb's words. Its
aria-valuetextsaysLightness 62%, chroma 0.19; passvalueText: (lightness, chroma) => …to say it in the page's language.
Without the behavior the field shows the color written in the markup, and the sliders, the select and the text field are plain controls that do not follow one another.
#Keyboard
| Keys | Action |
|---|---|
| ArrowUp, ArrowDown | On the field's thumb, make the color lighter or darker by 1% of lightness |
| ArrowRight, ArrowLeft | On the field's thumb, add or take away .005 of chroma, toward the end of the line or its start (mirrored right to left) |
| Shift with an arrow | On the field's thumb, ten times the step: 10% of lightness or .05 of chroma |
| PageUp, PageDown | On the field's thumb, make the color lighter or darker by 10% of lightness |
| Home, End | On the field's thumb, take the chroma to none (a grey) or to the end of the field |
| ArrowLeft, ArrowRight, ArrowUp, ArrowDown, Home, End, PageUp, PageDown | On the hue and the opacity, what the native range input does |
| Enter | In the text field, take what is typed; the field is written again in the format chosen, or put back when it is not a color |
#Accessibility
- The field's thumb is one slider. Its value is the lightness, since ArrowUp and ArrowDown move it, and its
aria-valuetextnames the lightness and the chroma together, so a screen reader says both whichever moved. Name it for what it chooses, such as "Lightness and chroma". - The text field is the way to give an exact color without the field, and the name of the color goes on it: "Brand color". Its
nameis what a form submits. - Name the hue and the opacity sliders and the format select; their keys are the platform's.
- Forced colors keep the field's colors, since they are what the reader came for; its edge and the thumb's focus are drawn in system colors.
#React, Svelte and Vue
The color-picker component draws the first value into the first render, the four numbers on the root included, and attaches the behavior, which moves every part after that. Its value is the color as text, in the format chosen, and it comes out on every change; give trigger (a snippet in Svelte, a slot in Vue) to open the picker from a button of the page's. Copy it into the project with npx master-ui add color-picker; React, Svelte and Vue says how the components are built and used.
#Classes
| Class | Description |
|---|---|
color-picker | The picker: a column 16rem wide of the field, the tracks and the text. When it holds a color-picker-popover it holds only the button and the popover, which takes the column |
color-picker-area | The field: lightness from 100% at the top to 0 at the bottom, chroma from 0 to .37 along the line, at the hue held. It holds the thumb and takes the pointer |
color-picker-thumb | The one thumb of the field, placed from the numbers on the root: a slider whose value is the lightness and whose aria-valuetext names the lightness and the chroma |
color-picker-hue | On a slider from 0 to 360: the hue, on a track painted with the circle of hues |
color-picker-alpha | On a slider from 0 to 100: the opacity, on a track where the color fades in over a checker. Leave it out for an opaque color |
color-picker-swatch | On a swatch: the color held, over a checker. In the column it stands at the start of the two tracks; in the button that opens a popover it shows the color while the picker is closed |
color-picker-fields | The row of the format and the text |
color-picker-format | On a small select: the format the text is written in, with the options hex, rgb and oklch |
color-picker-text | On a small text field: the color, written in the format chosen. It takes a color typed in any of the three, and carries the name a form submits |
color-picker-popover | On a popover inside the root: the column, opened from a button beside it, whose swatch shows the color |
#Custom properties
| Property | Default | Description |
|---|---|---|
--color-picker-width | 16rem | Width of the column |
--color-picker-lightness | 0 | The color's lightness, from 0 to 1. The behavior writes it on the root; write the first value in the markup's style |
--color-picker-chroma | 0 | The color's chroma, from 0; the field reaches .37 |
--color-picker-hue | 0 | The color's hue, in degrees without a unit |
--color-picker-alpha | 1 | The color's opacity, from 0 to 1 |
--color-picker-color | The color held, made from the four numbers: read it for anything else inside the root that shows the color |
#Related
- SwatchA color to look at or to pick, as a disc with a finished edge.
- SliderA native range input: a thin well with a pearl thumb.
- PopoverA floating sheet anchored to the button that opens it.