1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57-58 |
//color convert var color = function () {if (!) ( This is instanceof color) {var color = new color (); color._init.apply (color, arguments); return color;} if (Arguments.leng Th) {this._init.apply (this, arguments);} Set Get,set method var methods = ["Red", "green", "Blue", "ColorValue"]; var definesetgetmethod = function (FN, methods) {var fnprototype = Fn.prototype; for (var i = 0; i < methods.length; i+ +) {var methodname = Methods[i].charat (0). toLocaleUpperCase () + methods[i].substring (1); fn.prototype[' Set ' + MethodName] = new Function ("value", "this." + methods[i] + "= value;"); fn.prototype[' get ' + methodname] = the new Function ("return this." + methods[i] + ";"); fn.prototype[' toString '] = new Function (' Return "RGB (" +this.red+ "," +this.green+ "," +this.blue+ ")"; } }; Definesetgetmethod (Color, methods); instance method of extension function var extend = function (FN, option) {var fnprototype = Fn.prototype; for (var i) {Fnprototype[i] = O Ption[i]; } }; Extend (Color, {_init:function () {if (Arguments.length = 3) {this.red = arguments[0]; this.green = arguments[1]; this.blue = arguments[2]; th Is.getcolorvalue (); else {var colorvalue = Arguments[0].replace (/^#{1}/, ""); if (colorvalue.length = = 3) {ColorValue = Colorvalue.replace (/(.) /g, ' $1$1 '); } this.red = parseint (' 0x ' + colorvalue.substring (0, 2), 16); This.green = parseint (' 0x ' + colorvalue.substring (2, 4), 16); This.blue = parseint (' 0x ' + colorvalue.substring (4), 16); This.colorvalue = "#" + ColorValue; }, Getcolorvalue:function () {if (This.colorvalue) {return this.colorvalue;} var hR = this.red.toString; var HG = This.green.toString (16); var HB = this.blue.toString (16); return this.colorvalue = "#" + (This.red < 16?) ("0" + hr): HR) + (This.green < 16?) ("0" + Hg): HG) + (This.blue < 16?) ("0" + HB): HB); } }); |