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 id="time-container">
  <h2>Time as Color</h2>
  <p class="description">See any time as a color or auto-localize to your current time. To learn more about this project, check out the details.</p>
  <div id="settings">
    <input id="military-time-toggle" type="checkbox"/>
    <label for="military-time-toggle">Military Time</label>
    <div id="time-holder">
      <select id="hours"></select>
      <select id="minutes"></select>
      <select id="seconds"></select>
      <select id="am-pm" class="visible">
        <option id="time-of-day-am">AM</option>
        <option id="time-of-day-pm">PM</option>
      </select>
    </div>
    <button id="use-my-time">Use My Current Time</button>
  </div>
</div>
              
            
!

CSS

              
                :root {
  --background-color: #000000;
}

html, body {
  margin: 0;
  height: 100%;
}

body {
  background-color: var(--background-color);
  transition: 0.2s;
}

#time-container {
  background-color: white;
  padding: 1.25rem;
  border-radius: 6px;
  position: absolute;
  max-width: 40vw;
  top: 50%;
  left: 50%;
  transform: translate3d(-50%, -50%, 0);
  h2 {
    color: white;
    margin: 0 auto 1rem;
    $text-shadow: 1px 1px var(--background-color);
    @for $i from 2 to 10 {
      $text-shadow: $text-shadow, #{$i}px #{$i}px var(--background-color);
    }
    text-shadow: $text-shadow;
  }
}

.description, label {
  font-family: sans-serif;
  font-size: 0.9rem;
  letter-spacing: 0.2px;
  line-height: 1.2rem;
}

[type="checkbox"] {
  display: none;
  + label {
    margin-bottom: 0.5rem;
    display: inline-block;
    &::before {
      content: '';
      display: inline-block;
      width: 0.6rem;
      height: 0.6rem;
      border-radius: 2px;
      border: 1px solid var(--background-color);
      margin-right: 0.4rem;
      transition: 0.2s;
    }
    &:hover {
      cursor: pointer;
    }
  }
  &:checked + label::before {
    background-color: var(--background-color);
    box-shadow: 0 0 0 1px white inset;
  }
}

#am-pm {
  display: none;
  &.visible {
    display: initial;
  }
}

button {
  border: 0.5px solid var(--background-color);
  color: var(--background-color);
  padding: 0.4rem;
  font-size: 0.9rem;
  transition: 0.1s;
  margin-top: 1rem;
  &:hover {
    cursor: pointer;
    background-color: var(--background-color);
    color: white;
  }
}
              
            
!

JS

              
                function recursivelyAddOption(start, amt, cont){
  var container = document.getElementById(cont);
  container.innerHTML = '';
  for(var i = start; i <= amt; i++){
    var opt = document.createElement('option');
    opt.id = cont + '-' + i;
    opt.textContent = i.toString().length === 1 && cont !== 'hours' ? '0' + i.toString() : i;
    container.appendChild(opt);
  }
}

function getMaximumValue(id){
  var opts = document.getElementById(id).querySelectorAll('option');
  var select_vals = Array.prototype.map.call(opts, function(elm){
    return parseInt(elm.value);
  });
  return select_vals.sort(function(a, b){ return a - b })[select_vals.length - 1];
}

function convertTimeToRGB(value, id){
  return value * 255 / getMaximumValue(id);
}

window.onload = function(){
  recursivelyAddOption(1, 12, 'hours');
  recursivelyAddOption(0, 59, 'minutes');
  recursivelyAddOption(0, 59, 'seconds');
  var select_elms = document.querySelectorAll('select');
  document.getElementById('military-time-toggle').addEventListener('change', function(ev){
    var am_pm_elm = document.getElementById('am-pm');
    if(!ev.target.checked){
      recursivelyAddOption(1, 12, 'hours');
      am_pm_elm.className = 'visible';
    } else {
      recursivelyAddOption(0, 23, 'hours');
      am_pm_elm.className = '';
    }
  });
  Array.prototype.forEach.call(select_elms, function(elm){
    elm.addEventListener('change', function(ev){
      var hrs = parseInt(document.getElementById('hours').value);
      if(!document.getElementById('military-time-toggle').checked){
        console.log(document.getElementById('am-pm').value);
        hrs += document.getElementById('am-pm').value === 'PM' ? 12 : 0;
      } 
      var mins = parseInt(document.getElementById('minutes').value);
      var secs = parseInt(document.getElementById('seconds').value);
      document.documentElement.style.setProperty('--background-color', 'rgb(' + convertTimeToRGB(hrs, 'hours') + ',' + convertTimeToRGB(mins, 'minutes') + ',' + convertTimeToRGB(secs, 'seconds') + ')');
    });
  });
  
  document.getElementById('use-my-time').addEventListener('click', function(){
    var time = new Date();
    var hrs = time.getHours();
    if(!document.getElementById('military-time-toggle').checked){
      var am_pm = hrs > 12 ? 'PM' : 'AM';
      hrs -= hrs > 12 ? 12 : 0;
      document.getElementById('am-pm').value = am_pm;
    }
    var mins = time.getMinutes();
    var secs = time.getSeconds();
    document.getElementById('hours').value = hrs;
    document.getElementById('minutes').value = mins.toString().length === 1 ? '0' + mins.toString() : mins;
    document.getElementById('seconds').value = secs.toString().length === 1 ? '0' + secs.toString() : secs;
    document.documentElement.style.setProperty('--background-color', 'rgb(' + convertTimeToRGB(hrs, 'hours') + ',' + convertTimeToRGB(mins, 'minutes') + ',' + convertTimeToRGB(secs, 'seconds') + ')');
  });
}
              
            
!
999px

Console