Skip to content

Instantly share code, notes, and snippets.

@virtualstaticvoid
Created January 31, 2014 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save virtualstaticvoid/8734157 to your computer and use it in GitHub Desktop.
Save virtualstaticvoid/8734157 to your computer and use it in GitHub Desktop.
Random color generation
# Thanks to http://www.paulirish.com/2009/random-hex-color-code-snippets for the idea
module ColorSupport
# yields a random color
def self.random_color
color_for((rand() * ((0xFFFFFF + 1) << 0)).to_i.to_s(16))
end
# yields a random color based on the given color
# credit: http://stackoverflow.com/a/43235
def self.random_mix_color(base = Color::RGB::Blue)
red = rand(256)
green = rand(256)
blue = rand(256)
# mix in the base color
unless base.nil?
red = (red + base.red) / 2
green = (green + base.green) / 2
blue = (blue + base.blue) / 2
end
Color::RGB.new(red, green, blue).css_rgb
end
# yields a random pastel color
# credit: http://blog.functionalfun.net/2008/07/random-pastel-colour-generator.html
def self.random_pastel_color
red = rand(128) + 62
green = rand(128) + 62
blue = rand(128) + 62
Color::RGB.new(red, green, blue).css_rgb
end
private
def self.color_for(value)
'#' + Array.new(7 - value.length).join('0') + value
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment