reanimated-color-picker
Version:
A Pure JavaScript Color Picker for React Native
276 lines (268 loc) • 9.54 kB
JavaScript
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true,
});
exports.HSLSaturationSlider = HSLSaturationSlider;
var _react = _interopRequireDefault(require('react'));
var _reactNative = require('react-native');
var _reactNativeReanimated = _interopRequireWildcard(require('react-native-reanimated'));
var _index = _interopRequireDefault(require('../../../colorKit/index'));
var _AppContext = _interopRequireDefault(require('../../../AppContext'));
var _SliderCore = require('../SliderCore');
var _Thumb = _interopRequireDefault(require('../../Thumb/Thumb'));
var _utils = require('../../../utils');
function _interopRequireWildcard(e, t) {
if ('function' == typeof WeakMap)
var r = new WeakMap(),
n = new WeakMap();
return (_interopRequireWildcard = function (e, t) {
if (!t && e && e.__esModule) return e;
var o,
i,
f = { __proto__: null, default: e };
if (null === e || ('object' != typeof e && 'function' != typeof e)) return f;
if ((o = t ? n : r)) {
if (o.has(e)) return o.get(e);
o.set(e, f);
}
for (const t in e)
'default' !== t &&
{}.hasOwnProperty.call(e, t) &&
((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set)
? o(f, t, i)
: (f[t] = e[t]));
return f;
})(e, t);
}
function _interopRequireDefault(e) {
return e && e.__esModule ? e : { default: e };
}
/** @see [HSLSaturationSlider](https://alabsi91.github.io/reanimated-color-picker/api/sliders/hsl/saturation-slider/) */
function HSLSaturationSlider({ gestures = [], style = {}, vertical = false, reverse = false, ...props }) {
const { hueValue, saturationValue, brightnessValue, onGestureChange, onGestureEnd, ...ctx } = (0, _AppContext.default)();
const thumbShape = props.thumbShape ?? ctx.thumbShape;
const thumbSize = props.thumbSize ?? ctx.thumbSize;
const thumbColor = props.thumbColor ?? ctx.thumbColor;
const boundedThumb = props.boundedThumb ?? ctx.boundedThumb;
const renderThumb = props.renderThumb ?? ctx.renderThumb;
const thumbStyle = props.thumbStyle ?? ctx.thumbStyle ?? {};
const thumbInnerStyle = props.thumbInnerStyle ?? ctx.thumbInnerStyle ?? {};
const thumbScaleAnimationValue = props.thumbScaleAnimationValue ?? ctx.thumbScaleAnimationValue;
const thumbScaleAnimationDuration = props.thumbScaleAnimationDuration ?? ctx.thumbScaleAnimationDuration;
const adaptSpectrum = props.adaptSpectrum ?? ctx.adaptSpectrum;
const sliderThickness = props.sliderThickness ?? ctx.sliderThickness;
const borderRadius = (0, _utils.getStyle)(style, 'borderRadius') ?? 5;
const getWidth = (0, _utils.getStyle)(style, 'width');
const getHeight = (0, _utils.getStyle)(style, 'height');
const width = (0, _reactNativeReanimated.useSharedValue)(
vertical ? sliderThickness : typeof getWidth === 'number' ? getWidth : 0,
);
const height = (0, _reactNativeReanimated.useSharedValue)(
!vertical ? sliderThickness : typeof getHeight === 'number' ? getHeight : 0,
);
const handleScale = (0, _reactNativeReanimated.useSharedValue)(1);
// HSL saturation is mathematically undefined (collapses to 0) when luminance is 0 or 100,
// because those represent pure black and white regardless of saturation.
// This ref holds the last valid saturation so we can restore it when luminance moves away from the boundary.
const hslSaturationValue = (0, _reactNativeReanimated.useSharedValue)(0);
const hsl = (0, _reactNativeReanimated.useDerivedValue)(() => {
const hsvColor = {
h: hueValue.value,
s: saturationValue.value,
v: brightnessValue.value,
};
const { h, s, l } = _index.default.runOnUI().HSL(hsvColor).object(false);
// At l=0 (black) or l=100 (white), the conversion loses saturation information.
// Substitute the last known saturation so it's restored when luminance changes.
if (l === 0 || l === 100) {
return {
h,
s: hslSaturationValue.value,
l,
};
}
hslSaturationValue.value = s;
return {
h,
s,
l,
};
}, [hueValue, saturationValue, brightnessValue]);
const thumbAnimatedStyle = (0, _reactNativeReanimated.useAnimatedStyle)(() => {
const length = (vertical ? height.value : width.value) - (boundedThumb ? thumbSize : 0);
const percent = (hsl.value.s / 100) * length;
const pos = (reverse ? length - percent : percent) - (boundedThumb ? 0 : thumbSize / 2);
const posY = vertical ? pos : height.value / 2 - thumbSize / 2;
const posX = vertical ? width.value / 2 - thumbSize / 2 : pos;
return {
transform: [
{
translateY: posY,
},
{
translateX: posX,
},
{
scale: handleScale.value,
},
],
};
}, [width, height, hsl, handleScale, vertical, reverse, boundedThumb, thumbSize]);
const activeColorStyle = (0, _reactNativeReanimated.useAnimatedStyle)(() => {
return {
backgroundColor: `hsl(${hsl.value.h}, 100%, 50%)`,
};
}, [hsl]);
const activeBrightnessStyle = (0, _reactNativeReanimated.useAnimatedStyle)(() => {
if (!adaptSpectrum) {
return {};
}
if (hsl.value.l < 50) {
return {
backgroundColor: `rgba(0, 0, 0, ${1 - hsl.value.l / 50})`,
};
}
return {
backgroundColor: `rgba(255, 255, 255, ${(hsl.value.l - 50) / 50})`,
};
}, [hsl, adaptSpectrum]);
const imageStyle = (0, _reactNativeReanimated.useAnimatedStyle)(() => {
const imageRotate = vertical ? (reverse ? '270deg' : '90deg') : reverse ? '180deg' : '0deg';
const imageTranslateY =
((height.value - width.value) / 2) * ((reverse && _utils.isRtl) || (!reverse && !_utils.isRtl) ? 1 : -1);
return {
// Width and height are intentionally swapped to correct dimensions after the rotation
width: vertical ? height.value : '100%',
height: vertical ? width.value : '100%',
transform: [
{
rotate: imageRotate,
},
{
translateX: vertical ? ((height.value - width.value) / 2) * (reverse ? -1 : 1) : 0,
},
{
translateY: vertical ? imageTranslateY : 0,
},
],
};
}, [width, height, vertical, reverse]);
const onBegin = () => {
'worklet';
handleScale.value = (0, _reactNativeReanimated.withTiming)(thumbScaleAnimationValue, {
duration: thumbScaleAnimationDuration,
});
};
const onUpdate = newValue => {
'worklet';
// Converting back from HSL→HSV at l=0 or l=100 would zero out HSV saturation and brightness,
// locking the slider. Nudging l by ±0.01 keeps the conversion well-behaved without any
// visible effect on the output color (values are rounded before use).
const l = hsl.value.l === 0 ? 0.01 : hsl.value.l === 100 ? 99.99 : hsl.value.l;
const { s, v } = _index.default
.runOnUI()
.HSV({
h: hsl.value.h,
s: newValue,
l,
})
.object(false);
if (saturationValue.value === s && brightnessValue.value === v) {
return;
}
saturationValue.value = s;
brightnessValue.value = v;
onGestureChange();
};
const onEnd = () => {
'worklet';
handleScale.value = (0, _reactNativeReanimated.withTiming)(1, {
duration: thumbScaleAnimationDuration,
});
onGestureEnd();
};
const getAdaptiveColor = hsva => {
'worklet';
if (adaptSpectrum) {
return hsva;
}
const { h, s } = hsl.value;
return {
h,
s,
l: 50,
};
};
return /*#__PURE__*/ _react.default.createElement(
_SliderCore.SliderCore,
{
style: [
style,
{
position: 'relative',
borderRadius,
borderWidth: 0,
padding: 0,
},
activeColorStyle,
],
label: props.accessibilityLabel ?? 'HSL Saturation Slider',
hint: props.accessibilityHint,
currentValue: hslSaturationValue,
width: width,
height: height,
thumbSize: thumbSize,
boundedThumb: boundedThumb,
sliderThickness: sliderThickness,
gestures: gestures,
vertical: vertical,
reverse: reverse,
onBegin: onBegin,
onUpdate: onUpdate,
onEnd: onEnd,
},
/*#__PURE__*/ _react.default.createElement(
_reactNative.View,
{
style: {
flex: 1,
borderRadius,
overflow: 'hidden',
},
'aria-hidden': true,
},
/*#__PURE__*/ _react.default.createElement(_reactNativeReanimated.default.Image, {
source: require('../../../assets/blackGradient.png'),
style: imageStyle,
tintColor: '#888',
}),
),
/*#__PURE__*/ _react.default.createElement(
_utils.ConditionalRendering,
{
if: adaptSpectrum,
},
/*#__PURE__*/ _react.default.createElement(_reactNativeReanimated.default.View, {
style: [
{
borderRadius,
},
activeBrightnessStyle,
_reactNative.StyleSheet.absoluteFill,
],
'aria-hidden': true,
}),
),
/*#__PURE__*/ _react.default.createElement(_Thumb.default, {
thumbShape: thumbShape,
thumbSize: thumbSize,
thumbColor: thumbColor,
renderThumb: renderThumb,
innerStyle: thumbInnerStyle,
thumbAnimatedStyle: thumbAnimatedStyle,
style: thumbStyle,
vertical: vertical,
getAdaptiveColor: getAdaptiveColor,
}),
);
}