Skip to the page content

Color pickers

Color fields, from a simple swatch to a full picker with gradients, image sampling and a contrast check.

8 field types

Popup color picker

The same picker behind a swatch button and a hex box, for forms where color is one setting among many. It opens under the field. Esc or Cancel puts the old color back; Done or clicking away keeps it. The hex box takes 3, 4, 6 or 8 digits. Here the value feeds a tint scale.

Popup picker

Used in the variable names.

    Export as
    
    												
    												
    											
    Show code
    <div class="cpick-field" data-cpick-popup data-value="#1F4E79">
      <button type="button" class="cpick-field-swatch" aria-haspopup="dialog"
              aria-expanded="false" aria-controls="base-panel"></button>
      <input type="text" class="form-control cpick-field-hex" id="base-hex" value="#1F4E79">
      <div class="cpick-panel cpick-panel-popup" id="base-panel" role="dialog" hidden></div>
    </div>
    
    // Esc and Cancel revert, Done and clicking away keep the color.
    attachPopup(field, (color) => drawScale(color));

    Inline color picker

    The full picker set into the page: a saturation square, hue and opacity sliders, typed hex, RGB or HSL, an eyedropper where the browser has one, and preset and recent swatches. Use it when color is the main setting on the screen. Every part works from the keyboard.

    Inline picker

    Preview on the customer portal

    Book online. Same-day visits for calls before noon.

    Maintenance plan

    Garden tidy-up

    Mowing, edging, hedge trim and a 21-point garden check

    $129

    Book a visit
    Button text
    White, 6.2:1
    Against the page
    6.2:1, fine for links
    CSS
    #2F6B4F
    Show code
    <div class="cpick-panel" data-cpick-inline data-value="#2F6B4F"></div>
    
    const picker = buildPicker(panel, {
      value: '#2F6B4F',
      onInput: (color) => paintPortal(color),          // every move
      onCommit: (color) => rememberColor(hexOf(color)) // release, Enter or a swatch
    });
    picker.setColor('#1F4E79');

    Swatch radio group

    A fixed set of colors as radio buttons dressed as swatches. Arrow keys move between them, the chosen name shows in the legend and an unavailable color is disabled and skipped. Use it when only certain colors exist, like the case colors a device ships in.

    Swatch radios
    Sculpted Shoulder Blazer gift box: Chalk white

    32 in stock

    Show code
    <fieldset>
      <legend>Case color</legend>
      <label class="cpick-swatch" data-color="#3E5566">
        <input type="radio" name="case" value="Slate blue">
        <span class="cpick-swatch-dot"></span>
        <span class="visually-hidden">Slate blue</span>
      </label>
    </fieldset>

    Image color sampler

    Reads the color under the pointer, or under the arrow keys, from a picture; click or Enter keeps it. The main colors of the picture are worked out for you, people can load their own photo, and the eyedropper reaches anywhere on screen where the browser allows it.

    Image sampler

    #000000

    Main colors

    Kept

    Click the photo to keep a color.

    Show code
    const [r, g, b] = ctx.getImageData(x, y, 1, 1).data;
    
    // Main colors: bucket every fourth pixel, then keep the biggest
    // buckets that are not too alike.
    const key = (r >> 4) << 8 | (g >> 4) << 4 | (b >> 4);
    
    // Anywhere on screen, where the browser allows it:
    const { sRGBHex } = await new EyeDropper().open();

    Theme color swatches

    Swatch radios whose values are theme tokens instead of hex codes, so whatever is chosen recolors with the palette and dark mode. Text on each chip picks black or white to stay readable. Here they color visit tags.

    Theme colors

      Up to 20 characters. 4 of 8 tags

      Color

      Show code
      <label class="cpick-swatch tone-series-3">
        <input type="radio" name="tone" value="3">
        <span class="cpick-swatch-dot"></span>
        <span class="visually-hidden">Theme color 3</span>
      </label>
      
      // The saved value is the token, so it follows every palette.
      tag.style.backgroundColor = `var(--basalt-series-${tone})`;
      tag.style.color = inkFor(measure(tag.style.backgroundColor)).hex;
      document.addEventListener('basalt:appearance', drawTags);

      Color pair with contrast check

      Two colors chosen together, text and background, with the contrast ratio worked out as you go and checked against the usual WCAG levels. Swap flips them. When the pair fails, one click darkens or lightens the text color until it passes, keeping its hue.

      Contrast pair

      Next visit: April 2027

      Oakhaven Studio. Call (312) 555-0164 or book on the customer portal.

      8.7:1contrast

      • Small print4.5:1
      • Big heading3:1
      • Small print, strict7:1

      Show code
      const lum = ({ r, g, b }) => [r, g, b]
        .map((v) => (v /= 255) <= .03928 ? v / 12.92 : ((v + .055) / 1.055) ** 2.4)
        .reduce((sum, v, i) => sum + v * [.2126, .7152, .0722][i], 0);
      const ratio = (a, b) => {
        const [hi, lo] = [lum(a), lum(b)].sort((x, y) => y - x);
        return (hi + .05) / (lo + .05);
      };

      Gradient stop editor

      An input for a gradient rather than one color. Click the bar to add a stop, drag it or use the arrow keys to move it, and press Delete to remove it. Each stop has its own color and position, the shape is linear with an angle or radial, and the value comes out as CSS.

      Gradient

      Oakhaven StudioDana Zeller, carer

      Color stops

      Click the bar to add a stop. Arrow keys move the selected stop; Delete removes it.

      Shape
      135°
      
      												
      												
      											
      Show code
      const css = (stops, angle) =>
        `linear-gradient(${angle}deg, ${stops
          .slice().sort((a, b) => a.pos - b.pos)
          .map((s) => `${s.color} ${s.pos}%`).join(', ')})`;
      
      // A click on the bar adds a stop with the color already under it.
      bar.addEventListener('pointerdown', (e) => addStop(percentAt(e)));

      Native color input

      The browser's own input type="color". Light and accessible, but it only holds six-digit hex, has no opacity and its popup looks different in each browser. It takes Bootstrap's small and large sizes and a disabled state. Pair it with a hex box so people can paste a code, and check any rule on the value yourself.

      Native input

      On its own

      #1E2430

      Sizes

      Disabled

      Set by the fleet lease company.

      With a hex box and a contrast rule

      Six-digit hex; the # is optional.

      Needs 3:1 against the van color.

      Lettering contrast
      8.6:1
      Show code
      <input type="color" class="form-control form-control-sm form-control-color" id="roof">
      <input type="color" class="form-control form-control-lg form-control-color" id="roof">
      
      <input type="color" aria-label="Van color picker" value="#1F4E79">
      <input type="text" id="main" value="#1F4E79" aria-describedby="main-help main-error">
      
      native.addEventListener('input', () => { hex.value = native.value.toUpperCase(); });
      hex.addEventListener('change', () => { if (isHex(hex.value)) native.value = expand(hex.value); });
      if (ratio(main, text) < 3) showError(text, 'These two colors are too alike to read from the road.');