DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

View All Colors From A Set Of Stylesheets

10.04.2007
| 4620 views |
  • submit to reddit
        Rake task to grep out colors from a set of CSS stylesheets and display them on a web page. Default configuration works for OS X Safari and a Rails application.

BROWSER = "/Applications/Safari.app/Contents/MacOS/Safari"
CSS_FILES = "#{RAILS_ROOT}/public/stylesheets/**/*.css"

task :colors do
  require "tempfile"
  colors = Dir[CSS_FILES].map(&File.method(:read)).join.scan(/\#[0-9a-f]{3,6}/i).map{|c| c.upcase}.uniq
  Tempfile.open "colors" do |f|
    f.write <<-EOHTML
    <html>
      <head>
        <style type="text/css">
          div { width: 50px; height: 50px; display: inline-block }
        </style>
      </head>
      <body>
        #{colors.map{|clr| 
          "<div style='background: #{clr}'> </div> #{clr} <br />"
        }.join}
      </body>
    </html>
    EOHTML
    system BROWSER, f.path
  end
end