4

I know this is possible duplicated question. Ruby, Generate a random hex color

My question is slightly different. I need to know, how to generate the random hex light colors only, not the dark.

Community
  • 1
  • 1
Mr. Black
  • 11,692
  • 13
  • 60
  • 85

6 Answers6

7

In this thread colour lumincance is described with a formula of

(0.2126*r) + (0.7152*g) + (0.0722*b)

The same formula for luminance is given in wikipedia (and it is taken from this publication). It reflects the human perception, with green being the most "intensive" and blue the least.

Therefore, you can select r, g, b until the luminance value goes above the division between light and dark (255 to 0). For example:

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 article refers to brightness, being the arithmetic mean of r, g and b. Note that brightness is even more subjective, as a given target luminance can elicit different perceptions of brightness in different contexts (in particular, the surrounding colours can affect your perception).

All in all, it depends on which colours you consider "light".

Community
  • 1
  • 1
Miki
  • 7,052
  • 2
  • 29
  • 39
  • 1
    So, you want to say that #FF0000 is not light? Or #FF7777 for that matter? I don't think the RGB model is suitable for this. – Alin Purcaru May 31 '11 at 12:49
  • 1
    @Alin You are right. That is why the answer started with "depends how you define *light*". I hope now it makes more sense and is a little more precise. – Miki May 31 '11 at 13:03
  • Your answer is "close enough" now. You may want to add some links to (original) sources that define "lightness" using that formula. – Alin Purcaru May 31 '11 at 13:15
3

Just some pointers:

Use HSL and generate the individual values randomly, but keeping L in the interval of your choosing. Then convert to RGB, if needed.

It's a bit harder than generating RGB with all components over a certain value (say 0x7f), but this is the way to go if you want the colors distributed evenly.

Alin Purcaru
  • 43,655
  • 12
  • 77
  • 90
2

-- I found that 128 to 256 gives the 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))
Rits
  • 21
  • 1
1

I modified one of the answers from the linked question (Daniel Spiewak's answer) to come up with something that is pretty flexible in terms of excluding 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 will limit the output to lighter colors, and a lower value will allow darker colors.

siannopollo
  • 1,464
  • 11
  • 24
  • Super easy to use. I modified the floor to 150 to make sure all colors look good as a background for black text. – E L Rayle Feb 25 '22 at 13:41
1

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

color = (0..2).map{"%0x" % (rand * 0x80 + 0x80)}.join
Jonas Elfström
  • 30,834
  • 6
  • 70
  • 106
  • It shows the eight characters (alphanumeric). but, the hex color is six only, right?. irb(main):001:0> color = (0..3).map{"%0x" % (rand * 0x80 + 0x7f)}.join => "e1ad8f97" irb(main):002:0> color = (0..3).map{"%0x" % (rand * 0x80 + 0x7f)}.join => "d28cf5fe" irb(main):003:0> – Mr. Black May 31 '11 at 12:52
0

A really nice solution is provided by the color-generator gem, where you can call:

ColorGenerator.new(saturation: 0.75, lightness: 0.5).create_hex