Class: RETerm::ColorPair
- Inherits:
-
Object
- Object
- RETerm::ColorPair
- 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
-
#bg ⇒ Object
Color Identifiers.
-
#bgc ⇒ Object
Actual NCurses colors.
-
#fg ⇒ Object
Color Identifiers.
-
#fgc ⇒ Object
Actual NCurses colors.
-
#id ⇒ Object
Returns the value of attribute id.
-
#tags ⇒ Object
Returns the value of attribute tags.
Class Method Summary collapse
-
.all ⇒ Object
Return all colors pairs.
- .builtin ⇒ Object
-
.change(color, r, g, b) ⇒ Object
Redefined system RGB color.
- .default_bg ⇒ Object
- .default_bkgd ⇒ Object
- .default_bkgd_char ⇒ Object
- .default_bkgd_color ⇒ Object
- .default_color ⇒ Object
- .default_fg ⇒ Object
-
.define(color, r, g, b) ⇒ Object
An alias for #change.
-
.for(tag) ⇒ Object
Return Color Pairs found with the given tag or nil for no matches.
-
.get(color) ⇒ Object
Return RGB components corresponding to system color.
-
.next_color ⇒ Object
Alias for reserve.
-
.register(fg, bg, *tags) ⇒ Object
Create and store a new Color Pair in a static registry.
-
.reserve(n = 1) ⇒ Object
Reserves and returns block of N colors.
-
.use(colors = {}) ⇒ Object
Temporarily resassign RGB to named color, invoke callback block, and restore to original.
-
.with_bg(color) ⇒ Object
Return Color Pairs found with the given bg color.
-
.with_fg(color) ⇒ Object
Return Color Pairs found with the given fg color.
Instance Method Summary collapse
-
#cdk_fmt ⇒ Object
Return color in CDK format.
-
#format(win) ⇒ Object
Encapsulates window operation in color pair attribute.
-
#initialize(fg, bg, *tags) ⇒ ColorPair
constructor
Instantiate a new ColorPair, specifying foreground and background colors as well as any tags.
-
#nc ⇒ Object
Returns ncurses color pair corresponding to this instance.
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
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, *) @@id ||= 0 @@id += 1 @id = @@id @tags = Set.new() # 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
#bg ⇒ Object
Color Identifiers
11 12 13 |
# File 'lib/reterm/color_pair.rb', line 11 def bg @bg end |
#bgc ⇒ Object
Actual NCurses colors
14 15 16 |
# File 'lib/reterm/color_pair.rb', line 14 def bgc @bgc end |
#fg ⇒ Object
Color Identifiers
11 12 13 |
# File 'lib/reterm/color_pair.rb', line 11 def fg @fg end |
#fgc ⇒ Object
Actual NCurses colors
14 15 16 |
# File 'lib/reterm/color_pair.rb', line 14 def fgc @fgc end |
#id ⇒ Object
Returns the value of attribute id.
7 8 9 |
# File 'lib/reterm/color_pair.rb', line 7 def id @id end |
#tags ⇒ Object
Returns the value of attribute tags.
8 9 10 |
# File 'lib/reterm/color_pair.rb', line 8 def @tags end |
Class Method Details
.all ⇒ Object
Return all colors pairs
192 193 194 195 |
# File 'lib/reterm/color_pair.rb', line 192 def self.all @@registry ||= [] @@registry end |
.builtin ⇒ Object
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
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_bg ⇒ Object
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_bkgd ⇒ Object
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_char ⇒ Object
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_color ⇒ Object
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_color ⇒ Object
57 58 59 |
# File 'lib/reterm/color_pair.rb', line 57 def self.default_color @dcp ||= register default_fg, default_bg end |
.default_fg ⇒ Object
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..include?(tag) } end |
.get(color) ⇒ Object
Return RGB components corresponding to system color
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_color ⇒ Object
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, *) @@registry ||= [] @@registry << new(fg, bg, *) @@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
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_fmt ⇒ Object
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 |
#nc ⇒ Object
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 |