Class: RETerm::ColorPair

Inherits:
Object
  • Object
show all
Defined in:
lib/reterm/color_pair.rb

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fg, bg, *tags) ⇒ ColorPair

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

A unique id for this color pair will be autogenerated

Parameters:

  • fg (String, Symbol)

    forground color name

  • bg (String, Symbol)

    background color name

  • tags (Array<String, Symbol>)

    array of tags to assign to color pair



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# 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

Instance Attribute Details

#bgObject

Color Identifiers



11
12
13
# File 'lib/reterm/color_pair.rb', line 11

def bg
  @bg
end

#bgcObject

Actual NCurses colors



14
15
16
# File 'lib/reterm/color_pair.rb', line 14

def bgc
  @bgc
end

#fgObject

Color Identifiers



11
12
13
# File 'lib/reterm/color_pair.rb', line 11

def fg
  @fg
end

#fgcObject

Actual NCurses colors



14
15
16
# File 'lib/reterm/color_pair.rb', line 14

def fgc
  @fgc
end

#idObject

Returns the value of attribute id.



7
8
9
# File 'lib/reterm/color_pair.rb', line 7

def id
  @id
end

#tagsObject

Returns the value of attribute tags.



8
9
10
# File 'lib/reterm/color_pair.rb', line 8

def tags
  @tags
end

Class Method Details

.allObject

Return all colors pairs



192
193
194
195
# File 'lib/reterm/color_pair.rb', line 192

def self.all
  @@registry ||= []
  @@registry
end

.builtinObject



21
22
23
24
25
26
27
# 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) ⇒ Object

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

Parameters:

  • color (String, Symbol)

    name of color to change, ex “red”, “blue”, etc

  • r (Integer)

    value to assign to red component

  • g (Integer)

    value to assign to green component

  • b (Integer)

    value to assign to blue component



87
88
89
90
91
92
93
94
95
96
97
98
# 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_bgObject



49
50
51
52
53
54
55
# 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_bkgdObject



29
30
31
# File 'lib/reterm/color_pair.rb', line 29

def self.default_bkgd
  @dbkgd ||= Ncurses::WINDOW.new(1, 1, 1, 1).getbkgd
end

.default_bkgd_charObject



33
34
35
# File 'lib/reterm/color_pair.rb', line 33

def self.default_bkgd_char
  @dbkgdch ||= default_bkgg & Ncurses::A_CHARTEXT
end

.default_bkgd_colorObject



37
38
39
# File 'lib/reterm/color_pair.rb', line 37

def self.default_bkgd_color
  @dbkgdco ||= ((default_bkgd & Ncurses::A_COLOR) >> 8)
end

.default_colorObject



57
58
59
# File 'lib/reterm/color_pair.rb', line 57

def self.default_color
  @dcp ||= register default_fg, default_bg
end

.default_fgObject



41
42
43
44
45
46
47
# 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) ⇒ Object

An alias for #change



101
102
103
# File 'lib/reterm/color_pair.rb', line 101

def self.define(color, r, g, b)
  change(color, r, g, b)
end

.for(tag) ⇒ Object

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



199
200
201
202
# File 'lib/reterm/color_pair.rb', line 199

def self.for(tag)
  @@registry ||= []
  @@registry.select { |cp| cp.tags.include?(tag) }
end

.get(color) ⇒ Object

Return RGB components corresponding to system color

Parameters:

  • color (String, Symbol)

    name of color to return



108
109
110
111
112
113
114
115
116
# 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

.next_colorObject

Alias for reserve



75
76
77
# File 'lib/reterm/color_pair.rb', line 75

def self.next_color
  reserve
end

.register(fg, bg, *tags) ⇒ Object

Create and store a new Color Pair in a static registry



185
186
187
188
189
# 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) ⇒ Object

Reserves and returns block of N colors



62
63
64
65
66
67
68
69
70
71
72
# 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 = {}) ⇒ Object

Temporarily resassign RGB to named color, invoke callback block, and restore to original

Parameters:

  • colors (Hash<String,Symbol,Array<Integer>>) (defaults to: {})

    color assignments to use, mapping of color names to RBG pairs

  • r (Integer)

    value to assign to red component

  • g (Integer)

    value to assign to green component

  • b (Integer)

    value to assign to blue component



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# 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) ⇒ Object

Return Color Pairs found with the given bg color



215
216
217
218
219
220
# 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) ⇒ Object

Return Color Pairs found with the given fg color



206
207
208
209
210
211
# 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

Instance Method Details

#cdk_fmtObject

Return color in CDK format



17
18
19
# File 'lib/reterm/color_pair.rb', line 17

def cdk_fmt
  "</#{@id}>"
end

#format(win) ⇒ Object

Encapsulates window operation in color pair attribute



177
178
179
180
181
# File 'lib/reterm/color_pair.rb', line 177

def format(win)
  win.win.attron(nc)
  yield
  win.win.attroff(nc)
end

#ncObject

Returns ncurses color pair corresponding to this instance



172
173
174
# File 'lib/reterm/color_pair.rb', line 172

def nc
  Ncurses::COLOR_PAIR(@id)
end