7932e583485fbd4a5dab6f791b7110165a04e63885ec64fe5efb67320050ed7e9c11b1862677cd853365d4e842923cb9cb07573820bd069bc5deaa51e912b9 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.pad2 = exports.convertToPercentage = exports.boundAlpha = exports.isPercentage = exports.isOnePointZero = exports.clamp01 = exports.bound01 = void 0;
  4. /**
  5. * Take input from [0, n] and return it as [0, 1]
  6. * @hidden
  7. */
  8. function bound01(n, max) {
  9. if (isOnePointZero(n)) {
  10. n = '100%';
  11. }
  12. var isPercent = isPercentage(n);
  13. n = max === 360 ? n : Math.min(max, Math.max(0, parseFloat(n)));
  14. // Automatically convert percentage into number
  15. if (isPercent) {
  16. n = parseInt(String(n * max), 10) / 100;
  17. }
  18. // Handle floating point rounding errors
  19. if (Math.abs(n - max) < 0.000001) {
  20. return 1;
  21. }
  22. // Convert into [0, 1] range if it isn't already
  23. if (max === 360) {
  24. // If n is a hue given in degrees,
  25. // wrap around out-of-range values into [0, 360] range
  26. // then convert into [0, 1].
  27. n = (n < 0 ? (n % max) + max : n % max) / parseFloat(String(max));
  28. }
  29. else {
  30. // If n not a hue given in degrees
  31. // Convert into [0, 1] range if it isn't already.
  32. n = (n % max) / parseFloat(String(max));
  33. }
  34. return n;
  35. }
  36. exports.bound01 = bound01;
  37. /**
  38. * Force a number between 0 and 1
  39. * @hidden
  40. */
  41. function clamp01(val) {
  42. return Math.min(1, Math.max(0, val));
  43. }
  44. exports.clamp01 = clamp01;
  45. /**
  46. * Need to handle 1.0 as 100%, since once it is a number, there is no difference between it and 1
  47. * <http://stackoverflow.com/questions/7422072/javascript-how-to-detect-number-as-a-decimal-including-1-0>
  48. * @hidden
  49. */
  50. function isOnePointZero(n) {
  51. return typeof n === 'string' && n.indexOf('.') !== -1 && parseFloat(n) === 1;
  52. }
  53. exports.isOnePointZero = isOnePointZero;
  54. /**
  55. * Check to see if string passed in is a percentage
  56. * @hidden
  57. */
  58. function isPercentage(n) {
  59. return typeof n === 'string' && n.indexOf('%') !== -1;
  60. }
  61. exports.isPercentage = isPercentage;
  62. /**
  63. * Return a valid alpha value [0,1] with all invalid values being set to 1
  64. * @hidden
  65. */
  66. function boundAlpha(a) {
  67. a = parseFloat(a);
  68. if (isNaN(a) || a < 0 || a > 1) {
  69. a = 1;
  70. }
  71. return a;
  72. }
  73. exports.boundAlpha = boundAlpha;
  74. /**
  75. * Replace a decimal with it's percentage value
  76. * @hidden
  77. */
  78. function convertToPercentage(n) {
  79. if (n <= 1) {
  80. return "".concat(Number(n) * 100, "%");
  81. }
  82. return n;
  83. }
  84. exports.convertToPercentage = convertToPercentage;
  85. /**
  86. * Force a hex value to have 2 characters
  87. * @hidden
  88. */
  89. function pad2(c) {
  90. return c.length === 1 ? '0' + c : String(c);
  91. }
  92. exports.pad2 = pad2;