Module: RNDK::Color
- Defined in:
- lib/rndk/core/color.rb
Overview
The colors that we can print text with.
Internally we call Colors attributes - or attribs.
Usage
First of all, you should call Color#init. If your terminal doesn't support colors (come on, at 2010s?) Colors#has_colors? will tell.
All Widgets that have an attrib
argument can use a Color.
You call them like this:
color = RNDK::Color[:foreground_background]
If you want to use your terminal's current default background/foreground, use:
color = RNDK::Color[:default_background]
color = RNDK::Color[:foreground]
Also, there are special color modifiers. They change how the color appears and can make some interesting effects:
- RNDK::Color[:normal]
- RNDK::Color[:bold]
- RNDK::Color[:reverse]
- RNDK::Color[:underline]
- RNDK::Color[:blink]
- RNDK::Color[:dim]
- RNDK::Color[:invisible]
- RNDK::Color[:standout]
To apply them, "add" to the regular colors with the pipe ('|'):
color = RNDK::Color[:red] | RNDK::Color[:bold]
Examples
wb = Color[:white_black]
gm = Color[:green_magenta]
red = Color[:red]
bl = Color[:default_black]
x = RNDK::Color[:blue_yellow] | RNDK::Color[:reverse] # guess what?
y = RNDK::Color[:cyan] | RNDK::Color[:invisible] # opps
Developer Notes
Color#init creates 80 color pairs. Neat!
Also, to extract attributes from a chtype (mix between characters and ncurses colors) use
chtype & RNDK::Color[:extract]
Constant Summary collapse
- @@colors =
All possible colors on format
:foreground_background
.When
background
is not specified, it's the current terminal's default.They're defined on Color#init.
{}
- @@attributes =
These special attributes exist even if no color were initialized.
They modify the way colors show on the screen.
{ :normal => Ncurses::A_NORMAL, :bold => Ncurses::A_BOLD, :reverse => Ncurses::A_REVERSE, :underline => Ncurses::A_UNDERLINE, :blink => Ncurses::A_BLINK, :dim => Ncurses::A_DIM, :invisible => Ncurses::A_INVIS, :standout => Ncurses::A_STANDOUT, # To extract attributes from a chtype (mix between # characters and ncurses colors) use # `chtype & RNDK::Color[:extract] :extract => Ncurses::A_ATTRIBUTES }
Class Method Summary collapse
-
.[](label) ⇒ Object
Access individual color pairs or attributes.
-
.has_colors? ⇒ Boolean
Tells if the current terminal supports colors.
-
.init ⇒ Object
Start support for colors, initializing all color pairs.
Class Method Details
.[](label) ⇒ Object
Access individual color pairs or attributes.
If colors were not initialized and user request colors, returns white foreground over default background.
172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/rndk/core/color.rb', line 172 def self.[] label if @@attributes.include? label @@attributes[label] elsif @@colors.include? label Ncurses.COLOR_PAIR @@colors[label] else Ncurses.COLOR_PAIR 0 end end |
.has_colors? ⇒ Boolean
Tells if the current terminal supports colors.
Unless you've been living under a rock, this shouldn't be of any concern.
163 164 165 |
# File 'lib/rndk/core/color.rb', line 163 def self.has_colors? Ncurses.has_colors end |
.init ⇒ Object
Start support for colors, initializing all color pairs.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/rndk/core/color.rb', line 94 def self.init return unless self.has_colors? Ncurses.start_color # Will be able to use current terminal's # background color (value -1) Ncurses.use_default_colors # We will initialize 80 color pairs with all # combinations from the current Array. # # They'll have symbols with their names. # For example: # Color[:white_black] # Color[:green_magenta] color = [[Ncurses::COLOR_WHITE, :white], [Ncurses::COLOR_RED, :red], [Ncurses::COLOR_GREEN, :green], [Ncurses::COLOR_YELLOW, :yellow], [Ncurses::COLOR_BLUE, :blue], [Ncurses::COLOR_MAGENTA, :magenta], [Ncurses::COLOR_CYAN, :cyan], [Ncurses::COLOR_BLACK, :black]] limit = if Ncurses.COLORS < 8 then Ncurses.COLORS else 8 end pair = 1 # Create the color pairs (0...limit).each do |fg| (0...limit).each do |bg| Ncurses.init_pair(pair, color[fg][0], color[bg][0]) label = "#{color[fg][1]}_#{color[bg][1]}".to_sym @@colors[label] = pair pair += 1 end end # The color pairs with default background and foreground. # # They'll have symbols with their names and 'default' # where the default color is. # For example: # Color[:default_black] # Color[:magenta] color.each do |bg| Ncurses.init_pair(pair, -1, bg[0]) label = "default_#{bg[1]}".to_sym @@colors[label] = pair pair += 1 end color.each do |fg| Ncurses.init_pair(pair, fg[0], -1) label = "#{fg[1]}".to_sym @@colors[label] = pair pair += 1 end end |