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.

            
              <section id="main-form" class="main-form">

	<div id="spectrum-box"><span id="pointer"></span><div id="spectrum"></div></div>

<div id="satbar-box"><span id="pointer2"></span><div id="satbar"></div></div>

<div id="output-preview"></div>

<p>HSL :
	<input id="output-h" type="text" class="text" value="" />
	<input id="output-s" type="text" class="text" value="" />
	<input id="output-l" type="text" class="text" value="" />

	<input id="output-hsl" type="text" class="text" value="" />
</p>
<p>RGB :
	<input id="output-r" type="text" class="text" value="" />
	<input id="output-g" type="text" class="text" value="" />
	<input id="output-b" type="text" class="text" value="" />

	<input id="output-rgb" type="text" class="text" value="" />
</p>
<p>Hexadécimal :
	<input id="output-hex" type="text" class="text" value="" onchange="setColor(this)" />
</p>
</section>

            
          
!
            
              :root {
	--saturation: 100%;
}

.main-form {
	text-align: center;
}

/* =====================
   MAIN PICKER (big square) */
#spectrum,
#satbar,
#output-preview {
	border: 1px solid black;
	width: 360px;
	margin: 0 auto;
}
#spectrum-box {
	height: 360px;
	width: 360px;
	margin: 0 auto;
	position: relative;
}
#spectrum {
	width: 100%;
	height: 100%;
	cursor: crosshair;
  /* superposing two gradients : one with the hue (colors) scale from red (left) to red (right) AND the second on top of that from white (top) to transparent (middle) to black (bottom).
	All the colors except the greys are no displayed :). The greys are shown with the second bar. */
	background: linear-gradient(to bottom, hsla(0, 100%, 100%, 1), hsla(0, 0%, 0%, 0), hsla(0, 0%, 0%, 1) 100%),
				linear-gradient(to right, hsl(0,var(--saturation), 50%), hsl(60,var(--saturation), 50%), hsl(120,var(--saturation), 50%), hsl(180, var(--saturation), 50%), hsl(240,var(--saturation),50%),hsl(300,var(--saturation),50%), hsl(360,var(--saturation),50%) 100%);
}

#pointer {
	display: inline-block;
	border: 1px solid black;
	background: transparent;
	border-radius: 50%;
	height: 4px;
	width: 4px;
	position: absolute;
	top: 50%;
	left: 50%;
	margin-left: -4px;
	margin-top: -4px;
	cursor: crosshair;
	box-shadow: 0 0 0 1px white;
}

/* =====================
   SECONDARY BAR (saturation) */

#satbar-box {
	height: 30px;
	width: 350px;
	margin: 10px auto;
	position: relative;
}

#satbar {
	height: 100%;
	width: 100%;
	background: linear-gradient(to right, hsl(0, 0%, 100%), hsl(0, 0%, 0%) 100%);
}

#pointer2 {
	display: inline-block;
	background: linear-gradient(to right, rgba(255,255,255,0) 2px, rgba(0,0,0,1) 2px, rgba(0,0,0,1) 4px, rgba(255,255,255,0) 4px);
	height: 30px;
	width: 6px;
	position: absolute;
	top: 1px;
	left: 0%;
	cursor: col-resize;
}
#pointer2::before {
	content: '';
	width: 0; 
	height: 0; 
	border-left: 5px solid transparent;
	border-right: 5px solid transparent;
	border-top: 5px solid black;
	position: absolute;
	top: -5px;
	left: -3px;

}

#pointer2::after {
	content: '';
	width: 0; 
	height: 0; 
	border-left: 5px solid transparent;
	border-right: 5px solid transparent;
	border-bottom: 5px solid black;
  	position: absolute;
  	bottom: -5px;
  	left: -3px;

}

#output-preview {
	border: 20px solid white;
	height: 70px;
	width: 120px;
	margin: 20px auto;
	background: hsl(150, var(--saturation), 50%);
}


#output-h, #output-s, #output-l,
#output-r, #output-g, #output-b {
	width: 30px;
}
#output-hsl, #output-rgb {
	width: 150px;
}
#output-hex {
	width: 100px;
}

            
          
!
            
              
'use strict'

// MAIN COORDS / COLOR VARS
var color = {H: null, S: 100, L: null, R: null, G: null, B: null, HEX: null};

var picker = document.getElementById('spectrum');
pickerSizes = new Object();
var pickerSizes = {
	w: Math.round(picker.getBoundingClientRect().right - picker.getBoundingClientRect().left),
	h: Math.round(picker.getBoundingClientRect().bottom - picker.getBoundingClientRect().top)
};
var pointer = document.getElementById('pointer');
var outputPreview = document.getElementById('output-preview');

// mouse moves while clicked : update color + hide pointer.
picker.addEventListener("mousemove", function(e) {
	// only when left mouse is clicked :
	if (e.buttons === 1 || e.type == 'mouseup') {
		getColor(e);
	}

});

