How to calculate contrast color in Python
Using luminance to get contrasting foreground colors for the user-selectable background.Published: Sept. 22, 2022
In my ImpressKit project, users can choose an accent color for their app press kits. While this was quite easy to implement, it posed new challenges. A bunch of UI uses the accent color as a background for text.
This means I need to know if black or white contrasts better with the given color. After some experimentation, I arrived at a simple solution that uses luminance value to determine if I should use white or black foreground.
I am using HEX codes for simplicity. If you have colors in other formats, you probably need to convert to HEX first or get the red, blue, and green values.
Let’s look at the code. Here is my method to get the luminance:
def get_luminance(hex_color): color = hex_color[1:] hex_red = int(color[0:2], base=16) hex_green = int(color[2:4], base=16) hex_blue = int(color[4:6], base=16) return hex_red * 0.2126 + hex_green * 0.7152 + hex_blue * 0.0722
I am validating the input before the
hex_color is passed in to ensure it is in the correct format. The magic numbers in the return statements represent how the human eye sees colors and tries to get them equal weight.
Black or white?
The rest is choosing a luminance threshold that works well.
In my project that looks something like this:
luminance = get_luminance(hex_color=hex_color) if luminance < 140: return "var(--white)" else: return "var(--black)"
I found this to work quite well for most colors.
My previous method calculated contrast by adding up all the RGB components and dividing by 756 (3255). However, that did not work well, particularly for saturated shades of blue.*
Let me know if you have a better method that is also simple and does not require any 3rd party code!