react-smart-copy 1.17.0 → 1.17.1

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/rich.js CHANGED
@@ -473,6 +473,18 @@ const fromLinear = (c) => {
473
473
  const abs = Math.abs(c);
474
474
  return abs > .0031308 ? Math.sign(c) * (1.055 * abs ** (1 / 2.4) - .055) : 12.92 * c;
475
475
  };
476
+ /** Applies `f` to each component; the result stays a three-component vector. */
477
+ const map3 = ([x, y, z], f) => [
478
+ f(x, 0),
479
+ f(y, 1),
480
+ f(z, 2)
481
+ ];
482
+ /** Three numbers as a vector, or null when one is missing. */
483
+ const vec3 = ([x, y, z]) => typeof x === "number" && typeof y === "number" && typeof z === "number" ? [
484
+ x,
485
+ y,
486
+ z
487
+ ] : null;
476
488
  const linearRgb = (c) => [
477
489
  toLinear(c.r),
478
490
  toLinear(c.g),
@@ -654,12 +666,12 @@ const OKLAB_TO_LMS = [
654
666
  const toXyz = (c) => mul(SRGB_TO_XYZ, linearRgb(c));
655
667
  const fromXyz = (xyz, alpha) => fromLinearRgb(mul(XYZ_TO_SRGB, xyz), alpha);
656
668
  /** Display P3 channels (gamma-encoded, 0 to 1 inside P3). */
657
- const toP3 = (c) => mul(XYZ_TO_P3, toXyz(c)).map(fromLinear);
658
- const fromP3 = (p3, alpha) => fromXyz(mul(P3_TO_XYZ, p3.map(toLinear)), alpha);
669
+ const toP3 = (c) => map3(mul(XYZ_TO_P3, toXyz(c)), fromLinear);
670
+ const fromP3 = (p3, alpha) => fromXyz(mul(P3_TO_XYZ, map3(p3, toLinear)), alpha);
659
671
  const fromXyzD50 = (xyz, alpha) => fromXyz(mul(D50_TO_D65, xyz), alpha);
660
672
  /** OKLab: L in [0, 1], a and b around ±0.4. */
661
- const toOklab = (c) => mul(LMS_TO_OKLAB, mul(LRGB_TO_LMS, linearRgb(c)).map(Math.cbrt));
662
- const fromOklab = (lab, alpha) => fromLinearRgb(mul(LMS_TO_LRGB, mul(OKLAB_TO_LMS, lab).map((v) => v ** 3)), alpha);
673
+ const toOklab = (c) => mul(LMS_TO_OKLAB, map3(mul(LRGB_TO_LMS, linearRgb(c)), Math.cbrt));
674
+ const fromOklab = (lab, alpha) => fromLinearRgb(mul(LMS_TO_LRGB, map3(mul(OKLAB_TO_LMS, lab), (v) => v ** 3)), alpha);
663
675
  const D50_WHITE = [
664
676
  .3457 / .3585,
665
677
  1,
@@ -669,7 +681,7 @@ const KAPPA = 24389 / 27;
669
681
  const EPSILON = 216 / 24389;
670
682
  /** CIE Lab (D50): L in [0, 100], a and b around ±125. */
671
683
  const toLab = (c) => {
672
- const [x, y, z] = mul(D65_TO_D50, toXyz(c)).map((v, i) => v / D50_WHITE[i]);
684
+ const [x, y, z] = map3(mul(D65_TO_D50, toXyz(c)), (v, i) => v / D50_WHITE[i]);
673
685
  const f = (v) => v > EPSILON ? Math.cbrt(v) : (KAPPA * v + 16) / 116;
674
686
  const [fx, fy, fz] = [
675
687
  f(x),
@@ -691,7 +703,7 @@ const fromLab = ([l, a, b], alpha) => {
691
703
  l > KAPPA * EPSILON ? fy ** 3 : l / KAPPA,
692
704
  fz ** 3 > EPSILON ? fz ** 3 : (116 * fz - 16) / KAPPA
693
705
  ];
694
- return fromXyzD50(xyz.map((v, i) => v * D50_WHITE[i]), alpha);
706
+ return fromXyzD50(map3(xyz, (v, i) => v * D50_WHITE[i]), alpha);
695
707
  };
696
708
  /** Lab-like to its polar form: lightness, chroma, and hue in degrees from 0 to 360. */
697
709
  const toPolar = ([l, a, b]) => [
@@ -704,22 +716,37 @@ const fromPolar = ([l, c, h]) => [
704
716
  c * Math.cos(h * Math.PI / 180),
705
717
  c * Math.sin(h * Math.PI / 180)
706
718
  ];
707
- /** Hue, saturation and lightness of a colour inside sRGB (hue 0 for greys), with s and l in [0, 1]. */
719
+ const clip01 = (v) => Math.min(1, Math.max(0, v));
720
+ /**
721
+ * Below this spread between channels a colour is grey: what is left is rounding error from
722
+ * converting it (from Lab, OKLCH, P3...), and its hue and saturation would be noise. The
723
+ * faintest real tint in 8-bit sRGB, such as #fffffe, has a spread of about 0.004.
724
+ */
725
+ const GREY = 1e-9;
726
+ /**
727
+ * Hue, saturation and lightness of a colour inside sRGB (hue 0 for greys), with s and l in [0, 1].
728
+ * Channels are clipped first: colours count as inside sRGB within rounding error, a little past 0 or 1.
729
+ */
708
730
  function toHsl(c) {
709
- const max = Math.max(c.r, c.g, c.b);
710
- const min = Math.min(c.r, c.g, c.b);
731
+ const [r, g, b] = map3([
732
+ c.r,
733
+ c.g,
734
+ c.b
735
+ ], clip01);
736
+ const max = Math.max(r, g, b);
737
+ const min = Math.min(r, g, b);
711
738
  const l = (max + min) / 2;
712
739
  const d = max - min;
713
- if (d === 0) return [
740
+ if (d < GREY) return [
714
741
  0,
715
742
  0,
716
743
  l
717
744
  ];
718
745
  const s = (max - l) / Math.min(l, 1 - l);
719
746
  let h;
720
- if (max === c.r) h = (c.g - c.b) / d + (c.g < c.b ? 6 : 0);
721
- else if (max === c.g) h = (c.b - c.r) / d + 2;
722
- else h = (c.r - c.g) / d + 4;
747
+ if (max === r) h = (g - b) / d + (g < b ? 6 : 0);
748
+ else if (max === g) h = (b - r) / d + 2;
749
+ else h = (r - g) / d + 4;
723
750
  return [
724
751
  h * 60,
725
752
  s,
@@ -743,13 +770,18 @@ function fromHwb(h, w, b, alpha) {
743
770
  const scale = (v) => v * (1 - w - b) + w;
744
771
  return color(scale(pure.r), scale(pure.g), scale(pure.b), alpha);
745
772
  }
746
- /** Hue, whiteness and blackness of an sRGB colour, with w and b in [0, 1]. */
773
+ /** Hue, whiteness and blackness of an sRGB colour, with w and b in [0, 1] (channels clipped, as in `toHsl`). */
747
774
  function toHwb(c) {
748
775
  const [h] = toHsl(c);
776
+ const [r, g, b] = map3([
777
+ c.r,
778
+ c.g,
779
+ c.b
780
+ ], clip01);
749
781
  return [
750
782
  h,
751
- Math.min(c.r, c.g, c.b),
752
- 1 - Math.max(c.r, c.g, c.b)
783
+ Math.min(r, g, b),
784
+ 1 - Math.max(r, g, b)
753
785
  ];
754
786
  }
755
787
  /** Distance in OKLab: about 0.02 is the smallest difference people notice. */
@@ -763,9 +795,8 @@ const channels = (c, gamut) => gamut === "srgb" ? [
763
795
  c.g,
764
796
  c.b
765
797
  ] : toP3(c);
766
- const clip01 = (v) => Math.min(1, Math.max(0, v));
767
798
  const clipTo = (c, gamut) => {
768
- const [x, y, z] = channels(c, gamut).map(clip01);
799
+ const [x, y, z] = map3(channels(c, gamut), clip01);
769
800
  return gamut === "srgb" ? color(x, y, z, c.alpha) : fromP3([
770
801
  x,
771
802
  y,
@@ -942,7 +973,8 @@ function split(args) {
942
973
  function rgb(parts, legacy) {
943
974
  if (legacy && new Set(parts.map((part) => part.kind)).size !== 1) return null;
944
975
  const values = parts.map((part) => amount(part, 255, ["number", "percent"]));
945
- return values.some((v) => v === null) ? null : values.map((v) => clamp(v, 0, 255) / 255);
976
+ const vector = vec3(values);
977
+ return vector === null ? null : map3(vector, (v) => clamp(v, 0, 255) / 255);
946
978
  }
947
979
  const PREDEFINED = {
948
980
  srgb: ([r, g, b], alpha) => color(r, g, b, alpha),
@@ -961,7 +993,8 @@ function predefined(args) {
961
993
  if (convert === void 0 || parsed === null || parsed.legacy) return null;
962
994
  const values = parsed.parts.map((part) => amount(part, 1, ["number", "percent"]));
963
995
  const alpha = alphaOf(parsed.alpha);
964
- return values.some((v) => v === null) || alpha === null ? null : convert(values, alpha);
996
+ const vector = vec3(values);
997
+ return vector === null || alpha === null ? null : convert(vector, alpha);
965
998
  }
966
999
  function functional(name, args) {
967
1000
  if (name === "color") return predefined(args);