Skip to content

feat: support minimum contrast ratios for block text - #10707

Open
mikeharv wants to merge 6 commits into
RaspberryPiFoundation:rc/v14from
mikeharv:colour-contrast
Open

mikeharv wants to merge 6 commits into
RaspberryPiFoundation:rc/v14from
mikeharv:colour-contrast

Conversation

@mikeharv

@mikeharv mikeharv commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

The basics

The details

Resolves

Fixes #1640

Proposed Changes

Changes the text colour to black on blocks that don't pass a minimum contrast threshold. Users whose OS asks for more contrast get some new colours by default; for everyone else nothing changes unless the app raises config.textContrast.

New internal Colour utils

getLuminance, getContrastRatio and getReadableTextColour are added. These are used to measure and compare colours and ultimately choose a readable colour for text, e.g. on very bright blocks.
Calculations were implemented by Claude to reproduce those defined by W3C for WCAG compliance:

New Accessibility Util

Implementation in Core Blockly

#10672 just turns off CSS transitions with a @media rule, but adjusting the colour contrast has to be done in JS, with the resulting colour passed through to CSS. For this it uses --blockly-block-foreground. This name was chosen because I plan to use it for both text and icons (the latter being needed for the lesson authoring work).
BlockSvgs can now choose a different text colour via updateForegroundColour. The background colour is found by looking at the fill of svgPath rather than the block style to account for full-block fields. Disabled blocks are always light gray (#aaa) and handled as well.
To choose a text colour, the utils check the background colour contrast with white. Black is used if white falls short of the contrast ratio indicated by two new config fields and OS setting.
The text and icon colour must be applied after fields are coloured due to how full-block fields work.
Similarly, dropdowns perform these calculations while their menus are open, because the text at that point sits on a different (tertiary) coloured rect.

New config fields

Two new config fields, textContrast and increasedTextContrast were added. The former enforces a default contrast for all users while the latter does the same for users who have asked their OS for increased contrast. The current default textContrast is 1, which means any contrast is allowed. I'd recommend bumping this to 3 with the v14 launch, as this is the WCAG recommendation for large text and graphics. The default increasedTextContrast is 4.5 which is the WCAG minimum contrast ratio for normal-sized text. Applications can override either of these config fields before they inject. To avoid surprising developers and application end-users with suddenly new text colours, these changes are currently opt-in for users who haven't already asked for increased contrast.

Below is a screenshot of various blocks from core that use black text where contrast would otherwise be below 4.5:1.
image

Reason for Changes

Currently, block text is white, even on white or other brightly coloured blocks, which makes text hard to read and is a WCAG violation.

Test Coverage

Unit tests were added for the new colour utils.

A new block_foreground_test.ts was added which check for the expected text color with a variety of blocks (including disabled, full-block field, and shadow) using a 3:1 ratio.

Contrast sliders were added to the Advanced Playground.

Documentation

I verified that the new config values show up locally in docs where expected (reference/variables/config/).

Additional Information

I'd recommend bumping config.textContrast to 3 with the v14 launch, as this is the WCAG recommendation for large text and graphics.

Claude helped draft some of the code, especially the new colour utils and tests. It also identified the right source of truth for background colors and the zelos dropdown use case. Everything was tested thoroughly and all AI generated code was reviewed carefully.

@github-actions github-actions Bot added PR: feature Adds a feature and removed PR: feature Adds a feature labels Nov 20, 2026
@github-actions github-actions Bot added PR: feature Adds a feature and removed PR: feature Adds a feature labels Nov 23, 2026
@github-actions github-actions Bot added PR: feature Adds a feature and removed PR: feature Adds a feature labels Nov 23, 2026
@mikeharv
mikeharv changed the base branch from main to rc/v14 November 23, 2026 19:34
@github-actions github-actions Bot added PR: feature Adds a feature and removed PR: feature Adds a feature labels Nov 23, 2026
@mikeharv
mikeharv marked this pull request as ready for review November 23, 2026 19:34
@mikeharv
mikeharv requested a review from a team as a code owner November 23, 2026 19:34
@mikeharv
mikeharv requested a review from gonfunko November 23, 2026 19:34

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR: feature Adds a feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Label fields should be non-white for white/light colored blocks

2 participants