Global.helma.Color (R, G, B)
constructor for a color object

  • Parameter IntegerOrRgbObject:
    the red fraction or an rgb() object
  • Parameter Integer:
    the green fraction
  • Parameter Integer:
    the blue fraction
  • Returns
    the resulting color object
Sourcecode in /home/hannes/helma/modules/helma/Color.js:
29:   helma.Color = function(R, G, B) {
30:       var value = null, name, hex, rgb;
31:   
32:       /**
33:        * return the decimal value of a color object 
34:        * @return Integer the decimal value
35:        */
36:       this.valueOf = function() {
37:       if (arguments.length > 0) {
38:          if (!rgb) {
39:              var compose = function(n, bits) {
40:                  var div = Math.pow(2, bits);
41:                      remainder = n % div;          
42:                      return Math.floor(n/div);
43:                  }
44:                  var remainder = value;
45:                  rgb = {
46:                      red: compose(remainder, 16),
47:                      green: compose(remainder, 8),
48:                      blue: compose(remainder, 0)
49:                  }
50:              }
51:              return rgb[arguments[0]];
52:          }
53:          return value;
54:      };
55:  
56:      /**
57:       * return the hexidecimal value of a color object
58:       * @return String the hexidecimal value
59:       */
60:      this.toString = function() {
61:          if (!value)
62:              return null;
63:          if (!hex)
64:              hex = value.toString(16).pad("0", 6, String.LEFT);
65:          return hex;
66:      };
67:  
68:      /**
69:       * return the trivial name of this color
70:       * @return String the trivial name
71:       */
72:      this.getName = function() {
73:          return helma.Color.COLORVALUES[value];
74:      };
75:  
76:      // the main code
77:      if (arguments.length % 2 == 0)
78:          throw("Insufficient arguments for creating Color");
79:      if (arguments.length == 1) {
80:        if (R.constructor == Number) {
81:              value = R;
82:          } else if (R.constructor == String) {
83:              R = R.toLowerCase();
84:              if (helma.Color.COLORNAMES[R]) {
85:                  this.name = R;
86:                  R = helma.Color.COLORNAMES[R];
87:              } else if (helma.Color.COLORVALUES[R]) {
88:                  this.name = helma.Color.COLORVALUES[R];
89:              } else if (R.startsWith("#"))
90:                  R = R.substring(1);
91:              value = parseInt(R, 16);
92:          }
93:      } else
94:          value = R * Math.pow(2, 16) + G * Math.pow(2, 8) + B;
95:  
96:      if (value == null || isNaN(value))
97:          throw("Cannot create Color from " + R);
98:  
99:      for (var i in this)
100:          this.dontEnum(i);
101:  
102:      return this;
103:  }