Skip to content

Instantly share code, notes, and snippets.

@forksofpower
Last active March 12, 2020 02:59
Show Gist options
  • Save forksofpower/987787c01fe1e7f897dc78e53adf66b0 to your computer and use it in GitHub Desktop.
Save forksofpower/987787c01fe1e7f897dc78e53adf66b0 to your computer and use it in GitHub Desktop.
Get random color value
def random_color(code="hex")
rgb = []
# generate 3 random color values
3.times {rgb << (rand() * 255).floor}
# Return the rgb array or convert to hex
# if hex, take into account zero padding
if code === "rgb"
color = rgb
else
color = rgb.reduce("#"){|x, y| x + ((y<=16) ? "0" : "") + y.to_s(16)}
end
color
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment