THINK FIRST·CODE LATER

← All labs

WCAG contrast checker

Problem

Input lines: #foreground #background (6-digit hex). Compute each colour's relative luminance L = 0.2126 R + 0.7152 G + 0.0722 B, where each channel c (0–1) is linearized as c/12.92 if c ≤ 0.04045, else ((c + 0.055)/1.055)^2.4. The contrast ratio is (L_lighter + 0.05)/(L_darker + 0.05).

Print #777777 on #ffffff: 4.48:1 -> text FAIL, large text pass, graphics pass using the thresholds text 4.5, large text 3.0, graphics 3.0 (pass if ratio ≥ threshold, else FAIL).

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

v6-1-data-1.txt

#000000 #ffffff
#777777 #ffffff
#767676 #ffffff
#ffcc00 #ffffff
#1e3a8a #ffffff
What your program must print for v6-1-data-1.txt
#000000 on #ffffff: 21.00:1 -> text pass, large text pass, graphics pass
#777777 on #ffffff: 4.48:1 -> text FAIL, large text pass, graphics pass
#767676 on #ffffff: 4.54:1 -> text pass, large text pass, graphics pass
#ffcc00 on #ffffff: 1.51:1 -> text FAIL, large text FAIL, graphics FAIL
#1e3a8a on #ffffff: 10.36:1 -> text pass, large text pass, graphics pass

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), in the folder that holds both files:

python my_lab.py < v6-1-data-1.txt

Windows PowerShell (the default terminal of VS Code on Windows) does not understand <:

Get-Content v6-1-data-1.txt | python my_lab.py

PyCharm: Run → Edit Configurations → tick Redirect input from and choose the file.

Jupyter: put this at the top of the cell that holds your program:

import sys
sys.stdin = open("v6-1-data-1.txt", encoding="utf-8")

Write it here, in Jupyter or in your IDE, then paste it. Run it on the sample input yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.