Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this possible to detect a colour is a light or dark colour?

Consider these two pink square:

enter image description here

And this:

enter image description here

As you may know, one is lighter and one is darker or more sharp. The problem is, I can tell it by human eyes, but is this possible to use a system way or programme way to detect this information? At least, is this possible to have a value that tell me that colour is more like white or that colour is less like white? (Assume I got the RGB code of that colour.) Thanks.

like image 415
DNB5brims Avatar asked Dec 19 '22 16:12

DNB5brims


1 Answers

Below is the Python code to determine light or dark color. The formulation is based on the HSP value. HSP (Highly Sensitive Poo) equation is from http://alienryderflex.com/hsp.html used to determine whether the color is light or dark.

import math
def isLightOrDark(rgbColor=[0,128,255]):
    [r,g,b]=rgbColor
    hsp = math.sqrt(0.299 * (r * r) + 0.587 * (g * g) + 0.114 * (b * b))
    if (hsp>127.5):
        return 'light'
    else:
        return 'dark'
like image 159
Kardi Teknomo Avatar answered Dec 22 '22 06:12

Kardi Teknomo