Skip to content

Commit ff482ef

Browse files
committed
New check: com.google.fonts/check/color_cpal_brightness
1 parent c89b894 commit ff482ef

4 files changed

Lines changed: 59 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ A more detailed list of changes is available in the corresponding milestones for
77
#### Added to the Google Fonts Profile
88
- **[com.google.fonts/check/colorfont_tables]:** Fonts must have neither or both the tables `COLR` and `SVG`. (issue #3886)
99
- **[com.google.fonts/check/description/noto_has_article]:** Noto fonts must have an ARTICLE.en_us.html file. (issue #3841)
10+
- **[com.google.fonts/check/color_cpal_brightness]:** Warn if COLRv0 layers are colored too dark or too bright instead of foreground color
1011

1112
### BugFixes
1213
- **[com.adobe.fonts/check/varfont/valid_default_instance_nameids]:** The check did not account for nameID 17. (issue #3895)

Lib/fontbakery/profiles/googlefonts.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@
200200
'com.google.fonts/check/render_own_name',
201201
'com.google.fonts/check/STAT',
202202
'com.google.fonts/check/colorfont_tables',
203+
'com.google.fonts/check/color_cpal_brightness',
203204
]
204205

205206
GOOGLEFONTS_PROFILE_CHECKS = \
@@ -6181,6 +6182,50 @@ def com_google_fonts_check_colorfont_tables(ttFont):
61816182
else:
61826183
yield PASS, "Looks good!"
61836184

6185+
@check(
6186+
id = "com.google.fonts/check/color_cpal_brightness",
6187+
rationale = """
6188+
Layers of a COLRv0 font should not be too dark or too bright. When layer colors are
6189+
set explicitly, they can't be changed and they may turn out illegible against dark or bright backgrounds.
6190+
While traditional color-less fonts
6191+
can be colored in design apps or CSS, a black color definition in a COLRv0
6192+
font actually means that that layer will be rendered in black regardless of the
6193+
background color. This leads to text becoming invisible against a dark background,
6194+
for instance when using a dark theme in a web browser or operating system.
6195+
6196+
This check ensures that layer colors are at least 10% bright and at most 90% bright, when not
6197+
already set to the current color (0xFFFF).
6198+
""",
6199+
proposal = 'https://github.com/googlefonts/fontbakery/issues/3886'
6200+
)
6201+
def com_google_fonts_check_color_cpal_brightness(ttFont):
6202+
"""Color layers should have a minimum brightness"""
6203+
SUGGESTED_FIX = ("To fix this, please either set the color definitions of all layers "
6204+
"in question to current color (0xFFFF), "
6205+
"or alter the brightness of these layers significantly.")
6206+
minimum_brightness = 256 * .1
6207+
FOREGROUND_COLOR = 0xFFFF
6208+
# Generic color brightness formula
6209+
def color_brightness(hex):
6210+
return (hex[0] * 299 + hex[1] * 587 + hex[2] * 114) / 1000
6211+
dark_glyphs = []
6212+
if 'COLR' in ttFont.keys() and ttFont['COLR'].version == 0:
6213+
for key in ttFont['COLR'].ColorLayers:
6214+
for layer in ttFont['COLR'].ColorLayers[key]:
6215+
# 0xFFFF is the foreground color, ignore
6216+
if layer.colorID != FOREGROUND_COLOR:
6217+
layer_brightness = color_brightness(ttFont["CPAL"].palettes[0][layer.colorID])
6218+
if layer_brightness < minimum_brightness or layer_brightness > 256 - minimum_brightness:
6219+
if key not in dark_glyphs:
6220+
dark_glyphs.append(key)
6221+
if dark_glyphs:
6222+
yield WARN,\
6223+
Message('glyphs-too-dark-or-too-bright',
6224+
f"The following glyphs have layers that are too bright or too dark: {dark_glyphs}. " + SUGGESTED_FIX)
6225+
6226+
else:
6227+
yield PASS, "Looks good!"
6228+
61846229
@check(
61856230
id = 'com.google.fonts/check/description/noto_has_article',
61866231
conditions = ['is_noto'],
200 KB
Binary file not shown.

tests/profiles/googlefonts_test.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4249,6 +4249,19 @@ def test_check_colorfont_tables():
42494249
assert_PASS(check(ttFont),
42504250
f'with a good font without SVG or COLR tables.')
42514251

4252+
def test_check_color_cpal_brightness():
4253+
"""Color layers should have a minimum brightness"""
4254+
check = CheckTester(googlefonts_profile,
4255+
"com.google.fonts/check/color_cpal_brightness")
4256+
4257+
ttFont = TTFont(TEST_FILE("color_fonts/AmiriQuranColored_too_dark.ttf"))
4258+
assert_results_contain(check(ttFont),
4259+
WARN, 'glyphs-too-dark-or-too-bright',
4260+
'with a colrv0 font with doo dark layers')
4261+
4262+
ttFont = TTFont(TEST_FILE("color_fonts/AmiriQuranColored.ttf"))
4263+
assert_PASS(check(ttFont),
4264+
f'with a colrv0 font with good layer colors')
42524265

42534266
def test_check_noto_has_article():
42544267
"""Noto fonts must have an ARTICLE.en_us.html file"""

0 commit comments

Comments
 (0)