Color.html 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-draw-Color'>/**
  19. </span> * Represents an RGB color and provides helper functions get
  20. * color components in HSL color space.
  21. */
  22. Ext.define('Ext.draw.Color', {
  23. /* Begin Definitions */
  24. /* End Definitions */
  25. colorToHexRe: /(.*?)rgb\((\d+),\s*(\d+),\s*(\d+)\)/,
  26. rgbRe: /\s*rgb\s*\(\s*([0-9]+)\s*,\s*([0-9]+)\s*,\s*([0-9]+)\s*\)\s*/,
  27. hexRe: /\s*#([0-9a-fA-F][0-9a-fA-F]?)([0-9a-fA-F][0-9a-fA-F]?)([0-9a-fA-F][0-9a-fA-F]?)\s*/,
  28. <span id='Ext-draw-Color-cfg-lightnessFactor'> /**
  29. </span> * @cfg {Number} lightnessFactor
  30. *
  31. * The default factor to compute the lighter or darker color. Defaults to 0.2.
  32. */
  33. lightnessFactor: 0.2,
  34. <span id='Ext-draw-Color-method-constructor'> /**
  35. </span> * Creates new Color.
  36. * @param {Number} red Red component (0..255)
  37. * @param {Number} green Green component (0..255)
  38. * @param {Number} blue Blue component (0..255)
  39. */
  40. constructor : function(red, green, blue) {
  41. var me = this,
  42. clamp = Ext.Number.constrain;
  43. me.r = clamp(red, 0, 255);
  44. me.g = clamp(green, 0, 255);
  45. me.b = clamp(blue, 0, 255);
  46. },
  47. <span id='Ext-draw-Color-method-getRed'> /**
  48. </span> * Get the red component of the color, in the range 0..255.
  49. * @return {Number}
  50. */
  51. getRed: function() {
  52. return this.r;
  53. },
  54. <span id='Ext-draw-Color-method-getGreen'> /**
  55. </span> * Get the green component of the color, in the range 0..255.
  56. * @return {Number}
  57. */
  58. getGreen: function() {
  59. return this.g;
  60. },
  61. <span id='Ext-draw-Color-method-getBlue'> /**
  62. </span> * Get the blue component of the color, in the range 0..255.
  63. * @return {Number}
  64. */
  65. getBlue: function() {
  66. return this.b;
  67. },
  68. <span id='Ext-draw-Color-method-getRGB'> /**
  69. </span> * Get the RGB values.
  70. * @return {Number[]}
  71. */
  72. getRGB: function() {
  73. var me = this;
  74. return [me.r, me.g, me.b];
  75. },
  76. <span id='Ext-draw-Color-method-getHSL'> /**
  77. </span> * Get the equivalent HSL components of the color.
  78. * @return {Number[]}
  79. */
  80. getHSL: function() {
  81. var me = this,
  82. r = me.r / 255,
  83. g = me.g / 255,
  84. b = me.b / 255,
  85. max = Math.max(r, g, b),
  86. min = Math.min(r, g, b),
  87. delta = max - min,
  88. h,
  89. s = 0,
  90. l = 0.5 * (max + min);
  91. // min==max means achromatic (hue is undefined)
  92. if (min != max) {
  93. s = (l &lt; 0.5) ? delta / (max + min) : delta / (2 - max - min);
  94. if (r == max) {
  95. h = 60 * (g - b) / delta;
  96. } else if (g == max) {
  97. h = 120 + 60 * (b - r) / delta;
  98. } else {
  99. h = 240 + 60 * (r - g) / delta;
  100. }
  101. if (h &lt; 0) {
  102. h += 360;
  103. }
  104. if (h &gt;= 360) {
  105. h -= 360;
  106. }
  107. }
  108. return [h, s, l];
  109. },
  110. <span id='Ext-draw-Color-method-getLighter'> /**
  111. </span> * Return a new color that is lighter than this color.
  112. * @param {Number} factor Lighter factor (0..1), default to 0.2
  113. * @return Ext.draw.Color
  114. */
  115. getLighter: function(factor) {
  116. var hsl = this.getHSL();
  117. factor = factor || this.lightnessFactor;
  118. hsl[2] = Ext.Number.constrain(hsl[2] + factor, 0, 1);
  119. return this.fromHSL(hsl[0], hsl[1], hsl[2]);
  120. },
  121. <span id='Ext-draw-Color-method-getDarker'> /**
  122. </span> * Return a new color that is darker than this color.
  123. * @param {Number} factor Darker factor (0..1), default to 0.2
  124. * @return Ext.draw.Color
  125. */
  126. getDarker: function(factor) {
  127. factor = factor || this.lightnessFactor;
  128. return this.getLighter(-factor);
  129. },
  130. <span id='Ext-draw-Color-method-toString'> /**
  131. </span> * Return the color in the hex format, i.e. '#rrggbb'.
  132. * @return {String}
  133. */
  134. toString: function() {
  135. var me = this,
  136. round = Math.round,
  137. r = round(me.r).toString(16),
  138. g = round(me.g).toString(16),
  139. b = round(me.b).toString(16);
  140. r = (r.length == 1) ? '0' + r : r;
  141. g = (g.length == 1) ? '0' + g : g;
  142. b = (b.length == 1) ? '0' + b : b;
  143. return ['#', r, g, b].join('');
  144. },
  145. <span id='Ext-draw-Color-static-method-toHex'> /**
  146. </span> * Convert a color to hexadecimal format.
  147. *
  148. * **Note:** This method is both static and instance.
  149. *
  150. * @param {String/String[]} color The color value (i.e 'rgb(255, 255, 255)', 'color: #ffffff').
  151. * Can also be an Array, in this case the function handles the first member.
  152. * @returns {String} The color in hexadecimal format.
  153. * @static
  154. */
  155. toHex: function(color) {
  156. if (Ext.isArray(color)) {
  157. color = color[0];
  158. }
  159. if (!Ext.isString(color)) {
  160. return '';
  161. }
  162. if (color.substr(0, 1) === '#') {
  163. return color;
  164. }
  165. var digits = this.colorToHexRe.exec(color),
  166. red,
  167. green,
  168. blue,
  169. rgb;
  170. if (Ext.isArray(digits)) {
  171. red = parseInt(digits[2], 10);
  172. green = parseInt(digits[3], 10);
  173. blue = parseInt(digits[4], 10);
  174. rgb = blue | (green &lt;&lt; 8) | (red &lt;&lt; 16);
  175. return digits[1] + '#' + (&quot;000000&quot; + rgb.toString(16)).slice(-6);
  176. }
  177. else {
  178. return color;
  179. }
  180. },
  181. <span id='Ext-draw-Color-static-method-fromString'> /**
  182. </span> * Parse the string and create a new color.
  183. *
  184. * Supported formats: '#rrggbb', '#rgb', and 'rgb(r,g,b)'.
  185. *
  186. * If the string is not recognized, an undefined will be returned instead.
  187. *
  188. * **Note:** This method is both static and instance.
  189. *
  190. * @param {String} str Color in string.
  191. * @returns Ext.draw.Color
  192. * @static
  193. */
  194. fromString: function(str) {
  195. var values, r, g, b,
  196. parse = parseInt;
  197. if ((str.length == 4 || str.length == 7) &amp;&amp; str.substr(0, 1) === '#') {
  198. values = str.match(this.hexRe);
  199. if (values) {
  200. r = parse(values[1], 16) &gt;&gt; 0;
  201. g = parse(values[2], 16) &gt;&gt; 0;
  202. b = parse(values[3], 16) &gt;&gt; 0;
  203. if (str.length == 4) {
  204. r += (r * 16);
  205. g += (g * 16);
  206. b += (b * 16);
  207. }
  208. }
  209. }
  210. else {
  211. values = str.match(this.rgbRe);
  212. if (values) {
  213. r = values[1];
  214. g = values[2];
  215. b = values[3];
  216. }
  217. }
  218. return (typeof r == 'undefined') ? undefined : new Ext.draw.Color(r, g, b);
  219. },
  220. <span id='Ext-draw-Color-method-getGrayscale'> /**
  221. </span> * Returns the gray value (0 to 255) of the color.
  222. *
  223. * The gray value is calculated using the formula r*0.3 + g*0.59 + b*0.11.
  224. *
  225. * @returns {Number}
  226. */
  227. getGrayscale: function() {
  228. // http://en.wikipedia.org/wiki/Grayscale#Converting_color_to_grayscale
  229. return this.r * 0.3 + this.g * 0.59 + this.b * 0.11;
  230. },
  231. <span id='Ext-draw-Color-static-method-fromHSL'> /**
  232. </span> * Create a new color based on the specified HSL values.
  233. *
  234. * **Note:** This method is both static and instance.
  235. *
  236. * @param {Number} h Hue component (0..359)
  237. * @param {Number} s Saturation component (0..1)
  238. * @param {Number} l Lightness component (0..1)
  239. * @returns Ext.draw.Color
  240. * @static
  241. */
  242. fromHSL: function(h, s, l) {
  243. var C, X, m, i, rgb = [],
  244. abs = Math.abs,
  245. floor = Math.floor;
  246. if (s == 0 || h == null) {
  247. // achromatic
  248. rgb = [l, l, l];
  249. }
  250. else {
  251. // http://en.wikipedia.org/wiki/HSL_and_HSV#From_HSL
  252. // C is the chroma
  253. // X is the second largest component
  254. // m is the lightness adjustment
  255. h /= 60;
  256. C = s * (1 - abs(2 * l - 1));
  257. X = C * (1 - abs(h - 2 * floor(h / 2) - 1));
  258. m = l - C / 2;
  259. switch (floor(h)) {
  260. case 0:
  261. rgb = [C, X, 0];
  262. break;
  263. case 1:
  264. rgb = [X, C, 0];
  265. break;
  266. case 2:
  267. rgb = [0, C, X];
  268. break;
  269. case 3:
  270. rgb = [0, X, C];
  271. break;
  272. case 4:
  273. rgb = [X, 0, C];
  274. break;
  275. case 5:
  276. rgb = [C, 0, X];
  277. break;
  278. }
  279. rgb = [rgb[0] + m, rgb[1] + m, rgb[2] + m];
  280. }
  281. return new Ext.draw.Color(rgb[0] * 255, rgb[1] * 255, rgb[2] * 255);
  282. }
  283. }, function() {
  284. var prototype = this.prototype;
  285. //These functions are both static and instance. TODO: find a more elegant way of copying them
  286. this.addStatics({
  287. fromHSL: function() {
  288. return prototype.fromHSL.apply(prototype, arguments);
  289. },
  290. fromString: function() {
  291. return prototype.fromString.apply(prototype, arguments);
  292. },
  293. toHex: function() {
  294. return prototype.toHex.apply(prototype, arguments);
  295. }
  296. });
  297. });
  298. </pre>
  299. </body>
  300. </html>