pranayaryal
1/23/2018 - 8:02 PM

gistfile1.js

function averageColors(color1, color2){
  
  var purify1 = manipulateColor(color1);
  var purify2 = manipulateColor(color2);
  
  var onetwo = Math.floor(purify1.onetwo + purify2.onetwo/2);
  
  
  var threefour = Math.floor((purify1.threefour + purify2.threefour)/2 );
  var fivesix = Math.floor((purify1.fivesix + purify2.fivesix)/2);
  
  onetwo = onetwo.toString(16).toUpperCase();
  onetwo = ('0' + onetwo).slice(-2);
  
  
  threefour = threefour.toString(16).toUpperCase();
  
  /* padding with zero if a single digit */
  threefour = ('0' + threefour).slice(-2);
  
  
  fivesix = fivesix.toString(16).toUpperCase();
  fivesix = ('0' + fivesix).slice(-2);
  
  var combined = "#" + onetwo + threefour + fivesix
  
  return combined;
  
  
  
}

function manipulateColor(color){
   var onetwo =  parseInt(color.substr(1,2), 16);
    var threefour = parseInt(color.substr(3,2), 16);
    var fivesix = parseInt(color.substr(5,2), 16);
    
    
  
  return {
    onetwo:  onetwo,
    threefour: threefour,
    fivesix : fivesix
  }
}

averageColors('#0000ff','#012549')