1. 剃度颜色计算
公式:
r = start_r + 是否反转 * (end_r - start_r) / step * N;
g = start_g + 是否反转 * (end_g- start_g) / step * N;
b = start_b + 是否反转 * (end_b - start_b) / step * N;
可自行修改的默认颜色值:
startColor='rgb(194, 147, 56)'; // 默认开始颜色
endColor='rgb(0, 159, 13)'; // 默认结束颜色
eg:
function getColor(num, startColor, endColor) {
startColor = startColor || 'rgb(194, 147, 56)';
endColor = endColor || 'rgb(0, 159, 13)';
var startNum = 0;
var endNum = 100;
var s = startColor.replace(/rgb\((.*)\)/, '$1').split(',');
var ss = [].concat(s);
var e = endColor.replace(/rgb\((.*)\)/, '$1').split(',');
var b = [1, 1, 1];
for(var i=0; i<3; i++) {
e[i] = parseInt(e[i]);
s[i] = parseInt(s[i]);
ss[i] = parseInt(ss[i]);
if(e[i] < s[i]) {
b[i] = -1;
var temp = e[i];
e[i] = s[i];
s[i] = temp;
}
}
var step = endNum - startNum;
var rr = (ss[0] + b[0] * (e[0] - s[0]) / step * num).toFixed(0);
var gg = (ss[1] + b[1] * (e[1] - s[1]) / step * num).toFixed(0);
var bb = (ss[2] + b[2] * (e[2] - s[2]) / step * num).toFixed(0);
return 'rgb('+rr+', '+gg+', '+bb+')';
}
ES6 export:
export function getColor(num, startColor='rgb(194, 147, 56)', endColor = 'rgb(0, 159, 13)') {
let startNum = 0;
let endNum = 100;
let s = startColor.replace(/rgb\((.*)\)/, '$1').split(',');
let ss = [].concat(s);
let e = endColor.replace(/rgb\((.*)\)/, '$1').split(',');
let b = [1, 1, 1];
for(let i=0; i<3; i++) {
e[i] = parseInt(e[i]);
s[i] = parseInt(s[i]);
ss[i] = parseInt(ss[i]);
if(e[i] < s[i]) {
b[i] = -1;
let temp = e[i];
e[i] = s[i];
s[i] = temp;
}
}
let step = endNum - startNum;
let rr = (ss[0] + b[0] * (e[0] - s[0]) / step * num).toFixed(0);
let gg = (ss[1] + b[1] * (e[1] - s[1]) / step * num).toFixed(0);
let bb = (ss[2] + b[2] * (e[2] - s[2]) / step * num).toFixed(0);
return 'rgb('+rr+', '+gg+', '+bb+')';
}
2. console.log 打印颜色
console.log('%c abc, %c def, %c123', 'color:red', 'background: #333; color: #fff', 'color: blue');
3. 判断身份证号是否合法,并给出合法解
function calc(ssnum) {
var weight = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 ];
var verify = [ '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' ];
var result = {};
var is_ssnum = false;
var temp = 0;
for(var i=0; i<17; i++) {
var num = parseInt(ssnum[i]);
temp += (num * weight[i]);
}
var verify_num = verify[ temp % 11 ];
if(verify_num == ssnum[17]) {
is_ssnum = true;
} else {
is_ssnum = false;
result.right_ssnum = ssnum.replace(/\d$/, verify_num);
}
result.is_ssnum = is_ssnum;
return result;
}
4. 计算身份证中的年龄和性别(需要Moment)
function calc(ssnum) {
var ssnum_reg = /^[0-9]{6}(\d{4})(\d{2})(\d{2})\d{2}(\d{1})[\d|x|X]$/;
var temp = ssnum.match(ssnum_reg);
var age = Moment.duration(Moment().diff(''+temp[1]+'-'+temp[2]+'-'+temp[3])).years();
var sex = (parseInt(temp[4]) % 2)? 1 : 2;
console.log(temp[1], temp[2], temp[3], ssnum, age, sex);
return { sex: sex, age: age };
}
批量处理数组:
function calcList(ssnumList) {
var list = [];
for(var i in u) {
var result = calc(u[i]);
result.ssnum=u[i];
list.push(result)
}
return list;
}