Skip to content

Instantly share code, notes, and snippets.

@Nullreff
Created April 10, 2015 02:33
Show Gist options
  • Save Nullreff/6b1577ffe6db76a82425 to your computer and use it in GitHub Desktop.
Save Nullreff/6b1577ffe6db76a82425 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'erb'
COLORS = 256
abort 'You must provite a file name' unless ARGV.length == 1
image = ARGV[0]
abort 'You must provite a valid file' unless File.exist?(image)
puts "Analysing data from #{image}..."
output = `convert '#{image}' -colors #{COLORS} -colorspace HSB -format "%c" histogram:info:`
max_ranking = 0
results = output.split("\n").map do |line|
ranking, color, hue = line.match(/([0-9]+): \(.*?\) (#[A-F0-9]{6}).*?\(([0-9\.]+%)/).captures
ranking = ranking.to_f
max_ranking = ranking if ranking > max_ranking
{ranking: ranking, color: color, hue: hue}
end.compact
bars = results.map do |result|
"<div class=\"bar\" style=\"left: #{result[:hue]}; background-color: #{result[:color]}; height: #{result[:ranking] / max_ranking * 100}%; z-index: #{(max_ranking - result[:ranking]).to_i};\"></div>"
end.join("\n")
@title = "Colors of #{image}"
@bars = bars
html = ERB.new(DATA.read).result
File.write('graph.html', html)
__END__
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title><%= @title %></title>
<style>
body {
padding: 0;
margin: 0;
}
#graph {
position: relative;
width: 99%;
height: 99vh;
margin-top: 1vh;
}
.bar {
position: absolute;
bottom: 0px;
width: 5px;
}
</style>
</head>
<body>
<div id="graph">
<%= @bars %>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment