Average 2 hex colors together in javascript

后端 未结 8 2185
天命终不由人
天命终不由人 2021-01-31 20:13

Alright thought I would throw this one out there for the crowd to think over.

Given a function (written in javascript) that expects two strings

8条回答
  •  没有蜡笔的小新
    2021-01-31 20:20

    Only requires a few lines of POJS if you don't want to bother with lots of unnecessary stuff:

    // Expects input as 'nnnnnn' where each nn is a 
    // 2 character hex number for an RGB color value
    // e.g. #3f33c6
    // Returns the average as a hex number without leading #
    var averageRGB = (function () {
    
      // Keep helper stuff in closures
      var reSegment = /[\da-z]{2}/gi;
    
      // If speed matters, put these in for loop below
      function dec2hex(v) {return v.toString(16);}
      function hex2dec(v) {return parseInt(v,16);}
    
      return function (c1, c2) {
    
        // Split into parts
        var b1 = c1.match(reSegment);
        var b2 = c2.match(reSegment);
        var t, c = [];
    
        // Average each set of hex numbers going via dec
        // always rounds down
        for (var i=b1.length; i;) {
          t = dec2hex( (hex2dec(b1[--i]) + hex2dec(b2[i])) >> 1 );
    
          // Add leading zero if only one character
          c[i] = t.length == 2? '' + t : '0' + t; 
        }
        return  c.join('');
      }
    }());
    

提交回复
热议问题