Skip to content

Instantly share code, notes, and snippets.

@pachacamac
Created December 4, 2014 00:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pachacamac/33f8974f04a48e80f0b8 to your computer and use it in GitHub Desktop.
Save pachacamac/33f8974f04a48e80f0b8 to your computer and use it in GitHub Desktop.
text to colo
require 'digest/md5'
# def color_hash(str, opts={})
# ranges = opts[:ranges] || [(64..190),(64..190),(64..190)]
# factors = opts[:factors] || [1.0] * ranges.size
# factors = [factors.to_f] * ranges.size unless factors.is_a?(Array)
# str.bytes.reduce(ranges.each_with_index.map{|e,i| e.size/(i+1)}){ |s,e|
# s.each_with_index.map{|x,i|(x*e)%ranges[i].size+ranges[i].first}
# }.each_with_index.map{|e,i|
# x = (e*factors[i]).to_i
# "%02x" % [[x, 0].max, 255].min
# }.join
# end
$vals = Hash.new(0)
def color_hash(s, opts={})
edge_size = (opts[:edge_percent] || 0.4)
bin_size = 255 * (opts[:bin_size_percent] || 0.033)
p bin_size
colors = opts[:colors] || 3
Digest::MD5.digest(s).bytes[0...colors].map{|e|
i = (((e / bin_size).to_i * bin_size.to_i) * (1.0 - edge_size * 2) + (255 * edge_size)).to_i
$vals[i] += 1
"%02x" % i
}.join
end
x = 3001.times.map{|i| "<div style='background:##{color_hash(i.to_s)};'></div>"}.join
File.write('colors.html',"<style>div{width:25px;height:25px;display:inline-block;}</style>#{x}")
p Hash[$vals.sort_by{|k,v| k}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment