cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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.

Quick-add: + add another resource

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.

Quick-add: + add another resource

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.

            
              <div class="wrapper">
	<div class="hello dark">
		<h2>One</h2>
	</div>
	<div class="hello">
		<h2>Two</h2>
	</div>
	<div class="hello color" id="picker">
		<h2>Three</h2>
		<form>
			<input type="color" id="pick" aria-label="Change Color">
		</form>
	</div>
</div>
<p class="value">Brightness: <span id="valueBrightness"></span></p>
<p class="value" id="oldValue"></p><p class="value" id="newValue"></p>
            
          
!
            
              .wrapper{
	display: flex;
	flex-wrap: wrap;
}
.hello{
	height: 15vh;
	min-height: 60px;
	flex-grow: 1;
	flex-basis: 50%;
	
	display: flex;
	align-items: center;
	justify-content: center;
}

h2{
	font-family: sans-serif;
	margin: 0;
}

.dark{
	color: #FFF;
}
.light{
	color: #000;
}




.hello:nth-child(1){
	background-color: chocolate;
}
.hello:nth-child(2){
	background-color: hotpink;
}

.color{
	background-color: black;
	flex-direction: column;
}

.value{
	display: block;
	margin-top: 20px;
	text-align: center;
	font-size: 2em;
	font-family: sans-serif;
}
.value:nth-of-type(2){
	margin-bottom: 0;
}
.value:nth-of-type(3){
	margin-top: 5px;
}
            
          
!
            
              // From revealJS mostly -> https://github.com/hakimel/reveal.js/blob/master/js/reveal.js

function update(e) {
	picker.style.backgroundColor = e.target.value;

	var computedBackgroundStyle = colorToRgb(e.target.value);
	brightness = Math.floor(colorBrightness(computedBackgroundStyle));
	valueBrightness.innerHTML = brightness;
	
	if (brightness >= 115 && brightness <= 135){
		
		getHslDarken(computedBackgroundStyle);
		picker.style.backgroundColor = getHslDarken(computedBackgroundStyle);
		picker.classList.remove("light");
		picker.classList.add("dark");
		
	} else if (brightness < 115) {
		picker.classList.remove("light");
		picker.classList.add("dark");
		
		oldValue.innerHTML = "";
		newValue.innerHTML = "";
		
	} else if (brightness > 135)  {
		picker.classList.remove("dark");
		picker.classList.add("light");
		
		oldValue.innerHTML = "";
		newValue.innerHTML = "";
	}
}

pick.addEventListener("change", update, false);
pick.addEventListener("input", update, false);


function colorToRgb(color) {
	var hex3 = color.match(/^#([0-9a-f]{3})$/i);
	if (hex3 && hex3[1]) {
		hex3 = hex3[1];
		return {
			r: parseInt(hex3.charAt(0), 16) * 0x11,
			g: parseInt(hex3.charAt(1), 16) * 0x11,
			b: parseInt(hex3.charAt(2), 16) * 0x11
		};
	}

	var hex6 = color.match(/^#([0-9a-f]{6})$/i);
	if (hex6 && hex6[1]) {
		hex6 = hex6[1];
		return {
			r: parseInt(hex6.substr(0, 2), 16),
			g: parseInt(hex6.substr(2, 2), 16),
			b: parseInt(hex6.substr(4, 2), 16)
		};
	}

	var rgb = color.match(/^rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/i);
	if (rgb) {
		return {
			r: parseInt(rgb[1], 10),
			g: parseInt(rgb[2], 10),
			b: parseInt(rgb[3], 10)
		};
	}

	var rgba = color.match(
		/^rgba\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\,\s*([\d]+|[\d]*.[\d]+)\s*\)$/i
	);
	if (rgba) {
		return {
			r: parseInt(rgba[1], 10),
			g: parseInt(rgba[2], 10),
			b: parseInt(rgba[3], 10),
			a: parseFloat(rgba[4])
		};
	}

	return null;
}

function getHslDarken(currentCol){
	return rgbToHsl(currentCol.r, currentCol.g, currentCol.b);
}


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;
  }

	hue = Math.floor(h * 360);
	saturation = Math.floor(s * 100);
	lightness = Math.floor(l * 100 - 20);
	
	// Apply color

  result = "hsl(" + hue + ", " + saturation + "%, " + lightness + "%)";
  old = "hsl(" + hue + ", " + saturation + "%, " + Math.floor(l * 100) + "%)";
	
	oldValue.innerHTML = "Original HSL: " + old;
	newValue.innerHTML = "New HSL: " + result;
	
	return result;
}


function colorBrightness(color) {
	if (typeof color === "string") color = colorToRgb(color);

	if (color) {
		return (color.r * 299 + color.g * 587 + color.b * 114) / 1000;
	}

	return null;
}

            
          
!
999px
Loading ..................

Console