📎 Webclip
Generate Contrasting Text For Your Random Background Color
The article explains a simple way to keep text readable on a user-chosen background color. It converts a hexadecimal color to RGB, then uses the W3C YIQ brightness formula to decide whether the contrasting text should be black or white.
It also shows a Vanilla JavaScript demo with a color picker, where the selected hex value updates the page background and the text color. The implementation reuses an open-source color component from DeckDeckGo and notes that the component can be used in modern frameworks or plain JavaScript.
Reading notes#
- The project needs a way to show contrasted text because users can assign their own background colors.
- The solution is based on the Ionic Color Generator implementation.
- The code includes a hex-to-RGB conversion.
- The contrast calculation uses the W3C YIQ brightness formula.
- The brightness result is compared with a threshold to return either black or white text.
- The demo uses a color picker in an HTML page.
- The selected color is applied to the page background and to the text color.
- The
@deckdeckgo/colorweb component is open source. - The component works in Vanilla JavaScript and modern frameworks.
- The component is described as lightweight according to Bundlephobia.
