5fafe55e50130fc120b9ef62eb3f6b0072a1b326.svn-base 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. if (!dojo._hasResource["dojox.color._base"]) { // _hasResource checks added by
  2. // build. Do not use
  3. // _hasResource directly in your
  4. // code.
  5. dojo._hasResource["dojox.color._base"] = true;
  6. dojo.provide("dojox.color._base");
  7. dojo.require("dojo.colors");
  8. // alias all the dojo.Color mechanisms
  9. dojox.color.Color = dojo.Color;
  10. dojox.color.blend = dojo.blendColors;
  11. dojox.color.fromRgb = dojo.colorFromRgb;
  12. dojox.color.fromHex = dojo.colorFromHex;
  13. dojox.color.fromArray = dojo.colorFromArray;
  14. dojox.color.fromString = dojo.colorFromString;
  15. // alias the dojo.colors mechanisms
  16. dojox.color.greyscale = dojo.colors.makeGrey;
  17. // static methods
  18. dojo.mixin(dojox.color, {
  19. fromCmy : function(/* Object|Array|int */cyan, /* int */magenta, /* int */
  20. yellow) {
  21. // summary
  22. // Create a dojox.color.Color from a CMY defined color.
  23. // All colors should be expressed as 0-100 (percentage)
  24. if (dojo.isArray(cyan)) {
  25. magenta = cyan[1], yellow = cyan[2], cyan = cyan[0];
  26. } else if (dojo.isObject(cyan)) {
  27. magenta = cyan.m, yellow = cyan.y, cyan = cyan.c;
  28. }
  29. cyan /= 100, magenta /= 100, yellow /= 100;
  30. var r = 1 - cyan, g = 1 - magenta, b = 1 - yellow;
  31. return new dojox.color.Color({
  32. r : Math.round(r * 255),
  33. g : Math.round(g * 255),
  34. b : Math.round(b * 255)
  35. }); // dojox.color.Color
  36. },
  37. fromCmyk : function(/* Object|Array|int */cyan, /* int */magenta, /* int */
  38. yellow, /* int */black) {
  39. // summary
  40. // Create a dojox.color.Color from a CMYK defined color.
  41. // All colors should be expressed as 0-100 (percentage)
  42. if (dojo.isArray(cyan)) {
  43. magenta = cyan[1], yellow = cyan[2], black = cyan[3], cyan = cyan[0];
  44. } else if (dojo.isObject(cyan)) {
  45. magenta = cyan.m, yellow = cyan.y, black = cyan.b, cyan = cyan.c;
  46. }
  47. cyan /= 100, magenta /= 100, yellow /= 100, black /= 100;
  48. var r, g, b;
  49. r = 1 - Math.min(1, cyan * (1 - black) + black);
  50. g = 1 - Math.min(1, magenta * (1 - black) + black);
  51. b = 1 - Math.min(1, yellow * (1 - black) + black);
  52. return new dojox.color.Color({
  53. r : Math.round(r * 255),
  54. g : Math.round(g * 255),
  55. b : Math.round(b * 255)
  56. }); // dojox.color.Color
  57. },
  58. fromHsl : function(/* Object|Array|int */hue, /* int */saturation, /* int */
  59. luminosity) {
  60. // summary
  61. // Create a dojox.color.Color from an HSL defined color.
  62. // hue from 0-359 (degrees), saturation and luminosity 0-100.
  63. if (dojo.isArray(hue)) {
  64. saturation = hue[1], luminosity = hue[2], hue = hue[0];
  65. } else if (dojo.isObject(hue)) {
  66. saturation = hue.s, luminosity = hue.l, hue = hue.h;
  67. }
  68. saturation /= 100;
  69. luminosity /= 100;
  70. while (hue < 0) {
  71. hue += 360;
  72. }
  73. while (hue >= 360) {
  74. hue -= 360;
  75. }
  76. var r, g, b;
  77. if (hue < 120) {
  78. r = (120 - hue) / 60, g = hue / 60, b = 0;
  79. } else if (hue < 240) {
  80. r = 0, g = (240 - hue) / 60, b = (hue - 120) / 60;
  81. } else {
  82. r = (hue - 240) / 60, g = 0, b = (360 - hue) / 60;
  83. }
  84. r = 2 * saturation * Math.min(r, 1) + (1 - saturation);
  85. g = 2 * saturation * Math.min(g, 1) + (1 - saturation);
  86. b = 2 * saturation * Math.min(b, 1) + (1 - saturation);
  87. if (luminosity < 0.5) {
  88. r *= luminosity, g *= luminosity, b *= luminosity;
  89. } else {
  90. r = (1 - luminosity) * r + 2 * luminosity - 1;
  91. g = (1 - luminosity) * g + 2 * luminosity - 1;
  92. b = (1 - luminosity) * b + 2 * luminosity - 1;
  93. }
  94. return new dojox.color.Color({
  95. r : Math.round(r * 255),
  96. g : Math.round(g * 255),
  97. b : Math.round(b * 255)
  98. }); // dojox.color.Color
  99. },
  100. fromHsv : function(/* Object|Array|int */hue, /* int */saturation, /* int */
  101. value) {
  102. // summary
  103. // Create a dojox.color.Color from an HSV defined color.
  104. // hue from 0-359 (degrees), saturation and value 0-100.
  105. if (dojo.isArray(hue)) {
  106. saturation = hue[1], value = hue[2], hue = hue[0];
  107. } else if (dojo.isObject(hue)) {
  108. saturation = hue.s, value = hue.v, hue = hue.h;
  109. }
  110. if (hue == 360) {
  111. hue = 0;
  112. }
  113. saturation /= 100;
  114. value /= 100;
  115. var r, g, b;
  116. if (saturation == 0) {
  117. r = value, b = value, g = value;
  118. } else {
  119. var hTemp = hue / 60, i = Math.floor(hTemp), f = hTemp - i;
  120. var p = value * (1 - saturation);
  121. var q = value * (1 - (saturation * f));
  122. var t = value * (1 - (saturation * (1 - f)));
  123. switch (i) {
  124. case 0 : {
  125. r = value, g = t, b = p;
  126. break;
  127. }
  128. case 1 : {
  129. r = q, g = value, b = p;
  130. break;
  131. }
  132. case 2 : {
  133. r = p, g = value, b = t;
  134. break;
  135. }
  136. case 3 : {
  137. r = p, g = q, b = value;
  138. break;
  139. }
  140. case 4 : {
  141. r = t, g = p, b = value;
  142. break;
  143. }
  144. case 5 : {
  145. r = value, g = p, b = q;
  146. break;
  147. }
  148. }
  149. }
  150. return new dojox.color.Color({
  151. r : Math.round(r * 255),
  152. g : Math.round(g * 255),
  153. b : Math.round(b * 255)
  154. }); // dojox.color.Color
  155. }
  156. });
  157. // Conversions directly on dojox.color.Color
  158. dojo.extend(dojox.color.Color, {
  159. toCmy : function() {
  160. // summary
  161. // Convert this Color to a CMY definition.
  162. var cyan = 1 - (this.r / 255), magenta = 1 - (this.g / 255), yellow = 1
  163. - (this.b / 255);
  164. return {
  165. c : Math.round(cyan * 100),
  166. m : Math.round(magenta * 100),
  167. y : Math.round(yellow * 100)
  168. }; // Object
  169. },
  170. toCmyk : function() {
  171. // summary
  172. // Convert this Color to a CMYK definition.
  173. var cyan, magenta, yellow, black;
  174. var r = this.r / 255, g = this.g / 255, b = this.b / 255;
  175. black = Math.min(1 - r, 1 - g, 1 - b);
  176. cyan = (1 - r - black) / (1 - black);
  177. magenta = (1 - g - black) / (1 - black);
  178. yellow = (1 - b - black) / (1 - black);
  179. return {
  180. c : Math.round(cyan * 100),
  181. m : Math.round(magenta * 100),
  182. y : Math.round(yellow * 100),
  183. b : Math.round(black * 100)
  184. }; // Object
  185. },
  186. toHsl : function() {
  187. // summary
  188. // Convert this Color to an HSL definition.
  189. var r = this.r / 255, g = this.g / 255, b = this.b / 255;
  190. var min = Math.min(r, b, g), max = Math.max(r, g, b);
  191. var delta = max - min;
  192. var h = 0, s = 0, l = (min + max) / 2;
  193. if (l > 0 && l < 1) {
  194. s = delta / ((l < 0.5) ? (2 * l) : (2 - 2 * l));
  195. }
  196. if (delta > 0) {
  197. if (max == r && max != g) {
  198. h += (g - b) / delta;
  199. }
  200. if (max == g && max != b) {
  201. h += (2 + (b - r) / delta);
  202. }
  203. if (max == b && max != r) {
  204. h += (4 + (r - g) / delta);
  205. }
  206. h *= 60;
  207. }
  208. return {
  209. h : h,
  210. s : Math.round(s * 100),
  211. l : Math.round(l * 100)
  212. }; // Object
  213. },
  214. toHsv : function() {
  215. // summary
  216. // Convert this Color to an HSV definition.
  217. var r = this.r / 255, g = this.g / 255, b = this.b / 255;
  218. var min = Math.min(r, b, g), max = Math.max(r, g, b);
  219. var delta = max - min;
  220. var h = null, s = (max == 0) ? 0 : (delta / max);
  221. if (s == 0) {
  222. h = 0;
  223. } else {
  224. if (r == max) {
  225. h = 60 * (g - b) / delta;
  226. } else if (g == max) {
  227. h = 120 + 60 * (b - r) / delta;
  228. } else {
  229. h = 240 + 60 * (r - g) / delta;
  230. }
  231. if (h < 0) {
  232. h += 360;
  233. }
  234. }
  235. return {
  236. h : h,
  237. s : Math.round(s * 100),
  238. v : Math.round(max * 100)
  239. }; // Object
  240. }
  241. });
  242. }