2.4 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 6a22d77ddf034bc4e35b1d5a | Challenge 353: Contrast Rating 2 | 29 | challenge-353 |
--description--
Given two relative luminance values and a boolean indicating whether the text is large, return the WCAG contrast rating using the following method:
Calculate the contrast ratio by adding 0.05 to each luminance value, then dividing the lighter one by the darker one. The lighter one will always be the first argument.
Return the rating based on the contrast ratio using the following table:
| Rating | Normal Text | Large Text |
|---|---|---|
"AAA" |
7.0+ | 4.5+ |
"AA" |
4.5+ | 3.0+ |
"Fail" |
below 4.5 | below 3.0 |
--hints--
get_contrast_rating(1.0, 0.0, False) should return "AAA".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_contrast_rating(1.0, 0.0, False), "AAA")`)
}})
get_contrast_rating(0.9015, 0.1364, False) should return "AA".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_contrast_rating(0.9015, 0.1364, False), "AA")`)
}})
get_contrast_rating(0.8965, 0.1628, False) should return "Fail".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_contrast_rating(0.8965, 0.1628, False), "Fail")`)
}})
get_contrast_rating(0.7469, 0.0957, True) should return "AAA".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_contrast_rating(0.7469, 0.0957, True), "AAA")`)
}})
get_contrast_rating(0.7489, 0.2018, True) should return "AA".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_contrast_rating(0.7489, 0.2018, True), "AA")`)
}})
get_contrast_rating(0.6571, 0.1974, True) should return "Fail".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_contrast_rating(0.6571, 0.1974, True), "Fail")`)
}})
--seed--
--seed-contents--
def get_contrast_rating(l1, l2, is_large_text):
return l1
--solutions--
def get_contrast_rating(l1, l2, is_large_text):
ratio = (l1 + 0.05) / (l2 + 0.05)
if is_large_text:
if ratio >= 4.5:
return "AAA"
if ratio >= 3.0:
return "AA"
else:
if ratio >= 7.0:
return "AAA"
if ratio >= 4.5:
return "AA"
return "Fail"