Finding dark/light colors


I am trying to find the correct color. For example if I have a dark green color then I need a light green color or if I have a light green color then I need a dark green color. I have a code which tells me if the color is dark or light.

function calcBrightness(red,green,blue) {
    return Math.sqrt(
        red * red * .299 +
        green * green * .587 +
        blue * blue * .114);          

var brightness = calcBrightness(red, green, blue);
var foreColor = (brightness < 130) ? "light" : "green";

I am able to detect if the color is dark or light but how can I get the dark color if the result is light or light color if the foreColor value is green?


Convert RGB color to HSL (Hue, Saturation, Lightness) makes the problem much easier.

You can easily adjust the lightness of your color by changing the L value in HSL.

The algorithm is available here

(from the question Javascript convert HSB/HSV color to RGB accurately)

Answered By – MK Yung

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published