@react-stately/color 3.0.0-beta.4 → 3.0.0-beta.5

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/module.js CHANGED
@@ -1,11 +1,10 @@
1
1
  import { useRef, useState, useMemo, useEffect } from "react";
2
- import { useControlledState } from "@react-stately/utils";
3
2
  import { useSliderState } from "@react-stately/slider";
4
3
  import _babelRuntimeHelpersEsmObjectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
5
4
  import _babelRuntimeHelpersEsmExtends from "@babel/runtime/helpers/esm/extends";
6
5
  import { NumberFormatter } from "@internationalized/number";
7
6
  import { MessageDictionary } from "@internationalized/message";
8
- import { clamp } from "@react-aria/utils";
7
+ import { clamp, toFixedNumber, useControlledState } from "@react-stately/utils";
9
8
  import _babelRuntimeHelpersEsmInteropRequireDefault from "@babel/runtime/helpers/esm/interopRequireDefault";
10
9
  // ASSET: /Users/govett/dev/react-spectrum-v3/packages/@react-stately/color/intl/ar-AE.json
11
10
  var $b2cd9264f0b716f5f6077988ebeb8d5$exports = {};
@@ -257,6 +256,14 @@ class $db5f3c2f048bca5be854d051d3f2729$var$RGBColor extends $db5f3c2f048bca5be85
257
256
  case 'rgba':
258
257
  return this;
259
258
 
259
+ case 'hsb':
260
+ case 'hsba':
261
+ return this.toHSB();
262
+
263
+ case 'hsl':
264
+ case 'hsla':
265
+ return this.toHSL();
266
+
260
267
  default:
261
268
  throw new Error('Unsupported color conversion: rgb -> ' + format);
262
269
  }
@@ -265,6 +272,85 @@ class $db5f3c2f048bca5be854d051d3f2729$var$RGBColor extends $db5f3c2f048bca5be85
265
272
  toHexInt() {
266
273
  return this.red << 16 | this.green << 8 | this.blue;
267
274
  }
275
+ /**
276
+ * Converts an RGB color value to HSB.
277
+ * Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB.
278
+ * @returns An HSBColor object.
279
+ */
280
+
281
+
282
+ toHSB() {
283
+ const red = this.red / 255;
284
+ const green = this.green / 255;
285
+ const blue = this.blue / 255;
286
+ const min = Math.min(red, green, blue);
287
+ const brightness = Math.max(red, green, blue);
288
+ const chroma = brightness - min;
289
+ const saturation = brightness === 0 ? 0 : chroma / brightness;
290
+ let hue = 0; // achromatic
291
+
292
+ if (chroma !== 0) {
293
+ switch (brightness) {
294
+ case red:
295
+ hue = (green - blue) / chroma + (green < blue ? 6 : 0);
296
+ break;
297
+
298
+ case green:
299
+ hue = (blue - red) / chroma + 2;
300
+ break;
301
+
302
+ case blue:
303
+ hue = (red - green) / chroma + 4;
304
+ break;
305
+ }
306
+
307
+ hue /= 6;
308
+ }
309
+
310
+ return new $db5f3c2f048bca5be854d051d3f2729$var$HSBColor(toFixedNumber(hue * 360, 2), toFixedNumber(saturation * 100, 2), toFixedNumber(brightness * 100, 2), this.alpha);
311
+ }
312
+ /**
313
+ * Converts an RGB color value to HSL.
314
+ * Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB.
315
+ * @returns An HSLColor object.
316
+ */
317
+
318
+
319
+ toHSL() {
320
+ const red = this.red / 255;
321
+ const green = this.green / 255;
322
+ const blue = this.blue / 255;
323
+ const min = Math.min(red, green, blue);
324
+ const max = Math.max(red, green, blue);
325
+ const lightness = (max + min) / 2;
326
+ const chroma = max - min;
327
+ let hue;
328
+ let saturation;
329
+
330
+ if (chroma === 0) {
331
+ hue = saturation = 0; // achromatic
332
+ } else {
333
+ saturation = chroma / (lightness < .5 ? max + min : 2 - max - min);
334
+
335
+ switch (max) {
336
+ case red:
337
+ hue = (green - blue) / chroma + (green < blue ? 6 : 0);
338
+ break;
339
+
340
+ case green:
341
+ hue = (blue - red) / chroma + 2;
342
+ break;
343
+
344
+ case blue:
345
+ hue = (red - green) / chroma + 4;
346
+ break;
347
+ }
348
+
349
+ hue /= 6;
350
+ }
351
+
352
+ return new $db5f3c2f048bca5be854d051d3f2729$var$HSLColor(toFixedNumber(hue * 360, 2), toFixedNumber(saturation * 100, 2), toFixedNumber(lightness * 100, 2), this.alpha);
353
+ }
268
354
 
269
355
  clone() {
270
356
  return new $db5f3c2f048bca5be854d051d3f2729$var$RGBColor(this.red, this.green, this.blue, this.alpha);
@@ -319,6 +405,10 @@ class $db5f3c2f048bca5be854d051d3f2729$var$RGBColor extends $db5f3c2f048bca5be85
319
405
  return new NumberFormatter(locale, options).format(value);
320
406
  }
321
407
 
408
+ getColorSpace() {
409
+ return 'rgb';
410
+ }
411
+
322
412
  } // X = <negative/positive number with/without decimal places>
323
413
  // before/after a comma, 0 or more whitespaces are allowed
324
414
  // - hsb(X, X%, X%)
@@ -352,11 +442,17 @@ class $db5f3c2f048bca5be854d051d3f2729$var$HSBColor extends $db5f3c2f048bca5be85
352
442
  case 'css':
353
443
  return this.toHSL().toString('css');
354
444
 
445
+ case 'hex':
446
+ return this.toRGB().toString('hex');
447
+
448
+ case 'hexa':
449
+ return this.toRGB().toString('hexa');
450
+
355
451
  case 'hsb':
356
- return "hsb(" + this.hue + ", " + this.saturation + "%, " + this.brightness + "%)";
452
+ return "hsb(" + this.hue + ", " + toFixedNumber(this.saturation, 2) + "%, " + toFixedNumber(this.brightness, 2) + "%)";
357
453
 
358
454
  case 'hsba':
359
- return "hsba(" + this.hue + ", " + this.saturation + "%, " + this.brightness + "%, " + this.alpha + ")";
455
+ return "hsba(" + this.hue + ", " + toFixedNumber(this.saturation, 2) + "%, " + toFixedNumber(this.brightness, 2) + "%, " + this.alpha + ")";
360
456
 
361
457
  default:
362
458
  return this.toFormat(format).toString(format);
@@ -373,24 +469,49 @@ class $db5f3c2f048bca5be854d051d3f2729$var$HSBColor extends $db5f3c2f048bca5be85
373
469
  case 'hsla':
374
470
  return this.toHSL();
375
471
 
472
+ case 'rgb':
473
+ case 'rgba':
474
+ return this.toRGB();
475
+
376
476
  default:
377
477
  throw new Error('Unsupported color conversion: hsb -> ' + format);
378
478
  }
379
479
  }
480
+ /**
481
+ * Converts a HSB color to HSL.
482
+ * Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_HSL.
483
+ * @returns An HSLColor object.
484
+ */
485
+
380
486
 
381
487
  toHSL() {
382
- // determine the lightness in the range [0,100]
383
- var l = (2 - this.saturation / 100) * this.brightness / 2; // store the HSL components
488
+ let saturation = this.saturation / 100;
489
+ let brightness = this.brightness / 100;
490
+ let lightness = brightness * (1 - saturation / 2);
491
+ saturation = lightness === 0 || lightness === 1 ? 0 : (brightness - lightness) / Math.min(lightness, 1 - lightness);
492
+ return new $db5f3c2f048bca5be854d051d3f2729$var$HSLColor(toFixedNumber(this.hue, 2), toFixedNumber(saturation * 100, 2), toFixedNumber(lightness * 100, 2), this.alpha);
493
+ }
494
+ /**
495
+ * Converts a HSV color value to RGB.
496
+ * Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_RGB_alternative.
497
+ * @returns An RGBColor object.
498
+ */
499
+
384
500
 
