Ruby, generate random hexagon (only light colors)

I know this is a possible duplicate question. Ruby, Create a Random Hexagonal Color

My question is a little different. I need to know how to generate random hexagonal colors, not dark ones.

+2
source share
5 answers

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."

+7
source

Just some pointers:

Use HSL and randomly generate individual values, but keeping L in your chosen interval. Then convert to RGB if necessary.

It's a bit more complicated than generating RGB with all components at a specific value (say, 0x7f), but it's a way to go if you want to evenly distribute colors.

+2
source

- I found that 128 to 256 gives lighter colors

  Dim rand As New Random Dim col As Color col = Color.FromArgb(rand.Next(128, 256), rand.Next(128, 256), rand.Next(128, 256)) 
+2
source

All colors where each of r, g, b is greater than 0x7f

 color = (0..2).map{"%0x" % (rand * 0x80 + 0x80)}.join 
+1
source

I modified one of the answers of a related question ( Daniel Spievak's answer ) to come up with something quite flexible in terms of eliminating darker colors:

 floor = 22 # meaning darkest possible color is #222222 r = (rand(256-floor) + floor).to_s 16 g = (rand(256-floor) + floor).to_s 16 b = (rand(256-floor) + floor).to_s 16 [r,g,b].map {|h| h.rjust 2, '0'}.join 

You can change the floor value to suit your needs. A higher value restricts the output to lighter colors, and a lower value will have darker colors.

0
source

All Articles