import React from "@modules/react"; import DiscordModules from "@modules/discordmodules"; import Strings from "@modules/strings"; const {useState, useCallback} = React; const Checkmark = React.memo((props) => ( )); const Dropper = React.memo((props) => ( )); const defaultColors = [1752220, 3066993, 3447003, 10181046, 15277667, 15844367, 15105570, 15158332, 9807270, 6323595, 1146986, 2067276, 2123412, 7419530, 11342935, 12745742, 11027200, 10038562, 9936031, 5533306]; const resolveColor = (color, hex = true) => { switch (typeof color) { case (hex && "number"): return `#${color.toString(16)}`; case (!hex && "string"): return Number.parseInt(color.replace("#", ""), 16); case (!hex && "number"): return color; case (hex && "string"): return color; default: return color; } }; const getRGB = (color) => { let result = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(color); if (result) return [parseInt(result[1]), parseInt(result[2]), parseInt(result[3])]; result = /rgb\(\s*([0-9]+(?:\.[0-9]+)?)%\s*,\s*([0-9]+(?:\.[0-9]+)?)%\s*,\s*([0-9]+(?:\.[0-9]+)?)%\s*\)/.exec(color); if (result) return [parseFloat(result[1]) * 2.55, parseFloat(result[2]) * 2.55, parseFloat(result[3]) * 2.55]; result = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(color); if (result) return [parseInt(result[1], 16), parseInt(result[2], 16), parseInt(result[3], 16)]; result = /#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(color); if (result) return [parseInt(result[1] + result[1], 16), parseInt(result[2] + result[2], 16), parseInt(result[3] + result[3], 16)]; }; const luma = (color) => { const rgb = (typeof(color) === "string") ? getRGB(color) : color; return (0.2126 * rgb[0]) + (0.7152 * rgb[1]) + (0.0722 * rgb[2]); // SMPTE C, Rec. 709 weightings }; const getContrastColor = (color) => { return (luma(color) >= 165) ? "#000" : "#fff"; }; export default function Color({value: initialValue, onChange, colors = defaultColors, defaultValue}) { const [value, setValue] = useState(initialValue); const change = useCallback((e) => { onChange?.(resolveColor(e.target.value)); setValue(e.target.value); }, [onChange]); const intValue = resolveColor(value, false); return
{props => (
change({target: {value: defaultValue}})}> {intValue === resolveColor(defaultValue, false) ? : null }
)}
{props => (
)}
{ colors.map((int, index) => (
change({target: {value: int}})}> {intValue === int ? : null }
)) }
; }