How to generate (attractive) colors in code?

module Colors   class Random     # generates a count number of duo of a random color, one bright and one dark     def self.generate_trio(count = 1)         gen = ::Colors::Random.new         golden_ratio_conjugate = 0.618033988749895         h = rand         result = []         count.times do                duo = {}                h += golden_ratio_conjugate                h %= 1 
r, g, b = gen.hsv_to_rgb(h, 0.5, 0.95)
duo[:bright] = gen.rgb_to_hex(r, g, b)
r, g, b = gen.hsv_to_rgb(h, 0.5, 0.80)
duo[:middle] = gen.rgb_to_hex(r, g, b)
r, g, b = gen.hsv_to_rgb(h, 0.5, 0.70)
duo[:dark] = gen.rgb_to_hex(r, g, b)

result << duo
end return result end def hsv_to_rgb(h, s, v) h_i = (h*6).to_i
f = h*6 - h_i
p = v * (1 - s)
q = v * (1 - f*s)
t = v * (1 - (1 - f) * s)
r, g, b = v, t, p if h_i==0
r, g, b = q, v, p if h_i==1
r, g, b = p, v, t if h_i==2
r, g, b = p, q, v if h_i==3
r, g, b = t, p, v if h_i==4
r, g, b = v, p, q if h_i==5
[(r*256).to_i, (g*256).to_i, (b*256).to_i]
end def rgb_to_hex(r, g, b) return sprintf("#%02x%02x%02x", r, g, b) end end end
colors = ::Colors::Random.generate_trio(4) # generate 4 color sets colors.each do |color|        light_variant = color[:bright]        middle_variant = color[:middle]        dark_variant = color[:dark]        # ... each variant comes as a string in an HTML friendly format (#RRGGBB) end

--

--

DevOps-as-a-Service to help developers build, deploy and maintain apps on any Cloud. Sign-up for a free trial by visting: www.cloud66.com

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Cloud 66

Cloud 66

DevOps-as-a-Service to help developers build, deploy and maintain apps on any Cloud. Sign-up for a free trial by visting: www.cloud66.com