Class: TuiTui::Palette

Inherits:
Object
  • Object
show all
Defined in:
lib/tui_tui/palette.rb

Overview

Color math for downgrading 256-color and RGB values to ANSI-16.

Constant Summary collapse

ANSI16 =
[
  [[0, 0, 0], 30],
  [[205, 0, 0], 31],
  [[0, 205, 0], 32],
  [[205, 205, 0], 33],
  [[0, 0, 238], 34],
  [[205, 0, 205], 35],
  [[0, 205, 205], 36],
  [[229, 229, 229], 37],
  [[127, 127, 127], 90],
  [[255, 0, 0], 91],
  [[0, 255, 0], 92],
  [[255, 255, 0], 93],
  [[92, 92, 255], 94],
  [[255, 0, 255], 95],
  [[0, 255, 255], 96],
  [[255, 255, 255], 97]
].freeze

Instance Method Summary collapse

Instance Method Details

#nearest_code(rgb) ⇒ Object



25
26
27
# File 'lib/tui_tui/palette.rb', line 25

def nearest_code(rgb)
  ANSI16.min_by { |color, _code| distance(color, rgb) }.last
end

#rgb_from_256(index) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/tui_tui/palette.rb', line 29

def rgb_from_256(index)
  return ANSI16[index].first if index < 16

  if index <= 231
    i = index - 16
    [cube(i / 36), cube((i % 36) / 6), cube(i % 6)]
  else
    v = 8 + (10 * (index - 232))
    [v, v, v]
  end
end