문제

Maybe this has already been answered before but i couldn't find it. I want to generate LIGHT COLOR in HEX format for HTML Background in this format :-( #FFFFFF )

I tried writing codes to always pick the first, third and fifth Numeric in the HEX color String to be greater than 9. But that logic really doesn't necessary include all LIGHT colors. For ex. #00F890 is a light color too.

I know the perception of "light color" depends on human to human. I'm very curious if there is a way to write a function like this ? (I'm using perl)

sub random_light_color
{
  my $lightness_level=shift; ##Let's say between 0-100
  #Do some magic
  return $HEX;
  #Based on lightness_level. 0=>White 100=>BLACK
  #Closer $lightness_level to 0, lighter the color 
  #(BUT ALWAYS RANDOM=>Good mix of RGB)
}

Maybe i'm missing something ?

도움이 되었습니까?

해결책

What you are looking for is a way to select colors perceived as "light" by the human eye/brain. To do this you need to work in the HSL color space.

HSL is a way of expressing colors as Hue, Saturation and Lightness, instead of Red, Green and Blue. What you'll need to do is research the algorithm for converting from HSL to RGB (there are lots of resources available on the web, including online converters).

You can cycle through all possible combinations of Hue and Saturation, limiting yourself to colors with lightness above some threshold. Then run those combinations through the conversion algorithm to get the RGB equivalents.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top