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

              
                <div class="container">
  <h1>Color Palette Generator</h1>
  <div id="app">
    <div id="inputs">
      <div class="input">
        <label for="num-colors-input">Number of Colors:</label>
        <input type="number" id="num-colors-input" value="5" min="1" max="10">
      </div>
      <div class="input">
        <label for="color-format-input">Color Format:</label>
        <select id="color-format-input">
          <option value="hex">HEX</option>
          <option value="rgb">RGB</option>
          <option value="hsl">HSL</option>
        </select>
      </div>
      <div class="input">
        <label for="color-scheme-input">Color Scheme:</label>
        <select id="color-scheme-input">
          <option value="random">Random</option>
          <option value="monochromatic">Monochromatic</option>
          <option value="analogous">Analogous</option>
          <option value="complementary">Complementary</option>
          <option value="split-complementary">Split Complementary</option>
          <option value="triadic">Triadic</option>
          <option value="tetradic">Tetradic</option>
        </select>
      </div>
      <button id="generate-btn">Generate</button>
    </div>
    <div id="color-palette"></div>
  </div>
</div>

              
            
!

CSS

              
                body {
  background-color: #f9f9f9;
  font-family: 'Roboto', sans-serif;
}

.container {
  max-width: 800px;
  margin: 0 auto;
  padding: 40px;
}

h1 {
  text-align: center;
  font-weight: 400;
  margin-bottom: 20px;
}

#app {
  background-color: #fff;
  border-radius: 5px;
  padding: 20px;
}

.input {
  margin-bottom: 10px;
}

.input label {
  display: block;
  font-size: 1.2rem;
  margin-bottom: 5px;
}

#num-colors-input {
  width: calc(100% - 20px);
}

.input input,
.input select {
  font-size: 1.2rem;
  padding: 10px;
  border: none;
  border-radius: 5px;
  width: 100%;
  margin-bottom: 10px;
  background-color: #f2f2f2;
}

#generate-btn {
  background-color: #27ae60;
  color: #fff;
  border: none;
  border-radius: 5px;
  padding: 10px 20px;
  cursor: pointer;
  transition: background-color 0.3s ease-in-out;
}

#generate-btn:hover {
  background-color: #2ecc71;
}

#color-palette {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  margin-top: 20px;
}

.color-box {
  width: 150px;
  height: 150px;
  margin: 10px;
  border-radius: 5px;
  box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.2);
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  position: relative;
}

.color-code {
  position: absolute;
  bottom: 5px;
  left: 5px;
  font-size: 1.2rem;
  font-weight: bold;
}

.color-value {
  font-size: 1.2rem;
  font-weight: bold;
}


              
            
!

JS

              
                const numColorsInput = document.querySelector('#num-colors-input');
const colorFormatInput = document.querySelector('#color-format-input');
const colorSchemeInput = document.querySelector('#color-scheme-input');
const generateButton = document.querySelector('#generate-btn');
const colorPalette = document.querySelector('#color-palette');

const getRandomColor = () => {
  const letters = '0123456789ABCDEF';
  let color = '#';
  for (let i = 0; i < 6; i++) {
    color += letters[Math.floor(Math.random() * 16)];
  }
  return color;
};

const getMonochromaticColors = (baseColor) => {
  const lightnessValues = [10, 30, 50, 70, 90];
  return lightnessValues.map(lightness => {
    return `hsl(${baseColor.hue}, ${baseColor.saturation}%, ${lightness}%)`;
  });
};

const getAnalogousColors = (baseColor) => {
  const hueValues = [(baseColor.hue - 30 + 360) % 360, baseColor.hue, (baseColor.hue + 30) % 360];
  return hueValues.map(hue => {
    return `hsl(${hue}, ${baseColor.saturation}%, ${baseColor.lightness}%)`;
  });
};

const getComplementaryColors = (baseColor) => {
  const complementaryHue = (baseColor.hue + 180) % 360;
  return [`hsl(${baseColor.hue}, ${baseColor.saturation}%, ${baseColor.lightness}%)`, 
          `hsl(${complementaryHue}, ${baseColor.saturation}%, ${baseColor.lightness}%)`];
};

