-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcolor.go
89 lines (79 loc) · 3.02 KB
/
color.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// seehuhn.de/go/ncurses - a Go-wrapper for the ncurses library
// Copyright (C) 2018 Jochen Voss <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package ncurses
// #include <ncurses.h>
import "C"
// Color describes the text and background colors supported by
// ncurses.
type Color int
// These constants define default colors, corresponding to color
// values 0, 1, ..., 7. Additional color values in the range 8, ...,
// NumColors()-1 can be used after initializing them with
// Color.Init().
var (
ColorBlack Color = C.COLOR_BLACK
ColorRed Color = C.COLOR_RED
ColorGreen Color = C.COLOR_GREEN
ColorYellow Color = C.COLOR_YELLOW
ColorBlue Color = C.COLOR_BLUE
ColorMagenta Color = C.COLOR_MAGENTA
ColorCyan Color = C.COLOR_CYAN
ColorWhite Color = C.COLOR_WHITE
)
// NumColors returns the maximum number of colors the terminal can
// support.
func NumColors() int {
return int(C.COLORS)
}
// Init changes the definition of a Color. The value color must be in
// the range from 0 to NumColors()-1. The three arguments are RGB
// values (for the amounts of red, green, and blue components) in the
// range from 0 to 1000. When Init() is used, all occurrences of that
// color on the screen immediately change to the new definition.
func (color Color) Init(red, green, blue int) error {
rc := C.init_color(C.short(color),
C.short(red), C.short(green), C.short(blue))
if rc == C.ERR {
return ErrColorFailed
}
return nil
}
// NumColorPairs returns the maximum number of color-pairs the
// terminal can support.
func NumColorPairs() int {
return int(C.COLOR_PAIRS)
}
// A ColorPair represents a combination of foreground and background
// color. The default color-pair corresponds to the value 0.
// Additional color-pairs in the range 1, ..., NumColorPairs()-1 can
// be initialized using ColorPair.Init().
type ColorPair int
// Init changes the definition of a color-pair. The arguments give
// the new foreground color and background color. If the color-pair
// was previously initialized, the screen is refreshed and all
// occurrences of that color-pair are changed to the new definition.
func (pair ColorPair) Init(fg, bg Color) error {
rc := C.init_pair(C.short(pair), C.short(fg), C.short(bg))
if rc == C.ERR {
return ErrColorFailed
}
return nil
}
// AsAttr returns a new video attribute, corresponding to the color
// pair.
func (pair ColorPair) AsAttr() AttrType {
return AttrType(C.COLOR_PAIR(C.int(pair)))
}