1
0
mirror of https://github.com/gdamore/tcell.git synced 2025-04-24 13:48:51 +08:00
tcell/style.go

75 lines
1.9 KiB
Go
Raw Normal View History

2015-09-26 23:37:54 -07: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
// Color represents a color. We encode it in a 64-bit int for efficiency.
// The coding is (MSB): <16b rsvd><16b attr><16b fgcolor><16b bgcolor>.
// This gives 16bit color options, if it ever becomes truly necessary.
// I can't see a need to get beyond 256 colors.
type Style int64
func NewStyle() Style {
return Style(0)
}
const StyleDefault Style = 0
func (s Style) Foreground(c Color) Style {
2015-09-28 22:28:41 -07:00
return (s &^ Style(0xffff0000)) | (Style(c) << 16)
2015-09-26 23:37:54 -07:00
}
func (s Style) Background(c Color) Style {
return (s &^ (0xffff)) | Style(c)
}
2015-09-28 22:28:41 -07:00
func (s Style) Decompose() (Color, Color, AttrMask) {
2015-09-26 23:37:54 -07:00
return Color((s >> 16) & 0xffff),
Color(s & 0xfffff),
AttrMask((s >> 32) & 0xffff)
}
func (s Style) setAttrs(attrs Style, on bool) Style {
if on {
2015-09-28 22:28:41 -07:00
return s | (attrs << 32)
2015-09-26 23:37:54 -07:00
} else {
2015-09-28 22:28:41 -07:00
return s &^ (attrs << 32)
2015-09-26 23:37:54 -07:00
}
}
// Normal returns the style with all attributes disabled.
func (s Style) Normal() Style {
return s &^ (Style(0xfffff) << 32)
}
func (s Style) Bold(on bool) Style {
return s.setAttrs(Style(AttrBold), on)
}
func (s Style) Blink(on bool) Style {
return s.setAttrs(Style(AttrBlink), on)
}
func (s Style) Dim(on bool) Style {
return s.setAttrs(Style(AttrDim), on)
}
func (s Style) Reverse(on bool) Style {
return s.setAttrs(Style(AttrReverse), on)
}
func (s Style) Underline(on bool) Style {
return s.setAttrs(Style(AttrUnderline), on)
}