const getSplitComplementaryColors = (baseColor) => {
  const splitComplementaryHues = [(baseColor.hue - 150 + 360) % 360, baseColor.hue, (baseColor.hue + 150) % 360];
  return splitComplementaryHues.map(hue => {
    return `hsl(${hue}, ${baseColor.saturation}%, ${baseColor.lightness}%)`;
  });
};

const getTriadicColors = (baseColor) => {
  const triadicHues = [(baseColor.hue - 120 + 360) % 360, baseColor.hue, (baseColor.hue + 120) % 360];
  return triadicHues.map(hue => {
    return `hsl(${hue}, ${baseColor.saturation}%, ${baseColor.lightness}%)`;
  });
};

const getTetradicColors = (baseColor) => {
  const tetradicHues = [(baseColor.hue - 60 + 360) % 360, baseColor.hue, (baseColor.hue + 60) % 360, (baseColor.hue + 180) % 360];
  return tetradicHues.map(hue => {
    return `hsl(${hue}, ${baseColor.saturation}%, ${baseColor.lightness}%)`;
  });
};

const generateColorPalette = () => {
  const numColors = numColorsInput.value;
  const colorFormat = colorFormatInput.value;
  const colorScheme = colorSchemeInput.value;
  let baseColor = null;
  let colors = [];

  switch (colorScheme) {
    case 'random':
      for (let i = 0; i < numColors; i++) {
        const color = getRandomColor();
        colors.push(color);
      }
      break;
    case 'monochromatic':
      baseColor = {
        hue: Math.floor(Math.random() * 361),
        saturation: Math.floor(Math.random() * 101),
        lightness: Math.floor(Math.random() * 101)
      };
      colors = getMonochromaticColors(baseColor);
      break;
    case 'analogous':
      baseColor = {
        hue: Math.floor(Math.random() * 361),
        saturation: Math.floor(Math.random() * 101),
        lightness: Math.floor(Math.random() * 101)
      };
      colors = getAnalogousColors(baseColor);
      break;
    case 'complementary':
      baseColor = {
        hue: Math.floor(Math.random() * 361),
        saturation: Math.floor(Math.random() * 101),
        lightness: Math.floor(Math.random() * 101)
      };
      colors = getComplementaryColors(baseColor);
      break;
    case 'split-complementary':
      baseColor = {
        hue: Math.floor(Math.random() * 361),
        saturation: Math.floor(Math.random() * 101),
        lightness: Math.floor(Math.random() * 101)
      };
      colors = getSplitComplementaryColors(baseColor);
      break;
    case 'triadic':
      baseColor = {
        hue: Math.floor(Math.random() * 361),
        saturation: Math.floor(Math.random() * 101),
        lightness: Math.floor(Math.random() * 101)
      };
      colors = getTriadicColors(baseColor);
      break;
    case 'tetradic':
      baseColor = {
        hue: Math.floor(Math.random() * 361),
        saturation: Math.floor(Math.random() * 101),
        lightness: Math.floor(Math.random() * 101)
      };
      colors = getTetradicColors(baseColor);
      break;
  }
  
  colorPalette.innerHTML = '';
  colors.forEach((color, index) => {
  const colorBox = document.createElement('div');
  colorBox.classList.add('color-box');
  colorBox.style.backgroundColor = color;
  colorBox.addEventListener('click', () => {
    navigator.clipboard.writeText(colors[index]).then(() => {
      console.log('Copied color value to clipboard:', colors[index]);
    });
  });

  const colorValue = document.createElement('div');
  colorValue.classList.add('color-value');
  colorValue.textContent = colors[index];
  colorValue.style.color = isLightColor(color) ? '#000' : '#fff';
  colorBox.appendChild(colorValue);

  colorPalette.appendChild(colorBox);
});


  console.log(colors);
};

generateButton.addEventListener('click', generateColorPalette);

const isLightColor = (color) => {
  const rgbValues = color
    .substring(4, color.length - 1)
    .replace(/ /g, '')
    .split(',')
    .map(Number);

  const [r, g, b] = rgbValues;
  const luminance = 0.299 * r + 0.587 * g + 0.114 * b;

  return luminance > 186; // A threshold value between 0 and 255
};

              
            
!
999px

Console