webdevpuneet.com

Color Mixer — Blend Two Colors into a Swatch Scale

Color Mixer · Tools · Plain HTML, CSS & JS · Live preview

Copy & paste

Source Code

<div class="cm-card">
  <div class="cm-ends">
    <label class="cm-end"><span>From</span>
      <div class="cm-swatch"><input type="color" id="cmA" value="#6366f1"><input type="text" id="cmAHex" value="#6366F1" maxlength="7" spellcheck="false"></div>
    </label>
    <label class="cm-end"><span>To</span>
      <div class="cm-swatch"><input type="color" id="cmB" value="#22d3ee"><input type="text" id="cmBHex" value="#22D3EE" maxlength="7" spellcheck="false"></div>
    </label>
  </div>

  <label class="cm-steps">Steps <strong id="cmStepsVal">7</strong>
    <input type="range" id="cmSteps" min="3" max="11" step="1" value="7">
  </label>

  <div class="cm-scale" id="cmScale"></div>
  <code class="cm-css" id="cmCss"></code>
</div>

Extend with AI

Take This Snippet Further With AI

The AI prompt for this one is for Patreon supporters ($3 a month, 7 days free), along with a site with no ads. Supporters enter the passcode once and it is remembered in this browser.

· Become a supporter

What's included

Features

RGB interpolation
Even blends between two colors with exact endpoints.
Adjustable steps
A slider sets 3–11 swatches, re-rendered live.
Click-to-copy swatches
Each blend copies its hex with a Copied confirmation.
Luminance-aware labels
Hex labels flip dark/light to stay readable on any swatch.
Swatch + hex sync
Native pickers and hex fields stay in sync both ways.
Gradient output
Generates a copyable linear-gradient() from the stops.
Hex normalisation
Accepts #fff or #ffffff, with or without the hash.
No library
Pure HTML/CSS/JS — no color library.

About this UI Snippet

Color Mixer — Interpolate a Swatch Scale Between Two Colors

Screenshot of the Color Mixer snippet rendered live

A color mixer blends two colors into an evenly-spaced scale of swatches — the quick way to build a gradient, generate tints between a brand pair, or pick a mid-point shade. This snippet interpolates the colors in the browser, lets you set how many steps to generate, and copies any swatch or the full gradient, in plain HTML, CSS, and vanilla JavaScript.

Linear interpolation in RGB

The mixer converts each end color from hex to RGB, then for each step computes a position t from 0 to 1 and linearly interpolates every channel: c1 + (c2 - c1) * t. With n steps the positions are spread i / (n - 1) so the first swatch is exactly the "from" color, the last is exactly the "to" color, and the rest are even blends. The result is rounded back to a hex string per swatch — straightforward, predictable color math with no library.

Adjustable resolution

A range slider sets the number of steps from 3 to 11, re-rendering the scale live. Three steps give you a from / mid / to triad; eleven give a smooth ramp you can pull individual tints from. This is handy for generating a tint scale for a design system (50→500→900) or for finding the precise middle color between two brand hues.

Readable labels, click to copy

Each swatch shows its hex on hover, and the label color flips between dark and light based on the swatch's luminance so it stays legible on both pale and dark blends. Clicking a swatch copies its hex to the clipboard with a brief "Copied" confirmation — so the mixer doubles as a palette picker, not just a preview.

Dual input, kept in sync

Both end colors have a native <input type="color"> swatch and a hex field that stay in sync both ways: pick from the OS color picker and the hex updates, or type a hex (3- or 6-digit, with or without #) and the swatch follows. A small validator normalizes and ignores invalid input so the scale never breaks mid-typing.

Gradient output

Below the scale, the mixer prints a ready-to-use linear-gradient(90deg, …) built from the generated stops, and clicking it copies the whole declaration. That turns a two-color choice into a paste-ready CSS gradient or a discrete palette in one step. The pure interpolation helpers are easy to lift into a theme generator or design tool, making this a compact, dependency-free reference for blending colors.

Step by step

How to Use

  1. 1
    Paste HTML, CSS, and JSA mixer renders with two end colors and a generated swatch scale.
  2. 2
    Choose two colorsUse the swatch pickers or type hex values for the From and To colors.
  3. 3
    Set the stepsDrag the slider to generate 3–11 evenly-spaced blends.
  4. 4
    Copy a swatchHover to see its hex; click to copy it to the clipboard.
  5. 5
    Grab the gradientClick the linear-gradient() string to copy a paste-ready CSS gradient.
  6. 6
    Reuse the mathLift the mix/toHex helpers into a palette or theme generator.

Real-world uses

Common Use Cases

Design system tint scales
Generate a series of shades between two brand colours, then check them with a colour swatch in a design system.
Gradient stop discovery
Find stops for a hero or button, then apply them in a gradient button, choosing between 3 and 11 swatches.
Theme creation
Blend brand colours alongside a theme palette generator, with each swatch copying its hex on click.
Data visualisation ramps
Create sequential colour scales for a heatmap matrix, where exact endpoints keep the scale faithful to your two chosen colours.
Mid-point shade picking
Get the exact colour halfway between two values, with luminance-aware labels flipping dark or light to stay readable on every swatch.
Related: OKLCH Color Picker & Playground
See the OKLCH Color Picker & Playground for a related forms pattern worth pairing with this one.

Got questions?

Frequently Asked Questions

Each end color is converted to RGB, then for every step the mixer computes a position t from 0 to 1 and linearly interpolates each channel as c1 + (c2 - c1) * t, rounding back to hex. Positions are spread as i / (n - 1), so the first and last swatches exactly match your two colors and the middle ones are even blends.

Each label's color is chosen from the swatch's luminance (a weighted sum of its RGB channels): light swatches get a dark label and dark swatches get a light one, so the hex text stays legible across the whole scale. It is the same readability idea behind contrast-aware text on colored backgrounds.

This version interpolates in sRGB, which is simple and predictable and fine for most UI gradients and tint scales. If you need perceptually even steps you can swap the mix function to interpolate in HSL, OKLCH, or LAB — the rest of the snippet (steps, copy, gradient output) stays the same because it only depends on getting a color per step.

Yes. The mixer builds a linear-gradient(90deg, …) from the generated stops and shows it below the scale; click it to copy the full declaration. You can also click any individual swatch to copy its hex if you want discrete palette values rather than a gradient.

Keep the toRgb, toHex, and mix helpers as plain functions and hold the two colors and step count in state. Compute the swatch array as derived state and render it, with click handlers that copy via the Clipboard API. Two-way bind the color and hex inputs to the same state. Tailwind users swap the classes for utilities; the interpolation logic is unchanged.