WCAG contrast ratio from hex colours
Your task
Implement the WCAG 2.1 contrast ratio between two colours given as hex strings like "#1A1A1A". For each channel, convert the 0-255 value to sRGB by dividing by 255, then linearise it: if the sRGB value is at most 0.03928 divide by 12.92, otherwise raise ((value + 0.055) / 1.055) to the power 2.4. Relative luminance is 0.2126R + 0.7152G + 0.0722*B on the linearised channels. The ratio is (Lighter + 0.05) / (Darker + 0.05). Return the ratio rounded to 2 decimal places. Accept 3-digit shorthand such as "#abc" and treat the input as case-insensitive.
Examples
contrast_ratio(foreground: str, background: str) -> floatExample 1
Input
contrast_ratio("#000000", "#ffffff")Output
21Example 2
Input
contrast_ratio("#767676", "#ffffff")Output
4.54