def color_contrast_ratio(fore_color, back_color):
    """Calculated the contrast ratio between a foreground color (with optional
    alpha) and a background color.

    Args:
        fore_color: Color in the form of rgb [r,g,b] or rgba [r,g,b,a]
        back_color: Color in the form of rgb [r,g,b] or rgba [r,g,b,a]

    Returns:
        Contrast ratio between the two colors
    """

    def _calculate_luminance(color_code):
        """Helper function to calculate luminance for one channel of rgb"""
        index = float(color_code) / 255

        if index < 0.03928:
            return index / 12.92
        else:
            return ((index + 0.055) / 1.055) ** 2.4

    def _calculate_relative_luminance(rgb):
        """Helper function to calculate luminance for all channels of rgb"""
        return 0.2126 * _calculate_luminance(rgb[0]) + \
               0.7152 * _calculate_luminance(rgb[1]) + \
               0.0722 * _calculate_luminance(rgb[2])

    # find which color is lighter/darker
    light = back_color if sum(back_color[0:3]) >= sum(
        fore_color[0:3]) else fore_color
    dark = back_color if sum(back_color[0:3]) < sum(
        fore_color[0:3]) else fore_color

    # compute contrast ratio
    contrast_ratio = (_calculate_relative_luminance(light) + 0.05) / \
                     (_calculate_relative_luminance(dark) + 0.05)

    # Scale by the transparency of the el
    # contrast_ratio - 1 brings the color contrast range to [0, 20] so
    # that multiplying the contrast ratio by a decimal alpha won't result
    # in a contrast ratio < 1 (which is impossible)
    if len(fore_color) == 4:
        contrast_ratio = float(fore_color[3]) * (contrast_ratio - 1) + 1

    return contrast_ratio