Created
July 15, 2022 22:20
-
-
Save facelessuser/ec0829e0af84ff2227d0a2e1de2662a5 to your computer and use it in GitHub Desktop.
Color Contrast
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from coloraide.contrast import ColorContrast | |
class Weber(ColorContrast): | |
"""Weber contrast.""" | |
NAME = "weber" | |
@classmethod | |
def contrast(cls, color1, color2, **kwargs): | |
"""Contrast.""" | |
lum1 = color1.luminance() | |
lum2 = color2.luminance() | |
if lum1 > lum2: | |
lum2, lum1 = lum1, lum2 | |
if lum1 == 0: | |
return 0 | |
return (lum2 - lum1) / lum1 | |
class Michelson(ColorContrast): | |
"""Michelson contrast.""" | |
NAME = "michelson" | |
@classmethod | |
def contrast(cls, color1, color2, **kwargs): | |
"""Contrast.""" | |
lum1 = color1.luminance() | |
lum2 = color2.luminance() | |
if lum1 > lum2: | |
lum2, lum1 = lum1, lum2 | |
d = lum2 + lum1 | |
if d == 0: | |
return 0 | |
return (lum2 - lum1) / d | |
class Color2(Color): ... | |
Color2.register([Weber, Michelson]) | |
Color2('red').contrast('blue') | |
Color2('red').contrast('blue', method="weber") | |
Color2('red').contrast('blue', method="michelson") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment