test.js 55 KB

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