Skip to content

Instantly share code, notes, and snippets.

@kivanio
Forked from virtualstaticvoid/color_support.rb
Created June 22, 2014 21:42
Show Gist options
  • Save kivanio/183e5eed8e0f09bdb296 to your computer and use it in GitHub Desktop.
Save kivanio/183e5eed8e0f09bdb296 to your computer and use it in GitHub Desktop.
# 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