501
+ toRGB() {
385
502
  let hue = this.hue;
386
- let saturation = this.saturation * this.brightness / (l < 50 ? l * 2 : 200 - l * 2);
387
- let lightness = l; // correct a division-by-zero error
503
+ let saturation = this.saturation / 100;
504
+ let brightness = this.brightness / 100;
388
505
 
389
- if (isNaN(saturation)) {
390
- saturation = 0;
391
- }
506
+ let fn = function fn(n, k) {
507
+ if (k === void 0) {
508
+ k = (n + hue / 60) % 6;
509
+ }
392
510
 
393
- return new $db5f3c2f048bca5be854d051d3f2729$var$HSLColor(hue, saturation, lightness, this.alpha);
511
+ return brightness - saturation * brightness * Math.max(Math.min(k, 4 - k, 1), 0);
512
+ };
513
+
514
+ return new $db5f3c2f048bca5be854d051d3f2729$var$RGBColor(Math.round(fn(5) * 255), Math.round(fn(3) * 255), Math.round(fn(1) * 255), this.alpha);
394
515
  }
395
516
 
396
517
  clone() {
@@ -460,6 +581,10 @@ class $db5f3c2f048bca5be854d051d3f2729$var$HSBColor extends $db5f3c2f048bca5be85
460
581
  return new NumberFormatter(locale, options).format(value);
461
582
  }
462
583
 
584
+ getColorSpace() {
585
+ return 'hsb';
586
+ }
587
+
463
588
  } // X = <negative/positive number with/without decimal places>
464
589
  // before/after a comma, 0 or more whitespaces are allowed
465
590
  // - hsl(X, X%, X%)
