from PyQt5.QtGui import QColor


def calc_foreground_color(background_color):
    if type(background_color) is QColor:
        background_color = [background_color.red(), background_color.green(), background_color.blue(), ]
        to_qcolor = True
    colors = []
    for c in background_color[:3]:
        c /= 255  # 8bit to sRGB
        if c <= 0.03928:
            c /= 12.92
        else:
            c = ((c + 0.055) / 1.055) ** 2.4
        colors.append(c)
    # Luminance
    L = 0.2126 * colors[0] + 0.7152 * colors[1] + 0.0722 * colors[2]
    foreground_color = [(1 - round(L)) * 255] * 3
    if to_qcolor:
        foreground_color = QColor(*foreground_color)
    return foreground_color
