Skip to content

Instantly share code, notes, and snippets.

@b1nary
Created August 6, 2013 21:02
Show Gist options
  • Save b1nary/6168605 to your computer and use it in GitHub Desktop.
Save b1nary/6168605 to your computer and use it in GitHub Desktop.
Creates text walls which appear as images when select with CSS ::selection
#!/usr/bin/env ruby
# idea from: http://fichtre.net/yop.html
# script by: Roman Pramberger (setamagiga@gmail.com)
require 'open-uri'
require 'RMagick'
text = ""
500.times do |i|
text += "SELECT ME - "
end
LINE_LENGTH = 100
TEXT = text#open("http://loripsum.net/api/20/plaintext/verylong", &:read).gsub("\n","").split("")
IMAGE = "/home/roman/Bilder/asd.jpg"
colors = {}
current_text = 0
img = Magick::Image.read(IMAGE).first
w = LINE_LENGTH
h = img.rows/(img.columns/LINE_LENGTH)
img = img.resize(w, h)
_ele = ""
_css = ""
h.times do |ih|
w.times do |iw|
color = img.pixel_color(iw,ih)
cc = colors[{:red => (color.red/257).to_i, :green => (color.green/257).to_i, :blue => (color.blue/257).to_i}]
if cc.nil?
num = colors.size
colors[ {:red => (color.red/257).to_i, :green => (color.green/257).to_i, :blue => (color.blue/257).to_i}] = num
else
num = cc
end
_ele += "<span class='color-#{num}'>#{TEXT[current_text]}</span>"
if (current_text+1) % LINE_LENGTH == 0
_ele += "<br/>"
end
current_text += 1
end
end
colors.each do |k,v|
#_css += ".color-#{v} { color:rgb(#{k[:red]},#{k[:green]},#{k[:blue]}); }
#"
_css += ".color-#{v}::selection { color:rgb(#{k[:red]},#{k[:green]},#{k[:blue]}); background:rgb(#{k[:red]},#{k[:green]},#{k[:blue]}); }
"
_css += ".color-#{v}::-moz-selection { color:rgb(#{k[:red]},#{k[:green]},#{k[:blue]}); background:rgb(#{k[:red]},#{k[:green]},#{k[:blue]}); }
"
end
html = "<!DOCTYPE html>
<html>
<head>
<title>Generated text selection image</title>
<style type='text/css'>
body { font-family:Monospace; }
#{_css}
</style>
</head>
<body>
#{_ele}
</body>
</html>"
File.open("out.html", "w"){ |f| f.write html }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment