Requires

Provides

Class to create and manipulate colors. Includes HSB «-» RGB «-» HEX conversions. Supports alpha for each type.

  1. 11
  2. 12
  3. 13
  4. 14
  5. 15
  6. 16
  7. 17
  8. 18
  9. 19
  10. 20
  11. 21
  12. 22
  13. 23
  14. 24
  15. 25
  16. 26
  17. 27
  18. 28
  19. 29
  20. 30
  21. 31
  22. 32
  23. 33
  24. 34
  25. 35
  26. 36
  27. 37
  28. 38
  29. 39
  30. 40
  31. 41
  32. 42
  33. 43
  34. 44
  35. 45
  36. 46
  37. 47
  38. 48
  39. 49
  40. 50
  41. 51
  42. 52
  43. 53
  44. 54
  45. 55
  46. 56
  47. 57
  48. 58
  49. 59
  50. 60
  51. 61
  52. 62
  53. 63
  54. 64
  55. 65
  56. 66
  57. 67
  58. 68
  59. 69
  60. 70
  61. 71
  62. 72
  63. 73
  64. 74
  65. 75
  66. 76
  67. 77
  68. 78
  69. 79
  70. 80
  71. 81
  72. 82
  73. 83
  74. 84
  75. 85
  76. 86
  77. 87
  78. 88
  79. 89
  80. 90
  81. 91
  82. 92
  83. 93
  84. 94
  85. 95
  86. 96
  87. 97
  88. 98
  89. 99
  90. 100
  91. 101
  92. 102
  93. 103
  94. 104
  95. 105
  96. 106
  97. 107
  98. 108
  99. 109
  100. 110
  101. 111
  102. 112
  103. 113
  104. 114
  105. 115
  106. 116
  107. 117
  108. 118
  109. 119
  110. 120
  111. 121
  112. 122
  113. 123
  114. 124
  115. 125
  116. 126
  117. 127
  118. 128
  119. 129
  120. 130
  121. 131
  122. 132
  123. 133
  124. 134
  125. 135
  126. 136
  127. 137
  128. 138
  129. 139
  130. 140
  131. 141
  132. 142
  133. 143
  134. 144
  135. 145
  136. 146
  137. 147
  138. 148
  139. 149
  140. 150
  141. 151
  142. 152
  143. 153
  144. 154
  145. 155
  146. 156
  147. 157
  148. 158
  149. 159
  150. 160
  151. 161
  152. 162
  153. 163
  154. 164
  155. 165
  156. 166
  157. 167
  158. 168
