2015-09-27 14:37:54 +08:00
|
|
|
// Copyright 2015 The TCell Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the license at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package tcell
|
|
|
|
|
2015-10-06 06:46:51 +08:00
|
|
|
// Style represents a complete text style, including both foreground
|
|
|
|
// and background color. We encode it in a 64-bit int for efficiency.
|
2015-11-18 23:03:13 +08:00
|
|
|
// The coding is (MSB): <7b flags><1b><24b fgcolor><7b attr><1b><24b bgcolor>.
|
2015-11-04 06:47:31 +08:00
|
|
|
// The <1b> is set true to indicate that the color is an RGB color, rather
|
|
|
|
// than a named index.
|
2015-10-28 05:29:32 +08:00
|
|
|
//
|
|
|
|
// This gives 24bit color options, if it ever becomes truly necessary.
|
2015-10-06 06:46:51 +08:00
|
|
|
// However, applications must not rely on this encoding.
|
|
|
|
//
|
|
|
|
// Note that not all terminals can display all colors or attributes, and
|
|
|
|
// many might have specific incompatibilities between specific attributes
|
|
|
|
// and color combinations.
|
2015-10-08 11:15:33 +08:00
|
|
|
//
|
2015-10-28 05:29:32 +08:00
|
|
|
// The intention is to extend styles to support paletting, in which case
|
|
|
|
// some flag bit(s) would be set, and the foreground and background colors
|
|
|
|
// would be replaced with a palette number and palette index.
|
|
|
|
//
|
2015-10-08 11:15:33 +08:00
|
|
|
// To use Style, just declare a variable of its type.
|
2015-09-27 14:37:54 +08:00
|
|
|
type Style int64
|
|
|
|
|
2015-10-08 11:15:33 +08:00
|
|
|
// StyleDefault represents a default style, based upon the context.
|
|
|
|
// It is the zero value.
|
2015-09-27 14:37:54 +08:00
|
|
|
const StyleDefault Style = 0
|
|
|
|
|
2015-10-28 05:29:32 +08:00
|
|
|
// styleFlags -- used internally for now.
|
|
|
|
const (
|
2015-11-04 06:47:31 +08:00
|
|
|
styleBgSet = 1 << (iota + 57)
|
2015-10-28 05:29:32 +08:00
|
|
|
styleFgSet
|
|
|
|
stylePalette
|
|
|
|
)
|
|
|
|
|
2015-10-06 06:46:51 +08:00
|
|
|
// Foreground returns a new style based on s, with the foreground color set
|
|
|
|
// as requested. ColorDefault can be used to select the global default.
|
2015-09-27 14:37:54 +08:00
|
|
|
func (s Style) Foreground(c Color) Style {
|
2015-10-28 05:29:32 +08:00
|
|
|
if c == ColorDefault {
|
2015-11-04 04:52:42 +08:00
|
|
|
return (s &^ (0x1ffffff00000000 | styleFgSet))
|
2015-10-28 05:29:32 +08:00
|
|
|
}
|
2015-11-04 04:52:42 +08:00
|
|
|
return (s &^ Style(0x1ffffff00000000)) |
|
|
|
|
((Style(c) & 0x1ffffff) << 32) | styleFgSet
|
2015-09-27 14:37:54 +08:00
|
|
|
}
|
|
|
|
|
2015-10-06 06:46:51 +08:00
|
|
|
// Background returns a new style based on s, with the background color set
|
|
|
|
// as requested. ColorDefault can be used to select the global default.
|
2015-09-27 14:37:54 +08:00
|
|
|
func (s Style) Background(c Color) Style {
|
2015-10-28 05:29:32 +08:00
|
|
|
if c == ColorDefault {
|
2015-11-04 04:52:42 +08:00
|
|
|
return (s &^ (0x1ffffff | styleBgSet))
|
2015-10-28 05:29:32 +08:00
|
|
|
}
|
2015-11-04 04:52:42 +08:00
|
|
|
return (s &^ (0x1ffffff)) | (Style(c) & 0x1ffffff) | styleBgSet
|
2015-09-27 14:37:54 +08:00
|
|
|
}
|
|
|
|
|
2015-10-06 06:46:51 +08:00
|
|
|
// Decompose breaks a style up, returning the foreground, background,
|
|
|
|
// and other attributes.
|
|
|
|
func (s Style) Decompose() (fg Color, bg Color, attr AttrMask) {
|
2015-10-28 05:29:32 +08:00
|
|
|
if s&styleFgSet != 0 {
|
2015-11-04 04:52:42 +08:00
|
|
|
fg = Color(s>>32) & 0x1ffffff
|
2015-10-28 05:29:32 +08:00
|
|
|
} else {
|
|
|
|
fg = ColorDefault
|
|
|
|
}
|
|
|
|
if s&styleBgSet != 0 {
|
2015-11-04 04:52:42 +08:00
|
|
|
bg = Color(s & 0x1ffffff)
|
2015-10-28 05:29:32 +08:00
|
|
|
} else {
|
|
|
|
bg = ColorDefault
|
|
|
|
}
|
2015-11-05 09:05:24 +08:00
|
|
|
attr = AttrMask(s) & attrAll
|
2015-10-28 05:29:32 +08:00
|
|
|
|
|
|
|
return fg, bg, attr
|
2015-09-27 14:37:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s Style) setAttrs(attrs Style, on bool) Style {
|
|
|
|
if on {
|
2015-11-04 06:47:31 +08:00
|
|
|
return s | attrs
|
2015-09-27 14:37:54 +08:00
|
|
|
}
|
2015-11-04 06:47:31 +08:00
|
|
|
return s &^ attrs
|
2015-09-27 14:37:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Normal returns the style with all attributes disabled.
|
|
|
|
func (s Style) Normal() Style {
|
2015-11-05 09:05:24 +08:00
|
|
|
return s &^ Style(attrAll)
|
2015-09-27 14:37:54 +08:00
|
|
|
}
|
|
|
|
|
2015-10-06 06:46:51 +08:00
|
|
|
// Bold returns a new style based on s, with the bold attribute set
|
|
|
|
// as requested.
|
2015-09-27 14:37:54 +08:00
|
|
|
func (s Style) Bold(on bool) Style {
|
|
|
|
return s.setAttrs(Style(AttrBold), on)
|
|
|
|
}
|
|
|
|
|
2015-10-06 06:46:51 +08:00
|
|
|
// Blink returns a new style based on s, with the blink attribute set
|
|
|
|
// as requested.
|
2015-09-27 14:37:54 +08:00
|
|
|
func (s Style) Blink(on bool) Style {
|
|
|
|
return s.setAttrs(Style(AttrBlink), on)
|
|
|
|
}
|
|
|
|
|
2015-10-06 06:46:51 +08:00
|
|
|
// Dim returns a new style based on s, with the dim attribute set
|
|
|
|
// as requested.
|
2015-09-27 14:37:54 +08:00
|
|
|
func (s Style) Dim(on bool) Style {
|
|
|
|
return s.setAttrs(Style(AttrDim), on)
|
|
|
|
}
|
|
|
|
|
2015-10-06 06:46:51 +08:00
|
|
|
// Reverse returns a new style based on s, with the reverse attribute set
|
|
|
|
// as requested. (Reverse usually changes the foreground and background
|
|
|
|
// colors.)
|
2015-09-27 14:37:54 +08:00
|
|
|
func (s Style) Reverse(on bool) Style {
|
|
|
|
return s.setAttrs(Style(AttrReverse), on)
|
|
|
|
}
|
|
|
|
|
2015-10-08 11:15:33 +08:00
|
|
|
// Underline returns a new style based on s, with the underline attribute set
|
2015-10-06 06:46:51 +08:00
|
|
|
// as requested.
|
2015-09-27 14:37:54 +08:00
|
|
|
func (s Style) Underline(on bool) Style {
|
|
|
|
return s.setAttrs(Style(AttrUnderline), on)
|
|
|
|
}
|