reanimated-color-picker
Version:
A Pure JavaScript Color Picker for React Native
345 lines (336 loc) • 12 kB
JavaScript
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true,
});
exports.Panel2 = Panel2;
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 _PanelCore = require('./PanelCore');
var _styles = require('../../styles');
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 [Panel2](https://alabsi91.github.io/reanimated-color-picker/api/panels/panel2/) */
function Panel2({
verticalChannel = 'saturation',
reverseHue = false,
reverseVerticalChannel = false,
gestures = [],
style = {},
...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.thumbScaleUpValue ?? ctx.thumbScaleAnimationValue;
const thumbScaleAnimationDuration = props.thumbScaleUpDuration ?? ctx.thumbScaleAnimationDuration;
const adaptSpectrum = props.adaptSpectrum ?? ctx.adaptSpectrum;
const borderRadius = (0, _utils.getStyle)(style, 'borderRadius') ?? 5;
const heightStyle = (0, _utils.getStyle)(style, 'height') ?? 200;
const width = (0, _reactNativeReanimated.useSharedValue)(0);
const height = (0, _reactNativeReanimated.useSharedValue)(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 === 100 || l === 0) {
return {
h,
s: hslSaturationValue.value,
l,
};
}
hslSaturationValue.value = s;
return {
h,
s,
l,
};
}, [hueValue, saturationValue, brightnessValue]);
const verticalChannelValue = (0, _reactNativeReanimated.useDerivedValue)(() => {
if (verticalChannel === 'brightness') {
return brightnessValue.value;
}
if (verticalChannel === 'hsl-saturation') {
return hsl.value.s;
}
return saturationValue.value;
}, [brightnessValue, saturationValue, hsl]);
const thumbAnimatedStyle = (0, _reactNativeReanimated.useAnimatedStyle)(() => {
const length = {
x: width.value - (boundedThumb ? thumbSize : 0),
y: height.value - (boundedThumb ? thumbSize : 0),
};
const percentX = (hueValue.value / 360) * length.x;
const posX = (reverseHue ? length.x - percentX : percentX) - (boundedThumb ? 0 : thumbSize / 2);
const percentY = (verticalChannelValue.value / 100) * length.y;
const posY = (reverseVerticalChannel ? percentY : length.y - percentY) - (boundedThumb ? 0 : thumbSize / 2);
return {
transform: [
{
translateX: posX,
},
{
translateY: posY,
},
{
scale: handleScale.value,
},
],
};
}, [[width, height, hueValue, verticalChannelValue, handleScale, reverseHue, reverseVerticalChannel, boundedThumb, thumbSize]]);
const spectrumStyle = (0, _reactNativeReanimated.useAnimatedStyle)(() => {
if (!adaptSpectrum) return {};
if (verticalChannel === 'brightness') {
return {
backgroundColor: `rgba(255, 255, 255, ${1 - saturationValue.value / 100})`,
};
}
if (verticalChannel === 'hsl-saturation') {
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})`,
};
}
return {
backgroundColor: `rgba(0, 0, 0, ${1 - brightnessValue.value / 100})`,
};
}, [saturationValue, brightnessValue, hsl, adaptSpectrum, verticalChannel]);
const panelImageStyle = (0, _reactNativeReanimated.useAnimatedStyle)(() => {
return {
// Width and height are intentionally swapped to correct dimensions after the rotation
width: height.value,
height: width.value,
transform: [
{
rotate: reverseVerticalChannel ? '90deg' : '270deg',
},
{
translateX: ((width.value - height.value) / 2) * (reverseVerticalChannel ? -1 : 1),
},
{
translateY: ((width.value - height.value) / 2) * (_utils.isRtl ? -1 : 1) * (reverseVerticalChannel ? -1 : 1),
},
],
};
}, [width, height, reverseVerticalChannel]);
const onBegin = () => {
'worklet';
handleScale.value = (0, _reactNativeReanimated.withTiming)(thumbScaleAnimationValue, {
duration: thumbScaleAnimationDuration,
});
};
const onUpdate = (newXValue, newYValue) => {
'worklet';
if (hueValue.value === newXValue && verticalChannelValue.value === newYValue) {
return;
}
hueValue.value = newXValue;
if (verticalChannel === 'hsl-saturation') {
// 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: newYValue,
l,
})
.object(false);
saturationValue.value = s;
brightnessValue.value = v;
}
// Vertical channel is brightness
else if (verticalChannel === 'brightness') {
brightnessValue.value = newYValue;
}
// Vertical channel is saturation
else {
saturationValue.value = newYValue;
}
onGestureChange();
};
const onGestureUpdate = ({ x, y }) => {
'worklet';
const lengthX = width.value - (boundedThumb ? thumbSize : 0);
const lengthY = height.value - (boundedThumb ? thumbSize : 0);
const posX = (0, _utils.clamp)(x - (boundedThumb ? thumbSize / 2 : 0), lengthX);
const posY = (0, _utils.clamp)(y - (boundedThumb ? thumbSize / 2 : 0), lengthY);
const valueX = (posX / lengthX) * 360;
const valueY = (posY / lengthY) * 100;
const newXValue = reverseHue ? 360 - valueX : valueX;
const newYValue = reverseVerticalChannel ? valueY : 100 - valueY;
onUpdate(newXValue, newYValue);
};
const onEnd = () => {
'worklet';
handleScale.value = (0, _reactNativeReanimated.withTiming)(1, {
duration: thumbScaleAnimationDuration,
});
onGestureEnd();
};
const getAdaptiveColor = hsva => {
'worklet';
if (adaptSpectrum) {
return hsva;
}
switch (verticalChannel) {
case 'saturation':
return {
h: hsva.h,
s: hsva.s,
v: 100,
};
case 'brightness':
return {
h: hsva.h,
s: 100,
v: hsva.v,
};
case 'hsl-saturation': {
const { h, s } = hsl.value;
return {
h,
s,
l: 50,
};
}
default:
return hsva;
}
};
return /*#__PURE__*/ _react.default.createElement(
_PanelCore.PanelCore,
{
style: [
_styles.styles.panelContainer,
style,
{
position: 'relative',
height: heightStyle,
borderWidth: 0,
padding: 0,
},
],
label: props.accessibilityHint ?? `Hue and ${verticalChannel} 2D slider`,
hint: props.accessibilityHint ?? `Double tap to switch between hue and ${verticalChannel}`,
labelX: 'Hue',
currentXValue: hueValue,
maxXValue: 360,
labelY: verticalChannel,
currentYValue: verticalChannelValue,
reverseX: reverseHue,
reverseY: reverseVerticalChannel,
width: width,
height: height,
gestures: gestures,
onGestureUpdate: onGestureUpdate,
onBegin: onBegin,
onUpdate: onUpdate,
onEnd: onEnd,
},
/*#__PURE__*/ _react.default.createElement(
_reactNative.ImageBackground,
{
source: require('../../assets/Hue.png'),
style: [
_styles.styles.panelImage,
{
position: 'relative',
borderRadius,
transform: [
{
scaleX: reverseHue ? -1 : 1,
},
],
},
],
resizeMode: 'stretch',
'aria-hidden': true,
},
/*#__PURE__*/ _react.default.createElement(
_utils.ConditionalRendering,
{
if: adaptSpectrum && verticalChannel === 'brightness',
},
/*#__PURE__*/ _react.default.createElement(_reactNativeReanimated.default.View, {
style: [spectrumStyle, _reactNative.StyleSheet.absoluteFill],
}),
),
/*#__PURE__*/ _react.default.createElement(_reactNativeReanimated.default.Image, {
source: require('../../assets/blackGradient.png'),
style: [_styles.styles.panelImage, panelImageStyle],
tintColor: verticalChannel === 'saturation' ? '#fff' : verticalChannel === 'hsl-saturation' ? '#888' : undefined,
resizeMode: 'stretch',
}),
/*#__PURE__*/ _react.default.createElement(
_utils.ConditionalRendering,
{
if: adaptSpectrum && (verticalChannel === 'saturation' || verticalChannel === 'hsl-saturation'),
},
/*#__PURE__*/ _react.default.createElement(_reactNativeReanimated.default.View, {
style: [spectrumStyle, _reactNative.StyleSheet.absoluteFill],
}),
),
),
/*#__PURE__*/ _react.default.createElement(_Thumb.default, {
thumbShape: thumbShape,
thumbSize: thumbSize,
thumbColor: thumbColor,
renderThumb: renderThumb,
innerStyle: thumbInnerStyle,
thumbAnimatedStyle: thumbAnimatedStyle,
style: thumbStyle,
getAdaptiveColor: getAdaptiveColor,
}),
);
}