THINK FIRST·CODE LATER

← All labs

Is my palette safe for colour-blind readers?

Problem

Simulate colour-vision deficiency and find confusable colour pairs. Input: threshold T, then lines name #hex.

For each colour: linearize sRGB (as in Lab V6.1). To simulate, multiply the linear RGB vector by the Machado et al. (2009) matrix (severity 1.0):

  • deuteranopia: [[0.367322, 0.860646, −0.227968], [0.280085, 0.672501, 0.047413], [−0.011820, 0.042940, 0.968881]]
  • protanopia: [[0.152286, 1.052583, −0.204868], [0.114503, 0.786281, 0.099216], [−0.003882, −0.048116, 1.051998]]

Convert linear RGB (clipped to 0–1) to CIELAB: XYZ = M·rgb with M = [[0.4124, 0.3576, 0.1805], [0.2126, 0.7152, 0.0722], [0.0193, 0.1192, 0.9505]], divide by the D65 white (0.95047, 1.0, 1.08883), f(t) = t^(1/3) if t > (6/29)³ else t/(3(6/29)²) + 4/29, L = 116 f(Y) − 16, a = 500 (f(X) − f(Y)), b = 200 (f(Y) − f(Z)). The colour difference ΔE is the Euclidean distance in Lab.

For every pair (input order) print red-green: normal dE 119.8, deuteranopia dE 7.3 <- confusable — the normal ΔE and the smaller of the two simulated ΔEs with its deficiency name; mark pairs below the threshold. Finish with 1 confusable pair(s) for colour-vision deficiency (dE < 15).

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-2-data-1.txt

threshold 15
red #d62728
green #2ca02c
blue #1f77b4
What your program must print for v6-2-data-1.txt
red-green: normal dE 119.8, deuteranopia dE 7.3  <- confusable
red-blue: normal dE 108.1, protanopia dE 67.8
green-blue: normal dE 102.2, deuteranopia dE 87.3
1 confusable pair(s) for colour-vision deficiency (dE < 15)

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-2-data-1.txt

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

Get-Content v6-2-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-2-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.