Created
August 6, 2013 21:02
-
-
Save b1nary/6168605 to your computer and use it in GitHub Desktop.
Creates text walls which appear as images when select with CSS ::selection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# idea from: http://fichtre.net/yop.html | |
# script by: Roman Pramberger ([email protected]) | |
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