This article illustrates the method for JavaScript to implement color RGB and 16 conversion. Share to everyone for your reference. Specifically as follows:
How to use:
Color (12,34,56);
Color ("#fff")
color ("#defdcd")
Implementation code:
Color conversion var color = function () {if (!) (
This instanceof color) {var color = new color ();
Color._init.apply (color, arguments);
return color;
} if (arguments.length) {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].su
bstring (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 extended function 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 ();
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 (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); }
});
The
wants this article to help you with your JavaScript programming.