I’m looking for a way, with jQuery ideally, to determine the correct text color, based on the brightness of the background color?
E.g. white background, black text colour. I believe this could be done crudely with adding the HEX value and guesstimating but does anyone know a better way or jQuery way to do this?
You can try inverting the hex value of the color. So
#FFFFFFbecomes#000000and#AAAAAAbecomes#555555. Of course, this method falls through when you have#888888which when inverted, gives you#777777(they don’t contrast as much).This blog post describes another way (they only use black or white for the foreground color, depending on the background color). I’ve translated it into Javascript: