Module: Canis::ColorMap

Included in:
Canis
Defined in:
lib/canis/core/system/colormap.rb

Class Method Summary collapse

Class Method Details

.colorsObject



67
68
69
# File 'lib/canis/core/system/colormap.rb', line 67

def ColorMap.colors
  @@colors
end

.get_color(fgc, bgc = $def_bg_color) ⇒ Object

public returns a color_pair for a given foreground and background color



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/canis/core/system/colormap.rb', line 56

def ColorMap.get_color fgc, bgc=$def_bg_color
  fgc = fgc.to_sym if fgc.is_a? String
  bgc = bgc.to_sym if bgc.is_a? String
  if $color_map.include? [fgc, bgc]
    #$log.debug " get_color found #{fgc} #{@bgc} "
    return $color_map[[fgc, bgc]]
  else
    #$log.debug " get_color NOT found #{fgc} #{@bgc} "
    return ColorMap.install_color fgc, bgc
  end
end

.get_color_const(colorstring) ⇒ Object

2010-09-20 12:22 changed colors from string to symbol private returns a color constant for a human color string



23
24
25
26
27
28
# File 'lib/canis/core/system/colormap.rb', line 23

def ColorMap.get_color_const colorstring
  # added check for fixnum if we go beyond these constants 2011-11-28 
  # e.g. to use full 256 colors
  return colorstring if colorstring.is_a? Fixnum 
  ret = FFI::NCurses.const_get "COLOR_#{colorstring.to_s.upcase}"
end

.get_colors_for_pair(pair) ⇒ Symbol

returns the colors that make up the given pair you may want to find what makes up $bottomcolor and set color and bgcolor with it.

Examples:

color, bgcolor = get_colors_for_pair $datacolor

Parameters:

Returns:

  • (Symbol, Symbol)

    foreground and backgrounf color



51
52
53
# File 'lib/canis/core/system/colormap.rb', line 51

def ColorMap.get_colors_for_pair pair
  $color_map.invert[pair]
end

.install_color(fgc, bgc) ⇒ Object

private creates a new color pair, puts in color map and returns color_pair number



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/canis/core/system/colormap.rb', line 32

def ColorMap.install_color fgc, bgc
  #$log.debug " install_color found #{fgc} #{@bgc} "
  @color_id += 1
  # testing to see, since i get junk after 255 or so
  #@color_id = 255 if @color_id > 255
  fg = ColorMap.get_color_const fgc
  bg = ColorMap.get_color_const bgc
  FFI::NCurses.init_pair(@color_id, fg, bg);
  $color_map[[fgc, bgc]] = @color_id
  return @color_id
end

.is_color?(color) ⇒ Boolean

returns true if color is a valid one, else false

Parameters:

  • color (Symbol)

    such as :black :cyan :yellow

Returns:

  • (Boolean)

    true if valid, else false



73
74
75
76
# File 'lib/canis/core/system/colormap.rb', line 73

def ColorMap.is_color? color
  return true if color.is_a? Fixnum # so we can use 256 colors
  @@colors.include? color.to_sym
end

.reset_color_idObject

reset the color_id to zero so one can create colors from scratch. This is only to be used in the case of a color demo when you don’t want the colors

we originally made, since there seems to be a shortage of slots.


116
117
118
119
# File 'lib/canis/core/system/colormap.rb', line 116

def ColorMap.reset_color_id
  @color_id = 0
  $color_map = {}
end

.setupObject

public setup color map at start of application



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/canis/core/system/colormap.rb', line 80

def ColorMap.setup
  @color_id = 0
  $color_map = {}
  FFI::NCurses.start_color();
  # Initialize few color pairs 
  $def_fg_color ||= :white   # pls set these 2 for your application
  $def_bg_color ||= :black
  #COLORS = [COLOR_BLACK, COLOR_RED, COLOR_GREEN, COLOR_YELLOW, COLOR_BLUE, 
  #     COLOR_MAGENTA, COLOR_CYAN, COLOR_WHITE]
  @@colors = [:black, :red, :green, :yellow, :blue, :magenta, :cyan, :white]

  # make foreground colors
  bg = ColorMap.get_color_const $def_bg_color
  @@colors[0...@@colors.size].each_with_index do |color, i|
    next if color == $def_bg_color # NOTE hope this doesn't do something if you change def_bg
    ColorMap.install_color color, $def_bg_color
  end
  $reversecolor = ColorMap.get_color $def_bg_color, $def_fg_color
  $popupcolor = ColorMap.get_color :cyan, $def_fg_color

  $errorcolor = ColorMap.get_color :white, :red
  #$promptcolor = $selectedcolor = ColorMap.get_color(:yellow, :red)
  $promptcolor = ColorMap.get_color(:yellow, :red)
  $normalcolor = $datacolor = ColorMap.get_color($def_fg_color, $def_bg_color)
  $bottomcolor = $topcolor = ColorMap.get_color(:white, :blue)
  $selectedcolor = $datacolor # since we now use reverse attr in list

  $row_selected_attr = Ncurses::A_REVERSE
  $row_focussed_attr = Ncurses::A_BOLD
  $row_attr          = Ncurses::A_NORMAL

  #    $log.debug " colormap SETUP: #{$datacolor} #{$reversecolor} "
end