test.js 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. test("TinyColor initialization", function() {
  2. ok(typeof tinycolor != "undefined", "tinycolor is initialized on the page");
  3. ok(typeof tinycolor("red") == "object", "tinycolor is able to be instantiated");
  4. var r = tinycolor("red");
  5. ok(tinycolor(r) === r, "when given a tinycolor instance, tinycolor() returns it");
  6. ok(new tinycolor(r) === r, "when given a tinycolor instance, new tinycolor() returns it");
  7. equal(tinycolor("red", { format: "hex" }).toString(), "#ff0000", "tinycolor options are being parsed");
  8. equal(tinycolor.fromRatio({r: 1, g: 0, b: 0 }, { format: "hex" }).toString(), "#ff0000", "tinycolor options are being parsed");
  9. var obj = {h: 180, s: 0.5, l: 0.5};
  10. var color = tinycolor(obj);
  11. ok(obj.s === 0.5, "when given an object, the original object is not modified");
  12. });
  13. test("Original input", function() {
  14. var colorRgbUp = "RGB(39, 39, 39)";
  15. var colorRgbLow = "rgb(39, 39, 39)";
  16. var colorRgbMix = "RgB(39, 39, 39)";
  17. var tinycolorObj = tinycolor(colorRgbMix);
  18. var inputObj = {r:100,g:100,b:100};
  19. var r = tinycolor("red");
  20. ok(tinycolor(colorRgbLow).getOriginalInput() === colorRgbLow, "original lowercase input is returned");
  21. ok(tinycolor(colorRgbUp).getOriginalInput() === colorRgbUp, "original uppercase input is returned");
  22. ok(tinycolor(colorRgbMix).getOriginalInput() === colorRgbMix, "original mixed input is returned");
  23. ok(tinycolor(tinycolorObj).getOriginalInput() === colorRgbMix, "when given a tinycolor instance, the color string is returned");
  24. ok(tinycolor(inputObj).getOriginalInput() === inputObj, "when given an object, the object is returned");
  25. ok(new tinycolor("").getOriginalInput() === "", "when given an empty string, an empty string is returned");
  26. ok(new tinycolor(null).getOriginalInput() === "", "when given a null value, an empty string is returned");
  27. });
  28. test("Cloning color", function() {
  29. var originalColor = tinycolor("red");
  30. var originalColorRgbString = originalColor.toRgbString();
  31. var clonedColor = originalColor.clone();
  32. ok(clonedColor.toRgbString() === originalColor.toRgbString(), "cloned color is identical");
  33. clonedColor.setAlpha(0.5);
  34. ok(clonedColor.toRgbString() !== originalColor.toRgbString(), "cloned color is changing independently from original color");
  35. ok(originalColorRgbString === originalColor.toRgbString(), "original color was not changed by cloned color change");
  36. });
  37. // Taken from convertWikipediaColors.html
  38. var conversions = [
  39. {"hex":"#FFFFFF","hex8":"#FFFFFFFF","rgb":{"r":"100.0%","g":"100.0%","b":"100.0%"},"hsv":{"h":"0","s":"0.000","v":"1.000"},"hsl":{"h":"0","s":"0.000","l":"1.000"}},
  40. {"hex":"#808080","hex8":"#808080FF","rgb":{"r":"050.0%","g":"050.0%","b":"050.0%"},"hsv":{"h":"0","s":"0.000","v":"0.500"},"hsl":{"h":"0","s":"0.000","l":"0.500"}},
  41. {"hex":"#000000","hex8":"#000000FF","rgb":{"r":"000.0%","g":"000.0%","b":"000.0%"},"hsv":{"h":"0","s":"0.000","v":"0.000"},"hsl":{"h":"0","s":"0.000","l":"0.000"}},
  42. {"hex":"#FF0000","hex8":"#FF0000FF","rgb":{"r":"100.0%","g":"000.0%","b":"000.0%"},"hsv":{"h":"0.0","s":"1.000","v":"1.000"},"hsl":{"h":"0.0","s":"1.000","l":"0.500"}},
  43. {"hex":"#BFBF00","hex8":"#BFBF00FF","rgb":{"r":"075.0%","g":"075.0%","b":"000.0%"},"hsv":{"h":"60.0","s":"1.000","v":"0.750"},"hsl":{"h":"60.0","s":"1.000","l":"0.375"}},
  44. {"hex":"#008000","hex8":"#008000FF","rgb":{"r":"000.0%","g":"050.0%","b":"000.0%"},"hsv":{"h":"120.0","s":"1.000","v":"0.500"},"hsl":{"h":"120.0","s":"1.000","l":"0.250"}},
  45. {"hex":"#80FFFF","hex8":"#80FFFFFF","rgb":{"r":"050.0%","g":"100.0%","b":"100.0%"},"hsv":{"h":"180.0","s":"0.500","v":"1.000"},"hsl":{"h":"180.0","s":"1.000","l":"0.750"}},
  46. {"hex":"#8080FF","hex8":"#8080FFFF","rgb":{"r":"050.0%","g":"050.0%","b":"100.0%"},"hsv":{"h":"240.0","s":"0.500","v":"1.000"},"hsl":{"h":"240.0","s":"1.000","l":"0.750"}},
  47. {"hex":"#BF40BF","hex8":"#BF40BFFF","rgb":{"r":"075.0%","g":"025.0%","b":"075.0%"},"hsv":{"h":"300.0","s":"0.667","v":"0.750"},"hsl":{"h":"300.0","s":"0.500","l":"0.500"}},
  48. {"hex":"#A0A424","hex8":"#A0A424FF","rgb":{"r":"062.8%","g":"064.3%","b":"014.2%"},"hsv":{"h":"61.8","s":"0.779","v":"0.643"},"hsl":{"h":"61.8","s":"0.638","l":"0.393"}},
  49. {"hex":"#1EAC41","hex8":"#1EAC41FF","rgb":{"r":"011.6%","g":"067.5%","b":"025.5%"},"hsv":{"h":"134.9","s":"0.828","v":"0.675"},"hsl":{"h":"134.9","s":"0.707","l":"0.396"}},
  50. {"hex":"#B430E5","hex8":"#B430E5FF","rgb":{"r":"070.4%","g":"018.7%","b":"089.7%"},"hsv":{"h":"283.7","s":"0.792","v":"0.897"},"hsl":{"h":"283.7","s":"0.775","l":"0.542"}},
  51. {"hex":"#FEF888","hex8":"#FEF888FF","rgb":{"r":"099.8%","g":"097.4%","b":"053.2%"},"hsv":{"h":"56.9","s":"0.467","v":"0.998"},"hsl":{"h":"56.9","s":"0.991","l":"0.765"}},
  52. {"hex":"#19CB97","hex8":"#19CB97FF","rgb":{"r":"009.9%","g":"079.5%","b":"059.1%"},"hsv":{"h":"162.4","s":"0.875","v":"0.795"},"hsl":{"h":"162.4","s":"0.779","l":"0.447"}},
  53. {"hex":"#362698","hex8":"#362698FF","rgb":{"r":"021.1%","g":"014.9%","b":"059.7%"},"hsv":{"h":"248.3","s":"0.750","v":"0.597"},"hsl":{"h":"248.3","s":"0.601","l":"0.373"}},
  54. {"hex":"#7E7EB8","hex8":"#7E7EB8FF","rgb":{"r":"049.5%","g":"049.3%","b":"072.1%"},"hsv":{"h":"240.5","s":"0.316","v":"0.721"},"hsl":{"h":"240.5","s":"0.290","l":"0.607"}}
  55. ];
  56. module("Color translations");
  57. test("Color Equality", function() {
  58. for (var i = 0; i < conversions.length; i++) {
  59. var c = conversions[i];
  60. var tiny = tinycolor(c.hex);
  61. ok(true, tiny.isValid());
  62. ok(true,
  63. "Testing " + c.hex + ": " + tiny.toRgbString() + " " + tiny.toPercentageRgbString() + " " + tiny.toHsvString() + " " + tiny.toHslString() + " " + tiny.toHexString() +
  64. "Original: " + JSON.stringify(c.rgb) + " " + JSON.stringify(c.hsv) + " " + JSON.stringify(c.hsl)
  65. );
  66. ok(tinycolor.equals(c.rgb, c.hex), "RGB equals hex " + c.hex);
  67. ok(tinycolor.equals(c.rgb, c.hex8), "RGB equals hex " + c.hex);
  68. ok(tinycolor.equals(c.rgb, c.hsl), "RGB equals HSL " + c.hex);
  69. ok(tinycolor.equals(c.rgb, c.hsv), "RGB equals HSV " + c.hex);
  70. ok(tinycolor.equals(c.rgb, c.rgb), "RGB equals RGB " + c.hex);
  71. ok(tinycolor.equals(c.hex, c.hex), "hex equals hex " + c.hex);
  72. ok(tinycolor.equals(c.hex, c.hex8), "hex equals hex8 " + c.hex);
  73. ok(tinycolor.equals(c.hex, c.hsl), "hex equals HSL " + c.hex);
  74. ok(tinycolor.equals(c.hex, c.hsv), "hex equals HSV " + c.hex);
  75. ok(tinycolor.equals(c.hsl, c.hsv), "HSL equals HSV " + c.hex);
  76. }
  77. });
  78. module("Ratio Parsing");
  79. test("With Ratio", function() {
  80. equal(tinycolor.fromRatio({r: 1, g: 1, b: 1}).toHexString(), "#ffffff", "white");
  81. equal(tinycolor.fromRatio({r: 1, g: 0, b: 0, a: .5 }).toRgbString(), "rgba(255, 0, 0, 0.5)", "alpha works when ratio is parsed");
  82. equal(tinycolor.fromRatio({r: 1, g: 0, b: 0, a: 1 }).toRgbString(), "rgb(255, 0, 0)", "alpha = 1 works when ratio is parsed");
  83. equal(tinycolor.fromRatio({r: 1, g: 0, b: 0, a: 10 }).toRgbString(), "rgb(255, 0, 0)", "alpha > 1 works when ratio is parsed");
  84. equal(tinycolor.fromRatio({r: 1, g: 0, b: 0, a: -1 }).toRgbString(), "rgb(255, 0, 0)", "alpha < 1 works when ratio is parsed");
  85. });
  86. test("Without Ratio", function() {
  87. equal(tinycolor({r: 1, g: 1, b: 1}).toHexString(), "#010101", "010101");
  88. equal(tinycolor({r: .1, g: .1, b: .1}).toHexString(), "#000000", "000000");
  89. equal(tinycolor("rgb .1 .1 .1").toHexString(), "#000000", "000000");
  90. });
  91. module("String Parsing");
  92. test("RGB Text Parsing", function() {
  93. equal(tinycolor("rgb 255 0 0").toHexString(), "#ff0000", "spaced input");
  94. equal(tinycolor("rgb(255, 0, 0)").toHexString(), "#ff0000", "parenthesized input");
  95. equal(tinycolor("rgb (255, 0, 0)").toHexString(), "#ff0000", "parenthesized spaced input");
  96. equal(tinycolor({ r: 255, g: 0, b: 0 }).toHexString(), "#ff0000", "object input");
  97. deepEqual (tinycolor({ r: 255, g: 0, b: 0 }).toRgb(), { r: 255, g: 0, b: 0, a: 1 }, "object input and compare");
  98. ok(tinycolor.equals({r:200, g: 100, b: 0 }, "rgb(200, 100, 0)"));
  99. ok(tinycolor.equals({r:200, g: 100, b: 0 }, "rgb 200 100 0"));
  100. ok(tinycolor.equals({r:200, g: 100, b: 0 }, "rgb 200 100 0"));
  101. ok(tinycolor.equals({r:200, g: 100, b: 0, a: .4 }, "rgba 200 100 0 .4"));
  102. ok(!tinycolor.equals({r:199, g: 100, b: 0 }, "rgba 200 100 0 1"));
  103. ok(!tinycolor.equals({r:199, g: 100, b: 0 }, "rgb(200, 100, 0)"));
  104. ok(!tinycolor.equals({r:199, g: 100, b: 0 }, "rgb 200 100 0"));
  105. ok(!tinycolor.equals({r:199, g: 100, b: 0 }, "rgb 200 100 0"));
  106. ok(tinycolor.equals(tinycolor({r:200, g: 100, b: 0 }), "rgb(200, 100, 0)"));
  107. ok(tinycolor.equals(tinycolor({r:200, g: 100, b: 0 }), "rgb 200 100 0"));
  108. ok(tinycolor.equals(tinycolor({r:200, g: 100, b: 0 }), "rgb 200 100 0"));
  109. });
  110. test("Percentage RGB Text Parsing", function() {
  111. equal(tinycolor("rgb 100% 0% 0%").toHexString(), "#ff0000", "spaced input");
  112. equal(tinycolor("rgb(100%, 0%, 0%)").toHexString(), "#ff0000", "parenthesized input");
  113. equal(tinycolor("rgb (100%, 0%, 0%)").toHexString(), "#ff0000", "parenthesized spaced input");
  114. equal(tinycolor({ r: "100%", g: "0%", b: "0%" }).toHexString(), "#ff0000", "object input");
  115. deepEqual (tinycolor({ r: "100%", g: "0%", b: "0%" }).toRgb(), { r: 255, g: 0, b: 0, a: 1 }, "object input and compare");
  116. ok(tinycolor.equals({r:"90%", g: "45%", b: "0%" }, "rgb(90%, 45%, 0%)"));
  117. ok(tinycolor.equals({r:"90%", g: "45%", b: "0%" }, "rgb 90% 45% 0%"));
  118. ok(tinycolor.equals({r:"90%", g: "45%", b: "0%" }, "rgb 90% 45% 0%"));
  119. ok(tinycolor.equals({r:"90%", g: "45%", b: "0%", a: .4 }, "rgba 90% 45% 0% .4"));
  120. ok(!tinycolor.equals({r:"89%", g: "45%", b: "0%" }, "rgba 90% 45% 0% 1"));
  121. ok(!tinycolor.equals({r:"89%", g: "45%", b: "0%" }, "rgb(90%, 45%, 0%)"));
  122. ok(!tinycolor.equals({r:"89%", g: "45%", b: "0%" }, "rgb 90% 45% 0%"));
  123. ok(!tinycolor.equals({r:"89%", g: "45%", b: "0%" }, "rgb 90% 45% 0%"));
  124. ok(tinycolor.equals(tinycolor({r:"90%", g: "45%", b: "0%" }), "rgb(90%, 45%, 0%)"));
  125. ok(tinycolor.equals(tinycolor({r:"90%", g: "45%", b: "0%" }), "rgb 90% 45% 0%"));
  126. ok(tinycolor.equals(tinycolor({r:"90%", g: "45%", b: "0%" }), "rgb 90% 45% 0%"));
  127. });
  128. test("HSL parsing", function() {
  129. equal(tinycolor({ h: 251, s: 100, l: .38 }).toHexString(), "#2400c2", "to hex");
  130. equal(tinycolor({ h: 251, s: 100, l: .38 }).toRgbString(), "rgb(36, 0, 194)", "to rgb");
  131. equal(tinycolor({ h: 251, s: 100, l: .38 }).toHslString(), "hsl(251, 100%, 38%)", "to hsl");
  132. equal(tinycolor("hsl(251, 100, 38)").toHexString(), "#2400c2", "to hex");
  133. equal(tinycolor("hsl(251, 100%, 38%)").toRgbString(), "rgb(36, 0, 194)", "to rgb");
  134. equal(tinycolor("hsl(251, 100%, 38%)").toHslString(), "hsl(251, 100%, 38%)", "to hsl");
  135. equal(tinycolor("hsl 100 20 10").toHslString(), "hsl(100, 20%, 10%)", "problematic hsl");
  136. });
  137. test("Hex Parsing", function() {
  138. equal(tinycolor("rgb 255 0 0").toHexString(), "#ff0000");
  139. equal(tinycolor("rgb 255 0 0").toHexString(true), "#f00");
  140. equal(tinycolor("rgba 255 0 0 0.5").toHex8String(), "#ff000080");
  141. equal(tinycolor("rgba 255 0 0 0").toHex8String(), "#ff000000");
  142. equal(tinycolor("rgba 255 0 0 1").toHex8String(), "#ff0000ff");
  143. equal(tinycolor("rgba 255 0 0 1").toHex8String(true), "#f00f");
  144. equal(tinycolor("rgb 255 0 0").toHex(), "ff0000");
  145. equal(tinycolor("rgb 255 0 0").toHex(true), "f00");
  146. equal(tinycolor("rgba 255 0 0 0.5").toHex8(), "ff000080");
  147. });
  148. test("HSV Parsing", function() {
  149. equal(tinycolor("hsv 251.1 0.887 .918").toHsvString(), "hsv(251, 89%, 92%)");
  150. equal(tinycolor("hsv 251.1 0.887 0.918").toHsvString(), "hsv(251, 89%, 92%)");
  151. equal(tinycolor("hsva 251.1 0.887 0.918 0.5").toHsvString(), "hsva(251, 89%, 92%, 0.5)");
  152. });
  153. test("Invalid Parsing", function() {
  154. var invalidColor = tinycolor("this is not a color");
  155. equal(invalidColor.toHexString(), "#000000");
  156. equal(false, invalidColor.isValid());
  157. invalidColor = tinycolor("#red");
  158. equal(invalidColor.toHexString(), "#000000");
  159. equal(false, invalidColor.isValid());
  160. invalidColor = tinycolor(" #red");
  161. equal(invalidColor.toHexString(), "#000000");
  162. equal(false, invalidColor.isValid());
  163. invalidColor = tinycolor("##123456");
  164. equal(invalidColor.toHexString(), "#000000");
  165. equal(false, invalidColor.isValid());
  166. invalidColor = tinycolor(" ##123456");
  167. equal(invalidColor.toHexString(), "#000000");
  168. equal(false, invalidColor.isValid());
  169. invalidColor = tinycolor({r: 'invalid', g: 'invalid', b: 'invalid' });
  170. equal(invalidColor.toHexString(), "#000000");
  171. equal(false, invalidColor.isValid());
  172. invalidColor = tinycolor({h: 'invalid', s: 'invalid', l: 'invalid' });
  173. equal(invalidColor.toHexString(), "#000000");
  174. equal(false, invalidColor.isValid());
  175. invalidColor = tinycolor({h: 'invalid', s: 'invalid', v: 'invalid' });
  176. equal(invalidColor.toHexString(), "#000000");
  177. equal(false, invalidColor.isValid());
  178. });
  179. test("Named colors", function() {
  180. equal(tinycolor("aliceblue").toHex(), "f0f8ff");
  181. equal(tinycolor("antiquewhite").toHex(), "faebd7");
  182. equal(tinycolor("aqua").toHex(), "00ffff");
  183. equal(tinycolor("aquamarine").toHex(), "7fffd4");
  184. equal(tinycolor("azure").toHex(), "f0ffff");
  185. equal(tinycolor("beige").toHex(), "f5f5dc");
  186. equal(tinycolor("bisque").toHex(), "ffe4c4");
  187. equal(tinycolor("black").toHex(), "000000");
  188. equal(tinycolor("blanchedalmond").toHex(), "ffebcd");
  189. equal(tinycolor("blue").toHex(), "0000ff");
  190. equal(tinycolor("blueviolet").toHex(), "8a2be2");
  191. equal(tinycolor("brown").toHex(), "a52a2a");
  192. equal(tinycolor("burlywood").toHex(), "deb887");
  193. equal(tinycolor("cadetblue").toHex(), "5f9ea0");
  194. equal(tinycolor("chartreuse").toHex(), "7fff00");
  195. equal(tinycolor("chocolate").toHex(), "d2691e");
  196. equal(tinycolor("coral").toHex(), "ff7f50");
  197. equal(tinycolor("cornflowerblue").toHex(), "6495ed");
  198. equal(tinycolor("cornsilk").toHex(), "fff8dc");
  199. equal(tinycolor("crimson").toHex(), "dc143c");
  200. equal(tinycolor("cyan").toHex(), "00ffff");
  201. equal(tinycolor("darkblue").toHex(), "00008b");
  202. equal(tinycolor("darkcyan").toHex(), "008b8b");
  203. equal(tinycolor("darkgoldenrod").toHex(), "b8860b");
  204. equal(tinycolor("darkgray").toHex(), "a9a9a9");
  205. equal(tinycolor("darkgreen").toHex(), "006400");
  206. equal(tinycolor("darkkhaki").toHex(), "bdb76b");
  207. equal(tinycolor("darkmagenta").toHex(), "8b008b");
  208. equal(tinycolor("darkolivegreen").toHex(), "556b2f");
  209. equal(tinycolor("darkorange").toHex(), "ff8c00");
  210. equal(tinycolor("darkorchid").toHex(), "9932cc");
  211. equal(tinycolor("darkred").toHex(), "8b0000");
  212. equal(tinycolor("darksalmon").toHex(), "e9967a");
  213. equal(tinycolor("darkseagreen").toHex(), "8fbc8f");
  214. equal(tinycolor("darkslateblue").toHex(), "483d8b");
  215. equal(tinycolor("darkslategray").toHex(), "2f4f4f");
  216. equal(tinycolor("darkturquoise").toHex(), "00ced1");
  217. equal(tinycolor("darkviolet").toHex(), "9400d3");
  218. equal(tinycolor("deeppink").toHex(), "ff1493");
  219. equal(tinycolor("deepskyblue").toHex(), "00bfff");
  220. equal(tinycolor("dimgray").toHex(), "696969");
  221. equal(tinycolor("dodgerblue").toHex(), "1e90ff");
  222. equal(tinycolor("firebrick").toHex(), "b22222");
  223. equal(tinycolor("floralwhite").toHex(), "fffaf0");
  224. equal(tinycolor("forestgreen").toHex(), "228b22");
  225. equal(tinycolor("fuchsia").toHex(), "ff00ff");
  226. equal(tinycolor("gainsboro").toHex(), "dcdcdc");
  227. equal(tinycolor("ghostwhite").toHex(), "f8f8ff");
  228. equal(tinycolor("gold").toHex(), "ffd700");
  229. equal(tinycolor("goldenrod").toHex(), "daa520");
  230. equal(tinycolor("gray").toHex(), "808080");
  231. equal(tinycolor("grey").toHex(), "808080");
  232. equal(tinycolor("green").toHex(), "008000");
  233. equal(tinycolor("greenyellow").toHex(), "adff2f");
  234. equal(tinycolor("honeydew").toHex(), "f0fff0");
  235. equal(tinycolor("hotpink").toHex(), "ff69b4");
  236. equal(tinycolor("indianred ").toHex(), "cd5c5c");
  237. equal(tinycolor("indigo ").toHex(), "4b0082");
  238. equal(tinycolor("ivory").toHex(), "fffff0");
  239. equal(tinycolor("khaki").toHex(), "f0e68c");
  240. equal(tinycolor("lavender").toHex(), "e6e6fa");
  241. equal(tinycolor("lavenderblush").toHex(), "fff0f5");
  242. equal(tinycolor("lawngreen").toHex(), "7cfc00");
  243. equal(tinycolor("lemonchiffon").toHex(), "fffacd");
  244. equal(tinycolor("lightblue").toHex(), "add8e6");
  245. equal(tinycolor("lightcoral").toHex(), "f08080");
  246. equal(tinycolor("lightcyan").toHex(), "e0ffff");
  247. equal(tinycolor("lightgoldenrodyellow").toHex(), "fafad2");
  248. equal(tinycolor("lightgrey").toHex(), "d3d3d3");
  249. equal(tinycolor("lightgreen").toHex(), "90ee90");
  250. equal(tinycolor("lightpink").toHex(), "ffb6c1");
  251. equal(tinycolor("lightsalmon").toHex(), "ffa07a");
  252. equal(tinycolor("lightseagreen").toHex(), "20b2aa");
  253. equal(tinycolor("lightskyblue").toHex(), "87cefa");
  254. equal(tinycolor("lightslategray").toHex(), "778899");
  255. equal(tinycolor("lightsteelblue").toHex(), "b0c4de");
  256. equal(tinycolor("lightyellow").toHex(), "ffffe0");
  257. equal(tinycolor("lime").toHex(), "00ff00");
  258. equal(tinycolor("limegreen").toHex(), "32cd32");
  259. equal(tinycolor("linen").toHex(), "faf0e6");
  260. equal(tinycolor("magenta").toHex(), "ff00ff");
  261. equal(tinycolor("maroon").toHex(), "800000");
  262. equal(tinycolor("mediumaquamarine").toHex(), "66cdaa");
  263. equal(tinycolor("mediumblue").toHex(), "0000cd");
  264. equal(tinycolor("mediumorchid").toHex(), "ba55d3");
  265. equal(tinycolor("mediumpurple").toHex(), "9370db");
  266. equal(tinycolor("mediumseagreen").toHex(), "3cb371");
  267. equal(tinycolor("mediumslateblue").toHex(), "7b68ee");
  268. equal(tinycolor("mediumspringgreen").toHex(), "00fa9a");
  269. equal(tinycolor("mediumturquoise").toHex(), "48d1cc");
  270. equal(tinycolor("mediumvioletred").toHex(), "c71585");
  271. equal(tinycolor("midnightblue").toHex(), "191970");
  272. equal(tinycolor("mintcream").toHex(), "f5fffa");
  273. equal(tinycolor("mistyrose").toHex(), "ffe4e1");
  274. equal(tinycolor("moccasin").toHex(), "ffe4b5");
  275. equal(tinycolor("navajowhite").toHex(), "ffdead");
  276. equal(tinycolor("navy").toHex(), "000080");
  277. equal(tinycolor("oldlace").toHex(), "fdf5e6");
  278. equal(tinycolor("olive").toHex(), "808000");
  279. equal(tinycolor("olivedrab").toHex(), "6b8e23");
  280. equal(tinycolor("orange").toHex(), "ffa500");
  281. equal(tinycolor("orangered").toHex(), "ff4500");
  282. equal(tinycolor("orchid").toHex(), "da70d6");
  283. equal(tinycolor("palegoldenrod").toHex(), "eee8aa");
  284. equal(tinycolor("palegreen").toHex(), "98fb98");
  285. equal(tinycolor("paleturquoise").toHex(), "afeeee");
  286. equal(tinycolor("palevioletred").toHex(), "db7093");
  287. equal(tinycolor("papayawhip").toHex(), "ffefd5");
  288. equal(tinycolor("peachpuff").toHex(), "ffdab9");
  289. equal(tinycolor("peru").toHex(), "cd853f");
  290. equal(tinycolor("pink").toHex(), "ffc0cb");
  291. equal(tinycolor("plum").toHex(), "dda0dd");
  292. equal(tinycolor("powderblue").toHex(), "b0e0e6");
  293. equal(tinycolor("purple").toHex(), "800080");
  294. equal(tinycolor("rebeccapurple").toHex(), "663399");
  295. equal(tinycolor("red").toHex(), "ff0000");
  296. equal(tinycolor("rosybrown").toHex(), "bc8f8f");
  297. equal(tinycolor("royalblue").toHex(), "4169e1");
  298. equal(tinycolor("saddlebrown").toHex(), "8b4513");
  299. equal(tinycolor("salmon").toHex(), "fa8072");
  300. equal(tinycolor("sandybrown").toHex(), "f4a460");
  301. equal(tinycolor("seagreen").toHex(), "2e8b57");
  302. equal(tinycolor("seashell").toHex(), "fff5ee");
  303. equal(tinycolor("sienna").toHex(), "a0522d");
  304. equal(tinycolor("silver").toHex(), "c0c0c0");
  305. equal(tinycolor("skyblue").toHex(), "87ceeb");
  306. equal(tinycolor("slateblue").toHex(), "6a5acd");
  307. equal(tinycolor("slategray").toHex(), "708090");
  308. equal(tinycolor("snow").toHex(), "fffafa");
  309. equal(tinycolor("springgreen").toHex(), "00ff7f");
  310. equal(tinycolor("steelblue").toHex(), "4682b4");
  311. equal(tinycolor("tan").toHex(), "d2b48c");
  312. equal(tinycolor("teal").toHex(), "008080");
  313. equal(tinycolor("thistle").toHex(), "d8bfd8");
  314. equal(tinycolor("tomato").toHex(), "ff6347");
  315. equal(tinycolor("turquoise").toHex(), "40e0d0");
  316. equal(tinycolor("violet").toHex(), "ee82ee");
  317. equal(tinycolor("wheat").toHex(), "f5deb3");
  318. equal(tinycolor("white").toHex(), "ffffff");
  319. equal(tinycolor("whitesmoke").toHex(), "f5f5f5");
  320. equal(tinycolor("yellow").toHex(), "ffff00");
  321. equal(tinycolor("yellowgreen").toHex(), "9acd32");
  322. equal(tinycolor("#f00").toName(), "red");
  323. equal(tinycolor("#fa0a0a").toName(), false);
  324. });
  325. module("Alpha handling");
  326. test("Invalid alpha should normalize to 1", function() {
  327. equal(tinycolor({r:255,g:20,b:10,a: -1}).toRgbString(), "rgb(255, 20, 10)", "Negative value");
  328. equal(tinycolor({r:255,g:20,b:10,a: -0}).toRgbString(), "rgba(255, 20, 10, 0)", "Negative 0");
  329. equal(tinycolor({r:255,g:20,b:10,a: 0}).toRgbString(), "rgba(255, 20, 10, 0)", "0");
  330. equal(tinycolor({r:255,g:20,b:10,a: .5}).toRgbString(), "rgba(255, 20, 10, 0.5)", ".5");
  331. equal(tinycolor({r:255,g:20,b:10,a: 1}).toRgbString(), "rgb(255, 20, 10)", "1");
  332. equal(tinycolor({r:255,g:20,b:10,a: 100}).toRgbString(), "rgb(255, 20, 10)", "Greater than 1");
  333. equal(tinycolor({r:255,g:20,b:10,a: "asdfasd"}).toRgbString(), "rgb(255, 20, 10)", "Non Numeric");
  334. equal(tinycolor("#fff").toRgbString(), "rgb(255, 255, 255)", "Hex should be 1");
  335. equal(tinycolor("rgba 255 0 0 100").toRgbString(), "rgb(255, 0, 0)", "Greater than 1 in string parsing");
  336. });
  337. test("toString() with alpha set", function() {
  338. var redNamed = tinycolor.fromRatio({ r: 255, g: 0, b: 0, a: .6}, {format: "name"});
  339. var transparentNamed = tinycolor.fromRatio({ r: 255, g: 0, b: 0, a: 0 }, {format: "name"});
  340. var redHex = tinycolor.fromRatio({ r: 255, g: 0, b: 0, a: .4}, {format: "hex"});
  341. equal(redNamed.getFormat(), "name", "getFormat() is correct");
  342. equal(redHex.getFormat(), "hex", "getFormat() is correct");
  343. equal(redNamed.toString(), "rgba(255, 0, 0, 0.6)", "Names should default to rgba if alpha is < 1");
  344. equal(redHex.toString(), "rgba(255, 0, 0, 0.4)", "Hex should default to rgba if alpha is < 1");
  345. equal(redNamed.toString("hex"), "#ff0000", "Names should not be returned as rgba if format is specified");
  346. equal(redNamed.toString("hex6"), "#ff0000", "Names should not be returned as rgba if format is specified");
  347. equal(redNamed.toString("hex3"), "#f00", "Names should not be returned as rgba if format is specified");
  348. equal(redNamed.toString("hex8"), "#ff000099", "Names should not be returned as rgba if format is specified");
  349. equal(redNamed.toString("hex4"), "#f009", "Names should not be returned as rgba if format is specified");
  350. equal(redNamed.toString("name"), "#ff0000", "Semi transparent names should return hex in toString() if name format is specified");
  351. equal(redNamed.toName(), false, "Semi transparent names should be false in toName()");
  352. equal(redHex.toString(), "rgba(255, 0, 0, 0.4)", "Hex should default to rgba if alpha is < 1");
  353. equal(transparentNamed.toString(), "transparent", "Named color should equal transparent if alpha == 0");
  354. redHex.setAlpha(0);
  355. equal(redHex.toString(), "rgba(255, 0, 0, 0)", "Hex should default to rgba if alpha is = 0");
  356. });
  357. test("setting alpha", function() {
  358. var hexSetter = tinycolor("rgba(255, 0, 0, 1)");
  359. equal(hexSetter.getAlpha(), 1, "Alpha should start as 1");
  360. var returnedFromSetAlpha = hexSetter.setAlpha(.9);
  361. equal(returnedFromSetAlpha, hexSetter, "setAlpha return value should be the color.");
  362. equal(hexSetter.getAlpha(), .9, "setAlpha should change alpha value");
  363. hexSetter.setAlpha(.5);
  364. equal(hexSetter.getAlpha(), .5, "setAlpha should change alpha value");
  365. hexSetter.setAlpha(0);
  366. equal(hexSetter.getAlpha(), 0, "setAlpha should change alpha value");
  367. hexSetter.setAlpha(-1);
  368. equal(hexSetter.getAlpha(), 1, "setAlpha with value < 0 should be bound to 1");
  369. hexSetter.setAlpha(2);
  370. equal(hexSetter.getAlpha(), 1, "setAlpha with value > 1 should be bound to 1");
  371. hexSetter.setAlpha();
  372. equal(hexSetter.getAlpha(), 1, "setAlpha with invalid value should be bound to 1");
  373. hexSetter.setAlpha(null);
  374. equal(hexSetter.getAlpha(), 1, "setAlpha with invalid value should be bound to 1");
  375. hexSetter.setAlpha("test");
  376. equal(hexSetter.getAlpha(), 1, "setAlpha with invalid value should be bound to 1");
  377. });
  378. test("Alpha = 0 should act differently on toName()", function() {
  379. equal(tinycolor({r:255,g:20,b:10,a: 0}).toName(), "transparent", "0");
  380. equal(tinycolor("transparent").toString(), "transparent", "toString when passed");
  381. equal(tinycolor("transparent").toHex(), "000000", "toHex");
  382. });
  383. module("Brightness handling");
  384. test("getBrightness", function() {
  385. equal(tinycolor('#000').getBrightness(), 0, 'returns 0 for #000');
  386. equal(tinycolor('#fff').getBrightness(), 255, 'returns 255 for #fff');
  387. });
  388. test("getLuminance", function() {
  389. equal(tinycolor('#000').getLuminance(), 0, 'returns 0 for #000');
  390. equal(tinycolor('#fff').getLuminance(), 1, 'returns 1 for #fff');
  391. });
  392. test("isDark returns true/false for dark/light colors", function() {
  393. equal(tinycolor('#000').isDark(), true, '#000 is dark');
  394. equal(tinycolor('#111').isDark(), true, '#111 is dark');
  395. equal(tinycolor('#222').isDark(), true, '#222 is dark');
  396. equal(tinycolor('#333').isDark(), true, '#333 is dark');
  397. equal(tinycolor('#444').isDark(), true, '#444 is dark');
  398. equal(tinycolor('#555').isDark(), true, '#555 is dark');
  399. equal(tinycolor('#666').isDark(), true, '#666 is dark');
  400. equal(tinycolor('#777').isDark(), true, '#777 is dark');
  401. equal(tinycolor('#888').isDark(), false, '#888 is not dark');
  402. equal(tinycolor('#999').isDark(), false, '#999 is not dark');
  403. equal(tinycolor('#aaa').isDark(), false, '#aaa is not dark');
  404. equal(tinycolor('#bbb').isDark(), false, '#bbb is not dark');
  405. equal(tinycolor('#ccc').isDark(), false, '#ccc is not dark');
  406. equal(tinycolor('#ddd').isDark(), false, '#ddd is not dark');
  407. equal(tinycolor('#eee').isDark(), false, '#eee is not dark');
  408. equal(tinycolor('#fff').isDark(), false, '#fff is not dark');
  409. });
  410. test("isLight returns true/false for light/dark colors", function() {
  411. equal(tinycolor('#000').isLight(), false, '#000 is not light');
  412. equal(tinycolor('#111').isLight(), false, '#111 is not light');
  413. equal(tinycolor('#222').isLight(), false, '#222 is not light');
  414. equal(tinycolor('#333').isLight(), false, '#333 is not light');
  415. equal(tinycolor('#444').isLight(), false, '#444 is not light');
  416. equal(tinycolor('#555').isLight(), false, '#555 is not light');
  417. equal(tinycolor('#666').isLight(), false, '#666 is not light');
  418. equal(tinycolor('#777').isLight(), false, '#777 is not light');
  419. equal(tinycolor('#888').isLight(), true, '#888 is light');
  420. equal(tinycolor('#999').isLight(), true, '#999 is light');
  421. equal(tinycolor('#aaa').isLight(), true, '#aaa is light');
  422. equal(tinycolor('#bbb').isLight(), true, '#bbb is light');
  423. equal(tinycolor('#ccc').isLight(), true, '#ccc is light');
  424. equal(tinycolor('#ddd').isLight(), true, '#ddd is light');
  425. equal(tinycolor('#eee').isLight(), true, '#eee is light');
  426. equal(tinycolor('#fff').isLight(), true, '#fff is light');
  427. });
  428. module("Initialization from tinycolor output");
  429. test("HSL Object", function() {
  430. for (var i = 0; i < conversions.length; i++) {
  431. var c = conversions[i];
  432. var tiny = tinycolor(c.hex);
  433. equal(tiny.toHexString(), tinycolor(tiny.toHsl()).toHexString(), "HSL Object");
  434. }
  435. });
  436. test("HSL String", function() {
  437. for (var i = 0; i < conversions.length; i++) {
  438. var c = conversions[i];
  439. var tiny = tinycolor(c.hex);
  440. var input = tiny.toRgb();
  441. var output = tinycolor(tiny.toHslString()).toRgb();
  442. var maxDiff = 2;
  443. equal(Math.abs(input.r - output.r) <= maxDiff, true, "toHslString red value difference <= " + maxDiff);
  444. equal(Math.abs(input.g - output.g) <= maxDiff, true, "toHslString green value difference <= " + maxDiff);
  445. equal(Math.abs(input.b - output.b) <= maxDiff, true, "toHslString blue value difference <= " + maxDiff);
  446. }
  447. });
  448. test("HSV String", function() {
  449. for (var i = 0; i < conversions.length; i++) {
  450. var c = conversions[i];
  451. var tiny = tinycolor(c.hex);
  452. var input = tiny.toRgb();
  453. var output = tinycolor(tiny.toHsvString()).toRgb();
  454. var maxDiff = 2;
  455. equal(Math.abs(input.r - output.r) <= maxDiff, true, "toHsvString red value difference <= " + maxDiff);
  456. equal(Math.abs(input.g - output.g) <= maxDiff, true, "toHsvString green value difference <= " + maxDiff);
  457. equal(Math.abs(input.b - output.b) <= maxDiff, true, "toHsvString blue value difference <= " + maxDiff);
  458. }
  459. });
  460. test("HSV Object", function() {
  461. for (var i = 0; i < conversions.length; i++) {
  462. var c = conversions[i];
  463. var tiny = tinycolor(c.hex);
  464. equal(tiny.toHexString(), tinycolor(tiny.toHsv()).toHexString(), "HSV Object");
  465. }
  466. });
  467. test("RGB Object", function() {
  468. for (var i = 0; i < conversions.length; i++) {
  469. var c = conversions[i];
  470. var tiny = tinycolor(c.hex);
  471. equal(tiny.toHexString(), tinycolor(tiny.toRgb()).toHexString(), "RGB Object");
  472. }
  473. });
  474. test("RGB String", function() {
  475. for (var i = 0; i < conversions.length; i++) {
  476. var c = conversions[i];
  477. var tiny = tinycolor(c.hex);
  478. equal(tiny.toHexString(), tinycolor(tiny.toRgbString()).toHexString(), "RGB String");
  479. }
  480. });
  481. test("PRGB Object", function() {
  482. for (var i = 0; i < conversions.length; i++) {
  483. var c = conversions[i];
  484. var tiny = tinycolor(c.hex);
  485. var input = tiny.toRgb();
  486. var output = tinycolor(tiny.toPercentageRgb()).toRgb();
  487. var maxDiff = 2;
  488. equal(Math.abs(input.r - output.r) <= maxDiff, true, "Red value difference <= " + maxDiff);
  489. equal(Math.abs(input.g - output.g) <= maxDiff, true, "Green value difference <= " + maxDiff);
  490. equal(Math.abs(input.b - output.b) <= maxDiff, true, "Blue value difference <= " + maxDiff);
  491. }
  492. });
  493. test("PRGB String", function() {
  494. for (var i = 0; i < conversions.length; i++) {
  495. var c = conversions[i];
  496. var tiny = tinycolor(c.hex);
  497. var input = tiny.toRgb();
  498. var output = tinycolor(tiny.toPercentageRgbString()).toRgb();
  499. var maxDiff = 2;
  500. equal(Math.abs(input.r - output.r) <= maxDiff, true, "Red value difference <= " + maxDiff);
  501. equal(Math.abs(input.g - output.g) <= maxDiff, true, "Green value difference <= " + maxDiff);
  502. equal(Math.abs(input.b - output.b) <= maxDiff, true, "Blue value difference <= " + maxDiff);
  503. }
  504. });
  505. test("Object", function() {
  506. for (var i = 0; i < conversions.length; i++) {
  507. var c = conversions[i];
  508. var tiny = tinycolor(c.hex);
  509. equal(tiny.toHexString(), tinycolor(tiny).toHexString(), "Object");
  510. }
  511. });
  512. module("Utilities");
  513. test("Color equality", function() {
  514. ok(tinycolor.equals("#ff0000", "#ff0000"), "Same hex");
  515. ok(tinycolor.equals("#ff0000", "rgb(255, 0, 0)"), "Same alphas");
  516. ok(!tinycolor.equals("#ff0000", "rgba(255, 0, 0, .1)"), "Different alphas");
  517. ok(tinycolor.equals("#ff000066", "rgba(255, 0, 0, .4)"), "Same alphas");
  518. ok(tinycolor.equals("#f009", "rgba(255, 0, 0, .6)"), "Same alphas");
  519. ok(tinycolor.equals("#336699CC", "369C"), "Same hex");
  520. ok(tinycolor.equals("ff0000", "#ff0000"), "Same hex");
  521. ok(tinycolor.equals("#f00", "#ff0000"), "Same hex");
  522. ok(tinycolor.equals("#f00", "#ff0000"), "Same hex");
  523. ok(tinycolor.equals("f00", "#ff0000"), "Same hex");
  524. equal(tinycolor("010101").toHexString(), "#010101");
  525. ok(!tinycolor.equals("#ff0000", "#00ff00"), "Different hex");
  526. ok(tinycolor.equals("#ff8000", "rgb(100%, 50%, 0%)"), "Percentage bounds checking");
  527. });
  528. test("isReadable", function() {
  529. // "#ff0088", "#8822aa" (values used in old WCAG1 tests)
  530. ok(tinycolor.isReadable("#000000", "#ffffff",{level:"AA",size:"small"}), "white/black is readable");
  531. ok(!tinycolor.isReadable("#ff0088", "#5c1a72",{}), "not readable - empty wcag2 object");
  532. ok(!tinycolor.isReadable("#ff0088", "#8822aa",{level:"AA",size:"small"}), "not readable - AA small");
  533. ok(!tinycolor.isReadable("#ff0088", "#8822aa",{level:"AA",size:"large"}), "not readable - AA large");
  534. ok(!tinycolor.isReadable("#ff0088", "#8822aa",{level:"AAA",size:"small"}), "not readable - AAA small");
  535. ok(!tinycolor.isReadable("#ff0088", "#8822aa",{level:"AAA",size:"large"}), "not readable - AAA large");
  536. // values derived from and validated using the calculators at http://www.dasplankton.de/ContrastA/
  537. // and http://webaim.org/resources/contrastchecker/
  538. // "#ff0088", "#5c1a72": contrast ratio 3.04
  539. ok(!tinycolor.isReadable("#ff0088", "#5c1a72",{level:"AA",size:"small"}), "not readable - AA small");
  540. ok(tinycolor.isReadable("#ff0088", "#5c1a72",{level:"AA",size:"large"}), "readable - AA large");
  541. ok(!tinycolor.isReadable("#ff0088", "#5c1a72",{level:"AAA",size:"small"}), "not readable - AAA small");
  542. ok(!tinycolor.isReadable("#ff0088", "#5c1a72",{level:"AAA",size:"large"}), "not readable - AAA large");
  543. // "#ff0088", "#2e0c3a": contrast ratio 4.56
  544. ok(tinycolor.isReadable("#ff0088", "#2e0c3a",{level:"AA",size:"small"}), "readable - AA small");
  545. ok(tinycolor.isReadable("#ff0088", "#2e0c3a",{level:"AA",size:"large"}), "readable - AA large");
  546. ok(!tinycolor.isReadable("#ff0088", "#2e0c3a",{level:"AAA",size:"small"}), "not readable - AAA small");
  547. ok(tinycolor.isReadable("#ff0088", "#2e0c3a",{level:"AAA",size:"large"}), "readable - AAA large");
  548. // "#db91b8", "#2e0c3a": contrast ratio 7.12
  549. ok(tinycolor.isReadable("#db91b8", "#2e0c3a",{level:"AA",size:"small"}), "readable - AA small");
  550. ok(tinycolor.isReadable("#db91b8", "#2e0c3a",{level:"AA",size:"large"}), "readable - AA large");
  551. ok(tinycolor.isReadable("#db91b8", "#2e0c3a",{level:"AAA",size:"small"}), "readable - AAA small");
  552. ok(tinycolor.isReadable("#db91b8", "#2e0c3a",{level:"AAA",size:"large"}), "readable - AAA large");
  553. });
  554. test("readability", function() {
  555. // check return values from readability function. See isReadable above for standards tests.
  556. equal(tinycolor.readability("#000", "#000"), 1, "Readability function test 0");
  557. deepEqual(tinycolor.readability("#000", "#111"), 1.1121078324840545, "Readability function test 1");
  558. deepEqual(tinycolor.readability("#000", "#fff"), 21, "Readability function test 2");
  559. });
  560. test("mostReadable", function () {
  561. equal(tinycolor.mostReadable("#000", ["#111", "#222",{wcag2:{}}]).toHexString(), "#222222", "readable color present");
  562. equal(tinycolor.mostReadable("#f00", ["#d00", "#0d0"],{wcag2:{}}).toHexString(), "#00dd00", "readable color present");
  563. equal(tinycolor.mostReadable("#fff", ["#fff", "#fff"],{wcag2:{}}).toHexString(), "#ffffff", "no different color in list");
  564. //includeFallbackColors
  565. equal(tinycolor.mostReadable("#fff", ["#fff", "#fff"],{includeFallbackColors:true}).toHexString(), "#000000", "no different color in list");
  566. equal(tinycolor.mostReadable("#123", ["#124", "#125"],{includeFallbackColors:false}).toHexString(), "#112255", "no readable color in list");
  567. equal(tinycolor.mostReadable("#123", ["#000", "#fff"],{includeFallbackColors:false}).toHexString(), "#ffffff", "verify assumption");
  568. equal(tinycolor.mostReadable("#123", ["#124", "#125"],{includeFallbackColors:true}).toHexString(), "#ffffff", "no readable color in list");
  569. equal(tinycolor.mostReadable("#ff0088", ["#000", "#fff"],{includeFallbackColors:false}).toHexString(), "#000000", "verify assumption");
  570. equal(tinycolor.mostReadable("#ff0088", ["#2e0c3a"],{includeFallbackColors:true,level:"AAA",size:"large"}).toHexString(), "#2e0c3a", "readable color present");
  571. equal(tinycolor.mostReadable("#ff0088", ["#2e0c3a"],{includeFallbackColors:true,level:"AAA",size:"small"}).toHexString(), "#000000", "no readable color in list");
  572. equal(tinycolor.mostReadable("#371b2c", ["#000", "#fff"],{includeFallbackColors:false}).toHexString(), "#ffffff", "verify assumption");
  573. equal(tinycolor.mostReadable("#371b2c", ["#a9acb6"],{includeFallbackColors:true,level:"AAA",size:"large"}).toHexString(), "#a9acb6", "readable color present");
  574. equal(tinycolor.mostReadable("#371b2c", ["#a9acb6"],{includeFallbackColors:true,level:"AAA",size:"small"}).toHexString(), "#ffffff", "no readable color in list");
  575. });
  576. test("Filters", function () {
  577. equal(tinycolor("red").toFilter(), "progid:DXImageTransform.Microsoft.gradient(startColorstr=#ffff0000,endColorstr=#ffff0000)");
  578. equal(tinycolor("red").toFilter("blue"), "progid:DXImageTransform.Microsoft.gradient(startColorstr=#ffff0000,endColorstr=#ff0000ff)");
  579. equal(tinycolor("transparent").toFilter(), "progid:DXImageTransform.Microsoft.gradient(startColorstr=#00000000,endColorstr=#00000000)");
  580. equal(tinycolor("transparent").toFilter("red"), "progid:DXImageTransform.Microsoft.gradient(startColorstr=#00000000,endColorstr=#ffff0000)");
  581. equal(tinycolor("#f0f0f0dd").toFilter(), "progid:DXImageTransform.Microsoft.gradient(startColorstr=#ddf0f0f0,endColorstr=#ddf0f0f0)");
  582. equal(tinycolor("rgba(0, 0, 255, .5").toFilter(), "progid:DXImageTransform.Microsoft.gradient(startColorstr=#800000ff,endColorstr=#800000ff)");
  583. });
  584. module("Modifications");
  585. /* Originally generated with:
  586. var results = [];
  587. for (var i = 0; i <= 100; i++) results.push( tinycolor.saturate("red", i).toHex() )
  588. console.log(JSON.stringify(results))
  589. */
  590. var DESATURATIONS = ["ff0000","fe0101","fc0303","fb0404","fa0505","f90606","f70808","f60909","f50a0a","f40b0b","f20d0d","f10e0e","f00f0f","ee1111","ed1212","ec1313","eb1414","e91616","e81717","e71818","e61919","e41b1b","e31c1c","e21d1d","e01f1f","df2020","de2121","dd2222","db2424","da2525","d92626","d72828","d62929","d52a2a","d42b2b","d22d2d","d12e2e","d02f2f","cf3030","cd3232","cc3333","cb3434","c93636","c83737","c73838","c63939","c43b3b","c33c3c","c23d3d","c13e3e","bf4040","be4141","bd4242","bb4444","ba4545","b94646","b84747","b64949","b54a4a","b44b4b","b34d4d","b14e4e","b04f4f","af5050","ad5252","ac5353","ab5454","aa5555","a85757","a75858","a65959","a45b5b","a35c5c","a25d5d","a15e5e","9f6060","9e6161","9d6262","9c6363","9a6565","996666","986767","966969","956a6a","946b6b","936c6c","916e6e","906f6f","8f7070","8e7171","8c7373","8b7474","8a7575","887777","877878","867979","857a7a","837c7c","827d7d","817e7e","808080"];
  591. var SATURATIONS = ["ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000","ff0000"];
  592. var LIGHTENS = ["ff0000","ff0505","ff0a0a","ff0f0f","ff1414","ff1a1a","ff1f1f","ff2424","ff2929","ff2e2e","ff3333","ff3838","ff3d3d","ff4242","ff4747","ff4d4d","ff5252","ff5757","ff5c5c","ff6161","ff6666","ff6b6b","ff7070","ff7575","ff7a7a","ff8080","ff8585","ff8a8a","ff8f8f","ff9494","ff9999","ff9e9e","ffa3a3","ffa8a8","ffadad","ffb3b3","ffb8b8","ffbdbd","ffc2c2","ffc7c7","ffcccc","ffd1d1","ffd6d6","ffdbdb","ffe0e0","ffe5e5","ffebeb","fff0f0","fff5f5","fffafa","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff"];
  593. var BRIGHTENS = ["ff0000","ff0303","ff0505","ff0808","ff0a0a","ff0d0d","ff0f0f","ff1212","ff1414","ff1717","ff1919","ff1c1c","ff1f1f","ff2121","ff2424","ff2626","ff2929","ff2b2b","ff2e2e","ff3030","ff3333","ff3636","ff3838","ff3b3b","ff3d3d","ff4040","ff4242","ff4545","ff4747","ff4a4a","ff4c4c","ff4f4f","ff5252","ff5454","ff5757","ff5959","ff5c5c","ff5e5e","ff6161","ff6363","ff6666","ff6969","ff6b6b","ff6e6e","ff7070","ff7373","ff7575","ff7878","ff7a7a","ff7d7d","ff7f7f","ff8282","ff8585","ff8787","ff8a8a","ff8c8c","ff8f8f","ff9191","ff9494","ff9696","ff9999","ff9c9c","ff9e9e","ffa1a1","ffa3a3","ffa6a6","ffa8a8","ffabab","ffadad","ffb0b0","ffb2b2","ffb5b5","ffb8b8","ffbaba","ffbdbd","ffbfbf","ffc2c2","ffc4c4","ffc7c7","ffc9c9","ffcccc","ffcfcf","ffd1d1","ffd4d4","ffd6d6","ffd9d9","ffdbdb","ffdede","ffe0e0","ffe3e3","ffe5e5","ffe8e8","ffebeb","ffeded","fff0f0","fff2f2","fff5f5","fff7f7","fffafa","fffcfc","ffffff"];
  594. var DARKENS = ["ff0000","fa0000","f50000","f00000","eb0000","e60000","e00000","db0000","d60000","d10000","cc0000","c70000","c20000","bd0000","b80000","b30000","ad0000","a80000","a30000","9e0000","990000","940000","8f0000","8a0000","850000","800000","7a0000","750000","700000","6b0000","660000","610000","5c0000","570000","520000","4d0000","470000","420000","3d0000","380000","330000","2e0000","290000","240000","1f0000","190000","140000","0f0000","0a0000","050000","000000","000000","000000","000000","000000","000000","000000","000000","000000","000000","000000","000000","000000","000000","000000","000000","000000","000000","000000","000000","000000","000000","000000","000000","000000","000000","000000","000000","000000","000000","000000","000000","000000","000000","000000","000000","000000","000000","000000","000000","000000","000000","000000","000000","000000","000000","000000","000000","000000","000000","000000"];
  595. test("Modifications", function () {
  596. for (var i = 0; i <= 100; i++) {
  597. equal(tinycolor("red").desaturate(i).toHex(), DESATURATIONS[i], "Desaturation " + i + " works");
  598. }
  599. for (var i = 0; i <= 100; i++) {
  600. equal(tinycolor("red").saturate(i).toHex(), SATURATIONS[i], "Saturation " + i + " works");
  601. }
  602. for (var i = 0; i <= 100; i++) {
  603. equal(tinycolor("red").lighten(i).toHex(), LIGHTENS[i], "Lighten " + i + " works");
  604. }
  605. for (var i = 0; i <= 100; i++) {
  606. equal(tinycolor("red").brighten(i).toHex(), BRIGHTENS[i], "Brighter " + i + " works");
  607. }
  608. for (var i = 0; i <= 100; i++) {
  609. equal(tinycolor("red").darken(i).toHex(), DARKENS[i], "Darken " + i + " works");
  610. }
  611. equal(tinycolor("red").greyscale().toHex(), "808080", "Greyscale works");
  612. });
  613. test("Spin", function () {
  614. equal(Math.round(tinycolor("#f00").spin(-1234).toHsl().h), 206, "Spinning -1234 works");
  615. equal(Math.round(tinycolor("#f00").spin(-360).toHsl().h), 0, "Spinning -360 works");
  616. equal(Math.round(tinycolor("#f00").spin(-120).toHsl().h), 240, "Spinning -120 works");
  617. equal(Math.round(tinycolor("#f00").spin(0).toHsl().h), 0, "Spinning 0 works");
  618. equal(Math.round(tinycolor("#f00").spin(10).toHsl().h), 10, "Spinning 10 works");
  619. equal(Math.round(tinycolor("#f00").spin(360).toHsl().h), 0, "Spinning 360 works");
  620. equal(Math.round(tinycolor("#f00").spin(2345).toHsl().h), 185, "Spinning 2345 works");
  621. [-360, 0, 360].forEach(function (delta) {
  622. Object.keys(tinycolor.names).forEach(function (name) {
  623. equal(tinycolor(name).toHex(), tinycolor(name).spin(delta).toHex(), "Spinning " + delta.toString() + " has no effect")
  624. })
  625. })
  626. });
  627. test("Mix", function () {
  628. // amount 0 or none
  629. equal(tinycolor.mix('#000', '#fff').toHsl().l, 0.5, "Mixing without amount works");
  630. equal(tinycolor.mix('#f00', '#000', 0).toHex(), 'ff0000', "Mixing with 0 amount works");
  631. // This case checks the the problem with floating point numbers (eg 255/90)
  632. equal(tinycolor.mix('#fff', '#000', 90).toHex(), '1a1a1a', "Mixing with 90 amount works correctly");
  633. // black and white
  634. for (var i = 0; i < 100; i++) {
  635. equal(Math.round(tinycolor.mix('#000', '#fff', i).toHsl().l * 100) / 100, i / 100, "Mixing black and white with " + i + " amount works");
  636. }
  637. // with colors
  638. for (var i = 0; i < 100; i++) {
  639. var new_hex = Math.round((255 * (100 - i)) / 100).toString(16);
  640. if (new_hex.length === 1) {
  641. new_hex = '0' + new_hex;
  642. }
  643. equal(tinycolor.mix('#f00', '#000', i).toHex(), new_hex + '0000', "Mixing " + i + " (red channel)");
  644. equal(tinycolor.mix('#0f0', '#000', i).toHex(), '00' + new_hex + '00', "Mixing " + i + " (green channel)");
  645. equal(tinycolor.mix('#00f', '#000', i).toHex(), '0000' + new_hex, "Mixing " + i + " (blue channel)");
  646. equal(tinycolor.mix(tinycolor('transparent'), '#000', i).toRgb().a, i / 100, "Mixing " + i + " (alpha channel)");
  647. }
  648. });
  649. // The combination tests need to be expanded further
  650. module("Combinations");
  651. function colorsToHexString(colors) {
  652. return colors.map(function(c) {
  653. return c.toHex();
  654. }).join(",");
  655. }
  656. test("complement", function() {
  657. var complementDoesntModifyInstance = tinycolor("red");
  658. equal(complementDoesntModifyInstance.complement().toHex(), "00ffff", "Complement works");
  659. equal(complementDoesntModifyInstance.toHex(), "ff0000", "Complement did not modify this color");
  660. });
  661. test("analogous", function() {
  662. var combination = tinycolor("red").analogous();
  663. equal(colorsToHexString(combination), "ff0000,ff0066,ff0033,ff0000,ff3300,ff6600", "Correct Combination");
  664. });
  665. test("monochromatic", function() {
  666. var combination = tinycolor("red").monochromatic();
  667. equal(colorsToHexString(combination), "ff0000,2a0000,550000,800000,aa0000,d40000", "Correct Combination");
  668. });
  669. test("splitcomplement", function() {
  670. var combination = tinycolor("red").splitcomplement();
  671. equal(colorsToHexString(combination), "ff0000,ccff00,0066ff", "Correct Combination");
  672. });
  673. test("triad", function() {
  674. var combination = tinycolor("red").triad();
  675. equal(colorsToHexString(combination), "ff0000,00ff00,0000ff", "Correct Combination");
  676. });
  677. test("tetrad", function() {
  678. var combination = tinycolor("red").tetrad();
  679. equal(colorsToHexString(combination), "ff0000,80ff00,00ffff,7f00ff", "Correct Combination");
  680. });