colorizr 3.0.2 → 3.0.4

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.
package/dist/index.js CHANGED
@@ -20,8 +20,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
21
21
 
22
22
  // src/index.ts
23
- var src_exports = {};
24
- __export(src_exports, {
23
+ var index_exports = {};
24
+ __export(index_exports, {
25
25
  addAlphaToHex: () => addAlphaToHex,
26
26
  brightnessDifference: () => brightnessDifference,
27
27
  chroma: () => chroma,
@@ -31,7 +31,7 @@ __export(src_exports, {
31
31
  convert: () => convert,
32
32
  convertAlphaToHex: () => convertAlphaToHex,
33
33
  darken: () => darken,
34
- default: () => src_default,
34
+ default: () => index_default,
35
35
  desaturate: () => desaturate,
36
36
  extractAlphaFromHex: () => extractAlphaFromHex,
37
37
  extractColorParts: () => extractColorParts,
@@ -82,7 +82,7 @@ __export(src_exports, {
82
82
  textColor: () => textColor,
83
83
  transparentize: () => transparentize
84
84
  });
85
- module.exports = __toCommonJS(src_exports);
85
+ module.exports = __toCommonJS(index_exports);
86
86
 
87
87
  // src/modules/invariant.ts
88
88
  function invariant(condition, message) {
@@ -1241,9 +1241,15 @@ function textColor(input, options = {}) {
1241
1241
  const { darkColor = "#000000", lightColor = "#ffffff", threshold = 128 } = options;
1242
1242
  invariant(isString(input), MESSAGES.inputString);
1243
1243
  invariant(threshold >= 0 && threshold <= 255, MESSAGES.threshold);
1244
- const { r, g, b } = hex2rgb(parseCSS(input, "hex"));
1245
- const yiq = (r * 299 + g * 587 + b * 114) / 1e3;
1246
- return yiq >= threshold ? darkColor : lightColor;
1244
+ try {
1245
+ const { r, g, b } = hex2rgb(parseCSS(input, "hex"));
1246
+ const yiq = (r * 299 + g * 587 + b * 114) / 1e3;
1247
+ return yiq >= threshold ? darkColor : lightColor;
1248
+ } catch (error) {
1249
+ console.warn(`Invalid color input: ${input}`);
1250
+ console.warn(error);
1251
+ return darkColor;
1252
+ }
1247
1253
  }
1248
1254
 
1249
1255
  // src/transparentize.ts
@@ -1444,24 +1450,19 @@ function palette(input, options = {}) {
1444
1450
  invariant(isPlainObject(options), MESSAGES.options);
1445
1451
  const { format, lightness, saturation, size = 6, type } = options;
1446
1452
  const hsl = parseCSS(input, "hsl");
1447
- const output = [];
1448
1453
  const colorFormat = isHex(input) || isNamedColor(input) ? "hex" : extractColorParts(input).model;
1449
- switch (type) {
1450
- case "monochromatic": {
1451
- const step = 80 / size;
1452
- for (let index = size; index > 0; index--) {
1453
- output.push(hsl2hex({ ...hsl, l: step * index }));
1454
- }
1455
- break;
1454
+ const output = [];
1455
+ if (type === "monochromatic") {
1456
+ const step = 80 / size;
1457
+ for (let index = size; index > 0; index--) {
1458
+ output.push(hsl2hex({ ...hsl, l: step * index }));
1456
1459
  }
1457
- default: {
1458
- const step = 360 / size;
1459
- output.push(hsl2hex({ ...hsl, l: lightness ?? hsl.l, s: saturation ?? hsl.s }));
1460
- for (let index = 1; index < size; index++) {
1461
- const color = rotate(input, hsl.h + step * index, "hex");
1462
- output.push(hsl2hex({ ...hex2hsl(color), l: lightness ?? hsl.l, s: saturation ?? hsl.s }));
1463
- }
1464
- break;
1460
+ } else {
1461
+ const step = 360 / size;
1462
+ output.push(hsl2hex({ ...hsl, l: lightness ?? hsl.l, s: saturation ?? hsl.s }));
1463
+ for (let index = 1; index < size; index++) {
1464
+ const color = rotate(input, hsl.h + step * index, "hex");
1465
+ output.push(hsl2hex({ ...hex2hsl(color), l: lightness ?? hsl.l, s: saturation ?? hsl.s }));
1465
1466
  }
1466
1467
  }
1467
1468
  return output.map((color) => convert(color, format ?? colorFormat));
@@ -1568,51 +1569,72 @@ function scheme(input, typeOrOptions) {
1568
1569
  }
1569
1570
 
1570
1571
  // src/swatch.ts
1571
- var MIN_LIGHTNESS = 21;
1572
- var MAX_LIGHTNESS = 97;
1573
- function shadeColorDynamic(input, lightnessTuningFactor, chromaTuningFactor = 0) {
1572
+ var MIN_LIGHTNESS = 5;
1573
+ var MAX_LIGHTNESS = 95;
1574
+ function shadeColorDynamic(input, lightnessTuningFactor, chromaFactor = 0) {
1574
1575
  if (lightnessTuningFactor === 0) {
1575
- return rgb2hex(oklch2rgb({ ...input, l: input.l / 100 }));
1576
+ const { l } = input;
1577
+ return rgb2hex(oklch2rgb({ ...input, l: l / 100 }));
1576
1578
  }
1577
- return shadeColor(input, input.l + lightnessTuningFactor, chromaTuningFactor);
1579
+ return shadeColor(input, input.l + lightnessTuningFactor, chromaFactor);
1578
1580
  }
1579
- function shadeColor(input, lightness, chromaTuningFactor = 0) {
1581
+ function shadeColor(input, lightness, chromaFactor = 0) {
1580
1582
  const { c, h } = input;
1581
- return oklch2hex({ l: lightness / 100, c: clamp(c + chromaTuningFactor, 0, 0.4), h });
1583
+ const adjustedChroma = clamp(c + chromaFactor, 0, 0.4);
1584
+ return oklch2hex({ l: lightness / 100, c: adjustedChroma, h });
1582
1585
  }
1583
1586
  function swatch(input, options = {}) {
1584
1587
  invariant(isString(input), MESSAGES.inputString);
1585
- const { format, scale = "dynamic" } = options;
1588
+ const { format, monochromatic = false, scale = "dynamic", mode, variant = "base" } = options;
1586
1589
  const lch = parseCSS(input, "oklch");
1590
+ const chromaScale = {
1591
+ base: 1,
1592
+ deep: 0.8,
1593
+ neutral: 0.5,
1594
+ pastel: 0.3,
1595
+ subtle: 0.2,
1596
+ vibrant: 1.25
1597
+ }[variant];
1587
1598
  lch.l = 50;
1599
+ lch.c *= chromaScale;
1600
+ if (variant === "deep") {
1601
+ lch.l *= 0.7;
1602
+ }
1588
1603
  const colorFormat = isHex(input) || isNamedColor(input) ? "hex" : extractColorParts(input).model;
1589
1604
  const currentLightness = lch.l;
1590
- const safeMaxLightness = currentLightness >= 88.5 ? 99.5 : MAX_LIGHTNESS;
1591
- const safeMinLightness = currentLightness <= 33 ? 0 : MIN_LIGHTNESS;
1592
- const lightBase = (safeMaxLightness - currentLightness) / 5;
1593
- const darkBase = -1 * (currentLightness - safeMinLightness) / 8;
1605
+ let lightSteps = 5;
1606
+ let darkSteps = 8;
1607
+ if (mode) {
1608
+ lightSteps *= mode === "light" ? 0.75 : 1.25;
1609
+ darkSteps *= mode === "light" ? 1.25 : 0.75;
1610
+ }
1611
+ const lightStepSize = (MAX_LIGHTNESS - currentLightness) / lightSteps;
1612
+ const darkStepSize = -1 * (currentLightness - MIN_LIGHTNESS) / darkSteps;
1613
+ const getChromaFactor = (value) => {
1614
+ return monochromatic ? 0 : value;
1615
+ };
1594
1616
  const output = scale === "linear" ? {
1595
- 50: shadeColor(lch, 95, -375e-5),
1596
- 100: shadeColor(lch, 90, -375e-5),
1597
- 200: shadeColor(lch, 80, -375e-5),
1598
- 300: shadeColor(lch, 70, -375e-5),
1599
- 400: shadeColor(lch, 60, -375e-5),
1617
+ 50: shadeColor(lch, 95, getChromaFactor(-375e-5)),
1618
+ 100: shadeColor(lch, 90, getChromaFactor(-375e-5)),
1619
+ 200: shadeColor(lch, 80, getChromaFactor(-375e-5)),
1620
+ 300: shadeColor(lch, 70, getChromaFactor(-375e-5)),
1621
+ 400: shadeColor(lch, 60, getChromaFactor(-375e-5)),
1600
1622
  500: shadeColor(lch, 50),
1601
- 600: shadeColor(lch, 40, 0.025),
1602
- 700: shadeColor(lch, 30, 0.05),
1603
- 800: shadeColor(lch, 20, 0.075),
1604
- 900: shadeColor(lch, 10, 0.1)
1623
+ 600: shadeColor(lch, 40, getChromaFactor(0.025)),
1624
+ 700: shadeColor(lch, 30, getChromaFactor(0.05)),
1625
+ 800: shadeColor(lch, 20, getChromaFactor(0.075)),
1626
+ 900: shadeColor(lch, 10, getChromaFactor(0.1))
1605
1627
  } : {
1606
- 50: shadeColorDynamic(lch, 5 * lightBase, -375e-5),
1607
- 100: shadeColorDynamic(lch, 4 * lightBase, -375e-5),
1608
- 200: shadeColorDynamic(lch, 3 * lightBase, -375e-5),
1609
- 300: shadeColorDynamic(lch, 2 * lightBase, -375e-5),
1610
- 400: shadeColorDynamic(lch, lightBase, -375e-5),
1628
+ 50: shadeColorDynamic(lch, 5 * lightStepSize, getChromaFactor(-375e-5)),
1629
+ 100: shadeColorDynamic(lch, 4 * lightStepSize, getChromaFactor(-375e-5)),
1630
+ 200: shadeColorDynamic(lch, 3 * lightStepSize, getChromaFactor(-375e-5)),
1631
+ 300: shadeColorDynamic(lch, 2 * lightStepSize, getChromaFactor(-375e-5)),
1632
+ 400: shadeColorDynamic(lch, lightStepSize, getChromaFactor(-375e-5)),
1611
1633
  500: shadeColorDynamic(lch, 0),
1612
- 600: shadeColorDynamic(lch, 1.6 * darkBase, 0.025),
1613
- 700: shadeColorDynamic(lch, 1.875 * 2 * darkBase, 0.05),
1614
- 800: shadeColorDynamic(lch, 3 * 2 * darkBase, 0.075),
1615
- 900: shadeColorDynamic(lch, 4 * 2 * darkBase, 0.1)
1634
+ 600: shadeColorDynamic(lch, 1.6 * darkStepSize, getChromaFactor(0.025)),
1635
+ 700: shadeColorDynamic(lch, 3.2 * darkStepSize, getChromaFactor(0.05)),
1636
+ 800: shadeColorDynamic(lch, 4.8 * darkStepSize, getChromaFactor(0.075)),
1637
+ 900: shadeColorDynamic(lch, 6.4 * darkStepSize, getChromaFactor(0.1))
1616
1638
  };
1617
1639
  return Object.entries(output).reduce((acc, [key, value]) => {
1618
1640
  return {
@@ -1623,7 +1645,7 @@ function swatch(input, options = {}) {
1623
1645
  }
1624
1646
 
1625
1647
  // src/index.ts
1626
- var src_default = Colorizr;
1648
+ var index_default = Colorizr;
1627
1649
  // Annotate the CommonJS export names for ESM import in node:
1628
1650
  0 && (module.exports = {
1629
1651
  addAlphaToHex,