I'm trying to evaluate the darkness of a color chosen by a color picker to see if it's "too black", and if so, set it to white. I thought I could use the first characters of the hex value to pull this off. It's working, but it's switching some legitimately "light" colors too.
I have the following code:
if (lightcolor.substring(0, 3) == "#00" || lightcolor.substring(0, 3) == "#010") {
lightcolor = "#FFFFFF";
color = lightcolor;
}
There must be a more efficient way with hex math to know that a color has gone beyond a certain level of darkness? Like if lightcolor
+ "some hex value" <=
"some hex value" then set it to white
.
I have tinyColor
added, which might be of use for this, but I don't know for sure.