I've thought about the following for a while already, so now I want to know your opinions, possible solutions, and so on.
I am looking for a plugin or technique that changes a text's color or switches between predefined images/icons depending on the average brightness of the covered pixels of it's parent's background-image or -color.
If the covered area of it's background is rather dark, make the text white or switch the icons.
Additionally, it'd be great if the script would notice if the parent has no defined background-color or -image and then continue to search for the most nearest (from parent element to it's parent element..).
What do you think, know about this idea? Is there something similar out there already? script-examples?
Cheers, J.
This article on 24 ways about Calculating Color Contrast might be of interest to you. Ignore the first set of functions because they're wrong, but the YIQ formula will help you determine whether or not to use a light or dark foreground color.
Once you obtain the element's (or ancestor's) background color, you can use this function from the article to determine a suitable foreground color:
If you are using ES6, convert hex to RBG then can use this:
mix-blend-mode
does the trick:Addition (March 2018): Following, a nice tutorial explaining all different types of modes/implementations: https://css-tricks.com/css-techniques-and-effects-for-knockout-text/
Interesting resources for this:
Here's the W3C algorithm (with JSFiddle demo too):
I've found the BackgroundCheck script to be very useful.
It detects the overal brightness of the background (be it a background image or a color), and applies a class to the assigned text-element (
background--light
orbackground--dark
), dependent on the brightness of the background.It can be applied to still and moving elements.
(Source)
Interesting question. My immediate thought was to invert the color of the background as the text. This involves simply parsing the background and inverting its RGB value.
Something like this: http://jsfiddle.net/2VTnZ/2/