[JavaScript] Javascript 颜色转换rgb与16进制互转 →→→→→进入此内容的聊天室

来自 , 2020-01-16, 写在 JavaScript, 查看 95 次.
URL http://www.code666.cn/view/39555391
  1. //颜色转换
  2.                         var Color = function() {
  3.                                 if (!(this instanceof Color)) {
  4.                                         var color = new Color();
  5.                                         color._init.apply(color, arguments);
  6.                                         return color;
  7.                                 }
  8.                                 if (arguments.length) {
  9.                                         this._init.apply(this, arguments);
  10.                                 }
  11.                         }
  12.                         //设置get,set方法
  13.                         var methods = ["red", "green", "blue", "colorValue"];
  14.                         var defineSetGetMethod = function(fn, methods) {
  15.                                 var fnPrototype = fn.prototype;
  16.                                 for (var i = 0; i < methods.length; i++) {
  17.                                         var methodName = methods[i].charAt(0).toLocaleUpperCase() + methods[i].substring(1);
  18.                                         fn.prototype['set' + methodName] = new Function("value", "this." + methods[i] + "= value;");
  19.                                         fn.prototype['get' + methodName] = new Function("return this." + methods[i] + ";");
  20.                                         fn.prototype['toString'] = new Function('return "rgb("+this.red+","+this.green+","+this.blue+")";');
  21.                                 }
  22.                         };
  23.                         defineSetGetMethod(Color, methods);
  24.                         //扩展函数的实例方法
  25.                         var extend = function(fn, option) {
  26.                                 var fnPrototype = fn.prototype;
  27.                                 for (var i in option) {
  28.                                         fnPrototype[i] = option[i];
  29.                                 }
  30.                         };
  31.                         extend(Color, {
  32.                                 _init : function() {
  33.                                         if (arguments.length == 3) {
  34.                                                 this.red = arguments[0];
  35.                                                 this.green = arguments[1];
  36.                                                 this.blue = arguments[2];
  37.                                                 this.getColorValue();
  38.                                         } else {
  39.                                                 var colorValue = arguments[0].replace(/^\#{1}/, "");
  40.                                                 if (colorValue.length == 3) {
  41.                                                         colorValue = colorValue.replace(/(.)/g, '$1$1');
  42.                                                 }
  43.                                                 this.red = parseInt('0x' + colorValue.substring(0, 2), 16);
  44.                                                 this.green = parseInt('0x' + colorValue.substring(2, 4), 16);
  45.                                                 this.blue = parseInt('0x' + colorValue.substring(4), 16);
  46.                                                 this.colorValue = "#" + colorValue;
  47.                                         }
  48.                                 },
  49.                                 getColorValue : function() {
  50.                                         if (this.colorValue) {
  51.                                                 return this.colorValue;
  52.                                         }
  53.                                         var hR = this.red.toString(16);
  54.                                         var hG = this.green.toString(16);
  55.                                         var hB = this.blue.toString(16);
  56.                                         return this.colorValue = "#" + (this.red < 16 ? ("0" + hR) : hR) + (this.green < 16 ? ("0" + hG) : hG) + (this.blue < 16 ? ("0" + hB) : hB);
  57.                                 }
  58.                         });
  59. //javascript/5951

回复 " Javascript 颜色转换rgb与16进制互转"

这儿你可以回复上面这条便签

captcha