zoukankan      html  css  js  c++  java
  • 颜色值相互转换Color

    颜色值相互转换,由hex到rgb或由rgb到hex,注意主要是字符串的操作和正则表达式的应用
    var Color = function (){
       //将hex装换成rgb形式
       this.HexToRgb = function (str){
           var r = /^\#?[0-9a-f]{6}$/;
           if (!r.test(str)) return window.alert("输入hex颜色值有误!!!");   
           //test方法检查在字符串中是否存在一个模式,如果存在则返回true,否则返回false
                str = str.replace("#", ""); 
                //替换查找的到的字符串
                var hxs = str.match(/../g); 
                //得到查询数组
                for (var i = 0; i < 3; i++) {
                    hxs[i] = parseInt(hxs[i], 16);
                }
           return hxs;
       }

       //将rgb装换成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] == 1) hexs[i] = "0" + hexs[i];
               }
          return "#" + hexs.join("");
       }
    }


  • 相关阅读:
    ruby -- 进阶学习(十三)解说ckeditor在production环境下如何完整显示
    ruby -- 进阶学习(十二)fragment cache
    ruby -- 进阶学习(十一)配置解决production环境下无法加载css或js
    ruby -- 问题解决(六)link_to to destroy not working
    ruby -- 基础学习(七)时间的内置函数和格式说明
    ruby -- 进阶学习(十)自定义路由中:new, :collection和:member的区别
    ruby -- 进阶学习(九)定制错误跳转404和500
    C++ -- STL泛型编程(二)之set
    SVN库文件上传操作步骤
    jmeter 正则表达式基础语法
  • 原文地址:https://www.cnblogs.com/kuikui/p/2333837.html
Copyright © 2011-2022 走看看