Skip to content

Instantly share code, notes, and snippets.

@UserAd
Created October 27, 2009 13:05
Show Gist options
  • Save UserAd/219547 to your computer and use it in GitHub Desktop.
Save UserAd/219547 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'RMagick'
include Magick
WIDTH = 50
HEIGHT = 26
STEP = 32
image = Image.read("background.png")
data = image[0]
puts "Read image #{data.columns}x#{data.rows}"
css_classes = {}
css_class_name = 'a'
table = []
(1..WIDTH).each do |w|
table[w] = []
(1..HEIGHT).each do |y|
table[w][y] = ''
end
end
(1..WIDTH).each do |x|
(1..HEIGHT).each do |y|
pixel = data.pixel_color(x*STEP,y*STEP)
color_name = pixel.to_color(X11Compliance,false,8,true).to_s
unless css_classes.include? color_name
css_class_name = css_class_name.next
css_classes[color_name] = css_class_name
end
table[x][y] = css_classes[color_name]
end
end
puts "Dumping css file"
css = File.open('index.css','w')
css.puts '#table {width:100%;height:100%;border-collapse:collapse;}'
css_classes.each do |c,v|
css.puts "#table .#{v} {background:#{c}}"
end
css.close
puts "Dumping table file"
index = File.open('index.html','w')
header = <<-END
<html>
<head>
<link rel="stylesheet" type="text/css" href="index.css" />
</head>
<body>
<table id="table">
END
index.puts header
(1..HEIGHT).each do |y|
index.puts " <tr>"
(1..WIDTH).each do |x|
index.puts " <td class=\"#{table[x][y]}\">&nbsp;</td>"
end
index.puts " </tr>"
end
footer = <<-END
</table>
</html>
END
index.puts footer
index.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment