jquery.xcolor.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  1. /**
  2. * @license jQuery paging plugin v1.8.0 06/21/2010
  3. * http://www.xarg.org/project/jquery-color-plugin-xcolor/
  4. *
  5. * Copyright (c) 2010, Robert Eisele (robert@xarg.org)
  6. * Dual licensed under the MIT or GPL Version 2 licenses.
  7. **/
  8. // ([^a-z."/])m([^a-z:"])
  9. (function ($, undefined) {
  10. // http://www.w3.org/TR/css3-color/#svg-color
  11. var color_names = {
  12. "aliceblue": "f0f8ff",
  13. "antiquewhite": "faebd7",
  14. "aqua": "0ff",
  15. "aquamarine": "7fffd4",
  16. "azure": "f0ffff",
  17. "beige": "f5f5dc",
  18. "bisque": "ffe4c4",
  19. "black": "000",
  20. "blanchedalmond": "ffebcd",
  21. "blue": "00f",
  22. "blueviolet": "8a2be2",
  23. "brown": "a52a2a",
  24. "burlywood": "deb887",
  25. "burntsienna": "ea7e5d",
  26. "cadetblue": "5f9ea0",
  27. "chartreuse": "7fff00",
  28. "chocolate": "d2691e",
  29. "coral": "ff7f50",
  30. "cornflowerblue": "6495ed",
  31. "cornsilk": "fff8dc",
  32. "crimson": "dc143c",
  33. "cyan": "0ff",
  34. "darkblue": "00008b",
  35. "darkcyan": "008b8b",
  36. "darkgoldenrod": "b8860b",
  37. "darkgray": "a9a9a9",
  38. "darkgreen": "006400",
  39. "darkgrey": "a9a9a9",
  40. "darkkhaki": "bdb76b",
  41. "darkmagenta": "8b008b",
  42. "darkolivegreen": "556b2f",
  43. "darkorange": "ff8c00",
  44. "darkorchid": "9932cc",
  45. "darkred": "8b0000",
  46. "darksalmon": "e9967a",
  47. "darkseagreen": "8fbc8f",
  48. "darkslateblue": "483d8b",
  49. "darkslategray": "2f4f4f",
  50. "darkslategrey": "2f4f4f",
  51. "darkturquoise": "00ced1",
  52. "darkviolet": "9400d3",
  53. "deeppink": "ff1493",
  54. "deepskyblue": "00bfff",
  55. "dimgray": "696969",
  56. "dimgrey": "696969",
  57. "dodgerblue": "1e90ff",
  58. "firebrick": "b22222",
  59. "floralwhite": "fffaf0",
  60. "forestgreen": "228b22",
  61. "fuchsia": "f0f",
  62. "gainsboro": "dcdcdc",
  63. "ghostwhite": "f8f8ff",
  64. "gold": "ffd700",
  65. "goldenrod": "daa520",
  66. "gray": "808080",
  67. "green": "008000",
  68. "greenyellow": "adff2f",
  69. "grey": "808080",
  70. "honeydew": "f0fff0",
  71. "hotpink": "ff69b4",
  72. "indianred": "cd5c5c",
  73. "indigo": "4b0082",
  74. "ivory": "fffff0",
  75. "khaki": "f0e68c",
  76. "lavender": "e6e6fa",
  77. "lavenderblush": "fff0f5",
  78. "lawngreen": "7cfc00",
  79. "lemonchiffon": "fffacd",
  80. "lightblue": "add8e6",
  81. "lightcoral": "f08080",
  82. "lightcyan": "e0ffff",
  83. "lightgoldenrodyellow": "fafad2",
  84. "lightgray": "d3d3d3",
  85. "lightgreen": "90ee90",
  86. "lightgrey": "d3d3d3",
  87. "lightpink": "ffb6c1",
  88. "lightsalmon": "ffa07a",
  89. "lightseagreen": "20b2aa",
  90. "lightskyblue": "87cefa",
  91. "lightslategray": "789",
  92. "lightslategrey": "789",
  93. "lightsteelblue": "b0c4de",
  94. "lightyellow": "ffffe0",
  95. "lime": "0f0",
  96. "limegreen": "32cd32",
  97. "linen": "faf0e6",
  98. "magenta": "f0f",
  99. "maroon": "800000",
  100. "mediumaquamarine": "66cdaa",
  101. "mediumblue": "0000cd",
  102. "mediumorchid": "ba55d3",
  103. "mediumpurple": "9370db",
  104. "mediumseagreen": "3cb371",
  105. "mediumslateblue": "7b68ee",
  106. "mediumspringgreen": "00fa9a",
  107. "mediumturquoise": "48d1cc",
  108. "mediumvioletred": "c71585",
  109. "midnightblue": "191970",
  110. "mintcream": "f5fffa",
  111. "mistyrose": "ffe4e1",
  112. "moccasin": "ffe4b5",
  113. "navajowhite": "ffdead",
  114. "navy": "000080",
  115. "oldlace": "fdf5e6",
  116. "olive": "808000",
  117. "olivedrab": "6b8e23",
  118. "orange": "ffa500",
  119. "orangered": "ff4500",
  120. "orchid": "da70d6",
  121. "palegoldenrod": "eee8aa",
  122. "palegreen": "98fb98",
  123. "paleturquoise": "afeeee",
  124. "palevioletred": "db7093",
  125. "papayawhip": "ffefd5",
  126. "peachpuff": "ffdab9",
  127. "peru": "cd853f",
  128. "pink": "ffc0cb",
  129. "plum": "dda0dd",
  130. "powderblue": "b0e0e6",
  131. "purple": "800080",
  132. "red": "f00",
  133. "rosybrown": "bc8f8f",
  134. "royalblue": "4169e1",
  135. "saddlebrown": "8b4513",
  136. "salmon": "fa8072",
  137. "sandybrown": "f4a460",
  138. "seagreen": "2e8b57",
  139. "seashell": "fff5ee",
  140. "sienna": "a0522d",
  141. "silver": "c0c0c0",
  142. "skyblue": "87ceeb",
  143. "slateblue": "6a5acd",
  144. "slategray": "708090",
  145. "slategrey": "708090",
  146. "snow": "fffafa",
  147. "springgreen": "00ff7f",
  148. "steelblue": "4682b4",
  149. "tan": "d2b48c",
  150. "teal": "008080",
  151. "thistle": "d8bfd8",
  152. "tomato": "ff6347",
  153. "turquoise": "40e0d0",
  154. "violet": "ee82ee",
  155. "wheat": "f5deb3",
  156. "white": "fff",
  157. "whitesmoke": "f5f5f5",
  158. "yellow": "ff0",
  159. "yellowgreen": "9acd32"
  160. }, _RGBAtoCSS;
  161. /**
  162. * @constructor
  163. */
  164. function xColor(color) {
  165. /**
  166. * normalize function
  167. * @param {(number|string)=} n (optional)
  168. * @param {(number|string)=} s (optional)
  169. */
  170. function _normalize(n, s) {
  171. var m;
  172. if (undefined !== n) {
  173. n = parseFloat(n);
  174. }
  175. if (undefined === s) {
  176. s = 255;
  177. m = 255;
  178. } else if (1 === s) {
  179. if (undefined === n || 1 === n) {
  180. return 1;
  181. }
  182. s = 100;
  183. m = 1;
  184. } else {
  185. m = s;
  186. }
  187. if (isNaN(n) || n <= 0) {
  188. return 0;
  189. }
  190. if (s < n) {
  191. return m;
  192. }
  193. if (n < 1 || 1 === s) {
  194. if (1 === m) {
  195. return n;
  196. } else {
  197. return (n * m) | 0;
  198. }
  199. }
  200. return n * m / s;
  201. }
  202. function _hsl(h,s,l) {
  203. h = _normalize(h, 360) / 360;
  204. s = _normalize(s, 1);
  205. l = _normalize(l, 1);
  206. if (0 === s) {
  207. l = Math.round(255 * l);
  208. return [l, l, l];
  209. }
  210. function _hue(v1, v2, h) {
  211. h = ++h % 1;
  212. if (6 * h < 1) return v1 + (v2 - v1) * 6 * h;
  213. if (2 * h < 1) return v2;
  214. if (3 * h < 2) return v1 + (v2 - v1) * (4 - 6 * h);
  215. return v1;
  216. }
  217. var v = l < .5 ? (l + l * s) : (l + s - l * s);
  218. var m = l + l - v;
  219. return [
  220. Math.round(255 *_hue(m, v, h + 1 / 3)),
  221. Math.round(255 *_hue(m, v, h)),
  222. Math.round(255 *_hue(m, v, h - 1 / 3)) ];
  223. }
  224. function _hsv(h,s,v) {
  225. h = _normalize(h, 360) / 60;
  226. s = _normalize(s, 1);
  227. v = _normalize(v, 1);
  228. var hi = h|0;
  229. var f = h - hi;
  230. var p = Math.round(255 * v * (1 - s));
  231. var q = Math.round(255 * v * (1 - s * f));
  232. var t = Math.round(255 * v * (1 - s * (1 - f)));
  233. v = Math.round(255 * v);
  234. switch(hi) {
  235. case 1:
  236. return [q, v, p];
  237. case 2:
  238. return [p, v, t];
  239. case 3:
  240. return [p, q, v];
  241. case 4:
  242. return [t, p, v];
  243. case 5:
  244. return [v, p, q];
  245. }
  246. return [v, t, p];
  247. }
  248. this["setColor"] = function (color) {
  249. this.success = true;
  250. if ("number" === typeof color) {
  251. this["a"] =((color >> 24) & 0xff) / 255;
  252. this["r"] = (color >> 16) & 0xff;
  253. this["g"] = (color >> 8) & 0xff;
  254. this["b"] = (color ) & 0xff;
  255. return;
  256. }
  257. while ("object" === typeof color) {
  258. if (0 in color && 1 in color && 2 in color) {
  259. this["a"] = _normalize(color[3], 1);
  260. this["r"] = _normalize(color[0]);
  261. this["g"] = _normalize(color[1]);
  262. this["b"] = _normalize(color[2]);
  263. return;
  264. } else if ('r' in color && 'g' in color && 'b' in color) {
  265. this["a"] = _normalize(color["a"], 1);
  266. this["r"] = _normalize(color["r"]);
  267. this["g"] = _normalize(color["g"]);
  268. this["b"] = _normalize(color["b"]);
  269. return;
  270. } else if ('h' in color && 's' in color) {
  271. var rgb;
  272. if ('l' in color) {
  273. rgb = _hsl(color["h"], color["s"], color["l"]);
  274. } else if ('v' in color) {
  275. rgb = _hsv(color["h"], color["s"], color["v"]);
  276. } else if ('b' in color) {
  277. rgb = _hsv(color["h"], color["s"], color["b"]);
  278. } else {
  279. break;
  280. }
  281. this["a"] = _normalize(color["a"], 1);
  282. this["r"] = rgb[0];
  283. this["g"] = rgb[1];
  284. this["b"] = rgb[2];
  285. return;
  286. }
  287. break;
  288. }
  289. if ("string" === typeof color) {
  290. color = color.toLowerCase().replace(/[^a-z0-9,.()#%]/g, '');
  291. var part, c;
  292. if ('transparent' === color) {
  293. this["a"] = /* void */
  294. this["r"] = /* void */
  295. this["g"] = /* void */
  296. this["b"] = 0;
  297. return;
  298. }
  299. if ('rand' === color) {
  300. c = Math.random() * 0xffffff|0;
  301. this["a"] = 1;
  302. this["r"] = ((c >> 16) & 0xff);
  303. this["g"] = ((c >> 8) & 0xff);
  304. this["b"] = ((c ) & 0xff);
  305. return;
  306. }
  307. if (undefined !== color_names[color]) {
  308. color = '#' + color_names[color];
  309. }
  310. // #ff9000, #ff0000
  311. if ((part = /^#?([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/.exec(color))) {
  312. this["a"] = 1;
  313. this["r"] = parseInt(part[1], 16);
  314. this["g"] = parseInt(part[2], 16);
  315. this["b"] = parseInt(part[3], 16);
  316. return;
  317. }
  318. // #f00, fff
  319. if ((part = /^#?([0-9a-f])([0-9a-f])([0-9a-f])$/.exec(color))) {
  320. this["a"] = 1;
  321. this["r"] = parseInt(part[1] + part[1], 16);
  322. this["g"] = parseInt(part[2] + part[2], 16);
  323. this["b"] = parseInt(part[3] + part[3], 16);
  324. return;
  325. }
  326. // rgb(1, 234, 56)
  327. if ((part = /^rgba?\((\d{1,3}),(\d{1,3}),(\d{1,3})(,([0-9.]+))?\)$/.exec(color))) {
  328. this["a"] = _normalize(part[5], 1);
  329. this["r"] = _normalize(part[1]);
  330. this["g"] = _normalize(part[2]);
  331. this["b"] = _normalize(part[3]);
  332. return;
  333. }
  334. // rgb(66%, 55%, 44%) in [0,100]%, [0,100]%, [0,100]%
  335. if ((part = /^rgba?\(([0-9.]+\%),([0-9.]+\%),([0-9.]+\%)(,([0-9.]+)\%?)?\)$/.exec(color))) {
  336. this["a"] = _normalize(part[5], 1);
  337. this["r"] = Math.round(2.55 * _normalize(part[1], 100));
  338. this["g"] = Math.round(2.55 * _normalize(part[2], 100));
  339. this["b"] = Math.round(2.55 * _normalize(part[3], 100));
  340. return;
  341. }
  342. // hsv(64, 40, 16) in [0, 360], [0,100], [0,100]
  343. if ((part = /^hs([bvl])a?\((\d{1,3}),(\d{1,3}),(\d{1,3})(,([0-9.]+))?\)$/.exec(color))) {
  344. var func;
  345. if ("l" === part[1]) {
  346. func = _hsl;
  347. } else {
  348. func = _hsv;
  349. }
  350. c = func(parseInt(part[2], 10), parseInt(part[3], 10), parseInt(part[4], 10));
  351. this["a"] = _normalize(part[6], 1);
  352. this["r"] = c[0];
  353. this["g"] = c[1];
  354. this["b"] = c[2];
  355. return;
  356. }
  357. // 1, 234, 56
  358. if ((part = /^(\d{1,3}),(\d{1,3}),(\d{1,3})(,([0-9.]+))?$/.exec(color))) {
  359. this["a"] = _normalize(part[5], 1);
  360. this["r"] = _normalize(part[1]);
  361. this["g"] = _normalize(part[2]);
  362. this["b"] = _normalize(part[3]);
  363. return;
  364. }
  365. }
  366. this.success = false;
  367. };
  368. this["getColor"] = function (type) {
  369. if (undefined !== type) switch (type.toLowerCase()) {
  370. case "rgb":
  371. return this["getRGB"]();
  372. case "hsv":
  373. case "hsb":
  374. return this["getHSV"]();
  375. case "hsl":
  376. return this["getHSL"]();
  377. case "int":
  378. return this["getInt"]();
  379. case "array":
  380. return this["getArray"]();
  381. case "fraction":
  382. return this["getFraction"]();
  383. case "css":
  384. case "style":
  385. return this["getCSS"]();
  386. case "name":
  387. return this["getName"]();
  388. }
  389. return this["getHex"]();
  390. };
  391. this["getRGB"] = function () {
  392. if (this.success) {
  393. return {
  394. "r": this["r"],
  395. "g": this["g"],
  396. "b": this["b"],
  397. "a": this["a"]
  398. };
  399. }
  400. return null;
  401. };
  402. this["getCSS"] = function () {
  403. if (this.success) {
  404. if (0 === this["a"]) {
  405. return "transparent";
  406. }
  407. if (1 === this["a"]) {
  408. return 'rgb(' + this["r"] + ',' + this["g"] + ',' + this["b"] + ')';
  409. }
  410. return _RGBAtoCSS(this["r"], this["g"], this["b"], this["a"]);
  411. }
  412. return null;
  413. };
  414. this["getArray"] = function () {
  415. if (this.success) {
  416. return [this["r"], this["g"], this["b"], 100 * this["a"]|0];
  417. }
  418. return null;
  419. };
  420. this["getName"] = function () {
  421. if (this.success) {
  422. var lowest = null;
  423. var lowest_ndx;
  424. var table = color_names;
  425. var a = this["getHSL"]();
  426. for (var i in table) {
  427. /* We do not handle transparency */
  428. var b = new xColor(table[i])["getHSL"]();
  429. var tmp = Math.sqrt(.5 * (a["h"] - b["h"]) * (a["h"] - b["h"]) + .5 * (a["s"] - b["s"]) * (a["s"] - b["s"]) + (a["l"] - b["l"]) * (a["l"] - b["l"]));
  430. if (null === lowest || tmp < lowest) {
  431. lowest = tmp;
  432. lowest_ndx = i;
  433. }
  434. }
  435. return lowest_ndx;
  436. }
  437. return null;
  438. };
  439. this["getFraction"] = function () {
  440. if (this.success) {
  441. return {
  442. "r": this["r"] / 255,
  443. "g": this["g"] / 255,
  444. "b": this["b"] / 255,
  445. "a": this["a"]
  446. };
  447. }
  448. return null;
  449. };
  450. this["getHSL"] = function () {
  451. // inspiration: http://130.113.54.154/~monger/hsl-rgb.html
  452. if (this.success) {
  453. var r = this["r"] / 255;
  454. var g = this["g"] / 255;
  455. var b = this["b"] / 255;
  456. var min = Math.min(r, g, b);
  457. var max = Math.max(r, g, b);
  458. var delta = max - min;
  459. var h, s, l = (max + min) / 2;
  460. if (0 === delta) {
  461. h = 0;
  462. s = 0;
  463. } else {
  464. if (r === max) {
  465. h = (g - b) / delta;
  466. } else if (g === max) {
  467. h = 2 + (b - r) / delta;
  468. } else {
  469. h = 4 + (r - g) / delta;
  470. }
  471. s = delta / (l < .5 ? max + min : 2 - max - min);
  472. }
  473. return {
  474. "h": Math.round( 60 * ((6 + h) % 6)),
  475. "s": Math.round(100 * s),
  476. "l": Math.round(100 * l),
  477. "a": this["a"]
  478. };
  479. }
  480. return null;
  481. };
  482. this["getHSV"] = function () {
  483. if (this.success) {
  484. var r = this["r"] / 255;
  485. var g = this["g"] / 255;
  486. var b = this["b"] / 255;
  487. /*
  488. if (r > g) {
  489. max = r;
  490. min = g;
  491. } else {
  492. min = r;
  493. max = g;
  494. }
  495. if (b > max)
  496. max = b;
  497. if (b < min)
  498. min = b;
  499. */
  500. var min = Math.min(r, g, b);
  501. var max = Math.max(r, g, b);
  502. var delta = max - min;
  503. var h, s, v = max;
  504. if (0 === max) {
  505. s = 0;
  506. } else {
  507. s = delta / max;
  508. }
  509. if (0 === delta) {
  510. h = 0;
  511. } else if (r === max) {
  512. h = (g - b) / delta;
  513. } else if (g === max) {
  514. h = 2 + (b - r) / delta;
  515. } else {
  516. h = 4 + (r - g) / delta;
  517. }
  518. return {
  519. "h": Math.round( 60 * ((6 + h) % 6)),
  520. "s": Math.round(100 * s),
  521. "v": Math.round(100 * v),
  522. "a": this["a"]
  523. };
  524. }
  525. return null;
  526. };
  527. this["getHex"] = function () {
  528. if (this.success) {
  529. var chars = "0123456789abcdef";
  530. var r1 = this["r"] >> 4;
  531. var g1 = this["g"] >> 4;
  532. var b1 = this["b"] >> 4;
  533. var r2 = this["r"] & 0xf;
  534. var g2 = this["g"] & 0xf;
  535. var b2 = this["b"] & 0xf;
  536. return '#'
  537. + chars.charAt(r1) + chars.charAt(r2)
  538. + chars.charAt(g1) + chars.charAt(g2)
  539. + chars.charAt(b1) + chars.charAt(b2);
  540. }
  541. return null;
  542. };
  543. this["getInt"] = function (alpha) {
  544. if (this.success) {
  545. if (undefined !== alpha) {
  546. return ((100 * this["a"]|0) << 24 ^ this["r"] << 16 ^ this["g"] << 8 ^ this["b"]);
  547. }
  548. return (this["r"] << 16 ^ this["g"] << 8 ^ this["b"]) & 0xffffff;
  549. }
  550. return null;
  551. };
  552. this["toString"] = function () {
  553. return this["getHex"]();
  554. };
  555. this["setColor"](color);
  556. }
  557. $["each"](['color', 'backgroundColor', 'borderColor', 'borderTopColor', 'borderBottomColor', 'borderLeftColor', 'borderRightColor', 'outlineColor'], function(i, attr) {
  558. $["cssHooks"][attr] = {
  559. "set": function(elem, value) {
  560. elem["style"][attr] = (new xColor(value))["getCSS"]();
  561. }
  562. };
  563. $["fx"]["step"][attr] = function(fx) {
  564. if (undefined === fx["xinit"]) {
  565. if ("string" === typeof fx["end"] && -1 !== fx["end"].indexOf(";")) {
  566. var x, arr = fx["end"].split(";");
  567. if (arr.length > 2) {
  568. for (x in arr) {
  569. if (-1 === arr[x].indexOf('native')) {
  570. arr[x] = new xColor(arr[x]);
  571. } else {
  572. arr[x] = findColor(fx["elem"], attr);
  573. }
  574. }
  575. fx["start"] = null;
  576. fx["end"] = arr;
  577. } else {
  578. fx["start"] = new xColor(arr[0]);
  579. fx["end"] = new xColor(arr[1]);
  580. }
  581. } else {
  582. fx["start"] = findColor(fx["elem"], attr);
  583. fx["end"] = new xColor(fx["end"]);
  584. }
  585. fx["xinit"] = 1;
  586. }
  587. var S = fx["start"];
  588. var E = fx["end"];
  589. var P = fx["pos"];
  590. if (null === S) {
  591. var m = P * (E.length - 1),
  592. n = P < 1 ? m | 0 : E.length - 2;
  593. S = E[n];
  594. E = E[n + 1];
  595. P = m - n;
  596. }
  597. fx["elem"]["style"][attr] =_RGBAtoCSS(
  598. S["r"] + P * (E["r"] - S["r"])|0,
  599. S["g"] + P * (E["g"] - S["g"])|0,
  600. S["b"] + P * (E["b"] - S["b"])|0,
  601. S["a"] + P * (E["a"] - S["a"])
  602. );
  603. }
  604. });
  605. $(function() {
  606. var div = document.createElement("div"),
  607. div_style = div["style"];
  608. _RGBAtoCSS = function(r, g, b, a) {
  609. return "rgba(" + r + "," + g + "," + b + "," + a + ")";
  610. };
  611. div_style["cssText"] = "background-color:rgba(1,1,1,.5)";
  612. if (!($["support"]["rgba"] = div_style["backgroundColor"].indexOf("rgba") > -1)) {
  613. _RGBAtoCSS = function(r, g, b) {
  614. return "rgb(" + r + "," + g + "," + b + ")";
  615. };
  616. }
  617. });
  618. function findColor(elem, attr) {
  619. var color = "";
  620. do {
  621. color = $["css"](elem, attr);
  622. if ("" !== color && "transparent" !== color && "rgba(0, 0, 0, 0)" !== color || $["nodeName"](elem, "body")) break;
  623. } while ((elem = elem["parentNode"]));
  624. if ("" === color) {
  625. if ($["support"]["rgba"]) {
  626. color = "transparent";
  627. } else if ("backgroundColor" === attr) {
  628. color = "white";
  629. } else {
  630. color = "black";
  631. }
  632. }
  633. return new xColor(color);
  634. }
  635. /**
  636. * @constructor
  637. */
  638. function xColorMix() {
  639. this["test"] = function (col) {
  640. var c = new xColor(col);
  641. if (c.success) {
  642. return c;
  643. }
  644. return null;
  645. };
  646. this["red"] = function (col) {
  647. var c = new xColor(col);
  648. if (c.success) {
  649. c["g"] = 0xff;
  650. c["b"] = 0xff;
  651. return c;
  652. }
  653. return null;
  654. };
  655. this["blue"] = function (col) {
  656. var c = new xColor(col);
  657. if (c.success) {
  658. c["r"] = 0xff;
  659. c["g"] = 0xff;
  660. return c;
  661. }
  662. return null;
  663. };
  664. this["green"] = function (col) {
  665. var c = new xColor(col);
  666. if (c.success) {
  667. c["r"] = 0xff;
  668. c["b"] = 0xff;
  669. return c;
  670. }
  671. return null;
  672. };
  673. this["sepia"] = function(col) {
  674. var c = new xColor(col);
  675. // Microsoft's sepia function http://msdn.microsoft.com/en-us/magazine/cc163866.aspx
  676. if (c.success) {
  677. var r = c["r"], g = c["g"], b = c["b"];
  678. c["r"] = Math.round(r * .393 + g * .769 + b * .189);
  679. c["g"] = Math.round(r * .349 + g * .686 + b * .168);
  680. c["b"] = Math.round(r * .272 + g * .534 + b * .131);
  681. return c;
  682. }
  683. return null;
  684. };
  685. this["random"] = function () {
  686. return new xColor([
  687. (255 * Math.random())|0,
  688. (255 * Math.random())|0,
  689. (255 * Math.random())|0
  690. ]);
  691. };
  692. this["inverse"] = function (col) {
  693. var c = new xColor(col);
  694. if (c.success) {
  695. c["r"]^= 0xff;
  696. c["g"]^= 0xff;
  697. c["b"]^= 0xff;
  698. return c;
  699. }
  700. return null;
  701. };
  702. this["opacity"] = function (x, y, o) {
  703. var a = new xColor(x);
  704. var b = new xColor(y);
  705. if (a.success & b.success) {
  706. if (o > 1) {
  707. o/= 100;
  708. }
  709. o = Math.max(o - 1 + b["a"], 0);
  710. a["r"] = Math.round((b["r"] - a["r"]) * o + a["r"]);
  711. a["g"] = Math.round((b["g"] - a["g"]) * o + a["g"]);
  712. a["b"] = Math.round((b["b"] - a["b"]) * o + a["b"]);
  713. return a;
  714. }
  715. return null;
  716. };
  717. this["greyfilter"] = function (col, formula) {
  718. var v, c = new xColor(col);
  719. if (c.success) {
  720. switch (formula) {
  721. case 1:
  722. // My own formula
  723. v = .35 + 13 * (c["r"] + c["g"] + c["b"]) / 60;
  724. break;
  725. case 2:
  726. // Sun's formula: (1 - avg) / (100 / 35) + avg)
  727. v = (13 * (c["r"] + c["g"] + c["b"]) + 5355) / 60;
  728. break;
  729. default:
  730. v = c["r"] * .3 + c["g"] * .59 + c["b"] * .11;
  731. }
  732. c["r"] = c["g"] = c["b"] = Math.min(v|0, 255);
  733. return c;
  734. }
  735. return null;
  736. };
  737. this["webround"] = function (col) {
  738. var c = new xColor(col);
  739. if (c.success) {
  740. if ((c["r"]+= 0x33 - c["r"] % 0x33) > 0xff) c["r"] = 0xff;
  741. if ((c["g"]+= 0x33 - c["g"] % 0x33) > 0xff) c["g"] = 0xff;
  742. if ((c["b"]+= 0x33 - c["b"] % 0x33) > 0xff) c["b"] = 0xff;
  743. return c;
  744. }
  745. return null;
  746. };
  747. this["distance"] = function (x, y) {
  748. var a = new xColor(x);
  749. var b = new xColor(y);
  750. if (a.success & b.success) {
  751. // Approximation attempt of http://www.compuphase.com/cmetric.htm
  752. return Math.sqrt(3 * (b["r"] - a["r"]) * (b["r"] - a["r"]) + 4 * (b["g"] - a["g"]) * (b["g"] - a["g"]) + 2 * (b["b"] - a["b"]) * (b["b"] - a["b"]));
  753. }
  754. return null;
  755. };
  756. this["readable"] = function (bg, col, size) {
  757. // good ressource: http://www.hgrebdes.com/colour/spectrum/colourvisibility.html
  758. var a = new xColor(col);
  759. var b = new xColor(bg);
  760. size = size || 10;
  761. if (a.success & b.success) {
  762. // but here's my version based on the idea:
  763. var diff = b["r"] * 0.299 + b["g"] * 0.587 + b["b"] * 0.114 -
  764. a["r"] * 0.299 - a["g"] * 0.587 - a["b"] * 0.114;
  765. return !((diff < (1.5 + 141.162 * Math.pow(0.975, size)))
  766. && (diff > (-.5 - 154.709 * Math.pow(0.990, size))));
  767. }
  768. return null;
  769. };
  770. this["combine"] = function (x, y) {
  771. var a = new xColor(x);
  772. var b = new xColor(y);
  773. if (a.success & b.success) {
  774. a["r"]^= b["r"];
  775. a["g"]^= b["g"];
  776. a["b"]^= b["b"];
  777. return a;
  778. }
  779. return null;
  780. };
  781. this["breed"] = function (x, y) {
  782. var a = new xColor(x);
  783. var b = new xColor(y);
  784. var mask = 0, i = 6;
  785. if (a.success & b.success) {
  786. while (i--) {
  787. if (Math.random() < .5) {
  788. mask|= 0x0f << (i << 2);
  789. }
  790. }
  791. a["r"] = (a["r"] & ((mask >> 0x10) & 0xff)) | (b["r"] & (((mask >> 0x10) & 0xff) ^ 0xff));
  792. a["g"] = (a["g"] & ((mask >> 0x08) & 0xff)) | (b["g"] & (((mask >> 0x08) & 0xff) ^ 0xff));
  793. a["b"] = (a["b"] & ((mask >> 0x00) & 0xff)) | (b["b"] & (((mask >> 0x00) & 0xff) ^ 0xff));
  794. return a;
  795. }
  796. return null;
  797. };
  798. this["additive"] = function (x, y) {
  799. var a = new xColor(x);
  800. var b = new xColor(y);
  801. if (a.success & b.success) {
  802. if ((a["r"]+= b["r"]) > 0xff) a["r"] = 0xff;
  803. if ((a["g"]+= b["g"]) > 0xff) a["g"] = 0xff;
  804. if ((a["b"]+= b["b"]) > 0xff) a["b"] = 0xff;
  805. return a;
  806. }
  807. return null;
  808. };
  809. this["subtractive"] = function (x, y) {
  810. var a = new xColor(x);
  811. var b = new xColor(y);
  812. if (a.success & b.success) {
  813. if ((a["r"]+= b["r"] - 0xff) < 0) a["r"] = 0;
  814. if ((a["g"]+= b["g"] - 0xff) < 0) a["g"] = 0;
  815. if ((a["b"]+= b["b"] - 0xff) < 0) a["b"] = 0;
  816. return a;
  817. }
  818. return null;
  819. };
  820. this["subtract"] = function (x, y) {
  821. var a = new xColor(x);
  822. var b = new xColor(y);
  823. if (a.success & b.success) {
  824. if ((a["r"]-= b["r"]) < 0) a["r"] = 0;
  825. if ((a["g"]-= b["g"]) < 0) a["g"] = 0;
  826. if ((a["b"]-= b["b"]) < 0) a["b"] = 0;
  827. return a;
  828. }
  829. return null;
  830. };
  831. this["multiply"] = function (x, y) {
  832. var a = new xColor(x);
  833. var b = new xColor(y);
  834. if (a.success & b.success) {
  835. a["r"] = (a["r"] / 255 * b["r"])|0;
  836. a["g"] = (a["g"] / 255 * b["g"])|0;
  837. a["b"] = (a["b"] / 255 * b["b"])|0;
  838. return a;
  839. }
  840. return null;
  841. };
  842. this["average"] = function (x, y) {
  843. var a = new xColor(x);
  844. var b = new xColor(y);
  845. if (a.success & b.success) {
  846. a["r"] = (a["r"] + b["r"]) >> 1;
  847. a["g"] = (a["g"] + b["g"]) >> 1;
  848. a["b"] = (a["b"] + b["b"]) >> 1;
  849. return a;
  850. }
  851. return null;
  852. };
  853. this["triad"] = function (col) {
  854. var c = new xColor(col);
  855. if (c.success) {
  856. return [c,
  857. new xColor([c["b"], c["r"], c["g"]]),
  858. new xColor([c["g"], c["b"], c["r"]])];
  859. }
  860. return null;
  861. };
  862. this["tetrad"] = function (col) {
  863. var c = new xColor(col);
  864. if (c.success) {
  865. return [c,
  866. new xColor([c["b"], c["r"], c["b"]]),
  867. new xColor([c["b"], c["g"], c["r"]]),
  868. new xColor([c["r"], c["b"], c["r"]])];
  869. }
  870. return null;
  871. };
  872. this["gradientlevel"] = function (x, y, level, deg) {
  873. if (undefined === deg) deg = 1;
  874. if (level > deg) return null;
  875. var a = new xColor(x);
  876. var b = new xColor(y);
  877. if (a.success & b.success) {
  878. a["r"] = (a["r"] + ((b["r"] - a["r"]) / deg) * level)|0;
  879. a["g"] = (a["g"] + ((b["g"] - a["g"]) / deg) * level)|0;
  880. a["b"] = (a["b"] + ((b["b"] - a["b"]) / deg) * level)|0;
  881. return a;
  882. }
  883. return null;
  884. };
  885. this["gradientarray"] = function(arr, level, deg) {
  886. if (level > deg || !arr.length) return null;
  887. if (arr.length == 1) {
  888. return new xColor(arr[0]);
  889. }
  890. var e = level * (arr.length - 1) / (deg + 1) | 0;
  891. var step = deg / (arr.length - 1);
  892. return $["xcolor"]["gradientlevel"](arr[e], arr[e + 1], level - e * step, step);
  893. };
  894. this["nearestname"] = function (a) {
  895. a = new xColor(a);
  896. if (a.success) {
  897. return a["getName"]();
  898. }
  899. return null;
  900. };
  901. this["darken"] = function (col, by, shade) {
  902. if (undefined === by) {
  903. by = 1;
  904. } else if (by < 0) return this["lighten"](col, -by, shade);
  905. if (undefined === shade) {
  906. shade = 32;
  907. }
  908. var c = new xColor(col);
  909. if (c.success) {
  910. if ((c["r"]-= shade * by) < 0) c["r"] = 0;
  911. if ((c["g"]-= shade * by) < 0) c["g"] = 0;
  912. if ((c["b"]-= shade * by) < 0) c["b"] = 0;
  913. return c;
  914. }
  915. return null;
  916. };
  917. this["lighten"] = function (col, by, shade) {
  918. if (undefined === by) {
  919. by = 1;
  920. } else if (by < 0) return this["darken"](col, -by, shade);
  921. if (undefined === shade) {
  922. shade = 32;
  923. }
  924. var c = new xColor(col);
  925. if (c.success) {
  926. if ((c["r"]+= shade * by) > 0xff) c["r"] = 0xff;
  927. if ((c["g"]+= shade * by) > 0xff) c["g"] = 0xff;
  928. if ((c["b"]+= shade * by) > 0xff) c["b"] = 0xff;
  929. return c;
  930. }
  931. return null;
  932. };
  933. this["analogous"] = function (col, results, slices) {
  934. if (undefined === results) {
  935. results = 8;
  936. }
  937. if (undefined === slices) {
  938. slices = 30;
  939. }
  940. var c = new xColor(col);
  941. if (c.success) {
  942. var hsv = c["getHSV"]();
  943. var part = 360 / slices, ret = [ c ];
  944. for (hsv["h"] = ((hsv["h"] - (part * results >> 1)) + 720) % 360; --results; ) {
  945. hsv["h"]+= part;
  946. hsv["h"]%= 360;
  947. ret.push(new xColor(hsv));
  948. }
  949. return ret;
  950. }
  951. return null;
  952. };
  953. this["complementary"] = function(col) {
  954. var c = new xColor(col);
  955. if(c.success) {
  956. var hsl = c["getHSL"]();
  957. hsl["h"] = (hsl["h"] + 180) % 360;
  958. return new xColor(hsl);
  959. }
  960. return null;
  961. };
  962. this["splitcomplement"] = function (col) {
  963. var c = new xColor(col);
  964. if (c.success) {
  965. var hsv = c["getHSV"]();
  966. var ret = [ c ];
  967. hsv["h"]+= 72;
  968. hsv["h"]%= 360;
  969. ret.push(new xColor(hsv));
  970. hsv["h"]+= 144;
  971. hsv["h"]%= 360;
  972. ret.push(new xColor(hsv));
  973. return ret;
  974. }
  975. return null;
  976. };
  977. this["monochromatic"] = function (col, results) {
  978. if (undefined === results) {
  979. results = 6;
  980. }
  981. var c = new xColor(col);
  982. if (c.success) {
  983. var hsv = c["getHSV"]();
  984. var ret = [ c ];
  985. while (--results) {
  986. hsv["v"]+= 20;
  987. hsv["v"]%= 100;
  988. ret.push(new xColor(hsv));
  989. }
  990. return ret;
  991. }
  992. return null;
  993. };
  994. }
  995. $["xcolor"] = new xColorMix();
  996. $["fn"]["readable"] = function () {
  997. var elem = this[0];
  998. var f = "";
  999. var b = "";
  1000. do {
  1001. if ("" === f && ("transparent" === (f = $["css"](elem, "color")) || "rgba(0, 0, 0, 0)" === f)) {
  1002. f = "";
  1003. }
  1004. if ("" === b && ("transparent" === (b = $["css"](elem, "backgroundColor")) || "rgba(0, 0, 0, 0)" === b)) {
  1005. b = "";
  1006. }
  1007. if ("" !== f && "" !== b || $["nodeName"](elem, "body")) {
  1008. break;
  1009. }
  1010. } while ((elem = elem["parentNode"]));
  1011. if ("" === f) {
  1012. f = "black";
  1013. }
  1014. if ("" === b) {
  1015. b = "white";
  1016. }
  1017. // todo: if alpha != 1, use opacity() to calculate correct color on certain element and it's parent
  1018. return $["xcolor"]["readable"](b, f);
  1019. };
  1020. $["fn"]["colorize"] = function (FROM, TO, TYPE) {
  1021. var modifiers = {
  1022. // Returns number in [0, 1] (0 = FROM, 1 = TO)
  1023. "gradient": function (k, l, diff, c) {
  1024. return k / l;
  1025. },
  1026. "flip": function (k, l, diff, c) {
  1027. return (" " === c) ? diff : !diff;
  1028. },
  1029. "pillow": function (k, l, diff, c) {
  1030. k*= 2;
  1031. return (k <= l)
  1032. ? (k / l)
  1033. : (2 - k / l);
  1034. }
  1035. };
  1036. if ("function" === typeof TYPE) {
  1037. /* void */
  1038. } else if (void 0 === modifiers[TYPE]) {
  1039. return;
  1040. } else {
  1041. TYPE = modifiers[TYPE];
  1042. }
  1043. FROM = new xColor(FROM);
  1044. TO = new xColor(TO);
  1045. this["each"](function() {
  1046. var tmp = this.childNodes,
  1047. LEN = 0,
  1048. K = 0;
  1049. if (FROM.success & TO.success) {
  1050. for (var i = tmp.length; i--; LEN+= tmp[i]["textContent"].length){}
  1051. (function replace(node) {
  1052. var i = 0,
  1053. len;
  1054. if (3 === node.nodeType) {
  1055. var x = FROM;
  1056. var y = TO;
  1057. var l = LEN;
  1058. var elem, ctx, diff = 0, c, calc = TYPE;
  1059. len = node.nodeValue.length;
  1060. ctx = document.createElement('span');
  1061. for (i = 0; i < len; ++i) {
  1062. elem = document.createElement('span');
  1063. c = node.nodeValue.charAt(i);
  1064. diff = calc(K, l, diff, c);
  1065. elem["style"]["color"] =_RGBAtoCSS(
  1066. x["r"] + diff * (y["r"] - x["r"])|0,
  1067. x["g"] + diff * (y["g"] - x["g"])|0,
  1068. x["b"] + diff * (y["b"] - x["b"])|0,
  1069. x["a"] + diff * (y["a"] - x["a"])
  1070. );
  1071. elem.appendChild(document.createTextNode(
  1072. c
  1073. )
  1074. );
  1075. ctx.appendChild(elem);
  1076. ++K;
  1077. }
  1078. node.parentNode.replaceChild(ctx, node);
  1079. } else {
  1080. for (len = node.childNodes.length; i < len; ++i) {
  1081. replace(node.childNodes[i]);
  1082. }
  1083. }
  1084. })(this);
  1085. }
  1086. });
  1087. };
  1088. }(jQuery));