UNPKG

@public-ui/components

Version:

Contains all web components that belong to KoliBri - The accessible HTML-Standard.

101 lines (97 loc) 3.53 kB
/*! * KoliBri - The accessible HTML-Standard */ 'use strict'; var common = require('./common-DPb6NWR4.js'); var contrast = require('./contrast-BdZHneMx.js'); const HEX_REGEX = /^#((\d|[a-f]){8}|(\d|[a-f]){6}|(\d|[a-f]){3,4})$/i; function isHexString(value) { return HEX_REGEX.test(value); } function isColorObjectString(value) { if (value.startsWith('{')) { try { const parsed = JSON.parse(value); if (isValidColorPair(parsed)) return { type: 'ColorPair', value: parsed }; } catch (_a) { return { type: null, value: null }; } } return { type: null, value: null }; } function typeOfColor(value) { if (value) { if (typeof value === 'string') { if (isHexString(value)) return { type: 'string', valid: true, value: value }; else { const colorObject = isColorObjectString(value); if (colorObject.value) return { type: colorObject.type, valid: true, value: colorObject.value }; } } else { const asColorPair = value; if (isValidColorPair(asColorPair)) return { type: 'ColorPair', valid: true, value: asColorPair }; } } return { type: null, valid: false, value: '' }; } function isValidColorPair(value) { return !!(typeof value === 'object' && value && typeof value.backgroundColor === 'string' && typeof value.foregroundColor === 'string'); } function validatorFunction(value) { const valueType = typeOfColor(value); switch (valueType.type) { case null: return false; case 'string': case 'ColorPair': return valueType.valid; } } const validateColor = (component, value, options) => { common.watchValidator(component, '_color', validatorFunction, new Set(['rgb in hex', 'ColorPair']), value, options); }; const handleColorChange = (value) => { let colorContrast; const valueType = typeOfColor(value); switch (valueType.type) { case 'string': colorContrast = contrast.createContrastColorPair(valueType.value); break; case 'ColorPair': { const asColorPair = valueType.value; let foreground = ''; if (typeof asColorPair.foregroundColor === 'string') foreground = asColorPair.foregroundColor; if (!foreground || typeof foreground !== 'string') foreground = '#fff'; colorContrast = contrast.createContrastColorPair({ background: asColorPair.backgroundColor, foreground, }); break; } case null: common.Log.warn(`_color was empty or invalid (${JSON.stringify(value)})`); colorContrast = contrast.createContrastColorPair({ background: '#000', foreground: '#000', }); } if (colorContrast.contrast < 7) { common.a11yHint(`[KolBadge] The contrast of ${colorContrast.contrast} (≥7, AAA) is to low, between the color pair ${colorContrast.background} and ${colorContrast.foreground}.`); } return { backgroundColor: colorContrast.background, foregroundColor: colorContrast.foreground, }; }; exports.handleColorChange = handleColorChange; exports.validateColor = validateColor; //# sourceMappingURL=color-BVcCYLDJ.js.map //# sourceMappingURL=color-BVcCYLDJ.js.map