Skip to content

Color Slider RGB Picker

Build an RGB and alpha color picker with an RGB preview, preset swatches, a screen color picker, and custom thumbs. color-space="rgb" with channel="red", "green", or "blue" gives each slider one RGB channel, sv-color-group shares one color between them, and the thumb slot with :state(--active) styles the thumbs.

Each sv-color-swatch and the screen picker set the group color, and a ::part(thumb) transition glides the thumbs to it. While dragging, each thumb grows into a teardrop.

The screen picker button shows only in browsers with the EyeDropper API.

Reduced Motion

Your system is set to reduce motion, so the animations shown on this page will not play for you.

Code

The following example assumes that you have imported the component and set up the theme.

RGB Picker
<sv-color-group class="rgb-picker" value="#3b82f6">
51 collapsed lines
<div class="preview">
<div class="preview-swatch"></div>
<div class="preview-value">
<sv-text-flow class="rgb-value" use-diff value="rgb(59 130 246)"></sv-text-flow>
</div>
<button
type="button"
class="btn btn-surface eyedropper"
aria-label="Pick a color from the screen"
hidden
>
<svg aria-hidden="true" viewBox="0 -960 960 960">
<path
d="M148.08-182.31v-93.3q0-13.8 5.35-26.47 5.36-12.66 14.92-22.11l329.42-328.62-66.12-64.5 40.16-38.92 85.27 84.08 130.54-130.93q4.19-4.19 10-6.61t11.99-2.42q6.37 0 11.71 2.42 5.34 2.42 10.22 6.65l71.5 71.89q4.23 4.68 6.65 9.92 2.42 5.25 2.42 11.62 0 6.18-2.42 11.99-2.42 5.81-6.63 10.02L672.08-556.62l84.46 86.89-39.23 39.23-65.27-65.69-327.85 327.84q-9.45 9.56-22.11 14.92-12.67 5.35-26.47 5.35h-93.3q-14.35 0-24.29-9.94t-9.94-24.29Zm55.96-21.73h77.82l330.79-332.65-75.96-75.96-332.65 330.79v77.82Z"
/>
</svg>
</button>
</div>
<label for="rgb-picker-red">Red</label>
<sv-color-slider id="rgb-picker-red" adaptive channel="red" color-space="rgb" thumb-contained>
<div class="custom-thumb" aria-hidden="true" slot="thumb"></div>
</sv-color-slider>
<label for="rgb-picker-green">Green</label>
<sv-color-slider id="rgb-picker-green" adaptive channel="green" color-space="rgb" thumb-contained>
<div class="custom-thumb" aria-hidden="true" slot="thumb"></div>
</sv-color-slider>
<label for="rgb-picker-blue">Blue</label>
<sv-color-slider id="rgb-picker-blue" adaptive channel="blue" color-space="rgb" thumb-contained>
<div class="custom-thumb" aria-hidden="true" slot="thumb"></div>
</sv-color-slider>
<label for="rgb-picker-alpha">Alpha</label>
<sv-color-slider id="rgb-picker-alpha" adaptive channel="alpha" thumb-contained>
<div class="custom-thumb" aria-hidden="true" slot="thumb"></div>
</sv-color-slider>
<sv-divider></sv-divider>
<div class="swatches" aria-label="Presets" role="radiogroup">
<sv-color-swatch aria-label="Red" name="rgb-picker-preset" value="#ef4444"></sv-color-swatch>
<sv-color-swatch aria-label="Orange" name="rgb-picker-preset" value="#f97316"></sv-color-swatch>
<sv-color-swatch aria-label="Yellow" name="rgb-picker-preset" value="#eab308"></sv-color-swatch>
<sv-color-swatch aria-label="Green" name="rgb-picker-preset" value="#22c55e"></sv-color-swatch>
<sv-color-swatch aria-label="Cyan" name="rgb-picker-preset" value="#06b6d4"></sv-color-swatch>
<sv-color-swatch aria-label="Purple" name="rgb-picker-preset" value="#a855f7"></sv-color-swatch>
<sv-color-swatch aria-label="Pink" name="rgb-picker-preset" value="#ec4899"></sv-color-swatch>
</div>
</sv-color-group>
<script type="application/x-typescript" module>
40 collapsed lines
const picker = document.querySelector<ColorGroup>(".rgb-picker")!;
const rgbValue = picker.querySelector<TextFlow>(".rgb-value")!;
const eyedropperButton = picker.querySelector<HTMLButtonElement>(".eyedropper")!;
const swatches = picker.querySelector<HTMLDivElement>(".swatches")!;
function getRgbText(hexColor: string) {
const [red, green, blue, alpha = 255] = hexColor
.match(/\w\w/g)!
.map(pair => parseInt(pair, 16));
const alphaText = alpha === 255 ? "" : ` / ${Math.round((alpha / 255) * 100)}%`;
return `rgb(${red} ${green} ${blue}${alphaText})`;
}
function glideToColor(color: string) {
picker.classList.add("gliding");
// Setting `value` on the group moves every slider.
picker.value = color;
}
// Fires for every color change in the group, from the user or from code.
picker.addEventListener("valuechange", () => {
picker.style.setProperty("--color", picker.value);
rgbValue.value = getRgbText(picker.value);
});
// Dragging a thumb must not glide.
picker.addEventListener("pointerdown", () => picker.classList.remove("gliding"));
// A swatch sets the group color by itself. Its `input` reaches the swatches before the group.
swatches.addEventListener("input", () => picker.classList.add("gliding"));
// The EyeDropper API exists only in Chromium browsers.
eyedropperButton.hidden = !("EyeDropper" in window);
eyedropperButton.addEventListener("click", async () => {
const { sRGBHex } = await new (window as any).EyeDropper().open();
glideToColor(sRGBHex);
});
</script>
<style>
155 collapsed lines
.rgb-picker {
--color: #3b82f6;
--opaque-color: rgb(from var(--color) r g b / 100%);
display: grid;
gap: 0.5em;
inline-size: min(100%, 22em);
padding: 1em;
margin: 1em auto;
background: var(--sv-gray-6);
border: solid 1px var(--sv-gray-5);
border-radius: 1em;
label {
margin-block-start: 0.5em;
font-weight: 600;
}
.preview {
display: flex;
flex-wrap: wrap;
gap: 1em;
align-items: center;
}
/* The color sits on a checkerboard. This makes the alpha visible. */
.preview-swatch {
flex: none;
inline-size: 2em;
block-size: 2em;
background:
linear-gradient(var(--color), var(--color)),
repeating-conic-gradient(#ccc 0 25%, #fff 0 50%) 0 0 / 0.5em 0.5em;
border: 2px solid #fff;
border-radius: var(--sv-bdr-rad-md);
}
.preview-value {
display: grid;
flex-grow: 99;
font-size: 0.9em;
font-weight: 600;
white-space: nowrap;
}
.eyedropper {
flex-grow: 1;
padding: 0.5em;
border: solid 1px var(--sv-gray-4);
svg {
display: block;
inline-size: 1.2em;
block-size: 1.2em;
margin-inline: auto;
fill: currentcolor;
}
}
sv-color-slider {
--sv-slider-track-thickness: 1.6em;
--sv-slider-thumb-sz: 1.5em;
--sv-slider-bdr-rad: 2em;
--sv-slider-bdr-clr: #fff;
--sv-slider-ease: linear(
0,
0.039 2.1%,
0.12 4.5%,
0.265 7.6%,
0.713 16%,
0.879 19.7%,
0.999 23.1%,
1.084 26.5%,
1.139 30.1%,
1.163 34.1%,
1.154 39%,
1.007 58.3%,
0.976 66.4%,
0.979 78.5%,
1
);
&::part(thumb) {
background: none;
border: none;
box-shadow: none;
}
&::part(thumb):focus-visible {
outline: 4px solid var(--sv-accent);
outline-offset: 2px;
}
&:state(--active)::part(thumb) {
outline: none;
}
}
/* Grows into a teardrop while dragging, with its tip on the track center. */
.custom-thumb {
--loupe-sz: 2em;
box-sizing: border-box;
inline-size: var(--sv-slider-thumb-sz);
block-size: var(--sv-slider-thumb-sz);
background-color: var(--opaque-color);
border: 3px solid #fff;
border-radius: 50%;
box-shadow: 0 1px 6px rgb(0 0 0 / 50%);
rotate: -45deg;
transition: var(--sv-slider-dur, 300ms) var(--sv-slider-ease);
transition-property: inline-size, block-size, border-radius, translate;
@supports (color: contrast-color(red)) {
border-color: contrast-color(var(--opaque-color));
}
sv-color-slider:state(--active) > & {
inline-size: var(--loupe-sz);
block-size: var(--loupe-sz);
border-radius: 50% 50% 50% 0;
translate: 0 calc(var(--loupe-sz) * -0.7071);
}
}
&.gliding sv-color-slider::part(thumb) {
transition: inset-inline-start 500ms cubic-bezier(0.25, 1, 0.5, 1);
}
sv-divider {
margin-block: 1em 0.5em;
}
/* One row, with every swatch shrinking to fit. */
.swatches {
display: grid;
grid-template-columns: repeat(7, minmax(0, 1fr));
gap: 0.5em;
}
sv-color-swatch {
--sv-color-swatch-sz: 100%;
&::part(check) {
display: none;
}
}
@media (prefers-reduced-motion: reduce) {
.custom-thumb,
&.gliding sv-color-slider::part(thumb) {
transition-duration: 0s;
}
}
}
</style>