Class: CooCoo::Drawing::Sixel::Stringer

Inherits:
Object
  • Object
show all
Defined in:
lib/coo-coo/drawing/sixel.rb

Defined Under Namespace

Classes: HSL, RGB

Instance Method Summary collapse

Constructor Details

#initialize(options = Hash.new) ⇒ Stringer

Returns a new instance of Stringer.



46
47
48
49
50
# 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

Instance Method Details

#begin_sixelObject



115
116
117
# File 'lib/coo-coo/drawing/sixel.rb', line 115

def begin_sixel
  start_sixel + emit_all_colors
end

#crObject



123
124
125
# File 'lib/coo-coo/drawing/sixel.rb', line 123

def cr
  "$"
end

#emit_all_colorsObject



101
102
103
104
105
# 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) ⇒ Object



93
94
95
96
97
98
99
# 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_sixelObject



119
120
121
# File 'lib/coo-coo/drawing/sixel.rb', line 119

def finish_sixel
  "\e\\"
end

#from_array(a, width, height = nil) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 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) ⇒ Object



107
108
109
# File 'lib/coo-coo/drawing/sixel.rb', line 107

def in_color(c)
  "\##{c}"
end

#lfObject



127
128
129
# File 'lib/coo-coo/drawing/sixel.rb', line 127

def lf
  "-\n"
end

#move_to(x, y) ⇒ Object



79
80
81
# 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

#newlineObject



131
132
133
# File 'lib/coo-coo/drawing/sixel.rb', line 131

def newline
  cr + lf
end

#set_color(c, r, g, b) ⇒ Object



83
84
85
86
# 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) ⇒ Object



88
89
90
91
# 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) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# 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_sixelObject



111
112
113
# File 'lib/coo-coo/drawing/sixel.rb', line 111

def start_sixel
  "\ePq"
end