Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <head>
  <meta charset="UTF-8">
  <title>Match Color CMYK</title>
  <script src="https://cdn.tailwindcss.com"></script>
  <style>
    body { transform-origin: top; transform: scale(1.25); }
  </style>
</head>
<body class="min-h-screen bg-neutral-500 text-gray-100 flex flex-col items-center py-8">
  <h1 class="text-3xl mb-2">Match Color CMYK</h1>
  <p class="mb-6 text-center">Guess the CMYK mix of the left swatch. Click "Compare colors" to reveal answer.</p>

  <!-- Swatches -->
  <div class="flex w-full max-w-lg h-40 rounded-lg overflow-hidden shadow-inner mb-8 relative">
    <div id="targetSwatch" class="flex-1"></div>
    <div class="w-px bg-neutral-600"></div>
    <div id="guessSwatch" class="flex-1"></div>
    <div id="accuracyOverlay" class="absolute inset-0 flex items-center justify-center text-white text-5xl drop-shadow-lg" style="display:none;"></div>
  </div>

  <!-- Sliders -->
  <div class="w-full max-w-lg space-y-6 mb-6">
    <div>
      <div class="flex justify-between mb-1">
        <span>Cyan ↔ Red</span>
        <span id="cLabel" class="font-mono">0%</span>
      </div>
      <input id="cSlider" type="range" min="-100" max="100" value="0" class="w-full accent-neutral-700">
    </div>
    <div>
      <div class="flex justify-between mb-1">
        <span>Magenta ↔ Green</span>
        <span id="mLabel" class="font-mono">0%</span>
      </div>
      <input id="mSlider" type="range" min="-100" max="100" value="0" class="w-full accent-neutral-700">
    </div>
    <div>
      <div class="flex justify-between mb-1">
        <span>Yellow ↔ Blue</span>
        <span id="yLabel" class="font-mono">0%</span>
      </div>
      <input id="ySlider" type="range" min="-100" max="100" value="0" class="w-full accent-neutral-700">
    </div>
    <div>
      <div class="flex justify-between mb-1">
        <span>Black</span>
        <span id="kLabel" class="font-mono">0%</span>
      </div>
      <input id="kSlider" type="range" min="-100" max="100" value="0" class="w-full accent-neutral-700">
    </div>
  </div>

  <!-- Buttons -->
  <div class="w-full max-w-lg mb-4">
    <button id="compareBtn" class="w-full bg-blue-500 text-white py-2 rounded">Compare colors</button>
  </div>
  <div class="w-full max-w-lg">
    <button id="resetBtn" class="w-full bg-neutral-700 text-white py-2 rounded">Try again</button>
  </div>

<script>
document.addEventListener('DOMContentLoaded', () => {
  // Utility
  const rnd = (min,max) => Math.floor(Math.random()*(max-min+1))+min;
  const clamp = (n,min,max) => Math.min(max, Math.max(min, n));
  const cmykToRgb = (c,m,y,k) => { c/=100; m/=100; y/=100; k/=100; return {r:255*(1-c)*(1-k), g:255*(1-m)*(1-k), b:255*(1-y)*(1-k)}; };
  const toCss = rgb => `rgb(${Math.round(rgb.r)}, ${Math.round(rgb.g)}, ${Math.round(rgb.b)})`;
  const sliderToPerc = v => clamp((v+100)/2, 0, 100);
  const percToSlider = p => clamp(Math.round(p*2 - 100), -100, 100);

  // Elements
  const keys = ['c','m','y','k'];
  const sliders = keys.map(k => document.getElementById(k+'Slider'));
  const labels = keys.map(k => document.getElementById(k+'Label'));
  const targetSwatch = document.getElementById('targetSwatch');
  const guessSwatch = document.getElementById('guessSwatch');
  const overlay = document.getElementById('accuracyOverlay');
  const compareBtn = document.getElementById('compareBtn');
  const resetBtn = document.getElementById('resetBtn');

  let target = {};
  let answered = false;

  // Update slider label
  function updateLabel(i) {
    const sliderVal = parseInt(sliders[i].value, 10);
    const guessPerc = sliderToPerc(sliderVal);
    if (answered) {
      const correctSlider = percToSlider(target[keys[i]]);
      labels[i].innerHTML = `${sliderVal}% / <span class="text-green-400">${correctSlider}%</span>`;
    } else {
      labels[i].textContent = `${sliderVal}%`;
    }
  }

  // Compute match percentage
  function updateAccuracy(guess) {
    const tgtRgb = cmykToRgb(target.c, target.m, target.y, target.k);
    const curRgb = cmykToRgb(guess.c, guess.m, guess.y, guess.k);
    const dr = tgtRgb.r - curRgb.r;
    const dg = tgtRgb.g - curRgb.g;
    const db = tgtRgb.b - curRgb.b;
    const dist = Math.sqrt(dr*dr + dg*dg + db*db);
    const maxDist = Math.sqrt(255*255*3);
    const pct = clamp(Math.round((1 - dist/maxDist) * 100), 0, 100);
    overlay.textContent = `${pct}% Match`;
  }

  // Render swatches
  function updateSwatches() {
    // Left: target
    targetSwatch.style.backgroundColor = toCss(cmykToRgb(target.c, target.m, target.y, target.k));
    // Right: guess
    const guess = {};
    keys.forEach((k,i) => { guess[k] = sliderToPerc(parseInt(sliders[i].value, 10)); });
    guessSwatch.style.backgroundColor = toCss(cmykToRgb(guess.c, guess.m, guess.y, guess.k));
    if (answered) updateAccuracy(guess);
  }

  // Initialize
  function init() {
    answered = false;
    overlay.style.display = 'none';
    // Random target
    keys.forEach(k => { target[k] = rnd(0, k==='k'?50:100); });
    // Random initial guess around target ±20%
    keys.forEach((k,i) => {
      const randomPerc = clamp(target[k] + rnd(-20, 20), 0, k==='k'?50:100);
      sliders[i].value = percToSlider(randomPerc);
      updateLabel(i);
    });
    updateSwatches();
  }

  // Events
  sliders.forEach((s,i) => s.addEventListener('input', () => { updateLabel(i); updateSwatches(); }));
  compareBtn.addEventListener('click', () => {
    answered = true;
    overlay.style.display = 'flex';
    // update labels to show correct slider values
    sliders.forEach((_, i) => updateLabel(i));
    // update swatches and compute match percentage
    updateSwatches();
  });
  resetBtn.addEventListener('click', init);

  init();
});
</script>
</body>
              
            
!

CSS

              
                
              
            
!

JS

              
                
              
            
!
999px

Console