function getContrastColor(hexColor) {
    // Convert hex to RGB first
    const rgb = hexToRgb(hexColor);
    if (!rgb) return '#ffffff';

    // Calculate luminance using the relative luminance formula
    const luminance = (0.299 * rgb.r + 0.587 * rgb.g + 0.114 * rgb.b) / 255;

    // Return black for light colors, white for dark colors
    return luminance > 0.5 ? '#000000' : '#ffffff';
}

function hexToRgb(hex) {
    // Remove the # if present
    hex = hex.replace(/^#/, '');

    // Parse the hex values
    const bigint = parseInt(hex, 16);
    const r = (bigint >> 16) & 255;
    const g = (bigint >> 8) & 255;
    const b = bigint & 255;

    return { r, g, b };
}

function generateColorShades(baseColor) {
    // Convert RGB to hex
    const rgbToHex = (r, g, b) => {
        const toHex = (c) => {
            const hex = c.toString(16);
            return hex.length === 1 ? '0' + hex : hex;
        };
        return `#${toHex(r)}${toHex(g)}${toHex(b)}`;
    };

    // Convert base color to RGB
    const baseRgb = hexToRgb(baseColor);
    if (!baseRgb) return null;

    // Generate shades
    const shades = {
        '100': { ...baseRgb },
        '300': { ...baseRgb },
        '500': { ...baseRgb },
        '700': { ...baseRgb },
        '800': { ...baseRgb },
        '900': { ...baseRgb }
    };

    // Adjust brightness for each shade
    const adjustBrightness = (rgb, factor) => {
        return {
            r: Math.min(255, Math.max(0, Math.round(rgb.r * factor))),
            g: Math.min(255, Math.max(0, Math.round(rgb.g * factor))),
            b: Math.min(255, Math.max(0, Math.round(rgb.b * factor)))
        };
    };

    // Set brightness factors for each shade
    shades['100'] = adjustBrightness(baseRgb, 1.4); // Lightest
    shades['300'] = adjustBrightness(baseRgb, 1.2);
    shades['500'] = baseRgb; // Base color
    shades['700'] = adjustBrightness(baseRgb, 0.8);
    shades['800'] = adjustBrightness(baseRgb, 0.7);
    shades['900'] = adjustBrightness(baseRgb, 0.6); // Darkest

    // Convert all shades back to hex
    const result = Object.fromEntries(
        Object.entries(shades).map(([key, value]) => 
            [key, rgbToHex(value.r, value.g, value.b)]
        )
    );

    // Add contrast colors for each shade
    Object.entries(result).forEach(([key, value]) => {
        result[key + '-contrast'] = getContrastColor(value);
    });

    return result;
}
