2013-10-31 64 views
0

我有一個問題,希望有人能幫助我,嗨,應用標準平均顏色背景梯度

我申請的平均顏色,新的div塊的JSFiddle here, 所以我知道它是工作時,我有把它們應用到漸變背景,雖然麻煩了,我覺得我有認真做事搞砸了,here is the repo

的問題definetly在這個代碼部分不知何故,我建議克隆出來進行審查

var isWebkit = 'webkitRequestAnimationFrame' in window; 
    var values = $.makeArray($('.value')); 

    for(var i = 0; i < sigma; i++) 
    { 
     var newColor = [ 
      Math.floor(minColor[0]+maxIncrements[0]*i), 
      Math.floor(minColor[1]+maxIncrements[1]*i), 
      Math.floor(minColor[2]+maxIncrements[2]*i) 
     ]; 

     var hex = this.toHex(newColor[0], newColor[1], newColor[2]); 

     (isWebkit) ? $(values[i]).css('background', '-webkit-gradient(linear, left top, left bottom, from(#'+hex+'), to(#000));') 
        : $(values[i]).css('background', '-moz-linear-gradient(top, #'+hex+', #000);'); 
    } 

    for(var i = 1; i < sigma+1; i++) 
    { 
     var newColor = [ 
      Math.min(255,Math.floor(maxColor[0]+minIncrements[0]*i)), 
      Math.min(255,Math.floor(maxColor[1]+minIncrements[1]*i)), 
      Math.min(255,Math.floor(maxColor[2]+minIncrements[2]*i)) 
     ]; 

     var hex = this.toHex(newColor[0], newColor[1], newColor[2]); 

     var c = (sigma+i); 

     if (c <= values.length) // prevent overlap if we have an odd sigma 
     { 
      (isWebkit) ? $(values[c]).css('background', '-webkit-gradient(linear, left top, left bottom, from(#'+hex+'), to(#000));') 
         : $(values[c]).css('background', '-moz-linear-gradient(top, #'+hex+', #000);'); 

     } 
    } 

編輯

它看起來像在我的版本相比,我的小提琴我不是迭代,並始終以000000一個十六進制結束了???

+1

你需要從(「+十六進制+」)將在「#」像這樣從(#'+十六進制+') –

+0

沒有解決它,但非常好的一點,我只是補充說,加一個捕捉它 – ehime

回答

1

的問題是,它試圖解析一個HASH當哈希不佔

的修復

/** hex parsers */ 
    (
     function(a) 
     { 
      a["toRGB"] = function(a) 
      { 
       var b = parseInt(a.replace('#', ''), 16); //drop our hash if it exists 
       return[b>>16,b>>8&255,b&255] 
      }; 

      a["toHex"] = function(a,b,c) 
      { 
       return'#'+(c|b<<8|a<<16|1<<24).toString(16).slice(1) // re-add our hash 
      } 
     })(this);