how to see if a rgb color is too light how to see if a rgb color is too light php php

how to see if a rgb color is too light


If contrast is what you are looking for then check out this article.

They show a function like this to choose text color based on any arbitrary color:

function isTooLightYIQ(hexcolor){  var r = parseInt(hexcolor.substr(0,2),16);  var g = parseInt(hexcolor.substr(2,2),16);  var b = parseInt(hexcolor.substr(4,2),16);  var yiq = ((r*299)+(g*587)+(b*114))/1000;  return yiq >= 128;}// usage:element.style.color = isTooLightYIQ('ff0045') ? '#000' : '#fff';

The above function will return true if the color is too light for white text to be readable on top of this color.


'too light' relative to what? Some image? pure white? some shade of grey? Easiest method is to just make sure that one of the R,G,B member values is above some threshold, though that won't help if the user selects (128,0,0) and it's being pasted onto an image that's (127,0,0) - the difference is (1,0,0) and would be invisible or at minimum almost impossible to spot on most monitors.


You're not being too specific, but assuming you are using Javascript,

var v = Math.round((r+g+b)/3) and check that it is below a certain threshold?

Also, what form is the color in? RGB? HSL? Hex?