* get first color from gradient and transform from RGBA to RGB
9015 * @param {string} gradient - gradient 9016 * @returns {Array} - color in RGB format 9018function t_store_getFirstColorFromGradient(gradient) { 9019 var rgbaRegExp = /rgba\(\d+,\d+,\d+,\d+\)/gi; 9020 var rgbComponentsRegExp = /(\d+),(\d+),(\d+)/i; 9022 var gradientColors = gradient.match(rgbaRegExp); 9023 var colorRgba = gradientColors ? gradientColors[0] : null; 9025 var result = rgbComponentsRegExp.exec(colorRgba); 9026 var colorModel = result 9034 return result ? [colorModel.r, colorModel.g, colorModel.b] : null; 9038 * Parse string color to rgb components 9040 * @param {number[]|string} color - the color to parsed 9041 * @returns {number[]|null} - parsed color or `null` if parsing was not successful 9043function t_store_parsecolor(color) { 9044 if (Array.isArray(color) && color.length === 3) return color; 9045 if (!color || typeof color !== 'string') return null; 9047 // Парсинг глобального стиля цвета 9048 const varRegexp = /^var\((--uc-color-color-[a-zA-Z0-9]+),\s*(.+)\)$/; 9049 const varMatch = varRegexp.exec(color); 9050 let colorString = color; 9051 if (varMatch && varMatch[1]) { 9052 const varName = varMatch[1]; 9053 const bodyStyle = window.getComputedStyle(document.body); 9054 colorString = bodyStyle.getPropertyValue(varName) || varMatch[2]; 9057 const rgbMatch = /^rgba?\(([0-9]+),\s*([0-9]+),\s*([0-9]+)(?:,\s*[0-9.%]+)?\)$/i.exec(colorString); 9058 const hexMatch = /^#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})$/i.exec(colorString); 9061 const components = rgbMatch.splice(1).map(comp => parseInt(comp, 10)); 9066 const components = hexMatch.splice(1).map(comp => parseInt(comp, 16)); 9074 * Given a color, return the color that would be most visible against a background 9076 * @param {Array|string} color - the color to convert 9077 * @returns {string} 'black' or 'white' 9079function t_store_luma_rgb(color) { 9080 const DEFAULT_COLOR = 'black'; 9081 const checkColor = colorArray => 9082 0.2126 * colorArray[0] + 0.7152 * colorArray[1] + 0.0722 * colorArray[2] > 128 ? 'black' : 'white'; 9083 const parsedColor = t_store_parsecolor(color); 9084 if (!parsedColor) return DEFAULT_COLOR; 9085 return checkColor(parsedColor); 9089 * Given a color, return the lightness of that color to recolor the cross in the snippet 9091 * @param {string} color - The color to convert to a lightness value 9092 * @returns {number} The average of the red, green, and blue values 9094function t_store_getLightnessColor(color) { 9095 const parsedColor = t_store_parsecolor(color) || [0, 0, 0]; 9096 const normalizedColor = parsedColor.map(comp => comp / 255); 9097 return (Math.max(...normalizedColor) + Math.min(...normalizedColor)) / 2;