test.js 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191
  1. // This file is autogenerated.
  2. // Ideally it wouldn't exist, but it's here to test cjs in node
  3. // Changes should go into ./test.js, and if new assertions are needed
  4. // they'll need to be shimmed here as well
  5. const tinycolor = require("./tinycolor.js");
  6. const { Deno, testDefinitions } = require("@deno/shim-deno-test");
  7. async function runDenoTests() {
  8. for (const test of testDefinitions) {
  9. if (test.ignore) {
  10. console.log(`Ignoring ${test.name}`);
  11. continue;
  12. }
  13. console.log(`Running ${test.name}`);
  14. await test.fn();
  15. console.log(`> Passed ${test.name}`);
  16. }
  17. }
  18. (async () => {
  19. const { assertEquals, assert, assertThrows } = await import(
  20. "../deno_asserts@0.168.0.mjs"
  21. );
  22. // TEST_BEGINS_HERE
  23. Deno.test("TinyColor initialization", function () {
  24. assert(
  25. typeof tinycolor != "undefined",
  26. "tinycolor is initialized on the page"
  27. );
  28. assert(
  29. typeof tinycolor("red") == "object",
  30. "tinycolor is able to be instantiated"
  31. );
  32. var r = tinycolor("red");
  33. assert(
  34. tinycolor(r) === r,
  35. "when given a tinycolor instance, tinycolor() returns it"
  36. );
  37. assert(
  38. new tinycolor(r) === r,
  39. "when given a tinycolor instance, new tinycolor() returns it"
  40. );
  41. assertEquals(
  42. tinycolor("red", { format: "hex" }).toString(),
  43. "#ff0000",
  44. "tinycolor options are being parsed"
  45. );
  46. assertEquals(
  47. tinycolor.fromRatio({ r: 1, g: 0, b: 0 }, { format: "hex" }).toString(),
  48. "#ff0000",
  49. "tinycolor options are being parsed"
  50. );
  51. var obj = { h: 180, s: 0.5, l: 0.5 };
  52. var color = tinycolor(obj);
  53. assert(
  54. obj.s === 0.5,
  55. "when given an object, the original object is not modified"
  56. );
  57. });
  58. Deno.test("Original input", function () {
  59. var colorRgbUp = "RGB(39, 39, 39)";
  60. var colorRgbLow = "rgb(39, 39, 39)";
  61. var colorRgbMix = "RgB(39, 39, 39)";
  62. var tinycolorObj = tinycolor(colorRgbMix);
  63. var inputObj = { r: 100, g: 100, b: 100 };
  64. var r = tinycolor("red");
  65. assert(
  66. tinycolor(colorRgbLow).getOriginalInput() === colorRgbLow,
  67. "original lowercase input is returned"
  68. );
  69. assert(
  70. tinycolor(colorRgbUp).getOriginalInput() === colorRgbUp,
  71. "original uppercase input is returned"
  72. );
  73. assert(
  74. tinycolor(colorRgbMix).getOriginalInput() === colorRgbMix,
  75. "original mixed input is returned"
  76. );
  77. assert(
  78. tinycolor(tinycolorObj).getOriginalInput() === colorRgbMix,
  79. "when given a tinycolor instance, the color string is returned"
  80. );
  81. assert(
  82. tinycolor(inputObj).getOriginalInput() === inputObj,
  83. "when given an object, the object is returned"
  84. );
  85. assert(
  86. new tinycolor("").getOriginalInput() === "",
  87. "when given an empty string, an empty string is returned"
  88. );
  89. assert(
  90. new tinycolor(null).getOriginalInput() === "",
  91. "when given a null value, an empty string is returned"
  92. );
  93. });
  94. Deno.test("Cloning color", function () {
  95. var originalColor = tinycolor("red");
  96. var originalColorRgbString = originalColor.toRgbString();
  97. var clonedColor = originalColor.clone();
  98. assert(
  99. clonedColor.toRgbString() === originalColor.toRgbString(),
  100. "cloned color is identical"
  101. );
  102. clonedColor.setAlpha(0.5);
  103. assert(
  104. clonedColor.toRgbString() !== originalColor.toRgbString(),
  105. "cloned color is changing independently from original color"
  106. );
  107. assert(
  108. originalColorRgbString === originalColor.toRgbString(),
  109. "original color was not changed by cloned color change"
  110. );
  111. });
  112. Deno.test("Random color", function () {
  113. var randomColor = tinycolor.random();
  114. assertEquals(randomColor.getAlpha(), 1);
  115. assertEquals(randomColor.getFormat(), "prgb");
  116. randomColor.setAlpha(0.5);
  117. assertEquals(randomColor.toHex8String().slice(-2), "80");
  118. });
  119. // Taken from convertWikipediaColors.html
  120. var conversions = [
  121. {
  122. hex: "#FFFFFF",
  123. hex8: "#FFFFFFFF",
  124. rgb: { r: "100.0%", g: "100.0%", b: "100.0%" },
  125. hsv: { h: "0", s: "0.000", v: "1.000" },
  126. hsl: { h: "0", s: "0.000", l: "1.000" },
  127. },
  128. {
  129. hex: "#808080",
  130. hex8: "#808080FF",
  131. rgb: { r: "050.0%", g: "050.0%", b: "050.0%" },
  132. hsv: { h: "0", s: "0.000", v: "0.500" },
  133. hsl: { h: "0", s: "0.000", l: "0.500" },
  134. },
  135. {
  136. hex: "#000000",
  137. hex8: "#000000FF",
  138. rgb: { r: "000.0%", g: "000.0%", b: "000.0%" },
  139. hsv: { h: "0", s: "0.000", v: "0.000" },
  140. hsl: { h: "0", s: "0.000", l: "0.000" },
  141. },
  142. {
  143. hex: "#FF0000",
  144. hex8: "#FF0000FF",
  145. rgb: { r: "100.0%", g: "000.0%", b: "000.0%" },
  146. hsv: { h: "0.0", s: "1.000", v: "1.000" },
  147. hsl: { h: "0.0", s: "1.000", l: "0.500" },
  148. },
  149. {
  150. hex: "#BFBF00",
  151. hex8: "#BFBF00FF",
  152. rgb: { r: "075.0%", g: "075.0%", b: "000.0%" },
  153. hsv: { h: "60.0", s: "1.000", v: "0.750" },
  154. hsl: { h: "60.0", s: "1.000", l: "0.375" },
  155. },
  156. {
  157. hex: "#008000",
  158. hex8: "#008000FF",
  159. rgb: { r: "000.0%", g: "050.0%", b: "000.0%" },
  160. hsv: { h: "120.0", s: "1.000", v: "0.500" },
  161. hsl: { h: "120.0", s: "1.000", l: "0.250" },
  162. },
  163. {
  164. hex: "#80FFFF",
  165. hex8: "#80FFFFFF",
  166. rgb: { r: "050.0%", g: "100.0%", b: "100.0%" },
  167. hsv: { h: "180.0", s: "0.500", v: "1.000" },
  168. hsl: { h: "180.0", s: "1.000", l: "0.750" },
  169. },
  170. {
  171. hex: "#8080FF",
  172. hex8: "#8080FFFF",
  173. rgb: { r: "050.0%", g: "050.0%", b: "100.0%" },
  174. hsv: { h: "240.0", s: "0.500", v: "1.000" },
  175. hsl: { h: "240.0", s: "1.000", l: "0.750" },
  176. },
  177. {
  178. hex: "#BF40BF",
  179. hex8: "#BF40BFFF",
  180. rgb: { r: "075.0%", g: "025.0%", b: "075.0%" },
  181. hsv: { h: "300.0", s: "0.667", v: "0.750" },
  182. hsl: { h: "300.0", s: "0.500", l: "0.500" },
  183. },
  184. {
  185. hex: "#A0A424",
  186. hex8: "#A0A424FF",
  187. rgb: { r: "062.8%", g: "064.3%", b: "014.2%" },
  188. hsv: { h: "61.8", s: "0.779", v: "0.643" },
  189. hsl: { h: "61.8", s: "0.638", l: "0.393" },
  190. },
  191. {
  192. hex: "#1EAC41",
  193. hex8: "#1EAC41FF",
  194. rgb: { r: "011.6%", g: "067.5%", b: "025.5%" },
  195. hsv: { h: "134.9", s: "0.828", v: "0.675" },
  196. hsl: { h: "134.9", s: "0.707", l: "0.396" },
  197. },
  198. {
  199. hex: "#B430E5",
  200. hex8: "#B430E5FF",
  201. rgb: { r: "070.4%", g: "018.7%", b: "089.7%" },
  202. hsv: { h: "283.7", s: "0.792", v: "0.897" },
  203. hsl: { h: "283.7", s: "0.775", l: "0.542" },
  204. },
  205. {
  206. hex: "#FEF888",
  207. hex8: "#FEF888FF",
  208. rgb: { r: "099.8%", g: "097.4%", b: "053.2%" },
  209. hsv: { h: "56.9", s: "0.467", v: "0.998" },
  210. hsl: { h: "56.9", s: "0.991", l: "0.765" },
  211. },
  212. {
  213. hex: "#19CB97",
  214. hex8: "#19CB97FF",
  215. rgb: { r: "009.9%", g: "079.5%", b: "059.1%" },
  216. hsv: { h: "162.4", s: "0.875", v: "0.795" },
  217. hsl: { h: "162.4", s: "0.779", l: "0.447" },
  218. },
  219. {
  220. hex: "#362698",
  221. hex8: "#362698FF",
  222. rgb: { r: "021.1%", g: "014.9%", b: "059.7%" },
  223. hsv: { h: "248.3", s: "0.750", v: "0.597" },
  224. hsl: { h: "248.3", s: "0.601", l: "0.373" },
  225. },
  226. {
  227. hex: "#7E7EB8",
  228. hex8: "#7E7EB8FF",
  229. rgb: { r: "049.5%", g: "049.3%", b: "072.1%" },
  230. hsv: { h: "240.5", s: "0.316", v: "0.721" },
  231. hsl: { h: "240.5", s: "0.290", l: "0.607" },
  232. },
  233. ];
  234. Deno.test("Color Equality", function () {
  235. for (var i = 0; i < conversions.length; i++) {
  236. var c = conversions[i];
  237. var tiny = tinycolor(c.hex);
  238. assert(true, tiny.isValid());
  239. assert(
  240. true,
  241. "Testing " +
  242. c.hex +
  243. ": " +
  244. tiny.toRgbString() +
  245. " " +
  246. tiny.toPercentageRgbString() +
  247. " " +
  248. tiny.toHsvString() +
  249. " " +
  250. tiny.toHslString() +
  251. " " +
  252. tiny.toHexString() +
  253. "Original: " +
  254. JSON.stringify(c.rgb) +
  255. " " +
  256. JSON.stringify(c.hsv) +
  257. " " +
  258. JSON.stringify(c.hsl)
  259. );
  260. assert(tinycolor.equals(c.rgb, c.hex), "RGB equals hex " + c.hex);
  261. assert(tinycolor.equals(c.rgb, c.hex8), "RGB equals hex " + c.hex);
  262. assert(tinycolor.equals(c.rgb, c.hsl), "RGB equals HSL " + c.hex);
  263. assert(tinycolor.equals(c.rgb, c.hsv), "RGB equals HSV " + c.hex);
  264. assert(tinycolor.equals(c.rgb, c.rgb), "RGB equals RGB " + c.hex);
  265. assert(tinycolor.equals(c.hex, c.hex), "hex equals hex " + c.hex);
  266. assert(tinycolor.equals(c.hex, c.hex8), "hex equals hex8 " + c.hex);
  267. assert(tinycolor.equals(c.hex, c.hsl), "hex equals HSL " + c.hex);
  268. assert(tinycolor.equals(c.hex, c.hsv), "hex equals HSV " + c.hex);
  269. assert(tinycolor.equals(c.hsl, c.hsv), "HSL equals HSV " + c.hex);
  270. }
  271. });
  272. Deno.test("With Ratio", function () {
  273. assertEquals(
  274. tinycolor.fromRatio({ r: 1, g: 1, b: 1 }).toHexString(),
  275. "#ffffff",
  276. "white"
  277. );
  278. assertEquals(
  279. tinycolor.fromRatio({ r: 1, g: 0, b: 0, a: 0.5 }).toRgbString(),
  280. "rgba(255, 0, 0, 0.5)",
  281. "alpha works when ratio is parsed"
  282. );
  283. assertEquals(
  284. tinycolor.fromRatio({ r: 1, g: 0, b: 0, a: 1 }).toRgbString(),
  285. "rgb(255, 0, 0)",
  286. "alpha = 1 works when ratio is parsed"
  287. );
  288. assertEquals(
  289. tinycolor.fromRatio({ r: 1, g: 0, b: 0, a: 10 }).toRgbString(),
  290. "rgb(255, 0, 0)",
  291. "alpha > 1 works when ratio is parsed"
  292. );
  293. assertEquals(
  294. tinycolor.fromRatio({ r: 1, g: 0, b: 0, a: -1 }).toRgbString(),
  295. "rgb(255, 0, 0)",
  296. "alpha < 1 works when ratio is parsed"
  297. );
  298. });
  299. Deno.test("Without Ratio", function () {
  300. assertEquals(
  301. tinycolor({ r: 1, g: 1, b: 1 }).toHexString(),
  302. "#010101",
  303. "010101"
  304. );
  305. assertEquals(
  306. tinycolor({ r: 0.1, g: 0.1, b: 0.1 }).toHexString(),
  307. "#000000",
  308. "000000"
  309. );
  310. assertEquals(tinycolor("rgb .1 .1 .1").toHexString(), "#000000", "000000");
  311. });
  312. Deno.test("RGB Text Parsing", function () {
  313. assertEquals(
  314. tinycolor("rgb 255 0 0").toHexString(),
  315. "#ff0000",
  316. "spaced input"
  317. );
  318. assertEquals(
  319. tinycolor("rgb(255, 0, 0)").toHexString(),
  320. "#ff0000",
  321. "parenthesized input"
  322. );
  323. assertEquals(
  324. tinycolor("rgb (255, 0, 0)").toHexString(),
  325. "#ff0000",
  326. "parenthesized spaced input"
  327. );
  328. assertEquals(
  329. tinycolor({ r: 255, g: 0, b: 0 }).toHexString(),
  330. "#ff0000",
  331. "object input"
  332. );
  333. assertEquals(
  334. tinycolor({ r: 255, g: 0, b: 0 }).toRgb(),
  335. {
  336. r: 255,
  337. g: 0,
  338. b: 0,
  339. a: 1,
  340. },
  341. "object input and compare"
  342. );
  343. assert(tinycolor.equals({ r: 200, g: 100, b: 0 }, "rgb(200, 100, 0)"));
  344. assert(tinycolor.equals({ r: 200, g: 100, b: 0 }, "rgb 200 100 0"));
  345. assert(tinycolor.equals({ r: 200, g: 100, b: 0 }, "rgb 200 100 0"));
  346. assert(
  347. tinycolor.equals({ r: 200, g: 100, b: 0, a: 0.4 }, "rgba 200 100 0 .4")
  348. );
  349. assert(!tinycolor.equals({ r: 199, g: 100, b: 0 }, "rgba 200 100 0 1"));
  350. assert(!tinycolor.equals({ r: 199, g: 100, b: 0 }, "rgb(200, 100, 0)"));
  351. assert(!tinycolor.equals({ r: 199, g: 100, b: 0 }, "rgb 200 100 0"));
  352. assert(!tinycolor.equals({ r: 199, g: 100, b: 0 }, "rgb 200 100 0"));
  353. assert(
  354. tinycolor.equals(tinycolor({ r: 200, g: 100, b: 0 }), "rgb(200, 100, 0)")
  355. );
  356. assert(
  357. tinycolor.equals(tinycolor({ r: 200, g: 100, b: 0 }), "rgb 200 100 0")
  358. );
  359. assert(
  360. tinycolor.equals(tinycolor({ r: 200, g: 100, b: 0 }), "rgb 200 100 0")
  361. );
  362. });
  363. Deno.test("Percentage RGB Text Parsing", function () {
  364. assertEquals(
  365. tinycolor("rgb 100% 0% 0%").toHexString(),
  366. "#ff0000",
  367. "spaced input"
  368. );
  369. assertEquals(
  370. tinycolor("rgb(100%, 0%, 0%)").toHexString(),
  371. "#ff0000",
  372. "parenthesized input"
  373. );
  374. assertEquals(
  375. tinycolor("rgb (100%, 0%, 0%)").toHexString(),
  376. "#ff0000",
  377. "parenthesized spaced input"
  378. );
  379. assertEquals(
  380. tinycolor({ r: "100%", g: "0%", b: "0%" }).toHexString(),
  381. "#ff0000",
  382. "object input"
  383. );
  384. assertEquals(
  385. tinycolor({ r: "100%", g: "0%", b: "0%" }).toRgb(),
  386. {
  387. r: 255,
  388. g: 0,
  389. b: 0,
  390. a: 1,
  391. },
  392. "object input and compare"
  393. );
  394. assert(
  395. tinycolor.equals({ r: "90%", g: "45%", b: "0%" }, "rgb(90%, 45%, 0%)")
  396. );
  397. assert(tinycolor.equals({ r: "90%", g: "45%", b: "0%" }, "rgb 90% 45% 0%"));
  398. assert(tinycolor.equals({ r: "90%", g: "45%", b: "0%" }, "rgb 90% 45% 0%"));
  399. assert(
  400. tinycolor.equals(
  401. { r: "90%", g: "45%", b: "0%", a: 0.4 },
  402. "rgba 90% 45% 0% .4"
  403. )
  404. );
  405. assert(
  406. !tinycolor.equals({ r: "89%", g: "45%", b: "0%" }, "rgba 90% 45% 0% 1")
  407. );
  408. assert(
  409. !tinycolor.equals({ r: "89%", g: "45%", b: "0%" }, "rgb(90%, 45%, 0%)")
  410. );
  411. assert(!tinycolor.equals({ r: "89%", g: "45%", b: "0%" }, "rgb 90% 45% 0%"));
  412. assert(!tinycolor.equals({ r: "89%", g: "45%", b: "0%" }, "rgb 90% 45% 0%"));
  413. assert(
  414. tinycolor.equals(
  415. tinycolor({ r: "90%", g: "45%", b: "0%" }),
  416. "rgb(90%, 45%, 0%)"
  417. )
  418. );
  419. assert(
  420. tinycolor.equals(
  421. tinycolor({ r: "90%", g: "45%", b: "0%" }),
  422. "rgb 90% 45% 0%"
  423. )
  424. );
  425. assert(
  426. tinycolor.equals(
  427. tinycolor({ r: "90%", g: "45%", b: "0%" }),
  428. "rgb 90% 45% 0%"
  429. )
  430. );
  431. });
  432. Deno.test("HSL parsing", function () {
  433. assertEquals(
  434. tinycolor({ h: 251, s: 100, l: 0.38 }).toHexString(),
  435. "#2400c2",
  436. "to hex"
  437. );
  438. assertEquals(
  439. tinycolor({ h: 251, s: 100, l: 0.38 }).toRgbString(),
  440. "rgb(36, 0, 194)",
  441. "to rgb"
  442. );
  443. assertEquals(
  444. tinycolor({ h: 251, s: 100, l: 0.38 }).toHslString(),
  445. "hsl(251, 100%, 38%)",
  446. "to hsl"
  447. );
  448. assertEquals(
  449. tinycolor({ h: 251, s: 100, l: 0.38, a: 0.5 }).toHslString(),
  450. "hsla(251, 100%, 38%, 0.5)",
  451. "to hsla"
  452. );
  453. assertEquals(
  454. tinycolor("hsl(251, 100, 38)").toHexString(),
  455. "#2400c2",
  456. "to hex"
  457. );
  458. assertEquals(
  459. tinycolor("hsl(251, 100%, 38%)").toRgbString(),
  460. "rgb(36, 0, 194)",
  461. "to rgb"
  462. );
  463. assertEquals(
  464. tinycolor("hsl(251, 100%, 38%)").toHslString(),
  465. "hsl(251, 100%, 38%)",
  466. "to hsl"
  467. );
  468. assertEquals(
  469. tinycolor("hsl 100 20 10").toHslString(),
  470. "hsl(100, 20%, 10%)",
  471. "problematic hsl"
  472. );
  473. });
  474. Deno.test("Hex Parsing", function () {
  475. assertEquals(tinycolor("rgb 255 0 0").toHexString(), "#ff0000");
  476. assertEquals(tinycolor("rgb 255 0 0").toHexString(true), "#f00");
  477. assertEquals(tinycolor("rgba 255 0 0 0.5").toHex8String(), "#ff000080");
  478. assertEquals(tinycolor("rgba 255 0 0 0").toHex8String(), "#ff000000");
  479. assertEquals(tinycolor("rgba 255 0 0 1").toHex8String(), "#ff0000ff");
  480. assertEquals(tinycolor("rgba 255 0 0 1").toHex8String(true), "#f00f");
  481. assertEquals(tinycolor("rgb 255 0 0").toHex(), "ff0000");
  482. assertEquals(tinycolor("rgb 255 0 0").toHex(true), "f00");
  483. assertEquals(tinycolor("rgba 255 0 0 0.5").toHex8(), "ff000080");
  484. });
  485. Deno.test("HSV Parsing", function () {
  486. assertEquals(
  487. tinycolor("hsv 251.1 0.887 .918").toHsvString(),
  488. "hsv(251, 89%, 92%)"
  489. );
  490. assertEquals(
  491. tinycolor("hsv 251.1 0.887 0.918").toHsvString(),
  492. "hsv(251, 89%, 92%)"
  493. );
  494. assertEquals(
  495. tinycolor("hsva 251.1 0.887 0.918 0.5").toHsvString(),
  496. "hsva(251, 89%, 92%, 0.5)"
  497. );
  498. });
  499. Deno.test("Invalid Parsing", function () {
  500. var invalidColor = tinycolor("this is not a color");
  501. assertEquals(invalidColor.toHexString(), "#000000");
  502. assertEquals(false, invalidColor.isValid());
  503. invalidColor = tinycolor("#red");
  504. assertEquals(invalidColor.toHexString(), "#000000");
  505. assertEquals(false, invalidColor.isValid());
  506. invalidColor = tinycolor(" #red");
  507. assertEquals(invalidColor.toHexString(), "#000000");
  508. assertEquals(false, invalidColor.isValid());
  509. invalidColor = tinycolor("##123456");
  510. assertEquals(invalidColor.toHexString(), "#000000");
  511. assertEquals(false, invalidColor.isValid());
  512. invalidColor = tinycolor(" ##123456");
  513. assertEquals(invalidColor.toHexString(), "#000000");
  514. assertEquals(false, invalidColor.isValid());
  515. invalidColor = tinycolor({ r: "invalid", g: "invalid", b: "invalid" });
  516. assertEquals(invalidColor.toHexString(), "#000000");
  517. assertEquals(false, invalidColor.isValid());
  518. invalidColor = tinycolor({ h: "invalid", s: "invalid", l: "invalid" });
  519. assertEquals(invalidColor.toHexString(), "#000000");
  520. assertEquals(false, invalidColor.isValid());
  521. invalidColor = tinycolor({ h: "invalid", s: "invalid", v: "invalid" });
  522. assertEquals(invalidColor.toHexString(), "#000000");
  523. assertEquals(false, invalidColor.isValid());
  524. });
  525. Deno.test("Named colors", function () {
  526. assertEquals(tinycolor("aliceblue").toHex(), "f0f8ff");
  527. assertEquals(tinycolor("antiquewhite").toHex(), "faebd7");
  528. assertEquals(tinycolor("aqua").toHex(), "00ffff");
  529. assertEquals(tinycolor("aquamarine").toHex(), "7fffd4");
  530. assertEquals(tinycolor("azure").toHex(), "f0ffff");
  531. assertEquals(tinycolor("beige").toHex(), "f5f5dc");
  532. assertEquals(tinycolor("bisque").toHex(), "ffe4c4");
  533. assertEquals(tinycolor("black").toHex(), "000000");
  534. assertEquals(tinycolor("blanchedalmond").toHex(), "ffebcd");
  535. assertEquals(tinycolor("blue").toHex(), "0000ff");
  536. assertEquals(tinycolor("blueviolet").toHex(), "8a2be2");
  537. assertEquals(tinycolor("brown").toHex(), "a52a2a");
  538. assertEquals(tinycolor("burlywood").toHex(), "deb887");
  539. assertEquals(tinycolor("cadetblue").toHex(), "5f9ea0");
  540. assertEquals(tinycolor("chartreuse").toHex(), "7fff00");
  541. assertEquals(tinycolor("chocolate").toHex(), "d2691e");
  542. assertEquals(tinycolor("coral").toHex(), "ff7f50");
  543. assertEquals(tinycolor("cornflowerblue").toHex(), "6495ed");
  544. assertEquals(tinycolor("cornsilk").toHex(), "fff8dc");
  545. assertEquals(tinycolor("crimson").toHex(), "dc143c");
  546. assertEquals(tinycolor("cyan").toHex(), "00ffff");
  547. assertEquals(tinycolor("darkblue").toHex(), "00008b");
  548. assertEquals(tinycolor("darkcyan").toHex(), "008b8b");
  549. assertEquals(tinycolor("darkgoldenrod").toHex(), "b8860b");
  550. assertEquals(tinycolor("darkgray").toHex(), "a9a9a9");
  551. assertEquals(tinycolor("darkgreen").toHex(), "006400");
  552. assertEquals(tinycolor("darkkhaki").toHex(), "bdb76b");
  553. assertEquals(tinycolor("darkmagenta").toHex(), "8b008b");
  554. assertEquals(tinycolor("darkolivegreen").toHex(), "556b2f");
  555. assertEquals(tinycolor("darkorange").toHex(), "ff8c00");
  556. assertEquals(tinycolor("darkorchid").toHex(), "9932cc");
  557. assertEquals(tinycolor("darkred").toHex(), "8b0000");
  558. assertEquals(tinycolor("darksalmon").toHex(), "e9967a");
  559. assertEquals(tinycolor("darkseagreen").toHex(), "8fbc8f");
  560. assertEquals(tinycolor("darkslateblue").toHex(), "483d8b");
  561. assertEquals(tinycolor("darkslategray").toHex(), "2f4f4f");
  562. assertEquals(tinycolor("darkturquoise").toHex(), "00ced1");
  563. assertEquals(tinycolor("darkviolet").toHex(), "9400d3");
  564. assertEquals(tinycolor("deeppink").toHex(), "ff1493");
  565. assertEquals(tinycolor("deepskyblue").toHex(), "00bfff");
  566. assertEquals(tinycolor("dimgray").toHex(), "696969");
  567. assertEquals(tinycolor("dodgerblue").toHex(), "1e90ff");
  568. assertEquals(tinycolor("firebrick").toHex(), "b22222");
  569. assertEquals(tinycolor("floralwhite").toHex(), "fffaf0");
  570. assertEquals(tinycolor("forestgreen").toHex(), "228b22");
  571. assertEquals(tinycolor("fuchsia").toHex(), "ff00ff");
  572. assertEquals(tinycolor("gainsboro").toHex(), "dcdcdc");
  573. assertEquals(tinycolor("ghostwhite").toHex(), "f8f8ff");
  574. assertEquals(tinycolor("gold").toHex(), "ffd700");
  575. assertEquals(tinycolor("goldenrod").toHex(), "daa520");
  576. assertEquals(tinycolor("gray").toHex(), "808080");
  577. assertEquals(tinycolor("grey").toHex(), "808080");
  578. assertEquals(tinycolor("green").toHex(), "008000");
  579. assertEquals(tinycolor("greenyellow").toHex(), "adff2f");
  580. assertEquals(tinycolor("honeydew").toHex(), "f0fff0");
  581. assertEquals(tinycolor("hotpink").toHex(), "ff69b4");
  582. assertEquals(tinycolor("indianred ").toHex(), "cd5c5c");
  583. assertEquals(tinycolor("indigo ").toHex(), "4b0082");
  584. assertEquals(tinycolor("ivory").toHex(), "fffff0");
  585. assertEquals(tinycolor("khaki").toHex(), "f0e68c");
  586. assertEquals(tinycolor("lavender").toHex(), "e6e6fa");
  587. assertEquals(tinycolor("lavenderblush").toHex(), "fff0f5");
  588. assertEquals(tinycolor("lawngreen").toHex(), "7cfc00");
  589. assertEquals(tinycolor("lemonchiffon").toHex(), "fffacd");
  590. assertEquals(tinycolor("lightblue").toHex(), "add8e6");
  591. assertEquals(tinycolor("lightcoral").toHex(), "f08080");
  592. assertEquals(tinycolor("lightcyan").toHex(), "e0ffff");
  593. assertEquals(tinycolor("lightgoldenrodyellow").toHex(), "fafad2");
  594. assertEquals(tinycolor("lightgrey").toHex(), "d3d3d3");
  595. assertEquals(tinycolor("lightgreen").toHex(), "90ee90");
  596. assertEquals(tinycolor("lightpink").toHex(), "ffb6c1");
  597. assertEquals(tinycolor("lightsalmon").toHex(), "ffa07a");
  598. assertEquals(tinycolor("lightseagreen").toHex(), "20b2aa");
  599. assertEquals(tinycolor("lightskyblue").toHex(), "87cefa");
  600. assertEquals(tinycolor("lightslategray").toHex(), "778899");
  601. assertEquals(tinycolor("lightsteelblue").toHex(), "b0c4de");
  602. assertEquals(tinycolor("lightyellow").toHex(), "ffffe0");
  603. assertEquals(tinycolor("lime").toHex(), "00ff00");
  604. assertEquals(tinycolor("limegreen").toHex(), "32cd32");
  605. assertEquals(tinycolor("linen").toHex(), "faf0e6");
  606. assertEquals(tinycolor("magenta").toHex(), "ff00ff");
  607. assertEquals(tinycolor("maroon").toHex(), "800000");
  608. assertEquals(tinycolor("mediumaquamarine").toHex(), "66cdaa");
  609. assertEquals(tinycolor("mediumblue").toHex(), "0000cd");
  610. assertEquals(tinycolor("mediumorchid").toHex(), "ba55d3");
  611. assertEquals(tinycolor("mediumpurple").toHex(), "9370db");
  612. assertEquals(tinycolor("mediumseagreen").toHex(), "3cb371");
  613. assertEquals(tinycolor("mediumslateblue").toHex(), "7b68ee");
  614. assertEquals(tinycolor("mediumspringgreen").toHex(), "00fa9a");
  615. assertEquals(tinycolor("mediumturquoise").toHex(), "48d1cc");
  616. assertEquals(tinycolor("mediumvioletred").toHex(), "c71585");
  617. assertEquals(tinycolor("midnightblue").toHex(), "191970");
  618. assertEquals(tinycolor("mintcream").toHex(), "f5fffa");
  619. assertEquals(tinycolor("mistyrose").toHex(), "ffe4e1");
  620. assertEquals(tinycolor("moccasin").toHex(), "ffe4b5");
  621. assertEquals(tinycolor("navajowhite").toHex(), "ffdead");
  622. assertEquals(tinycolor("navy").toHex(), "000080");
  623. assertEquals(tinycolor("oldlace").toHex(), "fdf5e6");
  624. assertEquals(tinycolor("olive").toHex(), "808000");
  625. assertEquals(tinycolor("olivedrab").toHex(), "6b8e23");
  626. assertEquals(tinycolor("orange").toHex(), "ffa500");
  627. assertEquals(tinycolor("orangered").toHex(), "ff4500");
  628. assertEquals(tinycolor("orchid").toHex(), "da70d6");
  629. assertEquals(tinycolor("palegoldenrod").toHex(), "eee8aa");
  630. assertEquals(tinycolor("palegreen").toHex(), "98fb98");
  631. assertEquals(tinycolor("paleturquoise").toHex(), "afeeee");
  632. assertEquals(tinycolor("palevioletred").toHex(), "db7093");
  633. assertEquals(tinycolor("papayawhip").toHex(), "ffefd5");
  634. assertEquals(tinycolor("peachpuff").toHex(), "ffdab9");
  635. assertEquals(tinycolor("peru").toHex(), "cd853f");
  636. assertEquals(tinycolor("pink").toHex(), "ffc0cb");
  637. assertEquals(tinycolor("plum").toHex(), "dda0dd");
  638. assertEquals(tinycolor("powderblue").toHex(), "b0e0e6");
  639. assertEquals(tinycolor("purple").toHex(), "800080");
  640. assertEquals(tinycolor("rebeccapurple").toHex(), "663399");
  641. assertEquals(tinycolor("red").toHex(), "ff0000");
  642. assertEquals(tinycolor("rosybrown").toHex(), "bc8f8f");
  643. assertEquals(tinycolor("royalblue").toHex(), "4169e1");
  644. assertEquals(tinycolor("saddlebrown").toHex(), "8b4513");
  645. assertEquals(tinycolor("salmon").toHex(), "fa8072");
  646. assertEquals(tinycolor("sandybrown").toHex(), "f4a460");
  647. assertEquals(tinycolor("seagreen").toHex(), "2e8b57");
  648. assertEquals(tinycolor("seashell").toHex(), "fff5ee");
  649. assertEquals(tinycolor("sienna").toHex(), "a0522d");
  650. assertEquals(tinycolor("silver").toHex(), "c0c0c0");
  651. assertEquals(tinycolor("skyblue").toHex(), "87ceeb");
  652. assertEquals(tinycolor("slateblue").toHex(), "6a5acd");
  653. assertEquals(tinycolor("slategray").toHex(), "708090");
  654. assertEquals(tinycolor("snow").toHex(), "fffafa");
  655. assertEquals(tinycolor("springgreen").toHex(), "00ff7f");
  656. assertEquals(tinycolor("steelblue").toHex(), "4682b4");
  657. assertEquals(tinycolor("tan").toHex(), "d2b48c");
  658. assertEquals(tinycolor("teal").toHex(), "008080");
  659. assertEquals(tinycolor("thistle").toHex(), "d8bfd8");
  660. assertEquals(tinycolor("tomato").toHex(), "ff6347");
  661. assertEquals(tinycolor("turquoise").toHex(), "40e0d0");
  662. assertEquals(tinycolor("violet").toHex(), "ee82ee");
  663. assertEquals(tinycolor("wheat").toHex(), "f5deb3");
  664. assertEquals(tinycolor("white").toHex(), "ffffff");
  665. assertEquals(tinycolor("whitesmoke").toHex(), "f5f5f5");
  666. assertEquals(tinycolor("yellow").toHex(), "ffff00");
  667. assertEquals(tinycolor("yellowgreen").toHex(), "9acd32");
  668. assertEquals(tinycolor("#f00").toName(), "red");
  669. assertEquals(tinycolor("#fa0a0a").toName(), false);
  670. });
  671. Deno.test("Invalid alpha should normalize to 1", function () {
  672. assertEquals(
  673. tinycolor({ r: 255, g: 20, b: 10, a: -1 }).toRgbString(),
  674. "rgb(255, 20, 10)",
  675. "Negative value"
  676. );
  677. assertEquals(
  678. tinycolor({ r: 255, g: 20, b: 10, a: -0 }).toRgbString(),
  679. "rgba(255, 20, 10, 0)",
  680. "Negative 0"
  681. );
  682. assertEquals(
  683. tinycolor({ r: 255, g: 20, b: 10, a: 0 }).toRgbString(),
  684. "rgba(255, 20, 10, 0)",
  685. "0"
  686. );
  687. assertEquals(
  688. tinycolor({ r: 255, g: 20, b: 10, a: 0.5 }).toRgbString(),
  689. "rgba(255, 20, 10, 0.5)",
  690. ".5"
  691. );
  692. assertEquals(
  693. tinycolor({ r: 255, g: 20, b: 10, a: 1 }).toRgbString(),
  694. "rgb(255, 20, 10)",
  695. "1"
  696. );
  697. assertEquals(
  698. tinycolor({ r: 255, g: 20, b: 10, a: 100 }).toRgbString(),
  699. "rgb(255, 20, 10)",
  700. "Greater than 1"
  701. );
  702. assertEquals(
  703. tinycolor({ r: 255, g: 20, b: 10, a: "asdfasd" }).toRgbString(),
  704. "rgb(255, 20, 10)",
  705. "Non Numeric"
  706. );
  707. assertEquals(
  708. tinycolor("#fff").toRgbString(),
  709. "rgb(255, 255, 255)",
  710. "Hex should be 1"
  711. );
  712. assertEquals(
  713. tinycolor("rgba 255 0 0 100").toRgbString(),
  714. "rgb(255, 0, 0)",
  715. "Greater than 1 in string parsing"
  716. );
  717. });
  718. Deno.test("toString() with alpha set", function () {
  719. var redNamed = tinycolor.fromRatio(
  720. { r: 255, g: 0, b: 0, a: 0.6 },
  721. {
  722. format: "name",
  723. }
  724. );
  725. var transparentNamed = tinycolor.fromRatio(
  726. { r: 255, g: 0, b: 0, a: 0 },
  727. {
  728. format: "name",
  729. }
  730. );
  731. var redHex = tinycolor.fromRatio(
  732. { r: 255, g: 0, b: 0, a: 0.4 },
  733. {
  734. format: "hex",
  735. }
  736. );
  737. assertEquals(redNamed.getFormat(), "name", "getFormat() is correct");
  738. assertEquals(redHex.getFormat(), "hex", "getFormat() is correct");
  739. assertEquals(
  740. redNamed.toString(),
  741. "rgba(255, 0, 0, 0.6)",
  742. "Names should default to rgba if alpha is < 1"
  743. );
  744. assertEquals(
  745. redHex.toString(),
  746. "rgba(255, 0, 0, 0.4)",
  747. "Hex should default to rgba if alpha is < 1"
  748. );
  749. assertEquals(
  750. redNamed.toString("hex"),
  751. "#ff0000",
  752. "Names should not be returned as rgba if format is specified"
  753. );
  754. assertEquals(
  755. redNamed.toString("hex6"),
  756. "#ff0000",
  757. "Names should not be returned as rgba if format is specified"
  758. );
  759. assertEquals(
  760. redNamed.toString("hex3"),
  761. "#f00",
  762. "Names should not be returned as rgba if format is specified"
  763. );
  764. assertEquals(
  765. redNamed.toString("hex8"),
  766. "#ff000099",
  767. "Names should not be returned as rgba if format is specified"
  768. );
  769. assertEquals(
  770. redNamed.toString("hex4"),
  771. "#f009",
  772. "Names should not be returned as rgba if format is specified"
  773. );
  774. assertEquals(
  775. redNamed.toString("name"),
  776. "#ff0000",
  777. "Semi transparent names should return hex in toString() if name format is specified"
  778. );
  779. assertEquals(
  780. redNamed.toName(),
  781. false,
  782. "Semi transparent names should be false in toName()"
  783. );
  784. assertEquals(
  785. redHex.toString(),
  786. "rgba(255, 0, 0, 0.4)",
  787. "Hex should default to rgba if alpha is < 1"
  788. );
  789. assertEquals(
  790. transparentNamed.toString(),
  791. "transparent",
  792. "Named color should equal transparent if alpha == 0"
  793. );
  794. redHex.setAlpha(0);
  795. assertEquals(
  796. redHex.toString(),
  797. "rgba(255, 0, 0, 0)",
  798. "Hex should default to rgba if alpha is = 0"
  799. );
  800. });
  801. Deno.test("setting alpha", function () {
  802. var hexSetter = tinycolor("rgba(255, 0, 0, 1)");
  803. assertEquals(hexSetter.getAlpha(), 1, "Alpha should start as 1");
  804. var returnedFromSetAlpha = hexSetter.setAlpha(0.9);
  805. assertEquals(
  806. returnedFromSetAlpha,
  807. hexSetter,
  808. "setAlpha return value should be the color."
  809. );
  810. assertEquals(hexSetter.getAlpha(), 0.9, "setAlpha should change alpha value");
  811. hexSetter.setAlpha(0.5);
  812. assertEquals(hexSetter.getAlpha(), 0.5, "setAlpha should change alpha value");
  813. hexSetter.setAlpha(0);
  814. assertEquals(hexSetter.getAlpha(), 0, "setAlpha should change alpha value");
  815. hexSetter.setAlpha(-1);
  816. assertEquals(
  817. hexSetter.getAlpha(),
  818. 1,
  819. "setAlpha with value < 0 should be bound to 1"
  820. );
  821. hexSetter.setAlpha(2);
  822. assertEquals(
  823. hexSetter.getAlpha(),
  824. 1,
  825. "setAlpha with value > 1 should be bound to 1"
  826. );
  827. hexSetter.setAlpha();
  828. assertEquals(
  829. hexSetter.getAlpha(),
  830. 1,
  831. "setAlpha with invalid value should be bound to 1"
  832. );
  833. hexSetter.setAlpha(null);
  834. assertEquals(
  835. hexSetter.getAlpha(),
  836. 1,
  837. "setAlpha with invalid value should be bound to 1"
  838. );
  839. hexSetter.setAlpha("test");
  840. assertEquals(
  841. hexSetter.getAlpha(),
  842. 1,
  843. "setAlpha with invalid value should be bound to 1"
  844. );
  845. });
  846. Deno.test("Alpha = 0 should act differently on toName()", function () {
  847. assertEquals(
  848. tinycolor({ r: 255, g: 20, b: 10, a: 0 }).toName(),
  849. "transparent",
  850. "0"
  851. );
  852. assertEquals(
  853. tinycolor("transparent").toString(),
  854. "transparent",
  855. "toString when passed"
  856. );
  857. assertEquals(tinycolor("transparent").toHex(), "000000", "toHex");
  858. });
  859. Deno.test("getBrightness", function () {
  860. assertEquals(tinycolor("#000").getBrightness(), 0, "returns 0 for #000");
  861. assertEquals(tinycolor("#fff").getBrightness(), 255, "returns 255 for #fff");
  862. });
  863. Deno.test("getLuminance", function () {
  864. assertEquals(tinycolor("#000").getLuminance(), 0, "returns 0 for #000");
  865. assertEquals(tinycolor("#fff").getLuminance(), 1, "returns 1 for #fff");
  866. });
  867. Deno.test("isDark returns true/false for dark/light colors", function () {
  868. assertEquals(tinycolor("#000").isDark(), true, "#000 is dark");
  869. assertEquals(tinycolor("#111").isDark(), true, "#111 is dark");
  870. assertEquals(tinycolor("#222").isDark(), true, "#222 is dark");
  871. assertEquals(tinycolor("#333").isDark(), true, "#333 is dark");
  872. assertEquals(tinycolor("#444").isDark(), true, "#444 is dark");
  873. assertEquals(tinycolor("#555").isDark(), true, "#555 is dark");
  874. assertEquals(tinycolor("#666").isDark(), true, "#666 is dark");
  875. assertEquals(tinycolor("#777").isDark(), true, "#777 is dark");
  876. assertEquals(tinycolor("#888").isDark(), false, "#888 is not dark");
  877. assertEquals(tinycolor("#999").isDark(), false, "#999 is not dark");
  878. assertEquals(tinycolor("#aaa").isDark(), false, "#aaa is not dark");
  879. assertEquals(tinycolor("#bbb").isDark(), false, "#bbb is not dark");
  880. assertEquals(tinycolor("#ccc").isDark(), false, "#ccc is not dark");
  881. assertEquals(tinycolor("#ddd").isDark(), false, "#ddd is not dark");
  882. assertEquals(tinycolor("#eee").isDark(), false, "#eee is not dark");
  883. assertEquals(tinycolor("#fff").isDark(), false, "#fff is not dark");
  884. });
  885. Deno.test("isLight returns true/false for light/dark colors", function () {
  886. assertEquals(tinycolor("#000").isLight(), false, "#000 is not light");
  887. assertEquals(tinycolor("#111").isLight(), false, "#111 is not light");
  888. assertEquals(tinycolor("#222").isLight(), false, "#222 is not light");
  889. assertEquals(tinycolor("#333").isLight(), false, "#333 is not light");
  890. assertEquals(tinycolor("#444").isLight(), false, "#444 is not light");
  891. assertEquals(tinycolor("#555").isLight(), false, "#555 is not light");
  892. assertEquals(tinycolor("#666").isLight(), false, "#666 is not light");
  893. assertEquals(tinycolor("#777").isLight(), false, "#777 is not light");
  894. assertEquals(tinycolor("#888").isLight(), true, "#888 is light");
  895. assertEquals(tinycolor("#999").isLight(), true, "#999 is light");
  896. assertEquals(tinycolor("#aaa").isLight(), true, "#aaa is light");
  897. assertEquals(tinycolor("#bbb").isLight(), true, "#bbb is light");
  898. assertEquals(tinycolor("#ccc").isLight(), true, "#ccc is light");
  899. assertEquals(tinycolor("#ddd").isLight(), true, "#ddd is light");
  900. assertEquals(tinycolor("#eee").isLight(), true, "#eee is light");
  901. assertEquals(tinycolor("#fff").isLight(), true, "#fff is light");
  902. });
  903. Deno.test("HSL Object", function () {
  904. for (var i = 0; i < conversions.length; i++) {
  905. var c = conversions[i];
  906. var tiny = tinycolor(c.hex);
  907. assertEquals(
  908. tiny.toHexString(),
  909. tinycolor(tiny.toHsl()).toHexString(),
  910. "HSL Object"
  911. );
  912. }
  913. });
  914. Deno.test("HSL String", function () {
  915. for (var i = 0; i < conversions.length; i++) {
  916. var c = conversions[i];
  917. var tiny = tinycolor(c.hex);
  918. var input = tiny.toRgb();
  919. var output = tinycolor(tiny.toHslString()).toRgb();
  920. var maxDiff = 2;
  921. assertEquals(
  922. Math.abs(input.r - output.r) <= maxDiff,
  923. true,
  924. "toHslString red value difference <= " + maxDiff
  925. );
  926. assertEquals(
  927. Math.abs(input.g - output.g) <= maxDiff,
  928. true,
  929. "toHslString green value difference <= " + maxDiff
  930. );
  931. assertEquals(
  932. Math.abs(input.b - output.b) <= maxDiff,
  933. true,
  934. "toHslString blue value difference <= " + maxDiff
  935. );
  936. }
  937. });
  938. Deno.test("HSV String", function () {
  939. for (var i = 0; i < conversions.length; i++) {
  940. var c = conversions[i];
  941. var tiny = tinycolor(c.hex);
  942. var input = tiny.toRgb();
  943. var output = tinycolor(tiny.toHsvString()).toRgb();
  944. var maxDiff = 2;
  945. assertEquals(
  946. Math.abs(input.r - output.r) <= maxDiff,
  947. true,
  948. "toHsvString red value difference <= " + maxDiff
  949. );
  950. assertEquals(
  951. Math.abs(input.g - output.g) <= maxDiff,
  952. true,
  953. "toHsvString green value difference <= " + maxDiff
  954. );
  955. assertEquals(
  956. Math.abs(input.b - output.b) <= maxDiff,
  957. true,
  958. "toHsvString blue value difference <= " + maxDiff
  959. );
  960. }
  961. });
  962. Deno.test("HSV Object", function () {
  963. for (var i = 0; i < conversions.length; i++) {
  964. var c = conversions[i];
  965. var tiny = tinycolor(c.hex);
  966. assertEquals(
  967. tiny.toHexString(),
  968. tinycolor(tiny.toHsv()).toHexString(),
  969. "HSV Object"
  970. );
  971. }
  972. });
  973. Deno.test("RGB Object", function () {
  974. for (var i = 0; i < conversions.length; i++) {
  975. var c = conversions[i];
  976. var tiny = tinycolor(c.hex);
  977. assertEquals(
  978. tiny.toHexString(),
  979. tinycolor(tiny.toRgb()).toHexString(),
  980. "RGB Object"
  981. );
  982. }
  983. });
  984. Deno.test("RGB String", function () {
  985. for (var i = 0; i < conversions.length; i++) {
  986. var c = conversions[i];
  987. var tiny = tinycolor(c.hex);
  988. assertEquals(
  989. tiny.toHexString(),
  990. tinycolor(tiny.toRgbString()).toHexString(),
  991. "RGB String"
  992. );
  993. }
  994. });
  995. Deno.test("PRGB Object", function () {
  996. for (var i = 0; i < conversions.length; i++) {
  997. var c = conversions[i];
  998. var tiny = tinycolor(c.hex);
  999. var input = tiny.toRgb();
  1000. var output = tinycolor(tiny.toPercentageRgb()).toRgb();
  1001. var maxDiff = 2;
  1002. assertEquals(
  1003. Math.abs(input.r - output.r) <= maxDiff,
  1004. true,
  1005. "Red value difference <= " + maxDiff
  1006. );
  1007. assertEquals(
  1008. Math.abs(input.g - output.g) <= maxDiff,
  1009. true,
  1010. "Green value difference <= " + maxDiff
  1011. );
  1012. assertEquals(
  1013. Math.abs(input.b - output.b) <= maxDiff,
  1014. true,
  1015. "Blue value difference <= " + maxDiff
  1016. );
  1017. }
  1018. });
  1019. Deno.test("PRGB String", function () {
  1020. for (var i = 0; i < conversions.length; i++) {
  1021. var c = conversions[i];
  1022. var tiny = tinycolor(c.hex);
  1023. var input = tiny.toRgb();
  1024. var output = tinycolor(tiny.toPercentageRgbString()).toRgb();
  1025. var maxDiff = 2;
  1026. assertEquals(
  1027. Math.abs(input.r - output.r) <= maxDiff,
  1028. true,
  1029. "Red value difference <= " + maxDiff
  1030. );
  1031. assertEquals(
  1032. Math.abs(input.g - output.g) <= maxDiff,
  1033. true,
  1034. "Green value difference <= " + maxDiff
  1035. );
  1036. assertEquals(
  1037. Math.abs(input.b - output.b) <= maxDiff,
  1038. true,
  1039. "Blue value difference <= " + maxDiff
  1040. );
  1041. }
  1042. });
  1043. Deno.test("Object", function () {
  1044. for (var i = 0; i < conversions.length; i++) {
  1045. var c = conversions[i];
  1046. var tiny = tinycolor(c.hex);
  1047. assertEquals(tiny.toHexString(), tinycolor(tiny).toHexString(), "Object");
  1048. }
  1049. });
  1050. Deno.test("Color equality", function () {
  1051. assert(tinycolor.equals("#ff0000", "#ff0000"), "Same hex");
  1052. assert(tinycolor.equals("#ff0000", "rgb(255, 0, 0)"), "Same alphas");
  1053. assert(
  1054. !tinycolor.equals("#ff0000", "rgba(255, 0, 0, .1)"),
  1055. "Different alphas"
  1056. );
  1057. assert(tinycolor.equals("#ff000066", "rgba(255, 0, 0, .4)"), "Same alphas");
  1058. assert(tinycolor.equals("#f009", "rgba(255, 0, 0, .6)"), "Same alphas");
  1059. assert(tinycolor.equals("#336699CC", "369C"), "Same hex");
  1060. assert(tinycolor.equals("ff0000", "#ff0000"), "Same hex");
  1061. assert(tinycolor.equals("#f00", "#ff0000"), "Same hex");
  1062. assert(tinycolor.equals("#f00", "#ff0000"), "Same hex");
  1063. assert(tinycolor.equals("f00", "#ff0000"), "Same hex");
  1064. assertEquals(tinycolor("010101").toHexString(), "#010101");
  1065. assert(!tinycolor.equals("#ff0000", "#00ff00"), "Different hex");
  1066. assert(
  1067. tinycolor.equals("#ff8000", "rgb(100%, 50%, 0%)"),
  1068. "Percentage bounds checking"
  1069. );
  1070. });
  1071. Deno.test("isReadable", function () {
  1072. // "#ff0088", "#8822aa" (values used in old WCAG1 tests)
  1073. assert(
  1074. tinycolor.isReadable("#000000", "#ffffff", { level: "AA", size: "small" }),
  1075. "white/black is readable"
  1076. );
  1077. assert(
  1078. !tinycolor.isReadable("#ff0088", "#5c1a72", {}),
  1079. "not readable - empty wcag2 object"
  1080. );
  1081. assert(
  1082. !tinycolor.isReadable("#ff0088", "#8822aa", { level: "AA", size: "small" }),
  1083. "not readable - AA small"
  1084. );
  1085. assert(
  1086. !tinycolor.isReadable("#ff0088", "#8822aa", { level: "AA", size: "large" }),
  1087. "not readable - AA large"
  1088. );
  1089. assert(
  1090. !tinycolor.isReadable("#ff0088", "#8822aa", {
  1091. level: "AAA",
  1092. size: "small",
  1093. }),
  1094. "not readable - AAA small"
  1095. );
  1096. assert(
  1097. !tinycolor.isReadable("#ff0088", "#8822aa", {
  1098. level: "AAA",
  1099. size: "large",
  1100. }),
  1101. "not readable - AAA large"
  1102. );
  1103. // values derived from and validated using the calculators at http://www.dasplankton.de/ContrastA/
  1104. // and http://webaim.org/resources/contrastchecker/
  1105. // "#ff0088", "#5c1a72": contrast ratio 3.04
  1106. assert(
  1107. !tinycolor.isReadable("#ff0088", "#5c1a72", { level: "AA", size: "small" }),
  1108. "not readable - AA small"
  1109. );
  1110. assert(
  1111. tinycolor.isReadable("#ff0088", "#5c1a72", { level: "AA", size: "large" }),
  1112. "readable - AA large"
  1113. );
  1114. assert(
  1115. !tinycolor.isReadable("#ff0088", "#5c1a72", {
  1116. level: "AAA",
  1117. size: "small",
  1118. }),
  1119. "not readable - AAA small"
  1120. );
  1121. assert(
  1122. !tinycolor.isReadable("#ff0088", "#5c1a72", {
  1123. level: "AAA",
  1124. size: "large",
  1125. }),
  1126. "not readable - AAA large"
  1127. );
  1128. // "#ff0088", "#2e0c3a": contrast ratio 4.56
  1129. assert(
  1130. tinycolor.isReadable("#ff0088", "#2e0c3a", { level: "AA", size: "small" }),
  1131. "readable - AA small"
  1132. );
  1133. assert(
  1134. tinycolor.isReadable("#ff0088", "#2e0c3a", { level: "AA", size: "large" }),
  1135. "readable - AA large"
  1136. );
  1137. assert(
  1138. !tinycolor.isReadable("#ff0088", "#2e0c3a", {
  1139. level: "AAA",
  1140. size: "small",
  1141. }),
  1142. "not readable - AAA small"
  1143. );
  1144. assert(
  1145. tinycolor.isReadable("#ff0088", "#2e0c3a", { level: "AAA", size: "large" }),
  1146. "readable - AAA large"
  1147. );
  1148. // "#db91b8", "#2e0c3a": contrast ratio 7.12
  1149. assert(
  1150. tinycolor.isReadable("#db91b8", "#2e0c3a", { level: "AA", size: "small" }),
  1151. "readable - AA small"
  1152. );
  1153. assert(
  1154. tinycolor.isReadable("#db91b8", "#2e0c3a", { level: "AA", size: "large" }),
  1155. "readable - AA large"
  1156. );
  1157. assert(
  1158. tinycolor.isReadable("#db91b8", "#2e0c3a", { level: "AAA", size: "small" }),
  1159. "readable - AAA small"
  1160. );
  1161. assert(
  1162. tinycolor.isReadable("#db91b8", "#2e0c3a", { level: "AAA", size: "large" }),
  1163. "readable - AAA large"
  1164. );
  1165. });
  1166. Deno.test("readability", function () {
  1167. // check return values from readability function. See isReadable above for standards tests.
  1168. assertEquals(
  1169. tinycolor.readability("#000", "#000"),
  1170. 1,
  1171. "Readability function test 0"
  1172. );
  1173. assertEquals(
  1174. tinycolor.readability("#000", "#111"),
  1175. 1.1121078324840545,
  1176. "Readability function test 1"
  1177. );
  1178. assertEquals(
  1179. tinycolor.readability("#000", "#fff"),
  1180. 21,
  1181. "Readability function test 2"
  1182. );
  1183. });
  1184. Deno.test("mostReadable", function () {
  1185. assertEquals(
  1186. tinycolor
  1187. .mostReadable("#000", ["#111", "#222", { wcag2: {} }])
  1188. .toHexString(),
  1189. "#222222",
  1190. "readable color present"
  1191. );
  1192. assertEquals(
  1193. tinycolor
  1194. .mostReadable("#f00", ["#d00", "#0d0"], { wcag2: {} })
  1195. .toHexString(),
  1196. "#00dd00",
  1197. "readable color present"
  1198. );
  1199. assertEquals(
  1200. tinycolor
  1201. .mostReadable("#fff", ["#fff", "#fff"], { wcag2: {} })
  1202. .toHexString(),
  1203. "#ffffff",
  1204. "no different color in list"
  1205. );
  1206. //includeFallbackColors
  1207. assertEquals(
  1208. tinycolor
  1209. .mostReadable("#fff", ["#fff", "#fff"], {
  1210. includeFallbackColors: true,
  1211. })
  1212. .toHexString(),
  1213. "#000000",
  1214. "no different color in list"
  1215. );
  1216. assertEquals(
  1217. tinycolor
  1218. .mostReadable("#123", ["#124", "#125"], {
  1219. includeFallbackColors: false,
  1220. })
  1221. .toHexString(),
  1222. "#112255",
  1223. "no readable color in list"
  1224. );
  1225. assertEquals(
  1226. tinycolor
  1227. .mostReadable("#123", ["#000", "#fff"], {
  1228. includeFallbackColors: false,
  1229. })
  1230. .toHexString(),
  1231. "#ffffff",
  1232. "verify assumption"
  1233. );
  1234. assertEquals(
  1235. tinycolor
  1236. .mostReadable("#123", ["#124", "#125"], {
  1237. includeFallbackColors: true,
  1238. })
  1239. .toHexString(),
  1240. "#ffffff",
  1241. "no readable color in list"
  1242. );
  1243. assertEquals(
  1244. tinycolor
  1245. .mostReadable("#ff0088", ["#000", "#fff"], {
  1246. includeFallbackColors: false,
  1247. })
  1248. .toHexString(),
  1249. "#000000",
  1250. "verify assumption"
  1251. );
  1252. assertEquals(
  1253. tinycolor
  1254. .mostReadable("#ff0088", ["#2e0c3a"], {
  1255. includeFallbackColors: true,
  1256. level: "AAA",
  1257. size: "large",
  1258. })
  1259. .toHexString(),
  1260. "#2e0c3a",
  1261. "readable color present"
  1262. );
  1263. assertEquals(
  1264. tinycolor
  1265. .mostReadable("#ff0088", ["#2e0c3a"], {
  1266. includeFallbackColors: true,
  1267. level: "AAA",
  1268. size: "small",
  1269. })
  1270. .toHexString(),
  1271. "#000000",
  1272. "no readable color in list"
  1273. );
  1274. assertEquals(
  1275. tinycolor
  1276. .mostReadable("#371b2c", ["#000", "#fff"], {
  1277. includeFallbackColors: false,
  1278. })
  1279. .toHexString(),
  1280. "#ffffff",
  1281. "verify assumption"
  1282. );
  1283. assertEquals(
  1284. tinycolor
  1285. .mostReadable("#371b2c", ["#a9acb6"], {
  1286. includeFallbackColors: true,
  1287. level: "AAA",
  1288. size: "large",
  1289. })
  1290. .toHexString(),
  1291. "#a9acb6",
  1292. "readable color present"
  1293. );
  1294. assertEquals(
  1295. tinycolor
  1296. .mostReadable("#371b2c", ["#a9acb6"], {
  1297. includeFallbackColors: true,
  1298. level: "AAA",
  1299. size: "small",
  1300. })
  1301. .toHexString(),
  1302. "#ffffff",
  1303. "no readable color in list"
  1304. );
  1305. });
  1306. Deno.test("Filters", function () {
  1307. assertEquals(
  1308. tinycolor("red").toFilter(),
  1309. "progid:DXImageTransform.Microsoft.gradient(startColorstr=#ffff0000,endColorstr=#ffff0000)"
  1310. );
  1311. assertEquals(
  1312. tinycolor("red").toFilter("blue"),
  1313. "progid:DXImageTransform.Microsoft.gradient(startColorstr=#ffff0000,endColorstr=#ff0000ff)"
  1314. );
  1315. assertEquals(
  1316. tinycolor("transparent").toFilter(),
  1317. "progid:DXImageTransform.Microsoft.gradient(startColorstr=#00000000,endColorstr=#00000000)"
  1318. );
  1319. assertEquals(
  1320. tinycolor("transparent").toFilter("red"),
  1321. "progid:DXImageTransform.Microsoft.gradient(startColorstr=#00000000,endColorstr=#ffff0000)"
  1322. );
  1323. assertEquals(
  1324. tinycolor("#f0f0f0dd").toFilter(),
  1325. "progid:DXImageTransform.Microsoft.gradient(startColorstr=#ddf0f0f0,endColorstr=#ddf0f0f0)"
  1326. );
  1327. assertEquals(
  1328. tinycolor("rgba(0, 0, 255, .5").toFilter(),
  1329. "progid:DXImageTransform.Microsoft.gradient(startColorstr=#800000ff,endColorstr=#800000ff)"
  1330. );
  1331. });
  1332. /* Originally generated with:
  1333. var results = [];
  1334. for (var i = 0; i <= 100; i++) results.push( tinycolor.saturate("red", i).toHex() )
  1335. console.log(JSON.stringify(results))
  1336. */
  1337. var DESATURATIONS = [
  1338. "ff0000",
  1339. "fe0101",
  1340. "fc0303",
  1341. "fb0404",
  1342. "fa0505",
  1343. "f90606",
  1344. "f70808",
  1345. "f60909",
  1346. "f50a0a",
  1347. "f40b0b",
  1348. "f20d0d",
  1349. "f10e0e",
  1350. "f00f0f",
  1351. "ee1111",
  1352. "ed1212",
  1353. "ec1313",
  1354. "eb1414",
  1355. "e91616",
  1356. "e81717",
  1357. "e71818",
  1358. "e61919",
  1359. "e41b1b",
  1360. "e31c1c",
  1361. "e21d1d",
  1362. "e01f1f",
  1363. "df2020",
  1364. "de2121",
  1365. "dd2222",
  1366. "db2424",
  1367. "da2525",
  1368. "d92626",
  1369. "d72828",
  1370. "d62929",
  1371. "d52a2a",
  1372. "d42b2b",
  1373. "d22d2d",
  1374. "d12e2e",
  1375. "d02f2f",
  1376. "cf3030",
  1377. "cd3232",
  1378. "cc3333",
  1379. "cb3434",
  1380. "c93636",
  1381. "c83737",
  1382. "c73838",
  1383. "c63939",
  1384. "c43b3b",
  1385. "c33c3c",
  1386. "c23d3d",
  1387. "c13e3e",
  1388. "bf4040",
  1389. "be4141",
  1390. "bd4242",
  1391. "bb4444",
  1392. "ba4545",
  1393. "b94646",
  1394. "b84747",
  1395. "b64949",
  1396. "b54a4a",
  1397. "b44b4b",
  1398. "b34d4d",
  1399. "b14e4e",
  1400. "b04f4f",
  1401. "af5050",
  1402. "ad5252",
  1403. "ac5353",
  1404. "ab5454",
  1405. "aa5555",
  1406. "a85757",
  1407. "a75858",
  1408. "a65959",
  1409. "a45b5b",
  1410. "a35c5c",
  1411. "a25d5d",
  1412. "a15e5e",
  1413. "9f6060",
  1414. "9e6161",
  1415. "9d6262",
  1416. "9c6363",
  1417. "9a6565",
  1418. "996666",
  1419. "986767",
  1420. "966969",
  1421. "956a6a",
  1422. "946b6b",
  1423. "936c6c",
  1424. "916e6e",
  1425. "906f6f",
  1426. "8f7070",
  1427. "8e7171",
  1428. "8c7373",
  1429. "8b7474",
  1430. "8a7575",
  1431. "887777",
  1432. "877878",
  1433. "867979",
  1434. "857a7a",
  1435. "837c7c",
  1436. "827d7d",
  1437. "817e7e",
  1438. "808080",
  1439. ];
  1440. var SATURATIONS = [
  1441. "ff0000",
  1442. "ff0000",
  1443. "ff0000",
  1444. "ff0000",
  1445. "ff0000",
  1446. "ff0000",
  1447. "ff0000",
  1448. "ff0000",
  1449. "ff0000",
  1450. "ff0000",
  1451. "ff0000",
  1452. "ff0000",
  1453. "ff0000",
  1454. "ff0000",
  1455. "ff0000",
  1456. "ff0000",
  1457. "ff0000",
  1458. "ff0000",
  1459. "ff0000",
  1460. "ff0000",
  1461. "ff0000",
  1462. "ff0000",
  1463. "ff0000",
  1464. "ff0000",
  1465. "ff0000",
  1466. "ff0000",
  1467. "ff0000",
  1468. "ff0000",
  1469. "ff0000",
  1470. "ff0000",
  1471. "ff0000",
  1472. "ff0000",
  1473. "ff0000",
  1474. "ff0000",
  1475. "ff0000",
  1476. "ff0000",
  1477. "ff0000",
  1478. "ff0000",
  1479. "ff0000",
  1480. "ff0000",
  1481. "ff0000",
  1482. "ff0000",
  1483. "ff0000",
  1484. "ff0000",
  1485. "ff0000",
  1486. "ff0000",
  1487. "ff0000",
  1488. "ff0000",
  1489. "ff0000",
  1490. "ff0000",
  1491. "ff0000",
  1492. "ff0000",
  1493. "ff0000",
  1494. "ff0000",
  1495. "ff0000",
  1496. "ff0000",
  1497. "ff0000",
  1498. "ff0000",
  1499. "ff0000",
  1500. "ff0000",
  1501. "ff0000",
  1502. "ff0000",
  1503. "ff0000",
  1504. "ff0000",
  1505. "ff0000",
  1506. "ff0000",
  1507. "ff0000",
  1508. "ff0000",
  1509. "ff0000",
  1510. "ff0000",
  1511. "ff0000",
  1512. "ff0000",
  1513. "ff0000",
  1514. "ff0000",
  1515. "ff0000",
  1516. "ff0000",
  1517. "ff0000",
  1518. "ff0000",
  1519. "ff0000",
  1520. "ff0000",
  1521. "ff0000",
  1522. "ff0000",
  1523. "ff0000",
  1524. "ff0000",
  1525. "ff0000",
  1526. "ff0000",
  1527. "ff0000",
  1528. "ff0000",
  1529. "ff0000",
  1530. "ff0000",
  1531. "ff0000",
  1532. "ff0000",
  1533. "ff0000",
  1534. "ff0000",
  1535. "ff0000",
  1536. "ff0000",
  1537. "ff0000",
  1538. "ff0000",
  1539. "ff0000",
  1540. "ff0000",
  1541. "ff0000",
  1542. ];
  1543. var LIGHTENS = [
  1544. "ff0000",
  1545. "ff0505",
  1546. "ff0a0a",
  1547. "ff0f0f",
  1548. "ff1414",
  1549. "ff1a1a",
  1550. "ff1f1f",
  1551. "ff2424",
  1552. "ff2929",
  1553. "ff2e2e",
  1554. "ff3333",
  1555. "ff3838",
  1556. "ff3d3d",
  1557. "ff4242",
  1558. "ff4747",
  1559. "ff4d4d",
  1560. "ff5252",
  1561. "ff5757",
  1562. "ff5c5c",
  1563. "ff6161",
  1564. "ff6666",
  1565. "ff6b6b",
  1566. "ff7070",
  1567. "ff7575",
  1568. "ff7a7a",
  1569. "ff8080",
  1570. "ff8585",
  1571. "ff8a8a",
  1572. "ff8f8f",
  1573. "ff9494",
  1574. "ff9999",
  1575. "ff9e9e",
  1576. "ffa3a3",
  1577. "ffa8a8",
  1578. "ffadad",
  1579. "ffb3b3",
  1580. "ffb8b8",
  1581. "ffbdbd",
  1582. "ffc2c2",
  1583. "ffc7c7",
  1584. "ffcccc",
  1585. "ffd1d1",
  1586. "ffd6d6",
  1587. "ffdbdb",
  1588. "ffe0e0",
  1589. "ffe5e5",
  1590. "ffebeb",
  1591. "fff0f0",
  1592. "fff5f5",
  1593. "fffafa",
  1594. "ffffff",
  1595. "ffffff",
  1596. "ffffff",
  1597. "ffffff",
  1598. "ffffff",
  1599. "ffffff",
  1600. "ffffff",
  1601. "ffffff",
  1602. "ffffff",
  1603. "ffffff",
  1604. "ffffff",
  1605. "ffffff",
  1606. "ffffff",
  1607. "ffffff",
  1608. "ffffff",
  1609. "ffffff",
  1610. "ffffff",
  1611. "ffffff",
  1612. "ffffff",
  1613. "ffffff",
  1614. "ffffff",
  1615. "ffffff",
  1616. "ffffff",
  1617. "ffffff",
  1618. "ffffff",
  1619. "ffffff",
  1620. "ffffff",
  1621. "ffffff",
  1622. "ffffff",
  1623. "ffffff",
  1624. "ffffff",
  1625. "ffffff",
  1626. "ffffff",
  1627. "ffffff",
  1628. "ffffff",
  1629. "ffffff",
  1630. "ffffff",
  1631. "ffffff",
  1632. "ffffff",
  1633. "ffffff",
  1634. "ffffff",
  1635. "ffffff",
  1636. "ffffff",
  1637. "ffffff",
  1638. "ffffff",
  1639. "ffffff",
  1640. "ffffff",
  1641. "ffffff",
  1642. "ffffff",
  1643. "ffffff",
  1644. "ffffff",
  1645. ];
  1646. var BRIGHTENS = [
  1647. "ff0000",
  1648. "ff0303",
  1649. "ff0505",
  1650. "ff0808",
  1651. "ff0a0a",
  1652. "ff0d0d",
  1653. "ff0f0f",
  1654. "ff1212",
  1655. "ff1414",
  1656. "ff1717",
  1657. "ff1919",
  1658. "ff1c1c",
  1659. "ff1f1f",
  1660. "ff2121",
  1661. "ff2424",
  1662. "ff2626",
  1663. "ff2929",
  1664. "ff2b2b",
  1665. "ff2e2e",
  1666. "ff3030",
  1667. "ff3333",
  1668. "ff3636",
  1669. "ff3838",
  1670. "ff3b3b",
  1671. "ff3d3d",
  1672. "ff4040",
  1673. "ff4242",
  1674. "ff4545",
  1675. "ff4747",
  1676. "ff4a4a",
  1677. "ff4c4c",
  1678. "ff4f4f",
  1679. "ff5252",
  1680. "ff5454",
  1681. "ff5757",
  1682. "ff5959",
  1683. "ff5c5c",
  1684. "ff5e5e",
  1685. "ff6161",
  1686. "ff6363",
  1687. "ff6666",
  1688. "ff6969",
  1689. "ff6b6b",
  1690. "ff6e6e",
  1691. "ff7070",
  1692. "ff7373",
  1693. "ff7575",
  1694. "ff7878",
  1695. "ff7a7a",
  1696. "ff7d7d",
  1697. "ff7f7f",
  1698. "ff8282",
  1699. "ff8585",
  1700. "ff8787",
  1701. "ff8a8a",
  1702. "ff8c8c",
  1703. "ff8f8f",
  1704. "ff9191",
  1705. "ff9494",
  1706. "ff9696",
  1707. "ff9999",
  1708. "ff9c9c",
  1709. "ff9e9e",
  1710. "ffa1a1",
  1711. "ffa3a3",
  1712. "ffa6a6",
  1713. "ffa8a8",
  1714. "ffabab",
  1715. "ffadad",
  1716. "ffb0b0",
  1717. "ffb2b2",
  1718. "ffb5b5",
  1719. "ffb8b8",
  1720. "ffbaba",
  1721. "ffbdbd",
  1722. "ffbfbf",
  1723. "ffc2c2",
  1724. "ffc4c4",
  1725. "ffc7c7",
  1726. "ffc9c9",
  1727. "ffcccc",
  1728. "ffcfcf",
  1729. "ffd1d1",
  1730. "ffd4d4",
  1731. "ffd6d6",
  1732. "ffd9d9",
  1733. "ffdbdb",
  1734. "ffdede",
  1735. "ffe0e0",
  1736. "ffe3e3",
  1737. "ffe5e5",
  1738. "ffe8e8",
  1739. "ffebeb",
  1740. "ffeded",
  1741. "fff0f0",
  1742. "fff2f2",
  1743. "fff5f5",
  1744. "fff7f7",
  1745. "fffafa",
  1746. "fffcfc",
  1747. "ffffff",
  1748. ];
  1749. var DARKENS = [
  1750. "ff0000",
  1751. "fa0000",
  1752. "f50000",
  1753. "f00000",
  1754. "eb0000",
  1755. "e60000",
  1756. "e00000",
  1757. "db0000",
  1758. "d60000",
  1759. "d10000",
  1760. "cc0000",
  1761. "c70000",
  1762. "c20000",
  1763. "bd0000",
  1764. "b80000",
  1765. "b30000",
  1766. "ad0000",
  1767. "a80000",
  1768. "a30000",
  1769. "9e0000",
  1770. "990000",
  1771. "940000",
  1772. "8f0000",
  1773. "8a0000",
  1774. "850000",
  1775. "800000",
  1776. "7a0000",
  1777. "750000",
  1778. "700000",
  1779. "6b0000",
  1780. "660000",
  1781. "610000",
  1782. "5c0000",
  1783. "570000",
  1784. "520000",
  1785. "4d0000",
  1786. "470000",
  1787. "420000",
  1788. "3d0000",
  1789. "380000",
  1790. "330000",
  1791. "2e0000",
  1792. "290000",
  1793. "240000",
  1794. "1f0000",
  1795. "190000",
  1796. "140000",
  1797. "0f0000",
  1798. "0a0000",
  1799. "050000",
  1800. "000000",
  1801. "000000",
  1802. "000000",
  1803. "000000",
  1804. "000000",
  1805. "000000",
  1806. "000000",
  1807. "000000",
  1808. "000000",
  1809. "000000",
  1810. "000000",
  1811. "000000",
  1812. "000000",
  1813. "000000",
  1814. "000000",
  1815. "000000",
  1816. "000000",
  1817. "000000",
  1818. "000000",
  1819. "000000",
  1820. "000000",
  1821. "000000",
  1822. "000000",
  1823. "000000",
  1824. "000000",
  1825. "000000",
  1826. "000000",
  1827. "000000",
  1828. "000000",
  1829. "000000",
  1830. "000000",
  1831. "000000",
  1832. "000000",
  1833. "000000",
  1834. "000000",
  1835. "000000",
  1836. "000000",
  1837. "000000",
  1838. "000000",
  1839. "000000",
  1840. "000000",
  1841. "000000",
  1842. "000000",
  1843. "000000",
  1844. "000000",
  1845. "000000",
  1846. "000000",
  1847. "000000",
  1848. "000000",
  1849. "000000",
  1850. "000000",
  1851. ];
  1852. Deno.test("Modifications", function () {
  1853. for (var i = 0; i <= 100; i++) {
  1854. assertEquals(
  1855. tinycolor("red").desaturate(i).toHex(),
  1856. DESATURATIONS[i],
  1857. "Desaturation " + i + " works"
  1858. );
  1859. }
  1860. for (var i = 0; i <= 100; i++) {
  1861. assertEquals(
  1862. tinycolor("red").saturate(i).toHex(),
  1863. SATURATIONS[i],
  1864. "Saturation " + i + " works"
  1865. );
  1866. }
  1867. for (var i = 0; i <= 100; i++) {
  1868. assertEquals(
  1869. tinycolor("red").lighten(i).toHex(),
  1870. LIGHTENS[i],
  1871. "Lighten " + i + " works"
  1872. );
  1873. }
  1874. for (var i = 0; i <= 100; i++) {
  1875. assertEquals(
  1876. tinycolor("red").brighten(i).toHex(),
  1877. BRIGHTENS[i],
  1878. "Brighter " + i + " works"
  1879. );
  1880. }
  1881. for (var i = 0; i <= 100; i++) {
  1882. assertEquals(
  1883. tinycolor("red").darken(i).toHex(),
  1884. DARKENS[i],
  1885. "Darken " + i + " works"
  1886. );
  1887. }
  1888. assertEquals(
  1889. tinycolor("red").greyscale().toHex(),
  1890. "808080",
  1891. "Greyscale works"
  1892. );
  1893. });
  1894. Deno.test("Spin", function () {
  1895. assertEquals(
  1896. Math.round(tinycolor("#f00").spin(-1234).toHsl().h),
  1897. 206,
  1898. "Spinning -1234 works"
  1899. );
  1900. assertEquals(
  1901. Math.round(tinycolor("#f00").spin(-360).toHsl().h),
  1902. 0,
  1903. "Spinning -360 works"
  1904. );
  1905. assertEquals(
  1906. Math.round(tinycolor("#f00").spin(-120).toHsl().h),
  1907. 240,
  1908. "Spinning -120 works"
  1909. );
  1910. assertEquals(
  1911. Math.round(tinycolor("#f00").spin(0).toHsl().h),
  1912. 0,
  1913. "Spinning 0 works"
  1914. );
  1915. assertEquals(
  1916. Math.round(tinycolor("#f00").spin(10).toHsl().h),
  1917. 10,
  1918. "Spinning 10 works"
  1919. );
  1920. assertEquals(
  1921. Math.round(tinycolor("#f00").spin(360).toHsl().h),
  1922. 0,
  1923. "Spinning 360 works"
  1924. );
  1925. assertEquals(
  1926. Math.round(tinycolor("#f00").spin(2345).toHsl().h),
  1927. 185,
  1928. "Spinning 2345 works"
  1929. );
  1930. [-360, 0, 360].forEach(function (delta) {
  1931. Object.keys(tinycolor.names).forEach(function (name) {
  1932. assertEquals(
  1933. tinycolor(name).toHex(),
  1934. tinycolor(name).spin(delta).toHex(),
  1935. "Spinning " + delta.toString() + " has no effect"
  1936. );
  1937. });
  1938. });
  1939. });
  1940. Deno.test("Mix", function () {
  1941. // amount 0 or none
  1942. assertEquals(
  1943. tinycolor.mix("#000", "#fff").toHsl().l,
  1944. 0.5,
  1945. "Mixing without amount works"
  1946. );
  1947. assertEquals(
  1948. tinycolor.mix("#f00", "#000", 0).toHex(),
  1949. "ff0000",
  1950. "Mixing with 0 amount works"
  1951. );
  1952. // This case checks the the problem with floating point numbers (eg 255/90)
  1953. assertEquals(
  1954. tinycolor.mix("#fff", "#000", 90).toHex(),
  1955. "1a1a1a",
  1956. "Mixing with 90 amount works correctly"
  1957. );
  1958. // black and white
  1959. for (var i = 0; i < 100; i++) {
  1960. assertEquals(
  1961. Math.round(tinycolor.mix("#000", "#fff", i).toHsl().l * 100) / 100,
  1962. i / 100,
  1963. "Mixing black and white with " + i + " amount works"
  1964. );
  1965. }
  1966. // with colors
  1967. for (var i = 0; i < 100; i++) {
  1968. var new_hex = Math.round((255 * (100 - i)) / 100).toString(16);
  1969. if (new_hex.length === 1) {
  1970. new_hex = "0" + new_hex;
  1971. }
  1972. assertEquals(
  1973. tinycolor.mix("#f00", "#000", i).toHex(),
  1974. new_hex + "0000",
  1975. "Mixing " + i + " (red channel)"
  1976. );
  1977. assertEquals(
  1978. tinycolor.mix("#0f0", "#000", i).toHex(),
  1979. "00" + new_hex + "00",
  1980. "Mixing " + i + " (green channel)"
  1981. );
  1982. assertEquals(
  1983. tinycolor.mix("#00f", "#000", i).toHex(),
  1984. "0000" + new_hex,
  1985. "Mixing " + i + " (blue channel)"
  1986. );
  1987. assertEquals(
  1988. tinycolor.mix(tinycolor("transparent"), "#000", i).toRgb().a,
  1989. i / 100,
  1990. "Mixing " + i + " (alpha channel)"
  1991. );
  1992. }
  1993. });
  1994. // The combination tests need to be expanded furthe
  1995. function colorsToHexString(colors) {
  1996. return colors
  1997. .map(function (c) {
  1998. return c.toHex();
  1999. })
  2000. .join(",");
  2001. }
  2002. Deno.test("complement", function () {
  2003. var complementDoesntModifyInstance = tinycolor("red");
  2004. assertEquals(
  2005. complementDoesntModifyInstance.complement().toHex(),
  2006. "00ffff",
  2007. "Complement works"
  2008. );
  2009. assertEquals(
  2010. complementDoesntModifyInstance.toHex(),
  2011. "ff0000",
  2012. "Complement did not modify this color"
  2013. );
  2014. });
  2015. Deno.test("analogous", function () {
  2016. var combination = tinycolor("red").analogous();
  2017. assertEquals(
  2018. colorsToHexString(combination),
  2019. "ff0000,ff0066,ff0033,ff0000,ff3300,ff6600",
  2020. "Correct Combination"
  2021. );
  2022. });
  2023. Deno.test("monochromatic", function () {
  2024. var combination = tinycolor("red").monochromatic();
  2025. assertEquals(
  2026. colorsToHexString(combination),
  2027. "ff0000,2a0000,550000,800000,aa0000,d40000",
  2028. "Correct Combination"
  2029. );
  2030. });
  2031. Deno.test("splitcomplement", function () {
  2032. var combination = tinycolor("red").splitcomplement();
  2033. assertEquals(
  2034. colorsToHexString(combination),
  2035. "ff0000,ccff00,0066ff",
  2036. "Correct Combination"
  2037. );
  2038. });
  2039. Deno.test("triad", function () {
  2040. var combination = tinycolor("red").triad();
  2041. assertEquals(
  2042. colorsToHexString(combination),
  2043. "ff0000,00ff00,0000ff",
  2044. "Correct Combination"
  2045. );
  2046. });
  2047. Deno.test("tetrad", function () {
  2048. var combination = tinycolor("red").tetrad();
  2049. assertEquals(
  2050. colorsToHexString(combination),
  2051. "ff0000,80ff00,00ffff,7f00ff",
  2052. "Correct Combination"
  2053. );
  2054. });
  2055. Deno.test({
  2056. name: "polyad",
  2057. // Disabled until https://github.com/bgrins/TinyColor/issues/254
  2058. ignore: true,
  2059. fn: function () {
  2060. assertThrows(() => {
  2061. tinycolor("red").polyad();
  2062. });
  2063. assertThrows(() => {
  2064. tinycolor("red").polyad(-1);
  2065. });
  2066. assertThrows(() => {
  2067. tinycolor("red").polyad("invalid");
  2068. });
  2069. assertEquals(colorsToHexString(tinycolor("red").polyad(1)), "ff0000");
  2070. assertEquals(colorsToHexString(tinycolor("red").polyad("1")), "ff0000");
  2071. assertEquals(
  2072. colorsToHexString(tinycolor("red").polyad(2)),
  2073. "ff0000,00ffff"
  2074. );
  2075. assertEquals(
  2076. colorsToHexString(tinycolor("red").polyad(3)),
  2077. "ff0000,00ff00,0000ff"
  2078. );
  2079. assertEquals(
  2080. colorsToHexString(tinycolor("red").polyad(4)),
  2081. "ff0000,80ff00,00ffff,7f00ff"
  2082. );
  2083. assertEquals(
  2084. colorsToHexString(tinycolor("red").polyad(5)),
  2085. "ff0000,ccff00,00ff66,0066ff,cc00ff"
  2086. );
  2087. },
  2088. });
  2089. runDenoTests();
  2090. })();