@react-stately/color 3.0.0-beta.6 → 3.0.0-beta.9
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/main.js +430 -271
- package/dist/main.js.map +1 -1
- package/dist/module.js +423 -266
- package/dist/module.js.map +1 -1
- package/dist/types.d.ts +55 -3
- package/dist/types.d.ts.map +1 -1
- package/package.json +9 -8
- package/src/Color.ts +41 -9
- package/src/index.ts +2 -1
- package/src/useColorAreaState.ts +184 -0
- package/src/useColorFieldState.ts +7 -4
- package/src/useColorSliderState.ts +6 -11
- package/src/useColorWheelState.ts +21 -22
package/src/Color.ts
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
13
|
import {clamp, toFixedNumber} from '@react-stately/utils';
|
|
14
|
-
import {ColorChannel, ColorChannelRange, ColorFormat, Color as IColor} from '@react-types/color';
|
|
14
|
+
import {ColorAxes, ColorChannel, ColorChannelRange, ColorFormat, Color as IColor} from '@react-types/color';
|
|
15
15
|
// @ts-ignore
|
|
16
16
|
import intlMessages from '../intl/*.json';
|
|
17
17
|
import {MessageDictionary} from '@internationalized/message';
|
|
@@ -29,6 +29,14 @@ export function parseColor(value: string): IColor {
|
|
|
29
29
|
throw new Error('Invalid color value: ' + value);
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
+
export function normalizeColor(v: string | IColor) {
|
|
33
|
+
if (typeof v === 'string') {
|
|
34
|
+
return parseColor(v);
|
|
35
|
+
} else {
|
|
36
|
+
return v;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
32
40
|
abstract class Color implements IColor {
|
|
33
41
|
abstract toFormat(format: ColorFormat): IColor;
|
|
34
42
|
abstract toString(format: ColorFormat | 'css'): string;
|
|
@@ -63,6 +71,15 @@ abstract class Color implements IColor {
|
|
|
63
71
|
}
|
|
64
72
|
|
|
65
73
|
abstract getColorSpace(): ColorFormat
|
|
74
|
+
getColorSpaceAxes(xyChannels: {xChannel?: ColorChannel, yChannel?: ColorChannel}): ColorAxes {
|
|
75
|
+
let {xChannel, yChannel} = xyChannels;
|
|
76
|
+
let xCh = xChannel || this.getColorChannels().find(c => c !== yChannel);
|
|
77
|
+
let yCh = yChannel || this.getColorChannels().find(c => c !== xCh);
|
|
78
|
+
let zCh = this.getColorChannels().find(c => c !== xCh && c !== yCh);
|
|
79
|
+
|
|
80
|
+
return {xChannel: xCh, yChannel: yCh, zChannel: zCh};
|
|
81
|
+
}
|
|
82
|
+
abstract getColorChannels(): [ColorChannel, ColorChannel, ColorChannel]
|
|
66
83
|
}
|
|
67
84
|
|
|
68
85
|
const HEX_REGEX = /^#(?:([0-9a-f]{3})|([0-9a-f]{6}))$/i;
|
|
@@ -229,9 +246,9 @@ class RGBColor extends Color {
|
|
|
229
246
|
case 'red':
|
|
230
247
|
case 'green':
|
|
231
248
|
case 'blue':
|
|
232
|
-
return {minValue:
|
|
249
|
+
return {minValue: 0x0, maxValue: 0xFF, step: 0x1, pageSize: 0x11};
|
|
233
250
|
case 'alpha':
|
|
234
|
-
return {minValue: 0, maxValue: 1, step: 0.01};
|
|
251
|
+
return {minValue: 0, maxValue: 1, step: 0.01, pageSize: 0.1};
|
|
235
252
|
default:
|
|
236
253
|
throw new Error('Unknown color channel: ' + channel);
|
|
237
254
|
}
|
|
@@ -258,6 +275,11 @@ class RGBColor extends Color {
|
|
|
258
275
|
getColorSpace(): ColorFormat {
|
|
259
276
|
return 'rgb';
|
|
260
277
|
}
|
|
278
|
+
|
|
279
|
+
private static colorChannels: [ColorChannel, ColorChannel, ColorChannel] = ['red', 'green', 'blue'];
|
|
280
|
+
getColorChannels(): [ColorChannel, ColorChannel, ColorChannel] {
|
|
281
|
+
return RGBColor.colorChannels;
|
|
282
|
+
}
|
|
261
283
|
}
|
|
262
284
|
|
|
263
285
|
// X = <negative/positive number with/without decimal places>
|
|
@@ -356,12 +378,12 @@ class HSBColor extends Color {
|
|
|
356
378
|
getChannelRange(channel: ColorChannel): ColorChannelRange {
|
|
357
379
|
switch (channel) {
|
|
358
380
|
case 'hue':
|
|
359
|
-
return {minValue: 0, maxValue: 360, step: 1};
|
|
381
|
+
return {minValue: 0, maxValue: 360, step: 1, pageSize: 15};
|
|
360
382
|
case 'saturation':
|
|
361
383
|
case 'brightness':
|
|
362
|
-
return {minValue: 0, maxValue: 100, step: 1};
|
|
384
|
+
return {minValue: 0, maxValue: 100, step: 1, pageSize: 10};
|
|
363
385
|
case 'alpha':
|
|
364
|
-
return {minValue: 0, maxValue: 1, step: 0.01};
|
|
386
|
+
return {minValue: 0, maxValue: 1, step: 0.01, pageSize: 0.1};
|
|
365
387
|
default:
|
|
366
388
|
throw new Error('Unknown color channel: ' + channel);
|
|
367
389
|
}
|
|
@@ -391,6 +413,11 @@ class HSBColor extends Color {
|
|
|
391
413
|
getColorSpace(): ColorFormat {
|
|
392
414
|
return 'hsb';
|
|
393
415
|
}
|
|
416
|
+
|
|
417
|
+
private static colorChannels: [ColorChannel, ColorChannel, ColorChannel] = ['hue', 'saturation', 'brightness'];
|
|
418
|
+
getColorChannels(): [ColorChannel, ColorChannel, ColorChannel] {
|
|
419
|
+
return HSBColor.colorChannels;
|
|
420
|
+
}
|
|
394
421
|
}
|
|
395
422
|
|
|
396
423
|
// X = <negative/positive number with/without decimal places>
|
|
@@ -491,12 +518,12 @@ class HSLColor extends Color {
|
|
|
491
518
|
getChannelRange(channel: ColorChannel): ColorChannelRange {
|
|
492
519
|
switch (channel) {
|
|
493
520
|
case 'hue':
|
|
494
|
-
return {minValue: 0, maxValue: 360, step: 1};
|
|
521
|
+
return {minValue: 0, maxValue: 360, step: 1, pageSize: 15};
|
|
495
522
|
case 'saturation':
|
|
496
523
|
case 'lightness':
|
|
497
|
-
return {minValue: 0, maxValue: 100, step: 1};
|
|
524
|
+
return {minValue: 0, maxValue: 100, step: 1, pageSize: 10};
|
|
498
525
|
case 'alpha':
|
|
499
|
-
return {minValue: 0, maxValue: 1, step: 0.01};
|
|
526
|
+
return {minValue: 0, maxValue: 1, step: 0.01, pageSize: 0.1};
|
|
500
527
|
default:
|
|
501
528
|
throw new Error('Unknown color channel: ' + channel);
|
|
502
529
|
}
|
|
@@ -526,4 +553,9 @@ class HSLColor extends Color {
|
|
|
526
553
|
getColorSpace(): ColorFormat {
|
|
527
554
|
return 'hsl';
|
|
528
555
|
}
|
|
556
|
+
|
|
557
|
+
private static colorChannels: [ColorChannel, ColorChannel, ColorChannel] = ['hue', 'saturation', 'lightness'];
|
|
558
|
+
getColorChannels(): [ColorChannel, ColorChannel, ColorChannel] {
|
|
559
|
+
return HSLColor.colorChannels;
|
|
560
|
+
}
|
|
529
561
|
}
|
package/src/index.ts
CHANGED
|
@@ -10,7 +10,8 @@
|
|
|
10
10
|
* governing permissions and limitations under the License.
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
|
-
export
|
|
13
|
+
export {parseColor} from './Color';
|
|
14
|
+
export * from './useColorAreaState';
|
|
14
15
|
export * from './useColorSliderState';
|
|
15
16
|
export * from './useColorWheelState';
|
|
16
17
|
export * from './useColorFieldState';
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
3
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*
|
|
7
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
+
* governing permissions and limitations under the License.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import {clamp, snapValueToStep, useControlledState} from '@react-stately/utils';
|
|
14
|
+
import {Color, ColorAreaProps, ColorChannel} from '@react-types/color';
|
|
15
|
+
import {normalizeColor, parseColor} from './Color';
|
|
16
|
+
import {useMemo, useRef, useState} from 'react';
|
|
17
|
+
|
|
18
|
+
export interface ColorAreaState {
|
|
19
|
+
/** The current color value displayed by the color area. */
|
|
20
|
+
readonly value: Color,
|
|
21
|
+
/** Sets the current color value. If a string is passed, it will be parsed to a Color. */
|
|
22
|
+
setValue(value: string | Color): void,
|
|
23
|
+
|
|
24
|
+
/** The current value of the horizontal axis channel displayed by the color area. */
|
|
25
|
+
xValue: number,
|
|
26
|
+
/** Sets the value for the horizontal axis channel displayed by the color area, and triggers `onChange`. */
|
|
27
|
+
setXValue(value: number): void,
|
|
28
|
+
|
|
29
|
+
/** The current value of the vertical axis channel displayed by the color area. */
|
|
30
|
+
yValue: number,
|
|
31
|
+
/** Sets the value for the vertical axis channel displayed by the color area, and triggers `onChange`. */
|
|
32
|
+
setYValue(value: number): void,
|
|
33
|
+
|
|
34
|
+
/** Sets the x and y channels of the current color value based on a percentage of the width and height of the color area, and triggers `onChange`. */
|
|
35
|
+
setColorFromPoint(x: number, y: number): void,
|
|
36
|
+
/** Returns the coordinates of the thumb relative to the upper left corner of the color area as a percentage. */
|
|
37
|
+
getThumbPosition(): {x: number, y: number},
|
|
38
|
+
|
|
39
|
+
/** Increments the value of the horizontal axis channel by the channel step or page amount. */
|
|
40
|
+
incrementX(stepSize?: number): void,
|
|
41
|
+
/** Decrements the value of the horizontal axis channel by the channel step or page amount. */
|
|
42
|
+
decrementX(stepSize?: number): void,
|
|
43
|
+
|
|
44
|
+
/** Increments the value of the vertical axis channel by the channel step or page amount. */
|
|
45
|
+
incrementY(stepSize?: number): void,
|
|
46
|
+
/** Decrements the value of the vertical axis channel by the channel step or page amount. */
|
|
47
|
+
decrementY(stepSize?: number): void,
|
|
48
|
+
|
|
49
|
+
/** Whether the color area is currently being dragged. */
|
|
50
|
+
readonly isDragging: boolean,
|
|
51
|
+
/** Sets whether the color area is being dragged. */
|
|
52
|
+
setDragging(value: boolean): void,
|
|
53
|
+
|
|
54
|
+
/** Returns the xChannel, yChannel and zChannel names based on the color value. */
|
|
55
|
+
channels: {xChannel: ColorChannel, yChannel: ColorChannel, zChannel: ColorChannel},
|
|
56
|
+
xChannelStep: number,
|
|
57
|
+
yChannelStep: number,
|
|
58
|
+
xChannelPageStep: number,
|
|
59
|
+
yChannelPageStep: number,
|
|
60
|
+
|
|
61
|
+
/** Returns the color that should be displayed in the color area thumb instead of `value`. */
|
|
62
|
+
getDisplayColor(): Color
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const DEFAULT_COLOR = parseColor('#ffffff');
|
|
66
|
+
/**
|
|
67
|
+
* Provides state management for a color area component.
|
|
68
|
+
* Color area allows users to adjust two channels of an HSL, HSB or RGB color value against a two-dimensional gradient background.
|
|
69
|
+
*/
|
|
70
|
+
export function useColorAreaState(props: ColorAreaProps): ColorAreaState {
|
|
71
|
+
let {
|
|
72
|
+
value,
|
|
73
|
+
defaultValue,
|
|
74
|
+
xChannel,
|
|
75
|
+
yChannel,
|
|
76
|
+
onChange,
|
|
77
|
+
onChangeEnd
|
|
78
|
+
} = props;
|
|
79
|
+
|
|
80
|
+
if (!value && !defaultValue) {
|
|
81
|
+
defaultValue = DEFAULT_COLOR;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
let [color, setColor] = useControlledState(value && normalizeColor(value), defaultValue && normalizeColor(defaultValue), onChange);
|
|
85
|
+
let valueRef = useRef(color);
|
|
86
|
+
valueRef.current = color;
|
|
87
|
+
|
|
88
|
+
let channels = useMemo(() =>
|
|
89
|
+
valueRef.current.getColorSpaceAxes({xChannel, yChannel}),
|
|
90
|
+
[xChannel, yChannel]
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
let xChannelRange = color.getChannelRange(channels.xChannel);
|
|
94
|
+
let yChannelRange = color.getChannelRange(channels.yChannel);
|
|
95
|
+
let {minValue: minValueX, maxValue: maxValueX, step: stepX, pageSize: pageSizeX} = xChannelRange;
|
|
96
|
+
let {minValue: minValueY, maxValue: maxValueY, step: stepY, pageSize: pageSizeY} = yChannelRange;
|
|
97
|
+
|
|
98
|
+
let [isDragging, setDragging] = useState(false);
|
|
99
|
+
let isDraggingRef = useRef(false).current;
|
|
100
|
+
|
|
101
|
+
let xValue = color.getChannelValue(channels.xChannel);
|
|
102
|
+
let yValue = color.getChannelValue(channels.yChannel);
|
|
103
|
+
let setXValue = (v: number) => {
|
|
104
|
+
if (v === xValue) {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
valueRef.current = color.withChannelValue(channels.xChannel, v);
|
|
108
|
+
setColor(valueRef.current);
|
|
109
|
+
};
|
|
110
|
+
let setYValue = (v: number) => {
|
|
111
|
+
if (v === yValue) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
valueRef.current = color.withChannelValue(channels.yChannel, v);
|
|
115
|
+
setColor(valueRef.current);
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
return {
|
|
119
|
+
channels,
|
|
120
|
+
xChannelStep: stepX,
|
|
121
|
+
yChannelStep: stepY,
|
|
122
|
+
xChannelPageStep: pageSizeX,
|
|
123
|
+
yChannelPageStep: pageSizeY,
|
|
124
|
+
value: color,
|
|
125
|
+
setValue(value) {
|
|
126
|
+
let c = normalizeColor(value);
|
|
127
|
+
valueRef.current = c;
|
|
128
|
+
setColor(c);
|
|
129
|
+
},
|
|
130
|
+
xValue,
|
|
131
|
+
setXValue,
|
|
132
|
+
yValue,
|
|
133
|
+
setYValue,
|
|
134
|
+
setColorFromPoint(x: number, y: number) {
|
|
135
|
+
let newXValue = minValueX + clamp(x, 0, 1) * (maxValueX - minValueX);
|
|
136
|
+
let newYValue = minValueY + (1 - clamp(y, 0, 1)) * (maxValueY - minValueY);
|
|
137
|
+
let newColor:Color;
|
|
138
|
+
if (newXValue !== xValue) {
|
|
139
|
+
// Round new value to multiple of step, clamp value between min and max
|
|
140
|
+
newXValue = snapValueToStep(newXValue, minValueX, maxValueX, stepX);
|
|
141
|
+
newColor = color.withChannelValue(channels.xChannel, newXValue);
|
|
142
|
+
}
|
|
143
|
+
if (newYValue !== yValue) {
|
|
144
|
+
// Round new value to multiple of step, clamp value between min and max
|
|
145
|
+
newYValue = snapValueToStep(newYValue, minValueY, maxValueY, stepY);
|
|
146
|
+
newColor = (newColor || color).withChannelValue(channels.yChannel, newYValue);
|
|
147
|
+
}
|
|
148
|
+
if (newColor) {
|
|
149
|
+
setColor(newColor);
|
|
150
|
+
}
|
|
151
|
+
},
|
|
152
|
+
getThumbPosition() {
|
|
153
|
+
let x = (xValue - minValueX) / (maxValueX - minValueX);
|
|
154
|
+
let y = 1 - (yValue - minValueY) / (maxValueY - minValueY);
|
|
155
|
+
return {x, y};
|
|
156
|
+
},
|
|
157
|
+
incrementX(stepSize) {
|
|
158
|
+
setXValue(xValue + stepSize > maxValueX ? maxValueX : snapValueToStep(xValue + stepSize, minValueX, maxValueX, stepX));
|
|
159
|
+
},
|
|
160
|
+
incrementY(stepSize) {
|
|
161
|
+
setYValue(yValue + stepSize > maxValueY ? maxValueY : snapValueToStep(yValue + stepSize, minValueY, maxValueY, stepY));
|
|
162
|
+
},
|
|
163
|
+
decrementX(stepSize) {
|
|
164
|
+
setXValue(snapValueToStep(xValue - stepSize, minValueX, maxValueX, stepX));
|
|
165
|
+
},
|
|
166
|
+
decrementY(stepSize) {
|
|
167
|
+
setYValue(snapValueToStep(yValue - stepSize, minValueY, maxValueY, stepY));
|
|
168
|
+
},
|
|
169
|
+
setDragging(isDragging) {
|
|
170
|
+
let wasDragging = isDraggingRef;
|
|
171
|
+
isDraggingRef = isDragging;
|
|
172
|
+
|
|
173
|
+
if (onChangeEnd && !isDragging && wasDragging) {
|
|
174
|
+
onChangeEnd(valueRef.current);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
setDragging(isDragging);
|
|
178
|
+
},
|
|
179
|
+
isDragging,
|
|
180
|
+
getDisplayColor() {
|
|
181
|
+
return color.withChannelValue('alpha', 1);
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
}
|
|
@@ -14,7 +14,7 @@ import {Color, ColorFieldProps} from '@react-types/color';
|
|
|
14
14
|
import {parseColor} from './Color';
|
|
15
15
|
import {useColor} from './useColor';
|
|
16
16
|
import {useControlledState} from '@react-stately/utils';
|
|
17
|
-
import {
|
|
17
|
+
import {useMemo, useRef, useState} from 'react';
|
|
18
18
|
|
|
19
19
|
export interface ColorFieldState {
|
|
20
20
|
/**
|
|
@@ -63,11 +63,11 @@ export function useColorFieldState(
|
|
|
63
63
|
props: ColorFieldProps
|
|
64
64
|
): ColorFieldState {
|
|
65
65
|
let {
|
|
66
|
-
step = 1,
|
|
67
66
|
value,
|
|
68
67
|
defaultValue,
|
|
69
68
|
onChange
|
|
70
69
|
} = props;
|
|
70
|
+
let {step} = MIN_COLOR.getChannelRange('red');
|
|
71
71
|
|
|
72
72
|
let initialValue = useColor(value);
|
|
73
73
|
let initialDefaultValue = useColor(defaultValue);
|
|
@@ -85,9 +85,12 @@ export function useColorFieldState(
|
|
|
85
85
|
}
|
|
86
86
|
};
|
|
87
87
|
|
|
88
|
-
|
|
88
|
+
let prevValue = useRef(colorValue);
|
|
89
|
+
if (prevValue.current !== colorValue) {
|
|
89
90
|
setInputValue(colorValue ? colorValue.toString('hex') : '');
|
|
90
|
-
|
|
91
|
+
prevValue.current = colorValue;
|
|
92
|
+
}
|
|
93
|
+
|
|
91
94
|
|
|
92
95
|
let parsedValue = useMemo(() => {
|
|
93
96
|
let color;
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
13
|
import {Color, ColorSliderProps} from '@react-types/color';
|
|
14
|
-
import {parseColor} from './Color';
|
|
14
|
+
import {normalizeColor, parseColor} from './Color';
|
|
15
15
|
import {SliderState, useSliderState} from '@react-stately/slider';
|
|
16
16
|
import {useControlledState} from '@react-stately/utils';
|
|
17
17
|
|
|
@@ -30,14 +30,6 @@ interface ColorSliderStateOptions extends ColorSliderProps {
|
|
|
30
30
|
locale: string
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
function normalizeColor(v: string | Color) {
|
|
34
|
-
if (typeof v === 'string') {
|
|
35
|
-
return parseColor(v);
|
|
36
|
-
} else {
|
|
37
|
-
return v;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
33
|
/**
|
|
42
34
|
* Provides state management for a color slider component.
|
|
43
35
|
* Color sliders allow users to adjust an individual channel of a color value.
|
|
@@ -59,13 +51,14 @@ export function useColorSliderState(props: ColorSliderStateOptions): ColorSlider
|
|
|
59
51
|
setColor(color.withChannelValue(channel, v));
|
|
60
52
|
},
|
|
61
53
|
onChangeEnd([v]) {
|
|
62
|
-
// onChange will have already been called with the right value, this is just to trigger
|
|
54
|
+
// onChange will have already been called with the right value, this is just to trigger onChangeEnd
|
|
63
55
|
if (props.onChangeEnd) {
|
|
64
56
|
props.onChangeEnd(color.withChannelValue(channel, v));
|
|
65
57
|
}
|
|
66
58
|
}
|
|
67
59
|
});
|
|
68
60
|
|
|
61
|
+
let {step, pageSize} = color.getChannelRange(channel);
|
|
69
62
|
return {
|
|
70
63
|
...sliderState,
|
|
71
64
|
value: color,
|
|
@@ -92,6 +85,8 @@ export function useColorSliderState(props: ColorSliderStateOptions): ColorSlider
|
|
|
92
85
|
},
|
|
93
86
|
getThumbValueLabel() {
|
|
94
87
|
return color.formatChannelValue(channel, locale);
|
|
95
|
-
}
|
|
88
|
+
},
|
|
89
|
+
step,
|
|
90
|
+
pageSize
|
|
96
91
|
};
|
|
97
92
|
}
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
13
|
import {Color, ColorWheelProps} from '@react-types/color';
|
|
14
|
-
import {parseColor} from './Color';
|
|
14
|
+
import {normalizeColor, parseColor} from './Color';
|
|
15
15
|
import {useControlledState} from '@react-stately/utils';
|
|
16
16
|
import {useRef, useState} from 'react';
|
|
17
17
|
|
|
@@ -32,24 +32,18 @@ export interface ColorWheelState {
|
|
|
32
32
|
getThumbPosition(radius: number): {x: number, y: number},
|
|
33
33
|
|
|
34
34
|
/** Increments the hue by the given amount (defaults to 1). */
|
|
35
|
-
increment(
|
|
35
|
+
increment(stepSize?: number): void,
|
|
36
36
|
/** Decrements the hue by the given amount (defaults to 1). */
|
|
37
|
-
decrement(
|
|
37
|
+
decrement(stepSize?: number): void,
|
|
38
38
|
|
|
39
39
|
/** Whether the color wheel is currently being dragged. */
|
|
40
40
|
readonly isDragging: boolean,
|
|
41
41
|
/** Sets whether the color wheel is being dragged. */
|
|
42
42
|
setDragging(value: boolean): void,
|
|
43
43
|
/** Returns the color that should be displayed in the color wheel instead of `value`. */
|
|
44
|
-
getDisplayColor(): Color
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
function normalizeColor(v: string | Color) {
|
|
48
|
-
if (typeof v === 'string') {
|
|
49
|
-
return parseColor(v);
|
|
50
|
-
} else {
|
|
51
|
-
return v;
|
|
52
|
-
}
|
|
44
|
+
getDisplayColor(): Color,
|
|
45
|
+
step: number,
|
|
46
|
+
pageStep: number
|
|
53
47
|
}
|
|
54
48
|
|
|
55
49
|
const DEFAULT_COLOR = parseColor('hsl(0, 100%, 50%)');
|
|
@@ -97,7 +91,7 @@ function cartesianToAngle(x: number, y: number, radius: number): number {
|
|
|
97
91
|
* Color wheels allow users to adjust the hue of an HSL or HSB color value on a circular track.
|
|
98
92
|
*/
|
|
99
93
|
export function useColorWheelState(props: ColorWheelProps): ColorWheelState {
|
|
100
|
-
let {defaultValue, onChange, onChangeEnd
|
|
94
|
+
let {defaultValue, onChange, onChangeEnd} = props;
|
|
101
95
|
|
|
102
96
|
if (!props.value && !defaultValue) {
|
|
103
97
|
defaultValue = DEFAULT_COLOR;
|
|
@@ -107,6 +101,8 @@ export function useColorWheelState(props: ColorWheelProps): ColorWheelState {
|
|
|
107
101
|
let valueRef = useRef(value);
|
|
108
102
|
valueRef.current = value;
|
|
109
103
|
|
|
104
|
+
let channelRange = value.getChannelRange('hue');
|
|
105
|
+
let {minValue: minValueX, maxValue: maxValueX, step: step, pageSize: pageStep} = channelRange;
|
|
110
106
|
let [isDragging, setDragging] = useState(false);
|
|
111
107
|
let isDraggingRef = useRef(false).current;
|
|
112
108
|
|
|
@@ -126,6 +122,8 @@ export function useColorWheelState(props: ColorWheelProps): ColorWheelState {
|
|
|
126
122
|
|
|
127
123
|
return {
|
|
128
124
|
value,
|
|
125
|
+
step,
|
|
126
|
+
pageStep,
|
|
129
127
|
setValue(v) {
|
|
130
128
|
let color = normalizeColor(v);
|
|
131
129
|
valueRef.current = color;
|
|
@@ -139,22 +137,23 @@ export function useColorWheelState(props: ColorWheelProps): ColorWheelState {
|
|
|
139
137
|
getThumbPosition(radius) {
|
|
140
138
|
return angleToCartesian(value.getChannelValue('hue'), radius);
|
|
141
139
|
},
|
|
142
|
-
increment(
|
|
143
|
-
let
|
|
144
|
-
|
|
140
|
+
increment(stepSize = 1) {
|
|
141
|
+
let s = Math.max(stepSize, step);
|
|
142
|
+
let newValue = hue + s;
|
|
143
|
+
if (newValue >= maxValueX) {
|
|
145
144
|
// Make sure you can always get back to 0.
|
|
146
|
-
newValue =
|
|
145
|
+
newValue = minValueX;
|
|
147
146
|
}
|
|
148
|
-
setHue(newValue);
|
|
147
|
+
setHue(roundToStep(mod(newValue, 360), s));
|
|
149
148
|
},
|
|
150
|
-
decrement(
|
|
151
|
-
let s = Math.max(
|
|
149
|
+
decrement(stepSize = 1) {
|
|
150
|
+
let s = Math.max(stepSize, step);
|
|
152
151
|
if (hue === 0) {
|
|
153
152
|
// We can't just subtract step because this might be the case:
|
|
154
153
|
// |(previous step) - 0| < step size
|
|
155
154
|
setHue(roundDown(360 / s) * s);
|
|
156
155
|
} else {
|
|
157
|
-
setHue(hue - s);
|
|
156
|
+
setHue(roundToStep(mod(hue - s, 360), s));
|
|
158
157
|
}
|
|
159
158
|
},
|
|
160
159
|
setDragging(isDragging) {
|
|
@@ -169,7 +168,7 @@ export function useColorWheelState(props: ColorWheelProps): ColorWheelState {
|
|
|
169
168
|
},
|
|
170
169
|
isDragging,
|
|
171
170
|
getDisplayColor() {
|
|
172
|
-
return value.withChannelValue('saturation', 100).withChannelValue('lightness', 50);
|
|
171
|
+
return value.toFormat('hsl').withChannelValue('saturation', 100).withChannelValue('lightness', 50);
|
|
173
172
|
}
|
|
174
173
|
};
|
|
175
174
|
}
|