Skip to content

Instantly share code, notes, and snippets.

@asivitz
Last active December 19, 2015 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asivitz/6003257 to your computer and use it in GitHub Desktop.
Save asivitz/6003257 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Usage
#
# ./color-schemer.rb
# -> Pulls a random color scheme from colourlovers.com and outputs the values to sass/colors.scss
# The colors are names $color0, $color1, $color2, $color3, $color4
#
# ./color-schemer.rb -s
# -> Shuffles the colors in the existing sass/colors.scss file
#
# In one of your own .scss files, @import "colors"; , and then the use the color names in your palette.
file_name = "sass/colors.scss"
$scramble = ARGV.delete('-s')
if not $scramble.nil?
puts "Scrambling..."
current = File.readlines("sass/colors.scss")
new_lines = []
current.shuffle!
current.each_index do |i|
line = current[i]
new_lines << "$color#{i}: " + line[/(#\w+)/, 1] + ";"
end
IO.write(file_name, new_lines.join("\n"))
else
require 'net/http'
require 'rexml/document'
url = 'http://www.colourlovers.com/api/palettes/random'
# get the XML data as a string
xml_data = Net::HTTP.get_response(URI.parse(url)).body
xml_data.gsub!(/\t/,"")
xml_data.gsub!(/\n/,"")
# extract event information
doc = REXML::Document.new(xml_data)
colors = []
doc.elements.each('palettes/palette/colors/hex') do |ele|
colors << ele.text
end
output = <<HERE
$color0: ##{colors[0 % colors.length]};
$color1: ##{colors[1 % colors.length]};
$color2: ##{colors[2 % colors.length]};
$color3: ##{colors[3 % colors.length]};
$color4: ##{colors[4 % colors.length]};
HERE
IO.write(file_name, output)
doc.elements.each('palettes/palette/url') do |ele|
puts ele.text
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment