Javascript中常用util函数

//获取当前的日期时间函数,格式为“yyyy-MM-dd hh:mm:ss”
function getNowFormatDate(date) {
     if (date == null) {
         var date = new Date();
     }
     var seperator1 = "-";
     var seperator2 = ":";
     var month = date.getMonth() + 1;
     if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    var strDate = date.getDate();
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
   var hour = date.getHours();
    if (hour >= 0 && hour <= 9) {
        hour = "0" + hour;
    }
    var minute = date.getMinutes();
    if (minute >= 0 && minute <= 9) {
        minute = "0" + minute;
    }
    var sec = date.getSeconds();
    if (sec >= 0 && sec <= 9) {
        sec = "0" + sec;
    }
    var currentdate = date.getFullYear() + seperator1 + month + seperator1
        + strDate + " " + hour + seperator2 + minute
        + seperator2 + sec;
    return currentdate;
};

//获取浏览器地址栏参数
function GetQueryString(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)return  unescape(r[2]); return null;
}
 var id = GetQueryString('id');

 // 验证字符串中汉字或者字符的长度
 function checkStrLen(str) {
    var byteLen = 0;
    var len = str.length;
    if (str) {
        for (var i = 0; i < len; i++) {
            if (str.charCodeAt(i) > 255) {
                 byteLen += 2;
            } else {
                 byteLen++;
           }
     }
         return byteLen;
     } else {
         return 0;
    }
}

 // 颜色转换:Hex To RGB
function (color) {
    var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
    if (color && reg.test(color)) {
        if (color.length === 4) {
            var colorNew = "#";
            for (var i = 1; i < 4; i += 1) {
                colorNew += color.slice(i, i + 1).concat(color.slice(i, i + 1));
          }
            color = colorNew;
     }
      //处理六位的颜色值
      var colorChange = [];
       for (var i = 1; i < 7; i += 2) {
          colorChange.push(parseInt("0x" + color.slice(i, i + 2)));
        }
        return colorChange.join(",");
     } else {
        return color;
    }
}

// 颜色转换:RGB To Hex
function (color) {
    var regexp = /[0-9]{0,3}/g;
    var re = rgb.match(regexp);
    var hexColor = "#";
    var hex = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'];
    for (var i = 0; i < re.length; i++) {
        var r = null,
            c = re[i],
            l = c;
        var hexAr = [];
        while (c > 16) {
            r = c % 16;
            c = (c / 16) >> 0;
            hexAr.push(hex[r]);
        }
        hexAr.push(hex[c]);
         if (l < 16 && l != "") {
            hexAr.push(0)
        }
        hexColor += hexAr.reverse().join('');
    }
    return hexColor;
}

//颜色加深变浅
var Color = function () {
        //将hex颜色值str转化成rgb数组
        this.HexToRgb = function (str) {
            var r = /^\#?[0-9a-f]{6}$/;
            //test方法检查在字符串中是否存在一个模式,如果存在则返回true,否则返回false
            if (!r.test(str)) return window.alert("输入错误的hex颜色值");
            //replace替换查找的到的字符串
            str = str.replace("#", "");
            //match得到查询数组
            var hxs = str.match(/../g);
            for (var i = 0; i < 3; i++) hxs[i] = parseInt(hxs[i], 16);
            return hxs;
        }

        //将rgb颜色值为a,b,c转化成hex颜色值
        this.RgbToHex = function (a, b, c) {
            var r = /^\d{1,3}$/;
            if (!r.test(a) || !r.test(b) || !r.test(c)) return window.alert("输入错误的rgb颜色值");
            var hexs = [a.toString(16), b.toString(16), c.toString(16)];
            for (var i = 0; i < 3; i++) if (hexs[i].length == 1) hexs[i] = "0" + hexs[i];
            return "#" + hexs.join("");
        }

        //得到hex颜色值为color的加深颜色值,level为加深的程度,限0-1之间
        this.getDarkColor = function (color, level) {
            var r = /^\#?[0-9a-f]{6}$/;
            if (!r.test(color)) return window.alert("输入错误的hex颜色值");
            var rgbc = this.HexToRgb(color);
            //floor 向下取整
            for (var i = 0; i < 3; i++) rgbc[i] = Math.floor(rgbc[i] * (1 - level));
            return this.RgbToHex(rgbc[0], rgbc[1], rgbc[2]);
        }

        //得到hex颜色值为color的减淡颜色值,level为减淡的程度,限0-1之间
        this.getLightColor = function (color, level) {
            var r = /^\#?[0-9a-f]{6}$/;
            if (!r.test(color)) return window.alert("输入错误的hex颜色值");
            var rgbc = this.HexToRgb(color);
            for (var i = 0; i < 3; i++) rgbc[i] = Math.floor((255 - rgbc[i]) * level + rgbc[i]);
            return this.RgbToHex(rgbc[0], rgbc[1], rgbc[2]);
        }

        //得到hex颜色值为color的web安全色
        this.getWebSafeColor = function (color) {
            var r = /^\#?[0-9a-f]{6}$/;
            if (!r.test(color)) return window.alert("输入错误的hex颜色值");
            var rgbc = this.HexToRgb(color);
            for (var i = 0; i < 3; i++) {
                var q1 = Math.floor(rgbc[i] / 51) * 51;
                //ceil向上取整
                var q2 = Math.ceil(rgbc[i] / 51) * 51;
                //abs绝对值
                if (Math.abs(q1 - rgbc[i]) <= Math.abs(q2 - rgbc[i])) rgbc[i] = q1;
                else rgbc[i] = q2;
            }
            return this.RgbToHex(rgbc[0], rgbc[1], rgbc[2]);
        }
    }
    var color = new Color();
    console.log(color.getDarkColor("#ffffff",0.5));
    console.log(color.getLightColor("#999999",0.5));

你可能感兴趣的:(Javascript中常用util函数)