1 line
9.7 KiB
JSON
1 line
9.7 KiB
JSON
|
{"ast":null,"code":"import { TinyColor } from './index.js';\n// Readability Functions\n// ---------------------\n// <http://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef (WCAG Version 2)\n/**\n * AKA `contrast`\n *\n * Analyze the 2 colors and returns the color contrast defined by (WCAG Version 2)\n */\nexport function readability(color1, color2) {\n var c1 = new TinyColor(color1);\n var c2 = new TinyColor(color2);\n return (Math.max(c1.getLuminance(), c2.getLuminance()) + 0.05) / (Math.min(c1.getLuminance(), c2.getLuminance()) + 0.05);\n}\n/**\n * Ensure that foreground and background color combinations meet WCAG2 guidelines.\n * The third argument is an object.\n * the 'level' property states 'AA' or 'AAA' - if missing or invalid, it defaults to 'AA';\n * the 'size' property states 'large' or 'small' - if missing or invalid, it defaults to 'small'.\n * If the entire object is absent, isReadable defaults to {level:\"AA\",size:\"small\"}.\n *\n * Example\n * ```ts\n * new TinyColor().isReadable('#000', '#111') => false\n * new TinyColor().isReadable('#000', '#111', { level: 'AA', size: 'large' }) => false\n * ```\n */\nexport function isReadable(color1, color2, wcag2) {\n var _a, _b;\n if (wcag2 === void 0) {\n wcag2 = {\n level: 'AA',\n size: 'small'\n };\n }\n var readabilityLevel = readability(color1, color2);\n switch (((_a = wcag2.level) !== null && _a !== void 0 ? _a : 'AA') + ((_b = wcag2.size) !== null && _b !== void 0 ? _b : 'small')) {\n case 'AAsmall':\n case 'AAAlarge':\n return readabilityLevel >= 4.5;\n case 'AAlarge':\n return readabilityLevel >= 3;\n case 'AAAsmall':\n return readabilityLevel >= 7;\n default:\n return false;\n }\n}\n/**\n * Given a base color and a list of possible foreground or background\n * colors for that base, returns the most readable color.\n * Optionally returns Black or White if the most readable color is unreadable.\n *\n * @param baseColor - the base color.\n * @param colorList - array of colors to pick the most readable one from.\n * @param args - and object with extra arguments\n *\n * Example\n * ```ts\n * new TinyColor().mostReadable('#123', ['#124\", \"#125'], { includeFallbackColors: false }).toHexString(); // \"#112255\"\n * new TinyColor().mostReadable('#123', ['#124\", \"#125'],{ includeFallbackColors: true }).toHexString(); // \"#ffffff\"\n * new TinyColor().mostReadable('#a8015a', [\"#faf3f3\"], { includeFallbackColors:true, level: 'AAA', size: 'large' }).toHexString(); // \"#faf3f3\"\n * new TinyColor().mostReadable('#a8015a', [\"#faf3f3\"], { includeFallbackColors:true, level: 'AAA', size: 'small' }).toHexString(); // \"#ffffff\"\n * ```\n */\nexport function mostReadable(baseColor, colorList, args) {\n if (args === void 0) {\n args = {\n includeFallbackColors: false,\n level: 'AA',\n size: 'small'\n };\n }\n var bestColor = null;\n var bestScore = 0;\n var includeFallbackColors = args.includeFallbackColors,\n level = args.level,\n size = args.size;\n for (var _i = 0, colorList_1 = colorList; _i < colorList_1.length; _i++) {\n var color = colorList_1[_i];\n var score = readability(baseColor, color);\n if (score > bestScore) {\n bestScore = score;\n bestColor = new TinyColor(color);\n }\n }\n if (isReadable(baseColor, bestColor, {\n level: level,\n size: size\n }) || !includeFallbackColors) {\n return bestColor;\n }\n args.includeFallbackColors = false;\n return mostReadable(baseColor, ['#fff', '#000'], args);\n}","map":{"version":3,"names":["TinyColor","readability","color1","color2","c1","c2","Math","max","getLuminance","min","isReadable","wcag2","_a","_b","level","size","readabilityLevel","mostReadable","baseColor","colorList","args","includeFallbackColors","bestColor","bestScore","_i","colorList_1","length","color","score"],"sources":["C:/Users/Аришина)/source/repos/PromoCursed/node_modules/@ctrl/tinycolor/dist/module/readability.js"],"sourcesContent":["import { TinyColor } from './index.js';\n// Readabi
|