Pickr Color Picker with Contrast Check — Free JS Snippet

Pickr Inline Color Picker with Swatches and Contrast Check · Forms · Plain HTML, CSS & JS · Live preview

CategoryForms

What's included

Features

Inline picker panel embedded in the page instead of a popup
Swatch palette plus HEX, RGB and HSL input and readout
Live preview driven by a single --brand CSS custom property
WCAG relative-luminance contrast ratios for white and black text
AAA / AA / large-text / fail grading shown for each
Automatically chooses the more readable text colour for the preview
Continuous change event for smooth, real-time updates
Only the needed components enabled (no alpha, save or clear)

About this UI Snippet

Pickr Inline Color Picker with Contrast Check — HTML, CSS & JavaScript

Screenshot of the Pickr Inline Color Picker with Swatches and Contrast Check snippet rendered live

The native <input type="color"> is a poor building block for a designed interface: it opens an operating-system dialog that you cannot style, it offers no swatches, and it gives you a hex string and nothing else. Pickr is a purpose-built colour picker that renders its own panel — saturation square, hue slider, optional alpha, an input row that switches between HEX, RGB and HSL, and a swatch palette — and that panel can be styled or embedded directly in the page.

This snippet uses Pickr in inline mode. Passing inline: true renders the whole picker in place rather than hiding it behind a button, which is the right choice for a theme editor or settings pane where colour is the main task. The nano theme is loaded from its own stylesheet, the swatches option supplies the quick-pick palette, and the components object switches on exactly the parts that are needed — opacity is disabled because a brand colour is opaque, and clear and save are off because every change applies live.

Pickr's change event fires continuously while dragging, and the colour object it passes is more than a string: toHEXA(), toRGBA() and toHSLA() convert on demand, so the readouts for all three formats come from one value. The interesting part is what is done with that value. A brand-colour picker that only shows the colour is missing half the job, so the snippet computes WCAG contrast. It converts the RGB channels to relative luminance, then to a contrast ratio against white and against black, and grades each as AAA, AA, AA for large text only, or fail. The preview card automatically chooses whichever text colour reads better on the chosen background.

That auto-selection is the practical takeaway: instead of hoping a chosen colour works with white text, the UI states plainly which text colour passes and by how much. The recolouring itself is a single CSS custom property, --brand, set on the preview card, so the same approach drops straight into a real theme system that reads CSS variables.

Build with AI

Build, Understand, Optimize, and Extend It With AI

Ask an AI assistant like Claude to generate a full tint-and-shade scale from the chosen colour, add a dark-mode preview, or write the selected colour to a CSS variable used across the whole page.

Prompt to recreate it

Copy this into your AI assistant of choice to build the effect from scratch, or as a jumping-off point for your own variant:

text
Build an inline colour picker with Pickr 1.9 (nano theme) loaded from a CDN.

Requirements:
- Use Pickr.create with inline: true, a swatches array, and components for preview, hue and an interaction row with hex, rgba, hsla and input enabled (opacity, clear and save off).
- On the init and change events, show the colour as HEX, RGB and HSL using toHEXA(), toRGBA() and toHSLA().
- Recolour a preview card by setting one CSS custom property (--brand), and automatically choose white or black text for the better contrast.
- Compute WCAG relative luminance and show the contrast ratio against white and against black with an AAA / AA / large-text / fail grade for each.

Want to tighten it up first? Run this prompt through the AI Prompt Studio to score it across 8 quality dimensions, catch anti-patterns, and tune the wording for Claude, ChatGPT, or Gemini before you paste it in.

Source Code

<div class="pk-wrap">
  <div class="pk-left">
    <div id="pkHost"></div>
  </div>
  <div class="pk-right">
    <div class="pk-preview" id="pkPreview">
      <span class="pk-badge">New</span>
      <h3>Brand preview</h3>
      <p>This card recolours live as you pick.</p>
      <button type="button">Get started</button>
    </div>
    <dl class="pk-read">
      <div><dt>HEX</dt><dd id="pkHex">-</dd></div>
      <div><dt>RGB</dt><dd id="pkRgb">-</dd></div>
      <div><dt>HSL</dt><dd id="pkHsl">-</dd></div>
    </dl>
    <div class="pk-contrast">
      <div class="pk-c"><span class="pk-sample" id="pkSampleW">Aa</span><div><b id="pkRatioW">-</b><small id="pkGradeW"></small></div></div>
      <div class="pk-c"><span class="pk-sample dark" id="pkSampleB">Aa</span><div><b id="pkRatioB">-</b><small id="pkGradeB"></small></div></div>
    </div>
  </div>
</div>

Step by step

How to Use

  1. 1
    Drag in the colour squareMove the handle in the saturation square and the hue slider. The preview card recolours as you drag.
  2. 2
    Use the swatchesClick a swatch to jump to a preset brand colour.
  3. 3
    Switch formatsUse the format buttons under the picker and type an exact HEX, RGB or HSL value.
  4. 4
    Read the contrastCheck the white and black contrast ratios and their WCAG grades beside the preview.
  5. 5
    Find an accessible colourPick a very light yellow and watch white text fail while black text passes AAA.

Real-world uses

Common Use Cases

Theme and brand settings
Let customers choose an accent colour and see it applied instantly. Pair with the noUiSlider range controls for other settings panels.
Accessibility auditing
Check whether a proposed colour pairing meets WCAG contrast before it ships.
ADMIN
White-label dashboards
Give each tenant a brand colour and derive the readable text colour automatically.
Learning colour maths
See relative luminance and contrast ratio implemented in a dozen lines of JavaScript.

Got questions?

Frequently Asked Questions

It renders the picker panel directly in the element you provide instead of showing it as a popup from a button.

Each RGB channel is converted to linear light, weighted into a relative luminance, and the ratio is (lighter + 0.05) / (darker + 0.05).

WCAG AA needs 4.5:1 for normal text and 3:1 for large text; AAA needs 7:1 for normal text.

The colour object from the change event has toHEXA(), toRGBA(), toHSLA(), toHSVA() and toCMYK() methods.

Set components.opacity to true and read the alpha channel from toRGBA() or toHEXA().

Yes. The CDN script exposes a global Pickr, and the theme CSS is loaded separately.

Yes. Use the JSX, Vue, Angular or Tailwind export buttons on this page to convert the markup and styles. The behaviour comes from Pickr, so in a framework project install it with npm install @simonwep/pickr instead of the CDN tag, create it in useEffect / onMounted / ngAfterViewInit, and release it with destroyAndRemove() when the component unmounts.