Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ 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

Save Automatically?

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

              
                <section class="markdown-content">
  <h1>🌈 Random Rainbow 🌈</h1>
  <p>
    Uses <a href="https://randomcolor.lllllllllllllllll.com/">RandomColor</a> and <a href="https://vis4.net/chromajs">Chroma</a> via <a href="https://www.skypack.dev/">Skypack</a> to generate a
    rainbow palette. Some code has been adapted from <a href="https://github.com/jxnblk/palx">Palx</a> and simplified for educational purposes.
  </p>
</section>
<button id="color">🌈 New Random Rainbow!</button>

<div id="rainbow">
  <span id="red" class="color"></span>
  <span id="orange" class="color"></span>
  <span id="yellow" class="color"></span>
  <span id="green" class="color"></span>
  <span id="blue" class="color"></span>
  <span id="pink" class="color"></span>
  <span id="purple" class="color"></span>
</div>

              
            
!

CSS

              
                /* styles */

* {
  box-sizing: border-box;
}

body {
  font-family: "Benton Sans", "Helvetica Neue", helvetica, arial, sans-serif;
  margin: 2em;
}

h1 {
  font-style: italic;
  color: #373fff;
}

.bold {
  font-weight: bold;
}

p {
  max-width: 600px;
}

form {
  margin-bottom: 25px;
  padding: 15px;
  background-color: cyan;
  display: inline-block;
  width: 100%;
  max-width: 340px;
  border-radius: 3px;
}

input {
  display: block;
  margin-bottom: 10px;
  padding: 5px;
  width: 100%;
  border: 1px solid lightgrey;
  border-radius: 3px;
  font-size: 16px;
}

button {
  font-size: 16px;
  border-radius: 3px;
  background-color: lightgrey;
  border: 1px solid grey;
  box-shadow: 2px 2px teal;
  cursor: pointer;
}

button:hover {
  background-color: yellow;
}

button:active {
  box-shadow: none;
}

li {
  margin-bottom: 5px;
}

footer {
  margin-top: 50px;
  padding-top: 25px;
  border-top: 1px solid lightgrey;
}

@media (min-width: 768px) {
  #rainbow {
    display: -ms-flexbox;
    display: -webkit-flex;
    display: flex;
    height: 70vh;

    -webkit-align-items: stretch;
    -ms-flex-align: stretch;
    align-items: stretch;
  }
}

#rainbow .color {
  -webkit-flex: 1;
  -ms-flex: 1;
  flex: 1;
  border: 1px solid black;
  min-height: 100px;
  display: flex;
  align-items: center;
  justify-content: center;
}


#color {
  margin: 1em auto;
}
              
            
!

JS

              
                import randomColor from "https://cdn.skypack.dev/randomcolor";
import chroma from "https://cdn.skypack.dev/chroma-js";

var names = [
  "red", // 0
  "orange", // 30
  "yellow", // 60
  "lime", // 90
  "green", // 120
  "teal", // 150
  "cyan", // 180
  "blue", // 210
  "indigo", // 240
  "violet", // 270
  "fuschia", // 300
  "pink", // 330
  "red"
];
//grab the elements we want to rainbow-ize from the page
const red = document.getElementById("red");
const orange = document.getElementById("orange");
const yellow = document.getElementById("yellow");
const green = document.getElementById("green");
const blue = document.getElementById("blue");
const pink = document.getElementById("pink");
const purple = document.getElementById("purple");

function changeColors() {
  // first we get a base color with a red hue from randomColor
  let seedColor = randomColor();
  // then we use Chroma to analyze it for hue, saturation, and lightness
  const chromaAnalyze = chroma(seedColor).hsl();
  const colorHue = chromaAnalyze[0];
  const colorSat = chromaAnalyze[1];
  const colorLte = chromaAnalyze[2];

  // we use the createHues function from Palx to create some rainbow hues across the color spectrum
  const NewHues = createHues(colorHue);
  console.log(NewHues);
  // We take our new hues and use Chroma to create a color with the required hue but the same saturation and lightness of our original color
  let colors = {};
  NewHues.forEach(function (hue) {
    var newColor = chroma.hsl(hue, colorSat, colorLte);
    colors[hueName(hue)] = chroma(newColor).hex();
  });

  // store our colors so can use them to style the background and put a hex code in the html
  const redColor = colors.red;
  const orangeColor = colors.orange;
  const yellowColor = colors.yellow;
  const greenColor = colors.green;
  const blueColor = colors.blue;
  const pinkColor = colors.indigo;
  const purpleColor = colors.violet;

  // set our rainbow background colors
  red.style.background = redColor;
  orange.style.background = orangeColor;
  yellow.style.background = yellowColor;
  green.style.background = greenColor;
  blue.style.background = blueColor;
  pink.style.background = pinkColor;
  purple.style.background = purpleColor;
  // add the hex code to the rainbow too
  red.innerHTML = redColor;
  orange.innerHTML = orangeColor;
  yellow.innerHTML = yellowColor;
  green.innerHTML = greenColor;
  blue.innerHTML = blueColor;
  pink.innerHTML = pinkColor;
  purple.innerHTML = purpleColor;
}

// run it when we load the page
changeColors();

// tools from palx https://github.com/jxnblk/palx
function createArray(length) {
  var arr = [];
  for (var i = 0; i < length; i++) {
    arr.push(i);
  }
  return arr;
}

function createHues(base) {
  var hueStep = 360 / 12;
  var hues = createArray(12).map(function (n) {
    return Math.floor((base + n * hueStep) % 360);
  });

  return hues;
}

function hueName(h) {
  var i = Math.round((h - 2) / 30);
  console.log(names);
  var name = names[i];
  return name;
}
document.getElementById("color").addEventListener("click", changeColors);
              
            
!
999px

Console