class CooCoo::Drawing::Sixel::Stringer

Constants

HSL
RGB

Public Class Methods

new(options = Hash.new) click to toggle source
# File lib/coo-coo/drawing/sixel.rb, line 46
def initialize(options = Hash.new)
  @max_colors = options.fetch(:max_colors, 256)
  @colors = Array.new(@max_colors)
  @background = options.fetch(:background, 0)
end

Public Instance Methods

begin_sixel() click to toggle source
# File lib/coo-coo/drawing/sixel.rb, line 115
def begin_sixel
  start_sixel + emit_all_colors
end
cr() click to toggle source
# File lib/coo-coo/drawing/sixel.rb, line 123
def cr
  "$"
end
emit_all_colors() click to toggle source
# File lib/coo-coo/drawing/sixel.rb, line 101
def emit_all_colors
  @colors.each.with_index.collect do |c, i|
    emit_color(i, c) if c
  end.join
end
emit_color(n, c = nil) click to toggle source
# File lib/coo-coo/drawing/sixel.rb, line 93
def emit_color(n, c = nil)
  case c
  when RGB then "\##{n.to_i};2;#{c.r.to_i};#{c.g.to_i};#{c.b.to_i}"
  when HSL then "\##{n.to_i};1;#{c.h.to_i};#{c.s.to_i};#{c.l.to_i}"
  else @colors[n] && emit_color(n, @colors[n])
  end
end
finish_sixel() click to toggle source
# File lib/coo-coo/drawing/sixel.rb, line 119
def finish_sixel
  "\e\\"
end
from_array(a, width, height = nil) click to toggle source
# File lib/coo-coo/drawing/sixel.rb, line 52
def from_array(a, width, height = nil)
  height ||= (a.size / width.to_f).ceil
  max_color = a.max.ceil
  
  (height / 6.0).ceil.times.collect do |y|
    (max_color + 1).times.collect do |c|
      #next if c == 0
      in_color(c) + width.times.collect do |x|
        sixel_line(c, *6.times.collect { |i|
                     a[(y*6+i) * width + x] rescue @background
                   })
      end.join
    end.join(cr)
  end.join(newline)
end
in_color(c) click to toggle source
# File lib/coo-coo/drawing/sixel.rb, line 107
def in_color(c)
  "\##{c}"
end
lf() click to toggle source
# File lib/coo-coo/drawing/sixel.rb, line 127
def lf
  "-\n"
end
move_to(x, y) click to toggle source
# File lib/coo-coo/drawing/sixel.rb, line 79
def move_to(x, y)
  finish_sixel + "\e[#{y.to_i};#{x.to_i}H" + begin_sixel
end
newline() click to toggle source
# File lib/coo-coo/drawing/sixel.rb, line 131
def newline
  cr + lf
end
set_color(c, r, g, b) click to toggle source
# File lib/coo-coo/drawing/sixel.rb, line 83
def set_color(c, r, g, b)
  @colors[c] = RGB.new(r, g, b)
  emit_color(c)
end
set_color_hsl(c, h, s, l) click to toggle source
# File lib/coo-coo/drawing/sixel.rb, line 88
def set_color_hsl(c, h, s, l)
  @colors[c] = HSL.new(h, s, l)
  emit_color(c)
end
sixel_line(c, *pixels) click to toggle source
# File lib/coo-coo/drawing/sixel.rb, line 68
def sixel_line(c, *pixels)
  line = 6.times.inject(0) { |acc, i|
    if pixels[i].to_i == c
      (acc | (1 << i))
    else
      acc
    end
  }
  [ 63 + line ].pack('c')
end
start_sixel() click to toggle source
# File lib/coo-coo/drawing/sixel.rb, line 111
def start_sixel
  "\ePq"
end