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 class="page">
  <div class="display">
    <div class="colors">
      <div class="color"></div>
    </div>
    <div class="control">
      <input type="color" name="from" value="#07E3F2" />
      <input type="range" name="range" min="3" max="11" />
      <input type="color" name="to" value="#155994" />
    </div>
  </div>
</div>
              
            
!

CSS

              
                html,
body{
  padding:0;
  margin:0;
  background: #141416;
}

.display {
  max-width: 40rem;
  margin: 2rem auto;
  
  .control {
    display: flex;
    justify-content: space-between;
    
    [type=color] {
      width: 3rem;
      height: 3rem;
    }
  }

  .colors {
    display: flex;
    margin: 2rem 0;
    justify-content: space-between;
    
      .color {
      width: 3rem;
      height: 3rem;
      box-shadow: 0 1px 4px rgba(0, 0, 0, 0.4);
    }
  }
}

              
            
!

JS

              
                /*
  
*/
const Gradient = {
	inputA : '',
	inputB : '',
	inputC : '',
	gradientElement : '',
	
	// Convert a hex color to an RGB array e.g. [r,g,b]
	// Accepts the following formats: FFF, FFFFFF, #FFF, #FFFFFF
	hexToRgb : function(hex){
		var r, g, b, parts;
	    // Remove the hash if given
	    hex = hex.replace('#', '');
	    // If invalid code given return white
	    if(hex.length !== 3 && hex.length !== 6){
	        return [255,255,255];
	    }
	    // Double up charaters if only three suplied
	    if(hex.length == 3){
	        hex = hex[0]+hex[0]+hex[1]+hex[1]+hex[2]+hex[2];
	    }
	    // Convert to [r,g,b] array
	    r = parseInt(hex.substr(0, 2), 16);
	    g = parseInt(hex.substr(2, 2), 16);
	    b = parseInt(hex.substr(4, 2), 16);

	    return [r,g,b];
	},
	
	// Converts an RGB color array e.g. [255,255,255] into a hexidecimal color value e.g. 'FFFFFF'
	rgbToHex : function(color){
		// Set boundries of upper 255 and lower 0
		color[0] = (color[0] > 255) ? 255 : (color[0] < 0) ? 0 : color[0];
		color[1] = (color[1] > 255) ? 255 : (color[1] < 0) ? 0 : color[1];
		color[2] = (color[2] > 255) ? 255 : (color[2] < 0) ? 0 : color[2];
		
		return this.zeroFill(color[0].toString(16), 2) + this.zeroFill(color[1].toString(16), 2) + this.zeroFill(color[2].toString(16), 2);
	},
	
	// Pads a number with specified number of leading zeroes
	zeroFill : function( number, width ){
		width -= number.toString().length;
		if ( width > 0 ){
	  		return new Array( width + (/\./.test( number ) ? 2 : 1) ).join( '0' ) + number;
		}
		return number;
	},

	// Generates an array of color values in sequence from 'colorA' to 'colorB' using the specified number of steps
	generate : function(colorA, colorB, steps){
		var result = [], rInterval, gInterval, bInterval;
		
		colorA = this.hexToRgb(colorA); // [r,g,b]
		colorB = this.hexToRgb(colorB); // [r,g,b]
		steps -= 1; // Reduce the steps by one because we're including the first item manually
		
		// Calculate the intervals for each color
		rStep = ( Math.max(colorA[0], colorB[0]) - Math.min(colorA[0], colorB[0]) ) / steps;
		gStep = ( Math.max(colorA[1], colorB[1]) - Math.min(colorA[1], colorB[1]) ) / steps;
		bStep = ( Math.max(colorA[2], colorB[2]) - Math.min(colorA[2], colorB[2]) ) / steps;
	
		result.push( '#'+this.rgbToHex(colorA) );
		
		// Set the starting value as the first color value
		var rVal = colorA[0],
			gVal = colorA[1],
			bVal = colorA[2];
	
		// Loop over the steps-1 because we're includeing the last value manually to ensure it's accurate
		for (var i = 0; i < (steps-1); i++) {
			// If the first value is lower than the last - increment up otherwise increment down
			rVal = (colorA[0] < colorB[0]) ? rVal + Math.round(rStep) : rVal - Math.round(rStep);
			gVal = (colorA[1] < colorB[1]) ? gVal + Math.round(gStep) : gVal - Math.round(gStep);
			bVal = (colorA[2] < colorB[2]) ? bVal + Math.round(bStep) : bVal - Math.round(bStep);
			result.push( '#'+this.rgbToHex([rVal, gVal, bVal]) );
		};
		
		result.push( '#'+this.rgbToHex(colorB) );
		
		return result;
	},
	
	render : function(colorA, colorB, list){
		var list = (typeof list === 'object') ? list : document.querySelector(list);
		
		var listItems = list.children,
			steps  = listItems.length,
			colors = Gradient.generate(colorA, colorB, steps);

		for (var i = 0; i < listItems.length; i++) {
			var item = listItems[i];
			item.style.backgroundColor = colors[i];
		};
	}
}

/*
  UI for demo
*/
const rangeInput = document.querySelector('[name=range]');
const colorInputs = document.querySelectorAll('[type=color]');
const colors = document.querySelector('.colors');
const color = colors.querySelector('.color');

rangeInput.addEventListener('input', render)
colorInputs.forEach(e => e.addEventListener('input', render))

function render() {
  let from = document.querySelector('[name=from]').value
  let to = document.querySelector('[name=to]').value
  let count = rangeInput.value
  
  colors.innerHTML = "";  
  for(let i = 0; i < count; i++) {
    colors.appendChild(color.cloneNode(false))
  }
  
  Gradient.render(from, to, colors);  
}

render();
              
            
!
999px

Console