// mouse clicks : update color + moves pointer
picker.addEventListener("mouseup", function(e){
	// only when left mouse is clicked :
	if (e.buttons === 1 || e.type == 'mouseup') {
		getColor(e);
	}
	movePointer(e)
	pointer.style.display = 'inline-block';
});
picker.addEventListener("mousedown", function(e) {
	pointer.style.display = 'none';
});
picker.addEventListener("mouseout", function(e) {
	if (e.buttons === 1) {
		pointer.style.display = 'inline-block';
		movePointer(e);
	}
});

picker.addEventListener("mouseover", function(e) {
	if (e.buttons === 1) {
		pointer.style.display = 'none';
	}
});

// Get color from manual select (on the picker)
function getColor(e) {
	// DETECT CLICKED POSITION INSIDE BOX
	inBoxCoords = new Object();
	var inBoxCoords = {
		x: e.clientX - picker.getBoundingClientRect().left,
		y: e.clientY - picker.getBoundingClientRect().top
	};

	// HSV
	// get hue from x position
	var hue = inBoxCoords.x * 360 / pickerSizes.w;
	color.H = hue;

	// get saturation from global variable.
	var sat = color.S;

	// get luminance from y position
	var lum =  100 - inBoxCoords.y * 100 / pickerSizes.w;
	color.L = lum;

	updateOutputs();
}



function movePointer(e) {
	InBoxCoords = new Object();
	var InBoxCoords = {
		x: e.clientX - picker.getBoundingClientRect().left,
		y: e.clientY - picker.getBoundingClientRect().top
	};

	pointer.style.left = Math.min(pickerSizes.w , Math.max(1, InBoxCoords.x))+'px';
	pointer.style.top = Math.min(pickerSizes.h, Math.max(1, InBoxCoords.y))+'px';
}


// Saturation bar
var satbar = document.getElementById('satbar');
var pointer2 = document.getElementById('pointer2');

function chooseSat(e) {
	// only when left mouse is clicked :
	if (e.buttons === 1 || e.type == 'mouseup') {

		// DETECT CLICKED POSITION INSIDE BOX
		inBoxCoords = new Object();
		var inBoxCoords = {
			x: e.clientX - satbar.getBoundingClientRect().left,
			y: e.clientY - satbar.getBoundingClientRect().top
		};

		var sat = 100 - (inBoxCoords.x * 100 / pickerSizes.w);

		// MOVE CURSOR
		InBoxCoords = new Object();
		var InBoxCoords = {
			x: e.clientX - satbar.getBoundingClientRect().left,
			y: e.clientY - satbar.getBoundingClientRect().top
		};

		pointer2.style.left = Math.min(pickerSizes.w , Math.max(1, InBoxCoords.x))+'px';

		color.S = sat;

		updateOutputs();
	}
}


satbar.addEventListener("mousemove", chooseSat);
satbar.addEventListener("mouseup", function(e){
	chooseSat(e);
});



function updateOutputs() {
	// update in $var
	hslToRgb(color);
	rgbToHex(color);

	// update in CSS
	document.documentElement.style.setProperty('--saturation', color.S+'%');

	// preview
	outputPreview.style.background = 'hsl('+Math.round(color.H)+', '+Math.round(color.S)+'%, '+Math.round(color.L)+'%)';


	document.getElementById('output-h').value = Math.round(color.H);
	document.getElementById('output-s').value = Math.round(color.S);
	document.getElementById('output-l').value = Math.round(color.L);
	document.getElementById('output-hsl').value = 'hsl('+Math.round(color.H)+','+Math.round(color.S)+'%,'+Math.round(color.L)+'%)';

	document.getElementById('output-r').value = Math.round(color.R);
	document.getElementById('output-g').value = Math.round(color.G);
	document.getElementById('output-b').value = Math.round(color.B);
	document.getElementById('output-rgb').value = 'rgb('+Math.round(color.R)+','+Math.round(color.G)+','+Math.round(color.B)+')';

	document.getElementById('output-hex').value = color.HEX;
}


function rgbToHsl(color) {

	var r = color.R / 255;
	var g = color.G / 255;
	var b = color.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;
    }

    color.H = Math.round(h*360);
    color.S = Math.round(s*100);
    color.L = Math.round(l*100);
}

function rgbToHex(color) {
	var r = color.R;
	var g = color.G;
	var b = color.B;

    color.HEX =  "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1,7);
}
function hexToRgb(color) {
	color.R = parseInt((color.HEX).substr(1,2),16);
	color.G = parseInt((color.HEX).substr(3,2),16);
	color.B = parseInt((color.HEX).substr(5,2),16);    
}

function hslToRgb(color){
	var h = color.H/360;
	var s = color.S/100;
	var l = color.L/100;

    var r, g, b;

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

    color.R = Math.round(r * 255);
    color.G = Math.round(g * 255);
    color.B = Math.round(b * 255);
}


            
          
!
999px
🕑 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 ..................

Console