马一丁

Improve HTML's Automatic Color Replacement Function

@@ -2733,6 +2733,9 @@ const DEFAULT_CHART_COLORS = [ @@ -2733,6 +2733,9 @@ const DEFAULT_CHART_COLORS = [
2733 '#F39C12', '#D35400', '#27AE60', '#8E44AD' 2733 '#F39C12', '#D35400', '#27AE60', '#8E44AD'
2734 ]; 2734 ];
2735 const CSS_VAR_COLOR_MAP = { 2735 const CSS_VAR_COLOR_MAP = {
  2736 + 'var(--chart-color-green)': '#4BC0C0',
  2737 + 'var(--chart-color-red)': '#FF6384',
  2738 + 'var(--chart-color-blue)': '#36A2EB',
2736 'var(--color-accent)': '#4A90E2', 2739 'var(--color-accent)': '#4A90E2',
2737 'var(--re-accent-color)': '#4A90E2', 2740 'var(--re-accent-color)': '#4A90E2',
2738 'var(--re-accent-color-translucent)': 'rgba(74, 144, 226, 0.08)', 2741 'var(--re-accent-color-translucent)': 'rgba(74, 144, 226, 0.08)',
@@ -2838,18 +2841,24 @@ function normalizeDatasetColors(payload, chartType) { @@ -2838,18 +2841,24 @@ function normalizeDatasetColors(payload, chartType) {
2838 } 2841 }
2839 const type = chartType || 'bar'; 2842 const type = chartType || 'bar';
2840 const needsArrayColors = type === 'pie' || type === 'doughnut' || type === 'polarArea'; 2843 const needsArrayColors = type === 'pie' || type === 'doughnut' || type === 'polarArea';
  2844 + const pickColor = (value, fallback) => {
  2845 + if (Array.isArray(value) && value.length) return value[0];
  2846 + return value || fallback;
  2847 + };
2841 2848
2842 data.datasets.forEach((dataset, idx) => { 2849 data.datasets.forEach((dataset, idx) => {
2843 if (!isPlainObject(dataset)) return; 2850 if (!isPlainObject(dataset)) return;
2844 const paletteColor = normalizeColorToken(DEFAULT_CHART_COLORS[idx % DEFAULT_CHART_COLORS.length]); 2851 const paletteColor = normalizeColorToken(DEFAULT_CHART_COLORS[idx % DEFAULT_CHART_COLORS.length]);
2845 - const baseCandidate = Array.isArray(dataset.borderColor)  
2846 - ? dataset.borderColor[0]  
2847 - : dataset.borderColor || dataset.backgroundColor || dataset.color || paletteColor; 2852 + const borderInput = dataset.borderColor;
  2853 + const backgroundInput = dataset.backgroundColor;
  2854 + const borderIsArray = Array.isArray(borderInput);
  2855 + const bgIsArray = Array.isArray(backgroundInput);
  2856 + const baseCandidate = pickColor(borderInput, pickColor(backgroundInput, dataset.color || paletteColor));
2848 const liftedBase = liftDarkColor(baseCandidate || paletteColor); 2857 const liftedBase = liftDarkColor(baseCandidate || paletteColor);
2849 2858
2850 if (needsArrayColors) { 2859 if (needsArrayColors) {
2851 const labelCount = Array.isArray(data.labels) ? data.labels.length : 0; 2860 const labelCount = Array.isArray(data.labels) ? data.labels.length : 0;
2852 - const rawColors = Array.isArray(dataset.backgroundColor) ? dataset.backgroundColor : []; 2861 + const rawColors = bgIsArray ? backgroundInput : [];
2853 const dataLength = Array.isArray(dataset.data) ? dataset.data.length : 0; 2862 const dataLength = Array.isArray(dataset.data) ? dataset.data.length : 0;
2854 const total = Math.max(labelCount, rawColors.length, dataLength, 1); 2863 const total = Math.max(labelCount, rawColors.length, dataLength, 1);
2855 const normalizedColors = []; 2864 const normalizedColors = [];
@@ -2863,19 +2872,13 @@ function normalizeDatasetColors(payload, chartType) { @@ -2863,19 +2872,13 @@ function normalizeDatasetColors(payload, chartType) {
2863 return; 2872 return;
2864 } 2873 }
2865 2874
2866 - const borderIsArray = Array.isArray(dataset.borderColor);  
2867 - if (!dataset.borderColor) { 2875 + if (!borderInput) {
2868 dataset.borderColor = liftedBase; 2876 dataset.borderColor = liftedBase;
2869 changes.push(`dataset${idx}: 补全边框色`); 2877 changes.push(`dataset${idx}: 补全边框色`);
2870 } else if (borderIsArray) { 2878 } else if (borderIsArray) {
2871 - dataset.borderColor = dataset.borderColor.map(col => liftDarkColor(col)); 2879 + dataset.borderColor = borderInput.map(col => liftDarkColor(col));
2872 } else { 2880 } else {
2873 - dataset.borderColor = liftDarkColor(dataset.borderColor);  
2874 - }  
2875 -  
2876 - const bgIsArray = Array.isArray(dataset.backgroundColor);  
2877 - if (bgIsArray) {  
2878 - dataset.backgroundColor = dataset.backgroundColor.map(col => liftDarkColor(col)); 2881 + dataset.borderColor = liftDarkColor(borderInput);
2879 } 2882 }
2880 2883
2881 const typeAlpha = type === 'line' 2884 const typeAlpha = type === 'line'
@@ -2890,15 +2893,18 @@ function normalizeDatasetColors(payload, chartType) { @@ -2890,15 +2893,18 @@ function normalizeDatasetColors(payload, chartType) {
2890 2893
2891 if (typeAlpha !== null) { 2894 if (typeAlpha !== null) {
2892 if (bgIsArray && dataset.backgroundColor.length) { 2895 if (bgIsArray && dataset.backgroundColor.length) {
2893 - dataset.backgroundColor = dataset.backgroundColor.map(col => ensureAlpha(col, typeAlpha)); 2896 + dataset.backgroundColor = backgroundInput.map(col => ensureAlpha(liftDarkColor(col), typeAlpha));
2894 } else { 2897 } else {
2895 - dataset.backgroundColor = ensureAlpha(liftedBase, typeAlpha); 2898 + const bgSeed = pickColor(backgroundInput, pickColor(dataset.borderColor, paletteColor));
  2899 + dataset.backgroundColor = ensureAlpha(liftDarkColor(bgSeed), typeAlpha);
2896 } 2900 }
2897 if (dataset.fill || type !== 'line') { 2901 if (dataset.fill || type !== 'line') {
2898 changes.push(`dataset${idx}: 应用淡化填充以避免遮挡`); 2902 changes.push(`dataset${idx}: 应用淡化填充以避免遮挡`);
2899 } 2903 }
2900 } else if (!dataset.backgroundColor) { 2904 } else if (!dataset.backgroundColor) {
2901 dataset.backgroundColor = ensureAlpha(liftedBase, 0.85); 2905 dataset.backgroundColor = ensureAlpha(liftedBase, 0.85);
  2906 + } else if (bgIsArray) {
  2907 + dataset.backgroundColor = backgroundInput.map(col => liftDarkColor(col));
2902 } else if (!bgIsArray) { 2908 } else if (!bgIsArray) {
2903 dataset.backgroundColor = liftDarkColor(dataset.backgroundColor); 2909 dataset.backgroundColor = liftDarkColor(dataset.backgroundColor);
2904 } 2910 }