antd-color-editor
Version:
An open-source color editor for designing color system
93 lines (92 loc) • 5.68 kB
JavaScript
function _construct(Parent, args, Class) { if (_isNativeReflectConstruct()) { _construct = Reflect.construct.bind(); } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { useThemeMode } from 'antd-style';
import chroma from 'chroma-js';
import { memo } from 'react';
import * as Three from 'three';
import { hexToHct } from "../..";
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
var scalePositon = function scalePositon(pos, zoomPos) {
var _pos = _slicedToArray(pos, 3),
x = _pos[0],
y = _pos[1],
z = _pos[2];
var _zoomPos = _slicedToArray(zoomPos, 3),
xZoom = _zoomPos[0],
yZoom = _zoomPos[1],
zZoom = _zoomPos[2];
return [x * xZoom, y * yZoom, z * zZoom];
};
// @ts-ignore
var GenColor3D = /*#__PURE__*/memo(function (_ref) {
var name = _ref.name,
scale = _ref.scale,
colorType = _ref.colorType,
zoom = _ref.zoom,
hueZoom = _ref.hueZoom;
var _useThemeMode = useThemeMode(),
isDarkMode = _useThemeMode.isDarkMode;
var genPosition = function genPosition(c, midC, num) {
var position;
switch (colorType) {
case 'mix':
case 'hex':
{
position = scalePositon([hexToHct(midC)[0], 1 - num, 0], [1 / 3.6 * hueZoom, 100, 1]);
break;
}
case 'cts':
{
position = scalePositon([hexToHct(midC)[0], num, chroma.contrast(c, isDarkMode ? '#000' : '#fff')], [1 / 3.6 * hueZoom, 100, 20]);
break;
}
case 'rgb':
{
position = scalePositon(chroma(c).rgb(), [1 / 2.55, 1 / 2.55, 1 / 2.55]);
break;
}
case 'hsl':
{
position = scalePositon(chroma(c).hsl(), [1 / 3.6 * hueZoom, 100, 100]);
position = [position[0], position[2], position[1]];
break;
}
case 'hsv':
{
position = scalePositon(chroma(c).hsv(), [1 / 3.6 * hueZoom, 100, 100]);
position = [position[0], position[2], position[1]];
break;
}
default:
{
position = scalePositon(hexToHct(c), [1 / 3.6 * hueZoom, 1, 1]);
position = [position[0], position[2], position[1]];
break;
}
}
return _construct(Three.Vector3, _toConsumableArray(position));
};
return scale.map(function (c, index) {
return /*#__PURE__*/_jsxs("mesh", {
/* eslint-disable-next-line react/no-unknown-property */
position: genPosition(c, scale[Math.floor(scale.length / 2)], (index + 1) / scale.length),
scale: zoom,
children: [/*#__PURE__*/_jsx("boxGeometry", {}), /*#__PURE__*/_jsx("meshPhysicalMaterial", {
color: c
})]
}, name + index);
});
});
export default GenColor3D;