Skip to content

Instantly share code, notes, and snippets.

@JokerCatz
Created February 24, 2016 16:53
Show Gist options
  • Save JokerCatz/89de435bb99b511feac3 to your computer and use it in GitHub Desktop.
Save JokerCatz/89de435bb99b511feac3 to your computer and use it in GitHub Desktop.
lib_quilt
# -*- coding: utf-8 -*-
##source from https://github.com/swdyh/quilt
require 'digest/sha1'
module Quilt
SALT = ''
PATCHES = [[0,4,24,20,0],[0,4,20,0],[2,24,20,2],[0,2,22,20,0],[2,14,22,10,2],[0,14,24,22,0],[2,24,22,13,11,22,20,2],[0,14,22,0],[6,8,18,16,6],[4,20,10,12,2,4],[0,2,12,10,0],[10,14,22,10],[20,12,24,20],[10,2,12,10],[0,2,10,0],[]]
CENTER_PATCHES = [0,4,8,15]
PATCH_SIZE = 5
SCALE = PATCH_SIZE * 3
BACK_COLOR = 'rgb(255,255,255)'
def self.process(str)
str = Digest::SHA1.digest("#{str}#{SALT}")
code = [str.getbyte(0) << 24, str.getbyte(1) << 16, str.getbyte(2) << 8, str.getbyte(3)].inject(0){|r,i| r | ((i[31] == 1) ? -(i & 0x7fffffff) : i)}
fore_color = 'rgb(%d,%d,%d)' % [(((code >> 16) & 0x01f) << 3),(((code >> 21) & 0x01f) << 3),(((code >> 27) & 0x01f) << 3)]
return %(<svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 #{SCALE} #{SCALE}">#{
draw_patches(fore_color,[[1,1]],CENTER_PATCHES[code & 0x3],0,(((code >> 2) & 0x01) != 0))
}#{
draw_patches(fore_color,[[1,0],[2,1],[1,2],[0,1]],((code >> 10) & 0x0f),((code >> 15) & 0x03),(((code >> 14) & 0x01) != 0))
}#{
draw_patches(fore_color,[[0,0],[2,0],[2,2],[0,2]],((code >> 3) & 0x0f),((code >> 8) & 0x03),(((code >> 7) & 0x01) != 0))
}</svg>)
end
def self.draw_patches(fore_color,list, patch, turn, invert)
return list.map{|i|
temp = draw(fore_color , i[0] * PATCH_SIZE , i[1] * PATCH_SIZE , patch , turn % 4 , invert)
turn += 1
temp
}.join
end
def self.draw(fore_color,x,y,patch,turn,invert)
if invert
back_color = fore_color
fore_color = BACK_COLOR
else
back_color = BACK_COLOR
end
return %(<rect x="#{x}" y="#{y}" width="#{PATCH_SIZE}" height="#{PATCH_SIZE}" fill="#{back_color}" stroke-width="0" stroke="white"/><polygon points="#{
PATCHES[patch % PATCHES.size].map{|pt|
px = (pt % PATCH_SIZE).to_f / (PATCH_SIZE - 1) * PATCH_SIZE
py = (pt / PATCH_SIZE).to_f / (PATCH_SIZE - 1) * PATCH_SIZE
case turn
when 1
px , py = PATCH_SIZE - py , px
when 2
px , py = PATCH_SIZE - px , PATCH_SIZE - py
when 3
px , py = py , PATCH_SIZE - px
end
"#{x + px},#{y + py}"
}.join(' ')
}" stroke-width="0" fill="#{fore_color}"/>)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment