In this thread, the color lumincance is described by the formula
(0.2126*r) + (0.7152*g) + (0.0722*b)
The same formula for brightness is given in wikipedia (and taken from this publication ). It reflects human perception, with green being the most “intense” and blue.
Therefore, you can choose r, g, b until the brightness value is higher than the division between light and dark (from 255 to 0). For instance:
lum, ary = 0, [] while lum < 128 ary = (1..3).collect {rand(256)} lum = ary[0]*0.2126 + ary[1]*0.7152 + ary[2]*0.0722 end
Another paper relates to brightness, which is the arithmetic mean of r, g, and b. Note that brightness is even more subjective, since a given brightness of the target can cause different perceptions of brightness in different contexts (in particular, the surrounding colors can affect your perception).
In general, it depends on which colors you consider "light."
source share