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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="styles.css">
    <title>Color picker</title>
</head>
<body>
        <div class="colorWrapper">
    <h1>Pick a color:</h1>
    <input type="color" value="#e48100">
    <div class="colorInfo">
        <div class="hexInfo">
            <p><strong>HEX:</strong> <span class="hexValue"></span></p>
        </div>
        <div class="rgbInfo">
            <p><strong>RGB: </strong><span class="redValue"></span> <span class="blueValue"></span> <span class="greenValue"></span></p>
        </div>
        <div class="hslInfo">
            <p><strong>HSL: </strong><span class="hueValue"></span> <span class="saturationValue"></span> <span class="lightnessValue"></span></p>
        </div>
    </div>
    </div>
    <script src="scripts.js"></script>
</body>
</html>
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css?family=Raleway:400,700&display=swap');

h1 {
    font-family: "Raleway", sans-serif;
    font-weight: 700;
}
body {
    text-align: center;
    font-family: "Raleway", sans-serif;
}

/* --- COLOR --- */

.colorInfo {
    display: flex;
    justify-content: center;
}

.hexInfo, .rgbInfo, .hslInfo {
    min-width: 200px;
    margin: 0 20px;
}
              
            
!

JS

              
                "use strict";

const colorPicker = document.querySelector("input");

colorPicker.addEventListener("input", updateFirst);
colorPicker.addEventListener("change", showColorSample);

//updates the color sample as the user is browsing colors in the color picker
function updateFirst(event) {
  document.querySelector("body").style.background = event.target.value;
  //console.log(event.target.value);
}

//updates the color picker and the color sample until new input
function showColorSample(event) {
  document.querySelector("body").style.background = event.target.value;
  document.querySelector(".hexValue").textContent = event.target.value;
  //console.log(event.target.value);
  convertHEXtoRGB();
}

function convertHEXtoRGB() {
  //split the HEX code into substrings
  let hex12 = event.target.value.substring(1, 3);
  console.log(hex12);
  let hex34 = event.target.value.substring(3, 5);
  console.log(hex34);
  let hex56 = event.target.value.substring(5, 7);
  console.log(hex56);
  displayRGB();

  function displayRGB() {
    //using parseInt on substring parts to get RGB values
    document.querySelector(".redValue").textContent = parseInt(hex12, 16);
    document.querySelector(".blueValue").textContent = parseInt(hex34, 16);
    document.querySelector(".greenValue").textContent = parseInt(hex56, 16);
    convertRGBtoHSL();
  }
}

function convertRGBtoHSL() {
  //converting RGB to HSL sponsored by Peter
  let r = 255;
  let g = 255;
  let b = 255;

  let h, s, l;

  const min = Math.min(r, g, b);
  const max = Math.max(r, g, b);

  if (max === min) {
    h = 0;
  } else if (max === r) {
    h = 60 * (0 + (g - b) / (max - min));
  } else if (max === g) {
    h = 60 * (2 + (b - r) / (max - min));
  } else if (max === b) {
    h = 60 * (4 + (r - g) / (max - min));
  }

  if (h < 0) {
    h = h + 360;
  }

  l = (min + max) / 2;

  if (max === 0 || min === 1) {
    s = 0;
  } else {
    s = (max - l) / Math.min(l, 1 - l);
  }
  // multiply s and l by 100 to get the value in percent, rather than [0,1]
  s *= 100;
  l *= 100;

  console.log("hsl(%f,%f%,%f%)", h, s, l); // just for testing
  displayHSL();

  function displayHSL() {
    //putting HSL values into <p>
    document.querySelector(".hueValue").textContent = h;
    document.querySelector(".saturationValue").textContent = s;
    document.querySelector(".lightnessValue").textContent = l;
  }
}

              
            
!
999px

Console