Class: AAQ::AAQ
- Inherits:
-
Object
- Object
- AAQ::AAQ
- Defined in:
- lib/aaq.rb
Instance Attribute Summary collapse
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#img ⇒ Object
readonly
Returns the value of attribute img.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
- #convert ⇒ Object
- #encode(data, colors, blank_color) ⇒ Object
-
#initialize(input_img) ⇒ AAQ
constructor
A new instance of AAQ.
- #to_s ⇒ Object
Constructor Details
#initialize(input_img) ⇒ AAQ
Returns a new instance of AAQ.
14 15 16 17 |
# File 'lib/aaq.rb', line 14 def initialize(input_img) org_img = Magick::ImageList.new(input_img) @img, @width, @height = resize(org_img) end |
Instance Attribute Details
#code ⇒ Object (readonly)
Returns the value of attribute code
12 13 14 |
# File 'lib/aaq.rb', line 12 def code @code end |
#data ⇒ Object (readonly)
Returns the value of attribute data
12 13 14 |
# File 'lib/aaq.rb', line 12 def data @data end |
#height ⇒ Object (readonly)
Returns the value of attribute height
12 13 14 |
# File 'lib/aaq.rb', line 12 def height @height end |
#img ⇒ Object (readonly)
Returns the value of attribute img
12 13 14 |
# File 'lib/aaq.rb', line 12 def img @img end |
#width ⇒ Object (readonly)
Returns the value of attribute width
12 13 14 |
# File 'lib/aaq.rb', line 12 def width @width end |
Instance Method Details
#convert ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/aaq.rb', line 19 def convert qtz = quantize colors = Set.new(qtz.flatten).to_a.sort blank_color = remove_blank_color(colors) @data = separate_color(qtz, colors).map do |x| x.join.gsub(/0+|1+/) do |c| "#{c[0] == '0' ? ':' : '_'}#{c.size}" end end @code = encode(@data, colors, blank_color) self end |
#encode(data, colors, blank_color) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/aaq.rb', line 34 def encode(data, colors, blank_color) code = <<~"QUINE" eval $s = %w' b = #{data}.map{ |x| x.gsub(/:[0-9]+|_[0-9]+/){ |c| (c[0] == \":\" ? \"0\" : \"1\") * c[1..-1].to_i }.reverse.to_i(2) }; e = \"eval $s = %w\" << 39 << $s; o = \"\"; j = k = -1; d = \"\" << 39 << \".join\"; #{width * height}.times{ |i| e += (i < e.size) ? "" : $s; c = #{Array.new(data.size) { |j| "b[#{j}][i] == 1 ? #{colors[j]} :" }.join(' ')} #{blank_color}; ARGV.include?(\"--color\") ? o << \"\" << 27 << \"[38;5;%sm\" % c << 27 << \"[48;5;%sm\" % c << (i < 10 ? e[j+=1] : i > #{width * height - 7} ? d[k+=1] : c == #{blank_color} ? 32 : e[j+=1]) << 27 << \"[0m\" : o << (i < 10 ? e[j+=1] : c == #{blank_color} ? 32 : e[j+=1]); o << (i % #{width} == #{width - 1} ? 10 : \"\"); }; ARGV.include?(\"--color\") ? \"\" : o[-7, 6] = d; puts(o)#'.join QUINE code.gsub(/\s/, '') end |
#to_s ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/aaq.rb', line 62 def to_s b = data.map do |x| x.gsub(/:[0-9]+|_[0-9]+/) { |c| (c[0] == ':' ? '0' : '1') * c[1..-1].to_i }.reverse.to_i(2) end str = "eval$s=%w'" k = -1 c = co = @code.gsub("eval$s=%w'", '').gsub("'.join", '') height.times do |h| width.times do |w| i = h * width + w next if i < 10 c += co if i >= c.size str += b.map { |x| x[i] }.include?(1) ? c[k += 1] : ' ' end str += "\n" end str[-7, 6] = '' << 39 << '.join' str end |