@banyan_cloud/roots 1.0.345 → 1.0.346

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/cjs/index.js CHANGED
@@ -4996,11 +4996,15 @@ var generateColors = function generateColors(_ref4) {
4996
4996
  excludedColors = _ref4$excludedColors === void 0 ? [] : _ref4$excludedColors,
4997
4997
  _ref4$exclusionThresh = _ref4.exclusionThreshold,
4998
4998
  exclusionThreshold = _ref4$exclusionThresh === void 0 ? 30 : _ref4$exclusionThresh,
4999
- _ref4$minPerceptualDi = _ref4.minPerceptualDistance,
5000
- minPerceptualDistance = _ref4$minPerceptualDi === void 0 ? 25 : _ref4$minPerceptualDi;
4999
+ _ref4$distinctionThre = _ref4.distinctionThreshold,
5000
+ distinctionThreshold = _ref4$distinctionThre === void 0 ? 40 : _ref4$distinctionThre,
5001
+ _ref4$excludedHueRang = _ref4.excludedHueRanges,
5002
+ excludedHueRanges = _ref4$excludedHueRang === void 0 ? [] : _ref4$excludedHueRang;
5001
5003
  var colors = [];
5002
- var minLightness = 25;
5003
- var maxLightness = 75;
5004
+ var minLightness = 30;
5005
+ var maxLightness = 70;
5006
+ var minSaturation = 65;
5007
+ var maxSaturation = 100;
5004
5008
  var normalizedExcludedColors = excludedColors.map(function (color) {
5005
5009
  return color.startsWith('#') ? color : "#".concat(color);
5006
5010
  });
@@ -5010,16 +5014,49 @@ var generateColors = function generateColors(_ref4) {
5010
5014
  var b = parseInt(hex.slice(5, 7), 16);
5011
5015
  return [r, g, b];
5012
5016
  };
5017
+ var hexToHSL = function hexToHSL(hex) {
5018
+ var r = parseInt(hex.slice(1, 3), 16) / 255;
5019
+ var g = parseInt(hex.slice(3, 5), 16) / 255;
5020
+ var b = parseInt(hex.slice(5, 7), 16) / 255;
5021
+ var cmin = Math.min(r, g, b);
5022
+ var cmax = Math.max(r, g, b);
5023
+ var delta = cmax - cmin;
5024
+ var h = 0;
5025
+ var s = 0;
5026
+ var l = 0;
5027
+ if (delta === 0) {
5028
+ h = 0;
5029
+ } else if (cmax === r) {
5030
+ h = (g - b) / delta % 6;
5031
+ } else if (cmax === g) {
5032
+ h = (b - r) / delta + 2;
5033
+ } else {
5034
+ h = (r - g) / delta + 4;
5035
+ }
5036
+ h = Math.round(h * 60);
5037
+ if (h < 0) h += 360;
5038
+ l = (cmax + cmin) / 2;
5039
+ s = delta === 0 ? 0 : delta / (1 - Math.abs(2 * l - 1));
5040
+ s = Math.round(s * 100);
5041
+ l = Math.round(l * 100);
5042
+ return [h, s, l];
5043
+ };
5013
5044
  var colorDistance = function colorDistance(hex1, hex2) {
5014
5045
  var rgb1 = hexToRGB(hex1);
5015
5046
  var rgb2 = hexToRGB(hex2);
5016
- var rDiff = rgb1[0] - rgb2[0];
5017
- var gDiff = rgb1[1] - rgb2[1];
5018
- var bDiff = rgb1[2] - rgb2[2];
5019
- return Math.sqrt(rDiff * rDiff * 0.299 + gDiff * gDiff * 0.587 + bDiff * bDiff * 0.114);
5047
+ var rMean = (rgb1[0] + rgb2[0]) / 2;
5048
+ var r = rgb1[0] - rgb2[0];
5049
+ var g = rgb1[1] - rgb2[1];
5050
+ var b = rgb1[2] - rgb2[2];
5051
+ var weightR = 2 + rMean / 256;
5052
+ var weightG = 4.0;
5053
+ var weightB = 2 + (255 - rMean) / 256;
5054
+ return Math.sqrt(weightR * r * r + weightG * g * g + weightB * b * b);
5020
5055
  };
5021
5056
  function hslToHex(h, s, l) {
5022
- // eslint-disable-next-line no-param-reassign
5057
+ h = (h % 360 + 360) % 360;
5058
+ s = Math.max(0, Math.min(100, s));
5059
+ l = Math.max(0, Math.min(100, l));
5023
5060
  l /= 100;
5024
5061
  var a = s * Math.min(l, 1 - l) / 100;
5025
5062
  var f = function f(n) {
@@ -5029,7 +5066,21 @@ var generateColors = function generateColors(_ref4) {
5029
5066
  };
5030
5067
  return "#".concat(f(0)).concat(f(8)).concat(f(4));
5031
5068
  }
5069
+ var isHueExcluded = function isHueExcluded(hue) {
5070
+ return excludedHueRanges.some(function (range) {
5071
+ if (range.min > range.max) {
5072
+ return hue >= range.min || hue <= range.max;
5073
+ }
5074
+ return hue >= range.min && hue <= range.max;
5075
+ });
5076
+ };
5032
5077
  var isTooSimilarToExcluded = function isTooSimilarToExcluded(hexColor) {
5078
+ var _hexToHSL = hexToHSL(hexColor),
5079
+ _hexToHSL2 = _slicedToArray(_hexToHSL, 1),
5080
+ hue = _hexToHSL2[0];
5081
+ if (isHueExcluded(hue)) {
5082
+ return true;
5083
+ }
5033
5084
  var _iterator = _createForOfIteratorHelper(normalizedExcludedColors),
5034
5085
  _step;
5035
5086
  try {
@@ -5048,21 +5099,24 @@ var generateColors = function generateColors(_ref4) {
5048
5099
  };
5049
5100
  var generateDistinctColor = function generateDistinctColor() {
5050
5101
  var attempts = 0;
5051
- var maxAttempts = 150;
5102
+ var maxAttempts = 300;
5052
5103
  while (attempts < maxAttempts) {
5053
- var _h = Math.floor(Math.random() * 360);
5054
- var _s = Math.floor(Math.random() * 40) + 60;
5055
- var _l = Math.floor(Math.random() * (maxLightness - minLightness)) + minLightness;
5056
- var hexColor = hslToHex(_h, _s, _l);
5104
+ var h = Math.floor(Math.random() * 360);
5105
+ var s = Math.floor(Math.random() * (maxSaturation - minSaturation)) + minSaturation;
5106
+ var l = Math.floor(Math.random() * (maxLightness - minLightness)) + minLightness;
5107
+ if (isHueExcluded(h)) {
5108
+ attempts++;
5109
+ continue;
5110
+ }
5111
+ var hexColor = hslToHex(h, s, l);
5057
5112
  if (isTooSimilarToExcluded(hexColor)) {
5058
5113
  attempts++;
5059
- // eslint-disable-next-line no-continue
5060
5114
  continue;
5061
5115
  }
5062
5116
  var isDistinct = true;
5063
5117
  for (var _i2 = 0, _colors = colors; _i2 < _colors.length; _i2++) {
5064
5118
  var existingColor = _colors[_i2];
5065
- if (colorDistance(hexColor, existingColor) < minPerceptualDistance) {
5119
+ if (colorDistance(hexColor, existingColor) < distinctionThreshold) {
5066
5120
  isDistinct = false;
5067
5121
  break;
5068
5122
  }
@@ -5072,13 +5126,36 @@ var generateColors = function generateColors(_ref4) {
5072
5126
  }
5073
5127
  attempts++;
5074
5128
  }
5075
- var h = Math.floor(Math.random() * 360);
5076
- var s = Math.floor(Math.random() * 40) + 60;
5077
- var l = Math.floor(Math.random() * (maxLightness - minLightness)) + minLightness;
5078
- return hslToHex(h, s, l);
5129
+ for (var adjustedAttempts = 0; adjustedAttempts < 50; adjustedAttempts++) {
5130
+ var _h = Math.floor(Math.random() * 360);
5131
+ if (isHueExcluded(_h)) {
5132
+ continue;
5133
+ }
5134
+ var _s = 90;
5135
+ var _l = 50;
5136
+ var _hexColor = hslToHex(_h, _s, _l);
5137
+ if (!isTooSimilarToExcluded(_hexColor)) {
5138
+ var mostDistinct = true;
5139
+ for (var _i3 = 0, _colors2 = colors; _i3 < _colors2.length; _i3++) {
5140
+ var _existingColor = _colors2[_i3];
5141
+ var distance = colorDistance(_hexColor, _existingColor);
5142
+ if (distance < distinctionThreshold * 0.7) {
5143
+ mostDistinct = false;
5144
+ }
5145
+ }
5146
+ if (mostDistinct || adjustedAttempts >= 49) {
5147
+ return _hexColor;
5148
+ }
5149
+ }
5150
+ }
5151
+ return hslToHex(Math.floor(Math.random() * 360), 80, 50);
5079
5152
  };
5080
- for (var i = 0; i < count; i++) {
5081
- colors.push(generateDistinctColor());
5153
+ var failsafeCounter = 0;
5154
+ var maxFailsafe = count * 3;
5155
+ while (colors.length < count && failsafeCounter < maxFailsafe) {
5156
+ var newColor = generateDistinctColor();
5157
+ colors.push(newColor);
5158
+ failsafeCounter++;
5082
5159
  }
5083
5160
  return colors;
5084
5161
  };