I have a video downloaded from youtube, where there is present only black or white color. It is obviously compressed, so instead of (255, 255, 255)
or (0, 0, 0)
RGB I got some entries like (128, 128, 128)
I have searched the internet and found this equation Y = 0.2126*R + 0.7152*G + 0.0722*B
However when I coded this in python it sometimes gives me the wrong value. How can I determine by given RGB tuple whether it is closer to white or black?
I have also coded the equation from Formula to determine perceived brightness of RGB color but it still gives me the wrong value(that the color is more black than white)
def rgb_percent(r, g, b):
sR = r
sG = g
sB = b
vR = sR / 255
vG = sG / 255
vB = sB / 255
def sRGBtoLin(colorChannel):
if colorChannel <= 0.04045:
return colorChannel / 12.92
else:
return pow(((colorChannel + 0.055) / 1.055), 2.4)
Rlin = sRGBtoLin(vR)
Glin = sRGBtoLin(vG)
Blin = sRGBtoLin(vB)
y = (0.2126 * Rlin + 0.7152 * Glin + 0.0722 * Blin)
def YtoLstar(Y):
if Y <= (216 / 24389):
return Y * (24389 / 27)
else:
return pow(Y, (1 / 3)) * 116 - 16
return YtoLstar(y)
print(rgb_percent(3, 3,3))