UNPKG

@eccenca/gui-elements

Version:

GUI elements based on other libraries, usable in React application, written in Typescript.

140 lines 5.75 kB
import Color from "color"; import { CLASSPREFIX as eccgui, COLORMINDISTANCE } from "../../configuration/constants.js"; import { colorCalculateDistance } from "./colorCalculateDistance.js"; import CssCustomProperties from "./CssCustomProperties.js"; var getEnabledColorsFromPaletteCache = new Map(); export function getEnabledColorsFromPalette(_a) { var _b = _a.includePaletteGroup, includePaletteGroup = _b === void 0 ? ["layout"] : _b, _c = _a.includeColorWeight, includeColorWeight = _c === void 0 ? [100, 300, 500, 700, 900] : _c, // TODO (planned for later): includeMixedColors = false, _d = _a.minimalColorDistance, // TODO (planned for later): includeMixedColors = false, minimalColorDistance = _d === void 0 ? COLORMINDISTANCE : _d; var configId = JSON.stringify({ includePaletteGroup: includePaletteGroup, includeColorWeight: includeColorWeight, }); if (getEnabledColorsFromPaletteCache.has(configId)) { return getEnabledColorsFromPaletteCache.get(configId); } var colorsFromPalette = new CssCustomProperties({ selectorText: ":root", filterName: function (name) { if (!name.includes("--".concat(eccgui, "-color-palette-"))) { // only allow custom properties created for the palette return false; } // test for correct group and weight of the palette color var tint = name.substring("--".concat(eccgui, "-color-palette-").length).split("-"); var group = tint[0]; var weight = parseInt(tint[2], 10); return includePaletteGroup.includes(group) && includeColorWeight.includes(weight); }, removeDashPrefix: false, returnObject: true, }).customProperties(); var colorsFromPaletteValues = Object.values(colorsFromPalette); var colorsFromPaletteWithEnoughDistance = minimalColorDistance > 0 ? colorsFromPaletteValues.reduce(function (enoughDistance, color) { if (enoughDistance.includes(color)) { return enoughDistance.filter(function (checkColor) { var distance = colorCalculateDistance({ color1: color, color2: checkColor }); return checkColor === color || (distance && minimalColorDistance <= distance); }); } else { return enoughDistance; } }, colorsFromPaletteValues) : colorsFromPaletteValues; getEnabledColorsFromPaletteCache.set(configId, colorsFromPaletteWithEnoughDistance.map(function (color) { return Color(color); })); return getEnabledColorsFromPaletteCache.get(configId); } function getColorcode(text) { try { return Color(text); } catch (_a) { return false; } } /** * Map a text string to a color. * It always returns the same color for a text as long as the options stay the same. * It returns `false` in case there are no colors defined to chose from. */ export function textToColorHash(_a) { var text = _a.text, _b = _a.options, options = _b === void 0 ? { enabledColors: getEnabledColorsFromPalette({}), returnValidColorsDirectly: false, } : _b; var color = getColorcode(text); if (options.returnValidColorsDirectly && color) { // return color code for text because it was a valid color string return color.hex().toString(); } if (!color) { color = getColorcode(stringToHexColorHash(text)); } if (options.enabledColors === "all" && color) { // all colors are allowed as return value return color.hex().toString(); } var enabledColors = []; if (Array.isArray(options.enabledColors)) { enabledColors = options.enabledColors; } else { enabledColors = getEnabledColorsFromPalette(options.enabledColors); } if (enabledColors.length === 0) { // eslint-disable-next-line no-console console.warn("textToColorHash functionaliy need enabledColors list with at least 1 color."); return false; } return nearestColorNeighbour(color, enabledColors) .hex() .toString(); } function stringToIntegerHash(inputString) { /* this function is idempotend, meaning it retrieves the same result for the same input no matter how many times it's called */ // Convert the string to a hash code var hashCode = 0; for (var i = 0; i < inputString.length; i++) { hashCode = (hashCode << 5) - hashCode + inputString.charCodeAt(i); hashCode &= hashCode; // Convert to 32bit integer } return hashCode; } function integerToHexColor(number) { // Convert the hash code to a positive number (32unsigned) var hash = Math.abs(number + Math.pow(31, 2)); // Convert the number to a hex color (excluding white) var hexColor = "#" + (hash % 0xffffff).toString(16).padStart(6, "0"); return hexColor; } function stringToHexColorHash(inputString) { var integerHash = stringToIntegerHash(inputString); return integerToHexColor(integerHash); } function nearestColorNeighbour(color, enabledColors) { var nearestNeighbour = enabledColors.reduce(function (nearestColor, enabledColorsItem) { var distance = colorCalculateDistance({ color1: color, color2: enabledColorsItem, }); return distance && distance < nearestColor.distance ? { distance: distance, color: enabledColorsItem, } : nearestColor; }, { distance: Number.POSITIVE_INFINITY, color: enabledColors[0], }); return nearestNeighbour.color; } //# sourceMappingURL=colorHash.js.map