@eccenca/gui-elements
Version:
GUI elements based on other libraries, usable in React application, written in Typescript.
143 lines • 5.86 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getEnabledColorsFromPalette = getEnabledColorsFromPalette;
exports.textToColorHash = textToColorHash;
const color_1 = __importDefault(require("color"));
const constants_1 = require("../../configuration/constants");
const colorCalculateDistance_1 = require("./colorCalculateDistance");
const CssCustomProperties_1 = __importDefault(require("./CssCustomProperties"));
const getEnabledColorsFromPaletteCache = new Map();
function getEnabledColorsFromPalette({ includePaletteGroup = ["layout"], includeColorWeight = [100, 300, 500, 700, 900],
// TODO (planned for later): includeMixedColors = false,
minimalColorDistance = constants_1.COLORMINDISTANCE, }) {
const configId = JSON.stringify({
includePaletteGroup,
includeColorWeight,
});
if (getEnabledColorsFromPaletteCache.has(configId)) {
return getEnabledColorsFromPaletteCache.get(configId);
}
const colorsFromPalette = new CssCustomProperties_1.default({
selectorText: `:root`,
filterName: (name) => {
if (!name.includes(`--${constants_1.CLASSPREFIX}-color-palette-`)) {
// only allow custom properties created for the palette
return false;
}
// test for correct group and weight of the palette color
const tint = name.substring(`--${constants_1.CLASSPREFIX}-color-palette-`.length).split("-");
const group = tint[0];
const weight = parseInt(tint[2], 10);
return includePaletteGroup.includes(group) && includeColorWeight.includes(weight);
},
removeDashPrefix: false,
returnObject: true,
}).customProperties();
const colorsFromPaletteValues = Object.values(colorsFromPalette);
const colorsFromPaletteWithEnoughDistance = minimalColorDistance > 0
? colorsFromPaletteValues.reduce((enoughDistance, color) => {
if (enoughDistance.includes(color)) {
return enoughDistance.filter((checkColor) => {
const distance = (0, colorCalculateDistance_1.colorCalculateDistance)({ color1: color, color2: checkColor });
return checkColor === color || (distance && minimalColorDistance <= distance);
});
}
else {
return enoughDistance;
}
}, colorsFromPaletteValues)
: colorsFromPaletteValues;
getEnabledColorsFromPaletteCache.set(configId, colorsFromPaletteWithEnoughDistance.map((color) => {
return (0, color_1.default)(color);
}));
return getEnabledColorsFromPaletteCache.get(configId);
}
function getColorcode(text) {
try {
return (0, color_1.default)(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.
*/
function textToColorHash({ text, options = {
enabledColors: getEnabledColorsFromPalette({}),
returnValidColorsDirectly: false,
}, }) {
let 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();
}
let 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
let hashCode = 0;
for (let 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)
const hash = Math.abs(number + Math.pow(31, 2));
// Convert the number to a hex color (excluding white)
const hexColor = "#" + (hash % 0xffffff).toString(16).padStart(6, "0");
return hexColor;
}
function stringToHexColorHash(inputString) {
const integerHash = stringToIntegerHash(inputString);
return integerToHexColor(integerHash);
}
function nearestColorNeighbour(color, enabledColors) {
const nearestNeighbour = enabledColors.reduce((nearestColor, enabledColorsItem) => {
const distance = (0, colorCalculateDistance_1.colorCalculateDistance)({
color1: color,
color2: enabledColorsItem,
});
return distance && distance < nearestColor.distance
? {
distance,
color: enabledColorsItem,
}
: nearestColor;
}, {
distance: Number.POSITIVE_INFINITY,
color: enabledColors[0],
});
return nearestNeighbour.color;
}
//# sourceMappingURL=colorHash.js.map