html顏色有幾種表示方式:
英文單詞顏色值:background-color:Blue;
十六進制顏色值:background-color:#FFFFFF;
RGB顏色值三元數字:background-color:rgb(255,255,255)
RGB顏色值三元百分比:background-color:rgb(100%,100%,100%)
本文講述輸入兩個十六進制顏色,輸出rgb漸變數組的算法。
html部分:
<body> <form id="form1" runat="server"> <div id ="test_color"> </div> </form> </body>
JavaScript部分:
漸變色處理函數思路:
已知:RStart=50,REnd=200,RStart、REnd之間平均分成3份(Step=3),求每份的數值(StepN)分別是多少。
公式:Gradient = RStart+ (REnd-RStart) / Step * N (第N步的顏色rgb中R的值)
實現方法非常簡單,只是需要將顏色從rgb到hex的互轉。
輸入:
startColor: 十六進制開始顏色
endColor: 十六進制結束顏色
step: 漸變分的步數
輸出:漸變顏色rgb數組
function gradientColor(startColor, endColor, step) { startRGB = this.colorRgb(startColor);//轉換為rgb數組模式 startR = startRGB[0]; startG = startRGB[1]; startB = startRGB[2]; endRGB = this.colorRgb(endColor); endR = endRGB[0]; endG = endRGB[1]; endB = endRGB[2]; sR = (endR - startR) / step;//總差值 sG = (endG - startG) / step; sB = (endB - startB) / step; var colorArr = []; for (var i = 0; i < step; i++) { //計算每一步的hex值 var hex = this.colorHex('rgb('+ parseInt((sR * i + startR))+ ',' + parseInt((sG * i + startG))+ ',' + parseInt((sB * i + startB)) + ')'); colorArr.push(hex); } return colorArr; }
漸變色函數屬性:

// 將hex表示方式轉換為rgb表示方式(這里返回rgb數組模式) gradientColor.prototype.colorRgb = function (sColor) { var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/; var sColor = sColor.toLowerCase(); if (sColor && reg.test(sColor)) { if (sColor.length === 4) { var sColorNew = "#"; for (var i = 1; i < 4; i += 1) { sColorNew += sColor.slice(i, i + 1).concat(sColor.slice(i, i + 1)); } sColor = sColorNew; } //處理六位的顏色值 var sColorChange = []; for (var i = 1; i < 7; i += 2) { sColorChange.push(parseInt("0x" + sColor.slice(i, i + 2))); } return sColorChange; } else { return sColor; } }; // 將rgb表示方式轉換為hex表示方式 gradientColor.prototype.colorHex = function (rgb) { var _this = rgb; var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/; if (/^(rgb|RGB)/.test(_this)) { var aColor = _this.replace(/(?:(|)|rgb|RGB)*/g, "").split(","); var strHex = "#"; for (var i = 0; i < aColor.length; i++) { var hex = Number(aColor[i]).toString(16); hex = hex < 10 ? 0 + '' + hex : hex;// 保證每個rgb的值為2位 if (hex === "0") { hex += hex; } strHex += hex; } if (strHex.length !== 7) { strHex = _this; } return strHex; } else if (reg.test(_this)) { var aNum = _this.replace(/#/, "").split(""); if (aNum.length === 6) { return _this; } else if (aNum.length === 3) { var numHex = "#"; for (var i = 0; i < aNum.length; i += 1) { numHex += (aNum[i] + aNum[i]); } return numHex; } } else { return _this; } }
頁面加載腳本,生成div,輸入開始顏色,結束顏色,生成漸變色數組,賦值為背景色,
window.onload = function(){ var gradient = new gradientColor('#FF0000', '#00FF00', 101); console.log(gradient); //alert(gradient); for(var i= 0,j= 0;i<=100;i++){ var div = document.createElement("div"); div.style.position = "absolute"; div.style.width = "50px"; div.style.height = "50px"; div.style.left = ((i%10)*50+10)+"px"; div.style.border = "1px solid #ddd"; div.style.backgroundColor = gradient[i]; div.innerText = i; if(i%10==0){ ++j; } div.style.top = ((j-1)*50+10)+"px"; document.body.appendChild(div); } }
生成的結果如下圖:
從紅色漸變為綠色:
參考: http://yanue.net/post-80.html
版權聲明:本文為博主原創文章,轉載請注明出處。http://www.cnblogs.com/SweetMemory/p/6274388.html