graphics/color.js

  1. 'use strict';
  2. /**
  3. * @namespace Color
  4. */
  5. var Randomizer = require('./randomizer.js');
  6. /**
  7. * @class Color
  8. * @param {number} r - Red value.
  9. * @param {number} g - Green value.
  10. * @param {number} b - Blue value.
  11. */
  12. function Color(r, g, b) {
  13. this.r = r;
  14. this.g = g;
  15. this.b = b;
  16. }
  17. /**
  18. * Generate a hex representation of the color.
  19. * @returns {string}
  20. */
  21. Color.prototype.toString = function() {
  22. return Color.createFromRGB(this.r, this.g, this.b);
  23. };
  24. Color.random = Randomizer.nextColor;
  25. Color.constants = {
  26. red: '#FF0000',
  27. RED: '#FF0000',
  28. green: '#00FF00',
  29. GREEN: '#00FF00',
  30. blue: '#0000FF',
  31. BLUE: '#0000FF',
  32. yellow: '#FFFF00',
  33. YELLOW: '#FFFF00',
  34. cyan: '#00FFFF',
  35. CYAN: '#00FFFF',
  36. orange: '#FFA500',
  37. ORANGE: '#FFA500',
  38. white: '#FFFFFF',
  39. WHITE: '#FFFFFF',
  40. black: '#000000',
  41. BLACK: '#000000',
  42. gray: '#cccccc',
  43. GRAY: '#CCCCCC',
  44. grey: '#cccccc',
  45. GREY: '#CCCCCC',
  46. purple: '#9B30FF',
  47. PURPLE: '#9B30FF'
  48. };
  49. var name;
  50. for (name in Color.constants) {
  51. Color[name] = Color.constants[name];
  52. }
  53. /**
  54. * Helpers for createFromRGB
  55. */
  56. /**
  57. * Convert RGB to a hex string.
  58. *
  59. * @memberof Color
  60. * @param {number} r - Red component.
  61. * @param {number} g - Green component.
  62. * @param {number} b - Blue component.
  63. * @returns {string} Hex representation.
  64. */
  65. function rgbToHex(r, g, b) {
  66. r = Math.floor(r);
  67. g = Math.floor(g);
  68. b = Math.floor(b);
  69. if (r > 255 || g > 255 || b > 255) {
  70. throw 'Invalid color component';
  71. }
  72. return ((r << 16) | (g << 8) | b).toString(16);
  73. }
  74. function getColor(r, g, b) {
  75. return '#' + ('000000' + rgbToHex(r, g, b)).slice(-6);
  76. }
  77. /**
  78. * Create a hex color from RGB values.
  79. *
  80. * @param {number} r - Red value.
  81. * @param {number} g - Green value.
  82. * @param {number} b - Blue value .
  83. * @returns {string}
  84. */
  85. Color.createFromRGB = function(r, g, b) {
  86. return getColor(r, g, b);
  87. };
  88. /**
  89. * Generate a random red value.
  90. *
  91. * @returns {string} Hex representation of random red color.
  92. */
  93. Color.randomRed = function() {
  94. var r = Randomizer.nextInt(50, 255);
  95. return Color.createFromRGB(r, 0, 0);
  96. };
  97. /**
  98. * Generate a random green value.
  99. *
  100. * @returns {string} Hex representation of random green color.
  101. */
  102. Color.randomGreen = function() {
  103. var g = Randomizer.nextInt(50, 255);
  104. return Color.createFromRGB(0, g, 0);
  105. };
  106. /**
  107. * Generate a random blue value.
  108. *
  109. * @returns {string} Hex representation of random blue color.
  110. */
  111. Color.randomBlue = function() {
  112. var b = Randomizer.nextInt(50, 255);
  113. return Color.createFromRGB(0, 0, b);
  114. };
  115. /**
  116. * Creates a hex color string from a (r,g,b) value as well
  117. * as a lightness value l from [0, 1]. To do this we convert
  118. * the rgb color to hsl. Then we modify the l, take it back to
  119. * rgb, and then convert to a color string.
  120. *
  121. * @param {number} r - The red color value.
  122. * @param {number} g - The green color value.
  123. * @param {number} b - The blue color value.
  124. * @param {number} l - The lightness value [0,1].
  125. * @returns {string} The hex color string.
  126. */
  127. Color.createFromRGBL = function(r, g, b, l) {
  128. var hsl = Color.rgbToHsl(r, g, b);
  129. if (l < 0) {
  130. l = 0;
  131. }
  132. if (l > 1) {
  133. l = 1;
  134. }
  135. var rgb = Color.hslToRgb(hsl[0], hsl[1], l);
  136. return Color.createFromRGB(rgb[0], rgb[1], rgb[2]);
  137. };
  138. /**
  139. * Converts an RGB color value to HSL. Conversion formula
  140. * adapted from http://en.wikipedia.org/wiki/HSL_color_space.
  141. * Assumes r, g, and b are contained in the set [0, 255] and
  142. * returns h, s, and l in the set [0, 1].
  143. *
  144. * @param {number} r - The red color value.
  145. * @param {number} g - The green color value.
  146. * @param {number} b - The blue color value.
  147. * @returns {array} The HSL representation.
  148. */
  149. Color.rgbToHsl = function(r, g, b) {
  150. r /= 255, g /= 255, b /= 255;
  151. var max = Math.max(r, g, b), min = Math.min(r, g, b);
  152. var h, s, l = (max + min) / 2;
  153. if (max == min) {
  154. h = s = 0; // achromatic
  155. }else {
  156. var d = max - min;
  157. s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
  158. switch (max) {
  159. case r: h = (g - b) / d + (g < b ? 6 : 0); break;
  160. case g: h = (b - r) / d + 2; break;
  161. case b: h = (r - g) / d + 4; break;
  162. }
  163. h /= 6;
  164. }
  165. return [h, s, l];
  166. };
  167. /**
  168. * Helpers for hslToRgb
  169. */
  170. /**
  171. * Converts an HSL representation to RGB.
  172. *
  173. * @memberof Color
  174. * @param {number} p
  175. * @param {number} q
  176. * @param {number} t
  177. * @returns {number} RGB representation of component.
  178. */
  179. function hue2rgb(p, q, t) {
  180. if (t < 0) t += 1;
  181. if (t > 1) t -= 1;
  182. if (t < 1 / 6) return p + (q - p) * 6 * t;
  183. if (t < 1 / 2) return q;
  184. if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
  185. return p;
  186. }
  187. /**
  188. * Converts an HSL color value to RGB. Conversion formula
  189. * adapted from http://en.wikipedia.org/wiki/HSL_color_space.
  190. * Assumes h, s, and l are contained in the set [0, 1] and
  191. * returns r, g, and b in the set [0, 255].
  192. *
  193. * @param {number} h - The hue.
  194. * @param {number} s - The saturation.
  195. * @param {number} l - The lightness.
  196. * @returns {object} The RGB representation.
  197. */
  198. Color.hslToRgb = function(h, s, l) {
  199. var r, g, b;
  200. if (s === 0) {
  201. r = g = b = l; // achromatic
  202. } else {
  203. var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
  204. var p = 2 * l - q;
  205. r = hue2rgb(p, q, h + 1 / 3);
  206. g = hue2rgb(p, q, h);
  207. b = hue2rgb(p, q, h - 1 / 3);
  208. }
  209. return [r * 255, g * 255, b * 255];
  210. };
  211. /**
  212. * Generate the average of two hex colors.
  213. *
  214. * @param {string} colorOne - First hex color.
  215. * @param {string} colorTwo - Second hex color.
  216. * @returns {string} Averaged hex color.
  217. */
  218. Color.average = function(colorOne, colorTwo) {
  219. // functions for converting to/from hex/dec
  220. function getHex(num) { return num.toString(16); }
  221. function getDec(hex) { return parseInt(hex, 16); }
  222. var componentRegEx = /[\da-z]{2}/gi;
  223. var componentsOne = colorOne.match(componentRegEx);
  224. var componentsTwo = colorTwo.match(componentRegEx);
  225. var averageHex = '#';
  226. var colorOneComponent;
  227. var colorTwoComponent;
  228. var averageDec;
  229. var h;
  230. for (var i = 0; i < componentsOne.length; i++) {
  231. colorOneComponent = getDec(componentsOne[i]);
  232. colorTwoComponent = getDec(componentsTwo[i]);
  233. averageDec = Math.floor((colorOneComponent + colorTwoComponent) >> 1);
  234. h = getHex(averageDec);
  235. if (h.length == 1) h = '0' + h;
  236. averageHex += h;
  237. }
  238. return averageHex;
  239. };
  240. Color.getColor = function(colorString) {
  241. return Color.constants[colorString];
  242. };
  243. Color.getHexColor = function(hexString) {
  244. return hexString;
  245. };
  246. module.exports = Color;