(function(){ var colors = { maroon: '#800000', red: '#ff0000', orange: '#ffA500', yellow: '#ffff00', olive: '#808000', purple: '#800080', fuchsia: "#ff00ff", white: '#ffffff', lime: '#00ff00', green: '#008000', navy: '#000080', blue: '#0000ff', aqua: '#00ffff', teal: '#008080', black: '#000000', silver: '#c0c0c0', gray: '#808080' }; var Color = this.Color = function(color, type){ if (color.isColor){ this.red = color.red; this.green = color.green; this.blue = color.blue; this.alpha = color.alpha; } else { var namedColor = colors[color]; if (namedColor){ color = namedColor; type = 'hex'; } switch (typeof color){ case 'string': if (!type) type = (type = color.match(/^rgb|^hsb/)) ? type[0] : 'hex'; break; case 'object': type = type || 'rgb'; color = color.toString(); break; case 'number': type = 'hex'; color = color.toString(16); break; } color = Color['parse' + type.toUpperCase()](color); this.red = color[0]; this.green = color[1]; this.blue = color[2]; this.alpha = color[3]; } this.isColor = true; }; var limit = function(number, min, max){ return Math.min(max, Math.max(min, number)); }; var listMatch = /([-.\d]+)\s*,\s*([-.\d]+)\s*,\s*([-.\d]+)\s*,?\s*([-.\d]*)/; var hexMatch = /^#?([a-f0-9]{1,2})([a-f0-9]{1,2})([a-f0-9]{1,2})([a-f0-9]{0,2})$/i; Color.parseRGB = function(color){ return color.match(listMatch).slice(1).map(function(bit, i){ return (i < 3) ? Math.round(((bit %= 256) < 0) ? bit + 256 : bit) : limit(((bit === '') ? 1 : Number(bit)), 0, 1); }); }; Color.parseHEX = function(color){ if (color.length == 1) color = color + color + color; return color.match(hexMatch).slice(1).map(function(bit, i){ if (i == 3) return (bit) ? parseInt(bit, 16) / 255 : 1; return parseInt((bit.length == 1) ? bit + bit : bit, 16); }); }; Color.parseHSB = function(color){ var hsb = color.match(listMatch).slice(1).map(function(bit, i){ if (i === 0) return Math.round(((bit %= 360) < 0) ? (bit + 360) : bit); else if (i < 3) return limit(Math.round(bit), 0, 100); else return limit(((bit === '') ? 1 : Number(bit)), 0, 1); }); var a = hsb[3]; var br = Math.round(hsb[2] / 100 * 255); if (hsb[1] == 0) return [br, br, br, a]; var hue = hsb[0]; var f = hue % 60; var p = Math.round((hsb[2] * (100 - hsb[1])) / 10000 * 255); var q = Math.round((hsb[2] * (6000 - hsb[1] * f)) / 600000 * 255); var t = Math.round((hsb[2] * (6000 - hsb[1] * (60 - f))) / 600000 * 255); switch (Math.floor(hue / 60)){ case 0: return [br, t, p, a]; case 1: return [q, br, p, a]; case 2: return [p, br, t, a]; case 3: return [p, q, br, a]; case 4: return [t, p, br, a]; default: return [br, p, q, a]; } }; var toString = function(type, array){ if (array[3] != 1) type += 'a'; else array.pop(); return type + '(' + array.join(', ') + ')'; }; Color.prototype = { toHSB: function(array){ var red = this.red, green = this.green, blue = this.blue, alpha = this.alpha; var max = Math.max(red, green, blue), min = Math.min(red, green, blue), delta = max - min; var hue = 0, saturation = (max != 0) ? delta / max : 0, brightness = max / 255; if (saturation){ var rr = (max - red) / delta, gr = (max - green) / delta, br = (max - blue) / delta; hue = (red == max) ? br - gr : (green == max) ? 2 + rr - br : 4 + gr - rr; if ((hue /= 6) < 0) hue++; } var hsb = [Math.round(hue * 360), Math.round(saturation * 100), Math.round(brightness * 100), alpha]; return (array) ? hsb : toString('hsb', hsb); }, toHEX: function(array){ var a = this.alpha; var alpha = ((a = Math.round((a * 255)).toString(16)).length == 1) ? a + a : a; var hex = [this.red, this.green, this.blue].map(function(bit){ bit = bit.toString(16); return (bit.length == 1) ? '0' + bit : bit; }); return (array) ? hex.concat(alpha) : '#' + hex.join('') + ((alpha == 'ff') ? '' : alpha); }, toRGB: function(array){ var rgb = [this.red, this.green, this.blue, this.alpha]; return (array) ? rgb : toString('rgb', rgb); } }; Color.prototype.toString = Color.prototype.toRGB; Color.hex = function(hex){ return new Color(hex, 'hex'); }; if (this.hex == null) this.hex = Color.hex; Color.hsb = function(h, s, b, a){ return new Color([h || 0, s || 0, b || 0, (a == null) ? 1 : a], 'hsb'); }; if (this.hsb == null) this.hsb = Color.hsb; Color.rgb = function(r, g, b, a){ return new Color([r || 0, g || 0, b || 0, (a == null) ? 1 : a], 'rgb'); }; if (this.rgb == null) this.rgb = Color.rgb; if (this.Type) new Type('Color', Color); })();