Module: Branding::ANSI

Defined in:
lib/branding/ansi.rb

Constant Summary collapse

ATTRS =
(0..8).map { |i| "\e[#{i}m".to_sym }.freeze
FGCOLORS =
(0..256).map { |i| "\e[38;5;#{i}m".to_sym }.freeze
BGCOLORS =
(0..256).map { |i| "\e[48;5;#{i}m".to_sym }.freeze
SHADERS =

2580 - 259F

[:"\u2591", :"\u2592", :"\u2593"].freeze

Class Method Summary collapse

Class Method Details

.bg(r, g, b) ⇒ Object



15
16
17
# File 'lib/branding/ansi.rb', line 15

def bg(r, g, b)
  BGCOLORS[rgb_offset(r, g, b)]
end

.clamped(uint32) ⇒ Object

we probably shouldn’t be passing in non-ints



70
71
72
73
74
75
76
77
78
# File 'lib/branding/ansi.rb', line 70

def clamped(uint32)
  return [0, 0, 0] unless uint32.is_a?(Integer)

  r = (uint32 & 0xff000000) >> 24
  g = (uint32 & 0x00ff0000) >> 16
  b = (uint32 & 0x0000ff00) >> 8

  scale_color(r) & scale_color(g) & scale_color(b)
end

.clearObject



23
24
# File 'lib/branding/ansi.rb', line 23

def clear
end

.downObject



29
30
# File 'lib/branding/ansi.rb', line 29

def down
end

.fg(r, g, b) ⇒ Object



11
12
13
# File 'lib/branding/ansi.rb', line 11

def fg(r, g, b)
  FGCOLORS[rgb_offset(r, g, b)]
end

.leftObject



32
33
# File 'lib/branding/ansi.rb', line 32

def left
end

.resetObject



19
20
21
# File 'lib/branding/ansi.rb', line 19

def reset
  ATTRS[0]
end

.restore_cursorObject



42
43
44
# File 'lib/branding/ansi.rb', line 42

def restore_cursor
  :"\e[u"
end

.rgb_offset(r, g, b) ⇒ Object

0x10-0xE7: 6 × 6 × 6 = 216 colors



47
48
49
# File 'lib/branding/ansi.rb', line 47

def rgb_offset(r, g, b)
  16 + (36 * scale_color(r)) + (6 * scale_color(g)) + scale_color(b)
end

.rightObject



35
36
# File 'lib/branding/ansi.rb', line 35

def right
end

.save_cursorObject



38
39
40
# File 'lib/branding/ansi.rb', line 38

def save_cursor
  :"\e[s"
end

.scale_color(uint8) ⇒ Object

scale an 8bit number to 0-5 5*51==255



54
55
56
# File 'lib/branding/ansi.rb', line 54

def scale_color(uint8)
  (uint8 / 51.0).round
end

.uint32_to_rgb(uint32) ⇒ Object

we probably shouldn’t be passing in non-ints



59
60
61
62
63
64
65
66
67
# File 'lib/branding/ansi.rb', line 59

def uint32_to_rgb(uint32)
  return [0, 0, 0] unless uint32.is_a?(Integer)

  r = (uint32 & 0xff000000) >> 24
  g = (uint32 & 0x00ff0000) >> 16
  b = (uint32 & 0x0000ff00) >> 8

  [r, g, b]
end

.upObject



26
27
# File 'lib/branding/ansi.rb', line 26

def up
end