Created
March 10, 2012 00:28
-
-
Save kevinwuhoo/2009452 to your computer and use it in GitHub Desktop.
Awesome Random Color Generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# http://martin.ankerl.com/2009/12/09/how-to-create-random-colors-programmatically/ | |
# HSV values in [0..1[ | |
# returns [r, g, b] values from 0 to 255 | |
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 | |
# use golden ratio | |
golden_ratio_conjugate = 0.618033988749895 | |
h = rand # use random start value | |
gen_html { | |
h += golden_ratio_conjugate | |
h %= 1 | |
hsv_to_rgb(h, 0.5, 0.95) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment