class RETerm::ColorPair

Defines a pair of colors used for foreground / background rendering associated with id and tag list.

Attributes

bg[RW]

Color Identifiers

bgc[RW]

Actual NCurses colors

fg[RW]

Color Identifiers

fgc[RW]

Actual NCurses colors

id[RW]
tags[RW]

Public Class Methods

all() click to toggle source

Return all colors pairs

# File lib/reterm/color_pair.rb, line 192
def self.all
  @@registry ||= []
  @@registry
end
builtin() click to toggle source
# File lib/reterm/color_pair.rb, line 21
def self.builtin
  @builtin ||=  Ncurses.constants.select { |c|
                  c =~ /^COLOR.*/
                }.collect { |c|
                  c.to_s.gsub("COLOR_", "").downcase.intern
                }
end
change(color, r, g, b) click to toggle source

Redefined system RGB color. Color name should be specified as well as new RGB components

@param [String, Symbol] color name of color to change,

ex "red", "blue", etc

@param [Integer] r value to assign to red component @param [Integer] g value to assign to green component @param [Integer] b value to assign to blue component

# File lib/reterm/color_pair.rb, line 87
def self.change(color, r, g, b)
  # XXX shoehorning 256 colors into ncurses 0-1000 scale here,
  # possible explore if alternative solutions are better
  r = r.to_f / 255 * 1000
  g = g.to_f / 255 * 1000
  b = b.to_f / 255 * 1000

  c = builtin.include?(color.to_s.downcase.intern)    ?
      Ncurses.const_get("COLOR_#{color.to_s.upcase}") : color

  Ncurses::init_color c, r, g, b
end
default_bg() click to toggle source
# File lib/reterm/color_pair.rb, line 49
def self.default_bg
  @dbg ||= begin
    f, b = [], []
    Ncurses::pair_content(default_bkgd_color, f, b)
    b.first
  end
end
default_bkgd() click to toggle source
# File lib/reterm/color_pair.rb, line 29
def self.default_bkgd
  @dbkgd ||= Ncurses::WINDOW.new(1, 1, 1, 1).getbkgd
end
default_bkgd_char() click to toggle source
# File lib/reterm/color_pair.rb, line 33
def self.default_bkgd_char
  @dbkgdch ||= default_bkgg & Ncurses::A_CHARTEXT
end
default_bkgd_color() click to toggle source
# File lib/reterm/color_pair.rb, line 37
def self.default_bkgd_color
  @dbkgdco ||= ((default_bkgd & Ncurses::A_COLOR) >> 8)
end
default_color() click to toggle source
# File lib/reterm/color_pair.rb, line 57
def self.default_color
  @dcp ||= register default_fg, default_bg
end
default_fg() click to toggle source
# File lib/reterm/color_pair.rb, line 41
def self.default_fg
  @dfg ||= begin
    f, b = [], []
    Ncurses::pair_content(default_bkgd_color, f, b)
    f.first
  end
end
define(color, r, g, b) click to toggle source

An alias for change

# File lib/reterm/color_pair.rb, line 101
def self.define(color, r, g, b)
  change(color, r, g, b)
end
for(tag) click to toggle source

Return Color Pairs found with the given tag or nil for no matches

# File lib/reterm/color_pair.rb, line 199
def self.for(tag)
  @@registry ||= []
  @@registry.select { |cp| cp.tags.include?(tag) }
end
get(color) click to toggle source

Return RGB components corresponding to system color

@param [String, Symbol] color name of color to return

# File lib/reterm/color_pair.rb, line 108
def self.get(color)
  c = builtin.include?(color.to_s.downcase.intern)    ?
      Ncurses.const_get("COLOR_#{color.to_s.upcase}") : color

  r, g, b = [[],[],[]]
  Ncurses::color_content c, r, g, b

  [r.first, g.first, b.first]
end
new(fg, bg, *tags) click to toggle source

Instantiate a new ColorPair, specifying foreground and background colors as well as any tags.

A unique id for this color pair will be autogenerated

@param [String, Symbol] fg forground color name @param [String, Symbol] bg background color name @param [Array<String, Symbol>] tags array of tags to assign

to color pair
# File lib/reterm/color_pair.rb, line 150
def initialize(fg, bg, *tags)
  @@id ||= 0
  @@id  += 1
  @id    = @@id

  @tags = Set.new(tags)

  # FIXME need to verify input is in valid domain
  # before conveRETermng it to symbol w/ "intern"
  fg = fg.to_s.downcase.intern if fg.is_a?(String) || fg.is_a?(Symbol)
  bg = bg.to_s.downcase.intern if bg.is_a?(String) || bg.is_a?(Symbol)
  @fg, @bg = fg, bg

  fgc = fg.is_a?(Symbol) ? Ncurses.const_get("COLOR_#{fg.to_s.upcase}") : fg
  bgc = bg.is_a?(Symbol) ? Ncurses.const_get("COLOR_#{bg.to_s.upcase}") : bg

  @fgc, @bgc = fgc, bgc

  Ncurses.init_pair(@id, fgc, bgc)
end
next_color() click to toggle source

Alias for reserve

# File lib/reterm/color_pair.rb, line 75
def self.next_color
  reserve
end
register(fg, bg, *tags) click to toggle source

Create and store a new Color Pair in a static registry

# File lib/reterm/color_pair.rb, line 185
def self.register(fg, bg, *tags)
  @@registry ||= []
  @@registry  << new(fg, bg, *tags)
  @@registry.last
end
reserve(n=1) click to toggle source

Reserves and returns block of N colors

# File lib/reterm/color_pair.rb, line 62
def self.reserve(n=1)
  @reserved   ||= []
  @next_color ||= 50

  0.upto(n) {
    @reserved   << @next_color
    @next_color += 1
  }

  @reserved[-n..-1]
end
use(colors={}) { || ... } click to toggle source

Temporarily resassign RGB to named color, invoke callback block, and restore to original @param [Hash<String,Symbol,Array<Integer>>] colors color assignments

to use, mapping of color names to RBG pairs

@param [Integer] r value to assign to red component @param [Integer] g value to assign to green component @param [Integer] b value to assign to blue component

# File lib/reterm/color_pair.rb, line 125
def self.use(colors={})
  orig = {}
  colors.each { |n, rgb|
    orig[n] = get(n)
    change(n, *rgb)
  }

  yield

  colors.each { |n, rgb|
    change(n, *orig[n])
  }

  nil
end
with_bg(color) click to toggle source

Return Color Pairs found with the given bg color

# File lib/reterm/color_pair.rb, line 215
def self.with_bg(color)
  @@registry ||= []
  @@registry.select { |cp|
    color == (color.is_a?(Symbol) ? cp.bg : cp.bgc)
  }
end
with_fg(color) click to toggle source

Return Color Pairs found with the given fg color

# File lib/reterm/color_pair.rb, line 206
def self.with_fg(color)
  @@registry ||= []
  @@registry.select { |cp|
    color == (color.is_a?(Symbol) ? cp.fg : cp.fgc)
  }
end

Public Instance Methods

cdk_fmt() click to toggle source

Return color in CDK format

# File lib/reterm/color_pair.rb, line 17
def cdk_fmt
  "</#{@id}>"
end
format(win) { || ... } click to toggle source

Encapsulates window operation in color pair attribute

# File lib/reterm/color_pair.rb, line 177
def format(win)
  win.win.attron(nc)
  yield
  win.win.attroff(nc)
end
nc() click to toggle source

Returns ncurses color pair corresponding to this instance

# File lib/reterm/color_pair.rb, line 172
def nc
  Ncurses::COLOR_PAIR(@id)
end