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.

Editor Settings

Code Indentation

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

Visit your global Editor Settings.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

HTML

              
                #outerCont
  #topVals<
    rgb(
    %span{:class => "rgb", :"data-color" => 0}>=128
    ,
    %span{:class => "rgb", :"data-color" => 1}>=129
    ,
    %span{:class => "rgb", :"data-color" => 2}>=128
    )
    %br
    hsl(
    %span{:class => "hsl", :"data-color" => 0}>=180
    ,
    %span{:class => "hsl", :"data-color" => 1}>=50
    ,
    %span{:class => "hsl", :"data-color" => 2}>=50
    )
  #mainCont
    #rgbCont
      %label="R"
      %input{:class => "rgb", :value => 128, :type => "range", :"data-color" => 0, :min => 0, :max => 255}
      %val="128"
      
      %label="G"
      %input{:class => "rgb", :value => 128, :type => "range", :"data-color" => 1, :min => 0, :max => 255}
      %val="128"
      
      %label="B"
      %input{:class => "rgb", :value => 128, :type => "range", :"data-color" => 2, :min => 0, :max => 255}
      %val="128"

    #hslCont
      %label="H"
      %input{:class => "hsl", :type => "range", :"data-color" => 0, :value => 180, :min => 0, :max => 360}
      %val="180"

      %label="S"
      %input{:class => "hsl", :type => "range", :"data-color" => 1, :value => 50, :min => 0, :max => 100}
      %val="50"
      
      %label="L"
      %input{:class => "hsl", :type => "range", :"data-color" => 2, :value => 50, :min => 0, :max => 100}
      %val="50"
              
            
!

CSS

              
                body, html{
  height: 100%
}

body{
  margin: 0;
  background: gray;
  text-align: center;
  font: 1em Arial;
  display: flex;
  align-items: center;
}

#outerCont{
  display: flex;
  flex-flow: column;
  flex: 1 1 auto;
  margin: 50px 0;
}

#topVals{
  font-size: 2em;
  align-self: center;
}

#mainCont{
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: space-around;
}

#rgbCont, #hslCont{
  display: grid;
  grid-template-columns: .5fr 3fr .8fr;
  grid-template-rows: 1fr 1fr 1fr;
  grid-gap: 6px;
  margin-top: 60px;
  padding: 0 50px;
}

#topVals, label, val{
  background: white;
  padding: .1em .2em;
}

@media screen and (max-height: 315px){
  body{
    height: auto;
  }
}

              
            
!

JS

              
                const TYPE_ARR = ["rgb", "hsl"];
var curArr, oppArr, curType;

document.querySelectorAll("input[type=range]").forEach(function(el){
    el.onchange = function(){
        curType = el.className == "rgb"?TYPE_ARR[0]:TYPE_ARR[1];
        curArr = setCurArr(curType);   
        oppArr = setOppArr(curType);  
        updateHtml();
        setBodyColor();
    }
});

function setCurArr(type){
  valArr = [];
  for(var i=0; i<3; i++){
    valArr.push(parseInt(document.querySelector('input.' + type + '[data-color="' + i + '"]').value));  
  }
  return valArr;
}

function setOppArr(curType){
  valArr = [];
  if(curType == "hsl"){
    valArr = hslToRgb(curArr[0], curArr[1], curArr[2]); 
  }else{
    valArr = rgbToHsl(curArr[0], curArr[1], curArr[2]); 
  }
  return valArr;
}

function updateHtml(){
  TYPE_ARR.forEach(function(type){
      valArr = type == curType?curArr:oppArr;
  
      for(i = 0; i < 3; i++){
          input = document.querySelector('input.' + type + '[data-color="' + i + '"]');
          input.value  = valArr[i];
          input.nextElementSibling.innerHTML = valArr[i]; 
          
          span = document.querySelector('span.' + type + '[data-color="' + i + '"]');
          span.innerHTML = valArr[i];
      }  
  });
}

function setBodyColor(){
  var valArr = curType == "rgb"?curArr:oppArr;
  var ruleVal = 'rgb(' + valArr[0] + ',' + valArr[1] + ',' + valArr[2]  + ')'; 
  document.body.style.background = ruleVal;
}

/**
 * Converts an HSL color value to RGB. Conversion formula
 * adapted from http://en.wikipedia.org/wiki/HSL_color_space.
 * Assumes h is contained in the set [0, 360]
 * and s and l are contained in the set [0, 100].
 * Returns r, g, and b in the set [0, 255].
 *
 * @param   {number}  h       The hue
 * @param   {number}  s       The saturation
 * @param   {number}  l       The lightness
 * @return  {Array}           The RGB representation
 */
function hslToRgb(h, s, l){
  
    var r, g, b;
    h = h / 360;
    s = s / 100; 
    l = l / 100;
    
    if(s == 0){
        r = g = b = l; // achromatic
    }else{
        var hue2rgb = function hue2rgb(p, q, t){
            if(t < 0) t += 1;
            if(t > 1) t -= 1;
            if(t < 1/6) return p + (q - p) * 6 * t;
            if(t < 1/2) return q;
            if(t < 2/3) return p + (q - p) * (2/3 - t) * 6;
            return p;
        }

        var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
        var p = 2 * l - q;
        r = hue2rgb(p, q, h + 1/3);
        g = hue2rgb(p, q, h);
        b = hue2rgb(p, q, h - 1/3);
    }

    return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
}

/**
 * Converts an RGB color value to HSL. Conversion formula
 * adapted from http://en.wikipedia.org/wiki/HSL_color_space.
 * Assumes r, g, and b are contained in the set [0, 255] and
 * returns h in the set [0, 360]
 * and s and l in the set [0, 100].
 *
 * @param   {number}  r       The red color value
 * @param   {number}  g       The green color value
 * @param   {number}  b       The blue color value
 * @return  {Array}           The HSL representation
 */
function rgbToHsl(r, g, b){
    r /= 255;
    g /= 255;
    b /= 255;
    var max = Math.max(r, g, b), min = Math.min(r, g, b);
    var h, s, l = (max + min) / 2;

    if(max == min){
        h = s = 0; // achromatic
    }else{
        var d = max - min;
        s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
        switch(max){
            case r: h = (g - b) / d + (g < b ? 6 : 0); break;
            case g: h = (b - r) / d + 2; break;
            case b: h = (r - g) / d + 4; break;
        }
        h /= 6;
    }

    return [Math.round(h * 360), Math.round(s * 100), Math.round(l * 100)];
}
              
            
!
999px

Console