Requires

Provides

Color.js

Class for creating and manipulating colors in JavaScript. Supports HSB -> RGB Conversions and vice versa.

License:
MIT-style license
Authors:
Valerio Proietti
  1. 28
  2. 29
  3. 30
  4. 31
  5. 32
  6. 33
  7. 34
  8. 35
  9. 36
  10. 37
  11. 38
  12. 39
  13. 40
  14. 41
  15. 42
  16. 43
  17. 44
  18. 45
  19. 46
  20. 47
  21. 48
  22. 49
  23. 50
  24. 51
  25. 52
  26. 53
  27. 54
  28. 55
  29. 56
  30. 57
  31. 58
  32. 59
  33. 60
  34. 61
  35. 62
  36. 63
  37. 64
  38. 65
  39. 66
  40. 67
  41. 68
  42. 69
  43. 70
  44. 71
  45. 72
  46. 73
  47. 74
  48. 75
  49. 76
  50. 77
  51. 78
  52. 79
  53. 80
  54. 81
  55. 82
  56. 83
  57. 84
  58. 85
  59. 86
  60. 87
  61. 88
  62. 89
  63. 90
  64. 91
  65. 92
  66. 93
  67. 94
  68. 95
  69. 96
  70. 97
  71. 98
  72. 99
  73. 100
  74. 101
  75. 102
  76. 103
  77. 104
  78. 105
  79. 106
  80. 107
  81. 108
  82. 109
  83. 110
  84. 111
  85. 112
  86. 113
  87. 114
  88. 115
  89. 116
  90. 117
  91. 118
  92. 119
  93. 120
  94. 121
  95. 122
  96. 123
  97. 124
  98. 125
  99. 126
  100. 127
  101. 128
  102. 129
  103. 130
  104. 131
  105. 132
  106. 133
  107. 134
  108. 135
  109. 136
  110. 137
  111. 138
  112. 139
  113. 140
  114. 141
  115. 142
  116. 143
  117. 144
  118. 145
  119. 146
  120. 147
  121. 148
  122. 149
  123. 150
  124. 151
  125. 152
  126. 153
  127. 154
  128. 155
  129. 156
  130. 157
  131. 158
  132. 159
  133. 160
  134. 161
(function(){ var Color = this.Color = new Type('Color', function(color, type){ if (arguments.length >= 3){ type = 'rgb'; color = Array.slice(arguments, 0, 3); } else if (typeof color == 'string'){ if (color.match(/rgb/)) color = color.rgbToHex().hexToRgb(true); else if (color.match(/hsb/)) color = color.hsbToRgb(); else color = color.hexToRgb(true); } type = type || 'rgb'; switch (type){ case 'hsb': var old = color; color = color.hsbToRgb(); color.hsb = old; break; case 'hex': color = color.hexToRgb(true); break; } color.rgb = color.slice(0, 3); color.hsb = color.hsb || color.rgbToHsb(); color.hex = color.rgbToHex(); return Object.append(color, this); }); Color.implement({ mix: function(){ var colors = Array.slice(arguments); var alpha = (typeOf(colors.getLast()) == 'number') ? colors.pop() : 50; var rgb = this.slice(); colors.each(function(color){ color = new Color(color); for (var i = 0; i < 3; i++) rgb[i] = Math.round((rgb[i] / 100 * (100 - alpha)) + (color[i] / 100 * alpha)); }); return new Color(rgb, 'rgb'); }, invert: function(){ return new Color(this.map(function(value){ return 255 - value; })); }, setHue: function(value){ return new Color([value, this.hsb[1], this.hsb[2]], 'hsb'); }, setSaturation: function(percent){ return new Color([this.hsb[0], percent, this.hsb[2]], 'hsb'); }, setBrightness: function(percent){ return new Color([this.hsb[0], this.hsb[1], percent], 'hsb'); } }); var $RGB = function(r, g, b){ return new Color([r, g, b], 'rgb'); }; var $HSB = function(h, s, b){ return new Color([h, s, b], 'hsb'); }; var $HEX = function(hex){ return new Color(hex, 'hex'); }; Array.implement({ rgbToHsb: function(){ var red = this[0], green = this[1], blue = this[2], hue = 0; var max = Math.max(red, green, blue), min = Math.min(red, green, blue); var delta = max - min; var brightness = max / 255, saturation = (max != 0) ? delta / max : 0; if (saturation != 0){ var rr = (max - red) / delta; var gr = (max - green) / delta; var br = (max - blue) / delta; if (red == max) hue = br - gr; else if (green == max) hue = 2 + rr - br; else hue = 4 + gr - rr; hue /= 6; if (hue < 0) hue++; } return [Math.round(hue * 360), Math.round(saturation * 100), Math.round(brightness * 100)]; }, hsbToRgb: function(){ var br = Math.round(this[2] / 100 * 255); if (this[1] == 0){ return [br, br, br]; } else { var hue = this[0] % 360; var f = hue % 60; var p = Math.round((this[2] * (100 - this[1])) / 10000 * 255); var q = Math.round((this[2] * (6000 - this[1] * f)) / 600000 * 255); var t = Math.round((this[2] * (6000 - this[1] * (60 - f))) / 600000 * 255); switch (Math.floor(hue / 60)){ case 0: return [br, t, p]; case 1: return [q, br, p]; case 2: return [p, br, t]; case 3: return [p, q, br]; case 4: return [t, p, br]; case 5: return [br, p, q]; } } return false; } }); String.implement({ rgbToHsb: function(){ var rgb = this.match(/\d{1,3}/g); return (rgb) ? rgb.rgbToHsb() : null; }, hsbToRgb: function(){ var hsb = this.match(/\d{1,3}/g); return (hsb) ? hsb.hsbToRgb() : null; } }); })();