Skip to content

Instantly share code, notes, and snippets.

@hitode909
Created January 5, 2009 14:23
Show Gist options
  • Save hitode909/43410 to your computer and use it in GitHub Desktop.
Save hitode909/43410 to your computer and use it in GitHub Desktop.
#! /usr/bin/ruby
# generate hitode png
# 2009.01.05
# this script needs pureimage
# http://cappuccino.jp/keisuken/
require 'optparse'
require 'pureimage'
module PureImage
class Image
def plot(x, y, c)
red = (c >> 16) & 0xff
green = (c >> 8) & 0xff
blue = c & 0xff
col = get(x, y)
set(x, y, [red, green, blue, col[3]])
end
end
end
class Hitode
include PureImage
WHITE = 0xffffff
BLACK = 0x000000
WINERED = 0x800000
PINK = 0xef8284
def initialize(id)
@id = id
@size = 240
raise "out of order(0 ~ 999)" unless (0..999).include? @id
@image = PureImage::Image.new(16, 16)
self
end
attr_accessor :id
attr_accessor :size
def draw
draw_base
draw_id
self
end
def save(filename)
@outimage = PureImage::Image.new(@size, @size)
0.upto(@size) do |x|
0.upto(@size) do |y|
@outimage.set(x, y, @image.get(x*16/@size, y*16/@size))
end
end
PureImage::PNGIO.new.save(@outimage, filename)
self
end
protected
def draw_base
@image.fill_rect(0, 0, 16, 16, WHITE)
@image.draw_line(7, 0, 9, 0, WINERED)
@image.plot(6, 1, WINERED)
@image.plot(10, 1, WINERED)
@image.plot(5, 2, WINERED)
@image.plot(10, 2, WINERED)
@image.plot(4, 3, WINERED)
@image.plot(10, 3, WINERED)
@image.plot(4, 4, WINERED)
@image.draw_line(11, 4, 14, 4, WINERED)
@image.draw_line(1, 5, 3, 5, WINERED)
@image.plot(15, 5, WINERED)
@image.draw_line(0, 6, 0, 9, WINERED)
@image.draw_line(1, 10, 1, 12, WINERED)
@image.draw_line(0, 13, 0, 14, WINERED)
@image.draw_line(1, 15, 4, 15, WINERED)
@image.plot(5, 14, WINERED)
@image.draw_line(6, 13, 7, 13, WINERED)
@image.plot(8, 14, WINERED)
@image.draw_line(9, 15, 14, 15, WINERED)
@image.plot(15, 14, WINERED)
@image.plot(14, 13, WINERED)
@image.draw_line(13, 10, 13, 12, WINERED)
@image.plot(14, 9, WINERED)
@image.draw_line(15, 5, 15, 8, WINERED)
@image.draw_line(7, 1, 9, 1, PINK)
@image.draw_line(6, 2, 9, 2, PINK)
@image.draw_line(5, 3, 9, 3, PINK)
@image.draw_line(5, 4, 10, 4, PINK)
@image.draw_line(4, 5, 14, 5, PINK)
@image.draw_line(1, 6, 14, 6, PINK)
@image.draw_line(1, 7, 14, 7, PINK)
@image.draw_line(1, 8, 14, 8, PINK)
@image.draw_line(1, 9, 13, 9, PINK)
@image.draw_line(2, 10, 12, 10, PINK)
@image.draw_line(2, 11, 12, 11, PINK)
@image.draw_line(2, 12, 12, 12, PINK)
@image.draw_line(1, 13, 5, 13, PINK)
@image.draw_line(8, 13, 13, 13, PINK)
@image.draw_line(1, 14, 4, 14, PINK)
@image.draw_line(9, 14, 14, 14, PINK)
end
def draw_id
x = 2
y = 6
offset = 4
(@id + 1000).to_s.split('')[1..-1].each do |n|
draw_number(x, y, n)
x += offset
end
end
def draw_number(x, y, n)
case n
when '0'
@image.draw_line(x, y, x+2, y, BLACK)
@image.draw_line(x, y+4, x+2, y+4, BLACK)
@image.draw_line(x, y, x, y+4, BLACK)
@image.draw_line(x+2, y, x+2, y+4, BLACK)
when '1'
@image.draw_line(x+1, y, x+1, y+4, BLACK)
@image.plot(x, y+1, BLACK)
@image.draw_line(x, y+4, x+2, y+4, BLACK)
when '2'
@image.draw_line(x, y, x+2, y, BLACK)
@image.draw_line(x+2, y, x+2, y+2, BLACK)
@image.draw_line(x, y+2, x+2, y+2, BLACK)
@image.draw_line(x, y+2, x, y+4, BLACK)
@image.draw_line(x, y+4, x+2, y+4, BLACK)
when '3'
@image.draw_line(x, y, x+2, y, BLACK)
@image.draw_line(x, y+2, x+2, y+2, BLACK)
@image.draw_line(x, y+4, x+2, y+4, BLACK)
@image.draw_line(x+2, y, x+2, y+4, BLACK)
when '4'
@image.draw_line(x, y+1, x, y+3, BLACK)
@image.draw_line(x+1, y, x+1, y+4, BLACK)
@image.plot(x+2, y+3, BLACK)
when '5'
@image.draw_line(x, y, x+2, y, BLACK)
@image.draw_line(x, y, x, y+2, BLACK)
@image.draw_line(x, y+2, x+2, y+2, BLACK)
@image.draw_line(x+2, y+2, x+2, y+4, BLACK)
@image.draw_line(x, y+4, x+2, y+4, BLACK)
when '6'
@image.draw_line(x, y, x+2, y, BLACK)
@image.draw_line(x, y+2, x+2, y+2, BLACK)
@image.draw_line(x, y+4, x+2, y+4, BLACK)
@image.draw_line(x, y, x, y+4, BLACK)
@image.draw_line(x+2, y+2, x+2, y+4, BLACK)
when '7'
@image.draw_line(x, y, x+2, y, BLACK)
@image.draw_line(x+2, y, x+2, y+4, BLACK)
when '8'
@image.draw_line(x, y, x+2, y, BLACK)
@image.draw_line(x, y+2, x+2, y+2, BLACK)
@image.draw_line(x, y+4, x+2, y+4, BLACK)
@image.draw_line(x, y, x, y+4, BLACK)
@image.draw_line(x+2, y, x+2, y+4, BLACK)
when '9'
@image.draw_line(x, y, x+2, y, BLACK)
@image.draw_line(x, y+4, x+2, y+4, BLACK)
@image.draw_line(x, y, x, y+2, BLACK)
@image.draw_line(x+2, y, x+2, y+4, BLACK)
@image.draw_line(x, y+2, x+2, y+2, BLACK)
end
end
end
if $0 == __FILE__
setting = { }
opt = OptionParser.new
opt.on('-o <file>', '--output <file>'){
|v| setting[:out] = v
}
opt.on('-s <px>', '--size <px>'){
|v| setting[:size] = v.to_i
}
opt.banner += " id"
opt.parse!(ARGV)
if ARGV.size != 1 or not ARGV.first =~ /^\d+$/
puts opt.banner
exit(1)
end
id = ARGV.first.to_i
out = nil
if setting[:out]
out = setting[:out]
else
out = "hitode#{(id+1000).to_s[1..-1]}.png"
end
hitode = Hitode.new(id).draw
hitode.size = setting[:size] if setting[:size]
hitode.save(out)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment