| // 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 the get and set methods 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] = new Function ("return this." + methods [I] + ";"); Fn. prototype ['string'] = new Function ('Return "rgb (" + this. red + "," + this. green + "," + this. blue + ")";'); } }; DefineSetGetMethod (Color, methods ); // Instance method of the 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 ); } }); |