JavaScript implements the method of color RGB and 16 binary conversion. Share to everyone for your reference. Specific as follows:
Color (12,34,56); Color ("#fff") Color ("#defdcd")
//color Conversion var-color = function () { if (! ( This instanceof color) { var color = new color (); color._init.apply (color, arguments); & Nbsp;return color; } if (arguments.length) { this._init.apply (this, arguments); }}//Set Get,set method var Metho ds = ["Red", "green", "Blue", "ColorValue"]; var definesetgetmethod = function (FN, methods) { var fnprototype = Fn.prototype; for (var i = 0; i < meth Ods.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] = new Function ("return this." + methods[i] + ";"); fn.prototype[' toString ' = new Function (' Return ' RGB ("+this.red+", "+this.green+", "+this.blue+") ";"); }; Definesetgetmethod (Color, methods); Example methods for extension functions var extend = function (FN, option) { var fnprototype = Fn.prototype; for (var i in option) { fnprototype[i] = option[i]; }; Extend (Color, { _init:function () { if (arguments.length = = 3) { this.red = arguments[0 ]; This.green = arguments[1]; This.blue = arguments[2]; This.getcolorvalue (); &NBSP;} 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; &NBSP,} }, Getcolorvalue:function () { if (this.colorvalue) { return this . ColorValue; } var HR = this.red.toString (16); 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); });
Codego.net Excerpt Code
JavaScript color RGB with 16 binary conversion code