Frontend Development 9 min read

Adaptive Text Color and User‑Customizable Theme Colors in Web Development

This article explores how to make text color automatically adapt to background brightness and how to let users freely customize theme colors while ensuring sufficient contrast, using JavaScript libraries, CSS blend modes, relative color functions, and contrast‑adjustment algorithms.

Rare Earth Juejin Tech Community
Rare Earth Juejin Tech Community
Rare Earth Juejin Tech Community
Adaptive Text Color and User‑Customizable Theme Colors in Web Development

Most apps and web pages do not allow users to fully customize the colors of displayed information, because choosing readable colors without color theory knowledge is difficult. This article tackles two problems: making text color adapt to background color and allowing users to define theme colors while preserving readability.

Text Color Adaptive to Background

When the background has dark shades, black text becomes unreadable. Using the color JavaScript library, the isDark() and isLight() APIs can determine background brightness and switch the text color between white and black.

import Color from 'color'
const BgColors = ['#f87171', '#fef08a', '#042f2e', ...]
export default function Page() {
  return (
{BgColors.map(bg => (
Sample Text
))}
)
}

Another approach uses mix-blend-mode: difference , which computes the absolute difference between element and background colors, but it can produce low‑contrast or visually unappealing results.

CSS Relative Colors

CSS color functions such as rgb() , rgba() , hsl() , hwb() , and lch() support a "from" syntax that allows calculation of inverse or adjusted colors, enabling contrast‑based text color selection.

color: rgb(from var(--bg) calc(255 - r) calc(255 - g) calc(255 - b));

color‑contrast Function

The upcoming color-contrast() function can automatically pick the color with the highest contrast from a list, e.g., color-contrast(#ccc vs #000, #fff) returns #000 . However, it is currently only available in Safari with experimental flags.

Fully User‑Customizable Theme Colors

When users choose arbitrary foreground colors, the contrast with the background may fall below the recommended threshold of 3:1, harming readability. Two solutions are presented:

Prompt Users When Contrast Is Low

Calculate contrast with the color library (e.g., color.contrast(color2) ) and display a warning if it is below 3, letting users adjust their choice.

Automatically Adjust to a Safe Contrast

Iteratively modify the selected color’s lightness until its contrast with the background exceeds the threshold.

function calcLightColor(originColor) {
  const white = Color('#fff')
  let c = Color(originColor)
  while (c.contrast(white) < 3) {
    c = c.lightness(c.lightness() - 1)
  }
  return c.hex()
}

Both methods ensure that the final foreground color maintains adequate readability across light and dark themes.

Conclusion

Although modern CSS is powerful, it still lacks built‑in solutions for color adaptation. The color JavaScript library fills this gap, allowing developers to guarantee a contrast ratio greater than 3 (or a higher custom threshold) and thus preserve UI readability.

frontendJavaScriptaccessibilityCSSThemecolor contrast
Rare Earth Juejin Tech Community
Written by

Rare Earth Juejin Tech Community

Juejin, a tech community that helps developers grow.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.