|
201 | 201 | 'com.google.fonts/check/render_own_name', |
202 | 202 | 'com.google.fonts/check/STAT', |
203 | 203 | 'com.google.fonts/check/colorfont_tables', |
| 204 | + 'com.google.fonts/check/color_cpal_brightness', |
204 | 205 | ] |
205 | 206 |
|
206 | 207 | GOOGLEFONTS_PROFILE_CHECKS = \ |
@@ -6248,6 +6249,61 @@ def com_google_fonts_check_colorfont_tables(ttFont): |
6248 | 6249 | else: |
6249 | 6250 | yield PASS, "Looks good!" |
6250 | 6251 |
|
| 6252 | + |
| 6253 | +@check( |
| 6254 | + id = "com.google.fonts/check/color_cpal_brightness", |
| 6255 | + rationale = """ |
| 6256 | + Layers of a COLRv0 font should not be too dark or too bright. When layer colors |
| 6257 | + are set explicitly, they can't be changed and they may turn out illegible |
| 6258 | + against dark or bright backgrounds. |
| 6259 | +
|
| 6260 | + While traditional color-less fonts can be colored in design apps or CSS, a |
| 6261 | + black color definition in a COLRv0 font actually means that that layer will be |
| 6262 | + rendered in black regardless of the background color. This leads to text |
| 6263 | + becoming invisible against a dark background, for instance when using a dark |
| 6264 | + theme in a web browser or operating system. |
| 6265 | +
|
| 6266 | + This check ensures that layer colors are at least 10% bright and at most 90% |
| 6267 | + bright, when not already set to the current color (0xFFFF). |
| 6268 | + """, |
| 6269 | + proposal = 'https://github.com/googlefonts/fontbakery/pull/3908' |
| 6270 | +) |
| 6271 | +def com_google_fonts_check_color_cpal_brightness(config, ttFont): |
| 6272 | + """Color layers should have a minimum brightness""" |
| 6273 | + from fontbakery.utils import pretty_print_list |
| 6274 | + |
| 6275 | + def color_brightness(hex_value): |
| 6276 | + '''Generic color brightness formula''' |
| 6277 | + return (hex_value[0] * 299 + hex_value[1] * 587 + hex_value[2] * 114) / 1000 |
| 6278 | + |
| 6279 | + minimum_brightness = 256 * .1 |
| 6280 | + FOREGROUND_COLOR = 0xFFFF |
| 6281 | + dark_glyphs = [] |
| 6282 | + if 'COLR' in ttFont.keys() and ttFont['COLR'].version == 0: |
| 6283 | + for key in ttFont['COLR'].ColorLayers: |
| 6284 | + for layer in ttFont['COLR'].ColorLayers[key]: |
| 6285 | + # 0xFFFF is the foreground color, ignore |
| 6286 | + if layer.colorID != FOREGROUND_COLOR: |
| 6287 | + hex_value = ttFont["CPAL"].palettes[0][layer.colorID] |
| 6288 | + layer_brightness = color_brightness(hex_value) |
| 6289 | + if (layer_brightness < minimum_brightness |
| 6290 | + or layer_brightness > 256 - minimum_brightness): |
| 6291 | + if key not in dark_glyphs: |
| 6292 | + dark_glyphs.append(key) |
| 6293 | + if dark_glyphs: |
| 6294 | + dark_glyphs = pretty_print_list(config, sorted(dark_glyphs)) |
| 6295 | + yield WARN,\ |
| 6296 | + Message('glyphs-too-dark-or-too-bright', |
| 6297 | + f"The following glyphs have layers that are too bright or" |
| 6298 | + f" too dark: {dark_glyphs}.\n" |
| 6299 | + f"\n" |
| 6300 | + f" To fix this, please either set the color definitions of all" |
| 6301 | + f" layers in question to current color (0xFFFF), or alter" |
| 6302 | + f" the brightness of these layers significantly.") |
| 6303 | + else: |
| 6304 | + yield PASS, "Looks good!" |
| 6305 | + |
| 6306 | + |
6251 | 6307 | @check( |
6252 | 6308 | id = 'com.google.fonts/check/description/noto_has_article', |
6253 | 6309 | conditions = ['is_noto'], |
|
0 commit comments