@ztimson/utils 0.23.5 → 0.23.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Determine if either black or white provides more contrast to the provided color
3
+ * @param {string} background Color to compare against
4
+ * @return {"white" | "black"} Color with the most contrast
5
+ */
6
+ export declare function blackOrWhite(background: string): 'white' | 'black';
package/dist/index.cjs CHANGED
@@ -532,6 +532,13 @@ ${opts.message || this.desc}`;
532
532
  return this;
533
533
  }
534
534
  }
535
+ function blackOrWhite(background) {
536
+ const exploded = background.match(background.length >= 6 ? /\w\w/g : /\w/g);
537
+ if (!exploded) return "black";
538
+ const [r2, g, b2] = exploded.map((hex) => parseInt(hex, 16));
539
+ const luminance = (0.299 * r2 + 0.587 * g + 0.114 * b2) / 255;
540
+ return luminance > 0.5 ? "black" : "white";
541
+ }
535
542
  const LETTER_LIST = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
536
543
  const NUMBER_LIST = "0123456789";
537
544
  const SYMBOL_LIST = "~`!@#$%^&*()_-+={[}]|\\:;\"'<,>.?/";
@@ -1695,6 +1702,7 @@ ${opts.message || this.desc}`;
1695
1702
  exports.addUnique = addUnique;
1696
1703
  exports.adjustedInterval = adjustedInterval;
1697
1704
  exports.arrayDiff = arrayDiff;
1705
+ exports.blackOrWhite = blackOrWhite;
1698
1706
  exports.caseInsensitiveSort = caseInsensitiveSort;
1699
1707
  exports.clean = clean;
1700
1708
  exports.dec2Frac = dec2Frac;