d00b1f35b130d3fde682100bcbdd6d23c59624e58bd49360dd91bf493a1317a16a345ecfe348cd3ea980fe5c807c58a608c98cba77390fe85b3146459605fc 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { TinyColor } from './index.js';
  2. // Readability Functions
  3. // ---------------------
  4. // <http://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef (WCAG Version 2)
  5. /**
  6. * AKA `contrast`
  7. *
  8. * Analyze the 2 colors and returns the color contrast defined by (WCAG Version 2)
  9. */
  10. export function readability(color1, color2) {
  11. var c1 = new TinyColor(color1);
  12. var c2 = new TinyColor(color2);
  13. return ((Math.max(c1.getLuminance(), c2.getLuminance()) + 0.05) /
  14. (Math.min(c1.getLuminance(), c2.getLuminance()) + 0.05));
  15. }
  16. /**
  17. * Ensure that foreground and background color combinations meet WCAG2 guidelines.
  18. * The third argument is an object.
  19. * the 'level' property states 'AA' or 'AAA' - if missing or invalid, it defaults to 'AA';
  20. * the 'size' property states 'large' or 'small' - if missing or invalid, it defaults to 'small'.
  21. * If the entire object is absent, isReadable defaults to {level:"AA",size:"small"}.
  22. *
  23. * Example
  24. * ```ts
  25. * new TinyColor().isReadable('#000', '#111') => false
  26. * new TinyColor().isReadable('#000', '#111', { level: 'AA', size: 'large' }) => false
  27. * ```
  28. */
  29. export function isReadable(color1, color2, wcag2) {
  30. var _a, _b;
  31. if (wcag2 === void 0) { wcag2 = { level: 'AA', size: 'small' }; }
  32. var readabilityLevel = readability(color1, color2);
  33. switch (((_a = wcag2.level) !== null && _a !== void 0 ? _a : 'AA') + ((_b = wcag2.size) !== null && _b !== void 0 ? _b : 'small')) {
  34. case 'AAsmall':
  35. case 'AAAlarge':
  36. return readabilityLevel >= 4.5;
  37. case 'AAlarge':
  38. return readabilityLevel >= 3;
  39. case 'AAAsmall':
  40. return readabilityLevel >= 7;
  41. default:
  42. return false;
  43. }
  44. }
  45. /**
  46. * Given a base color and a list of possible foreground or background
  47. * colors for that base, returns the most readable color.
  48. * Optionally returns Black or White if the most readable color is unreadable.
  49. *
  50. * @param baseColor - the base color.
  51. * @param colorList - array of colors to pick the most readable one from.
  52. * @param args - and object with extra arguments
  53. *
  54. * Example
  55. * ```ts
  56. * new TinyColor().mostReadable('#123', ['#124", "#125'], { includeFallbackColors: false }).toHexString(); // "#112255"
  57. * new TinyColor().mostReadable('#123', ['#124", "#125'],{ includeFallbackColors: true }).toHexString(); // "#ffffff"
  58. * new TinyColor().mostReadable('#a8015a', ["#faf3f3"], { includeFallbackColors:true, level: 'AAA', size: 'large' }).toHexString(); // "#faf3f3"
  59. * new TinyColor().mostReadable('#a8015a', ["#faf3f3"], { includeFallbackColors:true, level: 'AAA', size: 'small' }).toHexString(); // "#ffffff"
  60. * ```
  61. */
  62. export function mostReadable(baseColor, colorList, args) {
  63. if (args === void 0) { args = { includeFallbackColors: false, level: 'AA', size: 'small' }; }
  64. var bestColor = null;
  65. var bestScore = 0;
  66. var includeFallbackColors = args.includeFallbackColors, level = args.level, size = args.size;
  67. for (var _i = 0, colorList_1 = colorList; _i < colorList_1.length; _i++) {
  68. var color = colorList_1[_i];
  69. var score = readability(baseColor, color);
  70. if (score > bestScore) {
  71. bestScore = score;
  72. bestColor = new TinyColor(color);
  73. }
  74. }
  75. if (isReadable(baseColor, bestColor, { level: level, size: size }) || !includeFallbackColors) {
  76. return bestColor;
  77. }
  78. args.includeFallbackColors = false;
  79. return mostReadable(baseColor, ['#fff', '#000'], args);
  80. }