Skip to content

Instantly share code, notes, and snippets.

@fuse
Created March 4, 2011 10:04
Show Gist options
  • Save fuse/854424 to your computer and use it in GitHub Desktop.
Save fuse/854424 to your computer and use it in GitHub Desktop.
def colors(size)
1.upto(size).inject([]) { |colors, index|
r, g, b = yield
colors << "#%02x%02x%02x" % [r, g, b]
}
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)
case h_i
when 0
r, g, b = v, t, p
when 1
r, g, b = q, v, p
when 2
r, g, b = p, v, t
when 3
r, g, b = p, q, v
when 4
r, g, b = t, p, v
when 5
r, g, b = v, p, q
end
[(r * 256).to_i, (g * 256).to_i, (b * 256).to_i]
end
srand 42
c = colors(5) { hsv_to_rgb(rand, 0.5, 0.30) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment