html - Ruby color generator -


I need to create random colors for more than one item in the list.

(Like picking up children from pickup school, pick dry cleaning and so on)

What is the best way to do this in Ruby and avoid colors that will be difficult to see (like gray , White, and so on) By using RGB you will find difficult times to avoid brown shades, as well as the color "It will be difficult to see"

If you need to be random, you can Can be used to stay away from gray, gray, and black spectra, it means that you set a threshold in the value and saturation parameters (for example, ~ 175 to 255) while the color is randomly selected independently. can go.

  def random_bright_color (threshold = 175) hue = rand (256 - threshold) + threshold saturation = rand (256 - threshold) Threshold value = Rand (256) hsv_to_rgb (color, saturation, value) end  

Where

  def hsv_to_rgb (h, s, v) if s == 0 r = g = b = (v * 2.55). Else h / = 60.0 s / = 100.0 v / = 100.0 i = h.floor f = h - ip = v * (1 - s) q = v * (1 - s * f) t = v * (1 - s * (1 - f)) RGB = case i when 0 then [v, t, p] when 1 then [q, v, p] when 2 [q, v, t] 3 then [p, q, v] when 4 then [t, p, v] other [v, p, que] end and rgb.map {| Color | (Color * 255) .round} Termination  

is ported and the explanation can be found on the same Wikipedia article


However, if you An additional requirement is to random colors different from each other , in fact, the true solution is actually selected from those base colors, and select from that set at random.


Comments