----修改主题功能
This commit is contained in:
88
src/utils/t-color.js
Normal file
88
src/utils/t-color.js
Normal file
@ -0,0 +1,88 @@
|
||||
const HexRegex = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
|
||||
|
||||
|
||||
function TColor(color){
|
||||
this.rgb = TColor.toRgb(color);
|
||||
}
|
||||
|
||||
TColor.prototype = {
|
||||
gradient:function(color,step){
|
||||
var to = TColor.toRgb(color);
|
||||
var sR = (to[0] - this.rgb[0]) / step;//总差值
|
||||
var sG = (to[1] - this.rgb[1]) / step;
|
||||
var sB = (to[2] - this.rgb[2]) / step;
|
||||
|
||||
var res = [];
|
||||
for (var i = 0; i < step; i++) {
|
||||
//计算每一步的hex值
|
||||
var hex = TColor.rgbHex('rgb('+ parseInt((sR * i + this.rgb[0]))+ ',' +
|
||||
parseInt((sG * i + this.rgb[1]))+ ',' + parseInt((sB * i + this.rgb[2])) + ')');
|
||||
res.push(hex);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
TColor.valueOf = function(color){
|
||||
return new TColor(color);
|
||||
}
|
||||
|
||||
TColor.rgbHex = function(rgb){
|
||||
var _this = rgb;
|
||||
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 (HexRegex.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;
|
||||
}
|
||||
}
|
||||
|
||||
TColor.toRgb= function(sColor){
|
||||
var sColor = sColor.toLowerCase();
|
||||
if (sColor && HexRegex.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;
|
||||
}
|
||||
}
|
||||
|
||||
if(window!=undefined){
|
||||
window.TColor = TColor;
|
||||
}
|
||||
export default TColor;
|
||||
Reference in New Issue
Block a user