@@ -470,8 +595,7 @@ const $db5f3c2f048bca5be854d051d3f2729$var$HSL_REGEX = /hsl\(([-+]?\d+(?:.\d+)?\
470
595
 
471
596
  function $db5f3c2f048bca5be854d051d3f2729$var$mod(n, m) {
472
597
  return (n % m + m) % m;
473
- } // eslint-disable-next-line @typescript-eslint/no-unused-vars
474
-
598
+ }
475
599
 
476
600
  class $db5f3c2f048bca5be854d051d3f2729$var$HSLColor extends $db5f3c2f048bca5be854d051d3f2729$var$Color {
477
601
  constructor(hue, saturation, lightness, alpha) {
@@ -480,8 +604,7 @@ class $db5f3c2f048bca5be854d051d3f2729$var$HSLColor extends $db5f3c2f048bca5be85
480
604
  this.saturation = saturation;
481
605
  this.lightness = lightness;
482
606
  this.alpha = alpha;
483
- } // eslint-disable-next-line @typescript-eslint/no-unused-vars
484
-
607
+ }
485
608
 
486
609
  static parse(value) {
487
610
  let m;
@@ -496,12 +619,18 @@ class $db5f3c2f048bca5be854d051d3f2729$var$HSLColor extends $db5f3c2f048bca5be85
496
619
 
497
620
  toString(format) {
498
621
  switch (format) {
622
+ case 'hex':
623
+ return this.toRGB().toString('hex');
624
+
625
+ case 'hexa':
626
+ return this.toRGB().toString('hexa');
627
+
499
628
  case 'hsl':
500
- return "hsl(" + this.hue + ", " + this.saturation + "%, " + this.lightness + "%)";
629
+ return "hsl(" + this.hue + ", " + toFixedNumber(this.saturation, 2) + "%, " + toFixedNumber(this.lightness, 2) + "%)";
501
630
 
502
631
  case 'css':
503
632
  case 'hsla':
504
- return "hsla(" + this.hue + ", " + this.saturation + "%, " + this.lightness + "%, " + this.alpha + ")";
633
+ return "hsla(" + this.hue + ", " + toFixedNumber(this.saturation, 2) + "%, " + toFixedNumber(this.lightness, 2) + "%, " + this.alpha + ")";
505
634
 
506
635
  default:
507
636
  return this.toFormat(format).toString(format);
@@ -514,10 +643,55 @@ class $db5f3c2f048bca5be854d051d3f2729$var$HSLColor extends $db5f3c2f048bca5be85
514
643
  case 'hsla':
515
644
  return this;
516
645
 
646
+ case 'hsb':
647
+ case 'hsba':
648
+ return this.toHSB();
649
+
650
+ case 'rgb':
651
+ case 'rgba':
652
+ return this.toRGB();
653
+
517
654
  default:
518
655
  throw new Error('Unsupported color conversion: hsl -> ' + format);
519
656
  }
520
657
  }
658
+ /**
659
+ * Converts a HSL color to HSB.
660
+ * Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_HSV.
661
+ * @returns An HSBColor object.
662
+ */
663
+
664
+
665
+ toHSB() {
666
+ let saturation = this.saturation / 100;
667
+ let lightness = this.lightness / 100;
668
+ let brightness = lightness + saturation * Math.min(lightness, 1 - lightness);
669
+ saturation = brightness === 0 ? 0 : 2 * (1 - lightness / brightness);
670
+ return new $db5f3c2f048bca5be854d051d3f2729$var$HSBColor(toFixedNumber(this.hue, 2), toFixedNumber(saturation * 100, 2), toFixedNumber(brightness * 100, 2), this.alpha);
671
+ }
672
+ /**
673
+ * Converts a HSL color to RGB.
674
+ * Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_RGB_alternative.
675
+ * @returns An RGBColor object.
676
+ */
677
+
678
+
679
+ toRGB() {
680
+ let hue = this.hue;
681
+ let saturation = this.saturation / 100;
682
+ let lightness = this.lightness / 100;
683
+ let a = saturation * Math.min(lightness, 1 - lightness);
684
+
685
+ let fn = function fn(n, k) {
686
+ if (k === void 0) {
687
+ k = (n + hue / 30) % 12;
688
+ }
689
+
690
+ return lightness - a * Math.max(Math.min(k - 3, 9 - k, 1), -1);
691
+ };
692
+
693
+ return new $db5f3c2f048bca5be854d051d3f2729$var$RGBColor(Math.round(fn(0) * 255), Math.round(fn(8) * 255), Math.round(fn(4) * 255), this.alpha);
694
+ }
521
695
 
522
696
  clone() {
523
697
  return new $db5f3c2f048bca5be854d051d3f2729$var$HSLColor(this.hue, this.saturation, this.lightness, this.alpha);
@@ -586,6 +760,10 @@ class $db5f3c2f048bca5be854d051d3f2729$var$HSLColor extends $db5f3c2f048bca5be85
586
760
  return new NumberFormatter(locale, options).format(value);
587
761
  }
588
762
 
763
+ getColorSpace() {
764
+ return 'hsl';
765
+ }
766
+
589
767
  }
590
768
 
591
769
  function $dfa319d4be3239bcd232b84e0e071$var$normalizeColor(v) {
@@ -746,6 +924,7 @@ export function useColorWheelState(props) {
746
924
  let valueRef = useRef(value);
747
925
  valueRef.current = value;
748
926
  let [isDragging, setDragging] = useState(false);
927
+ let isDraggingRef = useRef(false).current;
749
928
  let hue = value.getChannelValue('hue');
750
929
 
751
930
  function setHue(v) {
@@ -757,7 +936,9 @@ export function useColorWheelState(props) {
757
936
  v = $a54bad0097d5f20f12ac194ac13764$var$roundToStep($a54bad0097d5f20f12ac194ac13764$var$mod(v, 360), step);
758
937
 
759
938
  if (hue !== v) {
760
- setValue(value.withChannelValue('hue', v));
939
+ let color = value.withChannelValue('hue', v);
940
+ valueRef.current = color;
941
+ setValue(color);
761
942
  }
762
943
  }
763
944
 
@@ -813,13 +994,14 @@ export function useColorWheelState(props) {
813
994
  },
814
995
 
815
996
  setDragging(isDragging) {
816
- setDragging(wasDragging => {
817
- if (onChangeEnd && !isDragging && wasDragging) {
818
- onChangeEnd(valueRef.current);
819
- }
997
+ let wasDragging = isDraggingRef;
998
+ isDraggingRef = isDragging;
820
999
 
821
- return isDragging;
822
- });
1000
+ if (onChangeEnd && !isDragging && wasDragging) {
1001
+ onChangeEnd(valueRef.current);
1002
+ }
1003
+
1004
+ setDragging(isDragging);
823
1005
  },
824
1006
 
825
1007
  isDragging,