JavaScript color RGB with 16 binary conversion code

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.