UNPKG

reanimated-color-picker

Version:
159 lines (156 loc) 4.8 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true, }); exports.default = HwbWidget; var _react = _interopRequireWildcard(require('react')); var _reactNativeReanimated = require('react-native-reanimated'); var _index = _interopRequireDefault(require('../../../colorKit/index')); var _utils = require('../../../utils'); var _WidgetTextInput = _interopRequireDefault(require('./WidgetTextInput')); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } 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 HwbWidget(props) { const { onChange, colorResult, hueValue, saturationValue, brightnessValue, alphaValue, inputStyle, inputTitleStyle, inputProps, disableAlphaChannel, } = props; const hwb = (0, _react.useRef)(_index.default.HWB(colorResult().hwba).object(false)); const h = (0, _reactNativeReanimated.useSharedValue)(hwb.current.h.toString()); const w = (0, _reactNativeReanimated.useSharedValue)(hwb.current.w.toString()); const b = (0, _reactNativeReanimated.useSharedValue)(hwb.current.b.toString()); const a = (0, _reactNativeReanimated.useSharedValue)(hwb.current.a.toString()); (0, _reactNativeReanimated.useDerivedValue)(() => { const currentColor = { h: hueValue.value, s: saturationValue.value, v: brightnessValue.value, a: alphaValue.value, }; hwb.current = _index.default.runOnUI().HWB(colorResult(currentColor).hwba).object(false); h.value = hwb.current.h.toString(); w.value = hwb.current.w.toString(); b.value = hwb.current.b.toString(); a.value = hwb.current.a.toString(); }, [hueValue, saturationValue, brightnessValue, alphaValue]); const onHueEndEditing = text => { const hue = (0, _utils.clamp)(+text, 360); // Force update in case `h` value has not changed h.value = ''; onChange({ h: hue, w: +w.value, b: +b.value, a: +a.value, }); }; const onWhiteEndEditing = text => { const whiteness = (0, _utils.clamp)(+text, 100); w.value = ''; onChange({ h: +h.value, w: whiteness, b: +b.value, a: +a.value, }); }; const onBlackEndEditing = text => { const blackness = (0, _utils.clamp)(+text, 100); b.value = ''; onChange({ h: +h.value, w: +w.value, b: blackness, a: +a.value, }); }; const onAlphaEndEditing = text => { const alpha = (0, _utils.clamp)(+text, 1); a.value = ''; onChange({ h: +h.value, w: +w.value, b: +b.value, a: alpha, }); }; return /*#__PURE__*/ _react.default.createElement( _react.default.Fragment, null, /*#__PURE__*/ _react.default.createElement(_WidgetTextInput.default, { inputStyle: inputStyle, textStyle: inputTitleStyle, textValue: h, title: 'H', label: 'Hue channel in HWB color format', onEndEditing: onHueEndEditing, inputProps: inputProps, }), /*#__PURE__*/ _react.default.createElement(_WidgetTextInput.default, { inputStyle: inputStyle, textStyle: inputTitleStyle, textValue: w, title: 'W', label: 'Whiteness channel in HWB color format', onEndEditing: onWhiteEndEditing, inputProps: inputProps, }), /*#__PURE__*/ _react.default.createElement(_WidgetTextInput.default, { inputStyle: inputStyle, textStyle: inputTitleStyle, textValue: b, title: 'B', label: 'Blackness channel in HWB color format', onEndEditing: onBlackEndEditing, inputProps: inputProps, }), /*#__PURE__*/ _react.default.createElement( _utils.ConditionalRendering, { if: !disableAlphaChannel, }, /*#__PURE__*/ _react.default.createElement(_WidgetTextInput.default, { inputStyle: inputStyle, textStyle: inputTitleStyle, textValue: a, title: 'A', label: 'Alpha channel in HWB color format', onEndEditing: onAlphaEndEditing, inputProps: inputProps, decimal: true, }), ), ); }