JavaScript implementation of color RGB and 16 conversion method

Source: Internet
Author: User
Tags extend instance method

Specifically as follows:

How to use:

1 2 3 Color (12,34,56); Color ("#fff") Color ("#defdcd")

Implementation code:

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); } });
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.