css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation


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.

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.

              <aside class="sliders">
  <input type="range" id="red" min="0" max="255" value="200" step="1"> 
  <input type="range" id="green" min="0" max="255" value="60" step="1"> 
  <input type="range" id="blue" min="0" max="255" value="255" step="1">

<main class="buttons">  
  <h2>sRBG Luma method</h2>
  <button class="btn">Primary Color</button> 
  <button class="btn btn--secondary">Secondary Color</button> 
  <h2>W3C Luma method</h2>
  <button class="btn btn--w3c">Primary Color</button> 
  <button class="btn btn--w3c btn--secondary">Secondary Color</button>
The challenge: 
1) Set text to either black or white depending on the element background perceived lightness (luma)
2) Set a border as a darker variation of the base color to improve button visibility, ONLY if background luma is really high
3) Automatically generate a secondary, 60º rotated hue color

:root {
  /* theme color variables to use in RGB declarations */
  --red: 200;
  --green: 60;
  --blue: 255;
  /*the threshold at which colors are considered "light". 
Range: decimals from 0 to 1,
recommended 0.5 - 0.6*/
  --threshold: 0.5;
  /*the threshold at which a darker border will be applied.
Range: decimals from 0 to 1,
recommended 0.8+*/
  --border-threshold: 0.8;

.btn {
/*sets the background for the base class*/
	background: rgb(var(--red), var(--green), var(--blue));

Calcs perceived brightness using the 
sRGB Luma method
lightness = (red * 0.2126 + green * 0.7152 + blue * 0.0722) / 255
  --r: calc(var(--red) * 0.2126);
  --g: calc(var(--green) * 0.7152);
  --b: calc(var(--blue) * 0.0722);
  --sum: calc(var(--r) + var(--g) + var(--b));
  --perceived-lightness: calc(var(--sum) / 255);
1) Any lightness value above the threshold will be considered "light", therefore apply a black text color. Any bellow will be considered dark, and use white color. 
This results from appying either a sub-zero (negative) or a higher-than-100 lightness value, which are capped to 0 and 100 respectively, to a HSL declaration
  color: hsl(0, 0%, calc((var(--perceived-lightness) - var(--threshold)) * -10000000%));
2) sets the border as a 50% darker shade of the base color, ONLY if background color luma is higher than the border threshold.
To achieve this I use the same sub-zero or higher-than-max technique, only this time using the Alpha value from an RGBA declaration. 
This results in a border that's either fully transparent or fully opaque
 --border-alpha: calc((var(--perceived-lightness) - var(--border-threshold)) * 100);
  border-width: .2em;
  border-style: solid;
  border-color: rgba(calc(var(--red) - 50), calc(var(--green) - 50), calc(var(--blue) - 50), var(--border-alpha));

/* Alternative calc using the 
W3C luma method
lightness = (red * 0.299 + green * 0.587 + blue * 0.114) / 255
  --r: calc(var(--red) * 0.299);
  --g: calc(var(--green) * 0.587);
  --b: calc(var(--blue) * 0.114);

  /* 3) sets the background color as a 60º rotated hue */

/* just basic styles*/
  background: honeydew;
  max-width: 600px;
  margin: auto;

  grid-template-columns: repeat(auto-fit, minmax(150px,1fr));

  grid-template-columns: repeat(auto-fit, minmax(200px,1fr));
  grid-gap: 1em;

.buttons h2{
  grid-column: 1 / -1;

  padding: 1em;
  font-size: 1.5rem; 
  border-radius: 0.2em;
  box-sizing: border-box;

  color: black;
  margin: 10px;

  content: attr(id);
  text-transform: capitalize;

  counter-reset: red var(--red);
  content: counter(red);

  counter-reset: green var(--green);
  content: counter(green);

  counter-reset: blue var(--blue);
  content: counter(blue);
  JS is used only to set the CSS custom properties used in the base color
  Color changes are calculated in CSS 

const root = document.documentElement;
const inputs = [].slice.call(document.querySelectorAll('input'));

inputs.forEach(input => input.addEventListener('change', handleUpdate));
inputs.forEach(input => input.addEventListener('mousemove', handleUpdate));

function handleUpdate(e) {
  if (this.id === 'red') root.style.setProperty('--red', this.value);
  if (this.id === 'green') root.style.setProperty('--green', this.value);
  if (this.id === 'blue') root.style.setProperty('--blue', this.value);
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................