UNPKG

reanimated-color-picker

Version:
203 lines (197 loc) 6.36 kB
import React from 'react'; import Animated, { runOnJS, useAnimatedStyle, useDerivedValue } from 'react-native-reanimated'; import usePickerContext from '../../../AppContext'; import { styles } from '../../../styles'; import Thumb from '../../Thumb/Thumb'; import { ConditionalRendering, clamp } from '../../../utils'; import usePanel3Context from './Panel3Context'; import colorKit from '../../../colorKit/index'; /** @see [ExtraThumb](https://alabsi91.github.io/reanimated-color-picker/api/preview/extra-thumb/) */ export function ExtraThumb({ onChange, onChangeJS, colorTransform, hueTransform, saturationTransform, brightnessTransform, renderThumb, ...props }) { const { width, boundedThumb, centerChannel, centerChannelValue, adaptSpectrum, rotate, ...ctx } = usePanel3Context(); const { hueValue, saturationValue, brightnessValue, alphaValue, colorResult } = usePickerContext(); const thumbSize = props.thumbSize ?? ctx.thumbSize; const thumbShape = props.thumbShape ?? ctx.thumbShape; const thumbColor = props.thumbColor ?? ctx.thumbColor; const thumbStyle = props.thumbStyle ?? ctx.thumbStyle; const thumbInnerStyle = props.thumbInnerStyle ?? ctx.thumbInnerStyle; const renderCenterLine = props.renderCenterLine ?? ctx.renderCenterLine; const hsv = useDerivedValue(() => { const currentColor = { h: hueValue.value, s: saturationValue.value, v: brightnessValue.value, a: alphaValue.value, }; if (colorTransform) { return colorTransform(currentColor); } return currentColor; }, [hueValue, saturationValue, brightnessValue, alphaValue]); const hue = useDerivedValue(() => { if (colorTransform) { return hsv.value.h; } if (!hueTransform) { return hueValue.value; } const changeAmount = typeof hueTransform === 'string' ? hueValue.value * (parseFloat(hueTransform) / 100) : hueTransform; return clamp((hueValue.value + changeAmount) % 360, 360); }, [hsv, hueValue, hueTransform]); // Calculate saturation value const saturation = useDerivedValue(() => { if (colorTransform) { return hsv.value.s; } if (!saturationTransform) { return saturationValue.value; } const changeAmount = typeof saturationTransform === 'string' ? saturationValue.value * (parseFloat(saturationTransform) / 100) : saturationTransform; return clamp(saturationValue.value + changeAmount, 100); }, [hsv, saturationValue, saturationTransform]); // Calculate brightness value const brightness = useDerivedValue(() => { if (colorTransform) { return hsv.value.v; } if (!brightnessTransform) { return brightnessValue.value; } const changeAmount = typeof brightnessTransform === 'string' ? brightnessValue.value * (parseFloat(brightnessTransform) / 100) : brightnessTransform; return clamp(brightnessValue.value + changeAmount, 100); }, [hsv, brightnessValue, brightnessTransform]); // Call onChange prop on every value change useDerivedValue(() => { if (!onChange && !onChangeJS) return; const colors = colorResult({ h: hue.value, s: saturation.value, v: brightness.value, a: alphaValue.value, }); if (onChange) { onChange(colors); } if (onChangeJS) { runOnJS(onChangeJS)(colors); } }, [hue, saturation, brightness, alphaValue]); const thumbAnimatedStyle = useAnimatedStyle(() => { const center = width.value / 2 - (boundedThumb ? thumbSize / 2 : 0); const rotatedHue = (hue.value - rotate) % 360; const distance = (centerChannelValue.value / 100) * (width.value / 2 - (boundedThumb ? thumbSize / 2 : 0)); const angle = (rotatedHue * Math.PI) / 180; const posY = width.value - (Math.sin(angle) * distance + center) - (boundedThumb ? thumbSize : thumbSize / 2); const posX = width.value - (Math.cos(angle) * distance + center) - (boundedThumb ? thumbSize : thumbSize / 2); return { transform: [ { translateX: posX, }, { translateY: posY, }, { rotate: rotatedHue + 90 + 'deg', }, ], }; }, [thumbSize, boundedThumb, width, centerChannelValue, hue, rotate]); const centerLineStyle = useAnimatedStyle(() => { if (!renderCenterLine) { return {}; } const lineThickness = 1; const center = width.value / 2 - (boundedThumb ? thumbSize / 2 : 0); const rotatedHue = (hue.value - rotate) % 360; const distance = (centerChannelValue.value / 100) * center; const angle = (rotatedHue + 180) % 360; // reversed angle return { top: (width.value - lineThickness) / 2, left: (width.value - distance) / 2, height: lineThickness, width: distance, transform: [ { rotate: angle + 'deg', }, { translateX: distance / 2, }, ], }; }, [renderCenterLine, boundedThumb, thumbSize, width, hue, centerChannelValue, rotate]); const getAdaptiveColor = hsva => { 'worklet'; if (adaptSpectrum) { return hsva; } switch (centerChannel) { 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 } = colorKit.runOnUI().HSL(hsva).object(false); return { h, s, l: 50, }; } default: return hsva; } }; return /*#__PURE__*/ React.createElement( React.Fragment, null, /*#__PURE__*/ React.createElement( ConditionalRendering, { if: renderCenterLine, }, /*#__PURE__*/ React.createElement(Animated.View, { style: [styles.panel3Line, centerLineStyle], }), ), /*#__PURE__*/ React.createElement(Thumb, { thumbShape: thumbShape, thumbSize: thumbSize, thumbColor: thumbColor, renderThumb: renderThumb, innerStyle: thumbInnerStyle, thumbAnimatedStyle: thumbAnimatedStyle, style: thumbStyle, overrideHSV: { hue, saturation, brightness, }, getAdaptiveColor: getAdaptiveColor, }), ); }