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.mjs CHANGED
@@ -1163,9 +1163,15 @@ function textColor(input, options = {}) {
1163
1163
  const { darkColor = "#000000", lightColor = "#ffffff", threshold = 128 } = options;
1164
1164
  invariant(isString(input), MESSAGES.inputString);
1165
1165
  invariant(threshold >= 0 && threshold <= 255, MESSAGES.threshold);
1166
- const { r, g, b } = hex2rgb(parseCSS(input, "hex"));
1167
- const yiq = (r * 299 + g * 587 + b * 114) / 1e3;
1168
- return yiq >= threshold ? darkColor : lightColor;
1166
+ try {
1167
+ const { r, g, b } = hex2rgb(parseCSS(input, "hex"));
1168
+ const yiq = (r * 299 + g * 587 + b * 114) / 1e3;
1169
+ return yiq >= threshold ? darkColor : lightColor;
1170
+ } catch (error) {
1171
+ console.warn(`Invalid color input: ${input}`);
1172
+ console.warn(error);
1173
+ return darkColor;
1174
+ }
1169
1175
  }
1170
1176
 
1171
1177
  // src/transparentize.ts
@@ -1366,24 +1372,19 @@ function palette(input, options = {}) {
1366
1372
  invariant(isPlainObject(options), MESSAGES.options);
1367
1373
  const { format, lightness, saturation, size = 6, type } = options;
1368
1374
  const hsl = parseCSS(input, "hsl");
1369
- const output = [];
1370
1375
  const colorFormat = isHex(input) || isNamedColor(input) ? "hex" : extractColorParts(input).model;
1371
- switch (type) {
1372
- case "monochromatic": {
1373
- const step = 80 / size;
1374
- for (let index = size; index > 0; index--) {
1375
- output.push(hsl2hex({ ...hsl, l: step * index }));
1376
- }
1377
- break;
1376
+ const output = [];
1377
+ if (type === "monochromatic") {
1378
+ const step = 80 / size;
1379
+ for (let index = size; index > 0; index--) {
1380
+ output.push(hsl2hex({ ...hsl, l: step * index }));
1378
1381
  }
1379
- default: {
1380
- const step = 360 / size;
1381
- output.push(hsl2hex({ ...hsl, l: lightness ?? hsl.l, s: saturation ?? hsl.s }));
1382
- for (let index = 1; index < size; index++) {
1383
- const color = rotate(input, hsl.h + step * index, "hex");
1384
- output.push(hsl2hex({ ...hex2hsl(color), l: lightness ?? hsl.l, s: saturation ?? hsl.s }));
1385
- }
1386
- break;
1382
+ } else {
1383
+ const step = 360 / size;
1384
+ output.push(hsl2hex({ ...hsl, l: lightness ?? hsl.l, s: saturation ?? hsl.s }));
1385
+ for (let index = 1; index < size; index++) {
1386
+ const color = rotate(input, hsl.h + step * index, "hex");
1387
+ output.push(hsl2hex({ ...hex2hsl(color), l: lightness ?? hsl.l, s: saturation ?? hsl.s }));
1387
1388
  }
1388
1389
  }
1389
1390
  return output.map((color) => convert(color, format ?? colorFormat));
@@ -1490,51 +1491,72 @@ function scheme(input, typeOrOptions) {
1490
1491
  }
1491
1492
 
1492
1493
  // src/swatch.ts
1493
- var MIN_LIGHTNESS = 21;
1494
- var MAX_LIGHTNESS = 97;
1495
- function shadeColorDynamic(input, lightnessTuningFactor, chromaTuningFactor = 0) {
1494
+ var MIN_LIGHTNESS = 5;
1495
+ var MAX_LIGHTNESS = 95;
1496
+ function shadeColorDynamic(input, lightnessTuningFactor, chromaFactor = 0) {
1496
1497
  if (lightnessTuningFactor === 0) {
1497
- return rgb2hex(oklch2rgb({ ...input, l: input.l / 100 }));
1498
+ const { l } = input;
1499
+ return rgb2hex(oklch2rgb({ ...input, l: l / 100 }));
1498
1500
  }
1499
- return shadeColor(input, input.l + lightnessTuningFactor, chromaTuningFactor);
1501
+ return shadeColor(input, input.l + lightnessTuningFactor, chromaFactor);
1500
1502
  }
1501
- function shadeColor(input, lightness, chromaTuningFactor = 0) {
1503
+ function shadeColor(input, lightness, chromaFactor = 0) {
1502
1504
  const { c, h } = input;
1503
- return oklch2hex({ l: lightness / 100, c: clamp(c + chromaTuningFactor, 0, 0.4), h });
1505
+ const adjustedChroma = clamp(c + chromaFactor, 0, 0.4);
1506
+ return oklch2hex({ l: lightness / 100, c: adjustedChroma, h });
1504
1507
  }
1505
1508
  function swatch(input, options = {}) {
1506
1509
  invariant(isString(input), MESSAGES.inputString);
1507
- const { format, scale = "dynamic" } = options;
1510
+ const { format, monochromatic = false, scale = "dynamic", mode, variant = "base" } = options;
1508
1511
  const lch = parseCSS(input, "oklch");
1512
+ const chromaScale = {
1513
+ base: 1,
1514
+ deep: 0.8,
1515
+ neutral: 0.5,
1516
+ pastel: 0.3,
1517
+ subtle: 0.2,
1518
+ vibrant: 1.25
1519
+ }[variant];
1509
1520
  lch.l = 50;
1521
+ lch.c *= chromaScale;
1522
+ if (variant === "deep") {
1523
+ lch.l *= 0.7;
1524
+ }
1510
1525
  const colorFormat = isHex(input) || isNamedColor(input) ? "hex" : extractColorParts(input).model;
1511
1526
  const currentLightness = lch.l;
1512
- const safeMaxLightness = currentLightness >= 88.5 ? 99.5 : MAX_LIGHTNESS;
1513
- const safeMinLightness = currentLightness <= 33 ? 0 : MIN_LIGHTNESS;
1514
- const lightBase = (safeMaxLightness - currentLightness) / 5;
1515
- const darkBase = -1 * (currentLightness - safeMinLightness) / 8;
1527
+ let lightSteps = 5;
1528
+ let darkSteps = 8;
1529
+ if (mode) {
1530
+ lightSteps *= mode === "light" ? 0.75 : 1.25;
1531
+ darkSteps *= mode === "light" ? 1.25 : 0.75;
1532
+ }
1533
+ const lightStepSize = (MAX_LIGHTNESS - currentLightness) / lightSteps;
1534
+ const darkStepSize = -1 * (currentLightness - MIN_LIGHTNESS) / darkSteps;
1535
+ const getChromaFactor = (value) => {
1536
+ return monochromatic ? 0 : value;
1537
+ };
1516
1538
  const output = scale === "linear" ? {
1517
- 50: shadeColor(lch, 95, -375e-5),
1518
- 100: shadeColor(lch, 90, -375e-5),
1519
- 200: shadeColor(lch, 80, -375e-5),
1520
- 300: shadeColor(lch, 70, -375e-5),
1521
- 400: shadeColor(lch, 60, -375e-5),
1539
+ 50: shadeColor(lch, 95, getChromaFactor(-375e-5)),
1540
+ 100: shadeColor(lch, 90, getChromaFactor(-375e-5)),
1541
+ 200: shadeColor(lch, 80, getChromaFactor(-375e-5)),
1542
+ 300: shadeColor(lch, 70, getChromaFactor(-375e-5)),
1543
+ 400: shadeColor(lch, 60, getChromaFactor(-375e-5)),
1522
1544
  500: shadeColor(lch, 50),
1523
- 600: shadeColor(lch, 40, 0.025),
1524
- 700: shadeColor(lch, 30, 0.05),
1525
- 800: shadeColor(lch, 20, 0.075),
1526
- 900: shadeColor(lch, 10, 0.1)
1545
+ 600: shadeColor(lch, 40, getChromaFactor(0.025)),
1546
+ 700: shadeColor(lch, 30, getChromaFactor(0.05)),
1547
+ 800: shadeColor(lch, 20, getChromaFactor(0.075)),
1548
+ 900: shadeColor(lch, 10, getChromaFactor(0.1))
1527
1549
  } : {
1528
- 50: shadeColorDynamic(lch, 5 * lightBase, -375e-5),
1529
- 100: shadeColorDynamic(lch, 4 * lightBase, -375e-5),
1530
- 200: shadeColorDynamic(lch, 3 * lightBase, -375e-5),
1531
- 300: shadeColorDynamic(lch, 2 * lightBase, -375e-5),
1532
- 400: shadeColorDynamic(lch, lightBase, -375e-5),
1550
+ 50: shadeColorDynamic(lch, 5 * lightStepSize, getChromaFactor(-375e-5)),
1551
+ 100: shadeColorDynamic(lch, 4 * lightStepSize, getChromaFactor(-375e-5)),
1552
+ 200: shadeColorDynamic(lch, 3 * lightStepSize, getChromaFactor(-375e-5)),
1553
+ 300: shadeColorDynamic(lch, 2 * lightStepSize, getChromaFactor(-375e-5)),
1554
+ 400: shadeColorDynamic(lch, lightStepSize, getChromaFactor(-375e-5)),
1533
1555
  500: shadeColorDynamic(lch, 0),
1534
- 600: shadeColorDynamic(lch, 1.6 * darkBase, 0.025),
1535
- 700: shadeColorDynamic(lch, 1.875 * 2 * darkBase, 0.05),
1536
- 800: shadeColorDynamic(lch, 3 * 2 * darkBase, 0.075),
1537
- 900: shadeColorDynamic(lch, 4 * 2 * darkBase, 0.1)
1556
+ 600: shadeColorDynamic(lch, 1.6 * darkStepSize, getChromaFactor(0.025)),
1557
+ 700: shadeColorDynamic(lch, 3.2 * darkStepSize, getChromaFactor(0.05)),
1558
+ 800: shadeColorDynamic(lch, 4.8 * darkStepSize, getChromaFactor(0.075)),
1559
+ 900: shadeColorDynamic(lch, 6.4 * darkStepSize, getChromaFactor(0.1))
1538
1560
  };
1539
1561
  return Object.entries(output).reduce((acc, [key, value]) => {
1540
1562
  return {
@@ -1545,7 +1567,7 @@ function swatch(input, options = {}) {
1545
1567
  }
1546
1568
 
1547
1569
  // src/index.ts
1548
- var src_default = Colorizr;
1570
+ var index_default = Colorizr;
1549
1571
  export {
1550
1572
  addAlphaToHex,
1551
1573
  brightnessDifference,
@@ -1556,7 +1578,7 @@ export {
1556
1578
  convert,
1557
1579
  convertAlphaToHex,
1558
1580
  darken,
1559
- src_default as default,
1581
+ index_default as default,
1560
1582
  desaturate,
1561
1583
  extractAlphaFromHex,
1562
1584
  extractColorParts,