I was trying to make a function that switches colors to dark mode, and stumbled across this instead.
function invertColor(color) {
var c = color.levels; // Gets an array of color values
for (var i = 0; i < 3; i++) {
c[i] = 255 - c[i]; // Reverses the color
}
c[3] = color._array[3]; // Preserves transparency if present
return rgb(c[0], c[1], c[2], c[3]);
}
It takes an rgb color as a parameter and returns its high contrast equivalent. So, rgb(255, 100, 0)
(orange) becomes rgb(0, 155, 255)
(light blue).
The usefulness of this is limited, but it could make for a cool feature in a game or chat. You can see a demo of it here.
Edit: found a more efficient way to calculate the inverse of a color, and updated the function to show this change.