Programmatically choose high-contrast colors

前端 未结 10 691
野的像风
野的像风 2020-12-14 07:29

This should be a simple question, but I haven\'t been able to find a way to make it work.

Essentially, I have a silly localhost page that I use in my webdevelopment.

10条回答
  •  [愿得一人]
    2020-12-14 07:56

    You need a difference in brightness for text to be readable, as color vision itself has too low resolution.

    So as an algorithm I'd suggest the following:

    • Pick a random background color.

    • Then decide whether it is a light or a dark color. For example you could check whether the average of the three primary colors is greater or equal 128.

    • For a light color use black text, for a dark one white text.

提交回复
热议问题