Skip to content

Commit

Permalink
feat!: use color.Color instead of TerminalColor
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Aug 22, 2024
1 parent 69eab51 commit 1dbb773
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 48 deletions.
3 changes: 2 additions & 1 deletion borders.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lipgloss

import (
"image/color"
"strings"

"github.com/charmbracelet/x/ansi"
Expand Down Expand Up @@ -401,7 +402,7 @@ func renderHorizontalEdge(left, middle, right string, width int) string {
}

// Apply foreground and background styling to a border.
func (s Style) styleBorder(border string, fg, bg TerminalColor) string {
func (s Style) styleBorder(border string, fg, bg color.Color) string {
if fg == noColor && bg == noColor {
return border
}
Expand Down
12 changes: 4 additions & 8 deletions color.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lipgloss

import (
"image/color"
"strconv"

"github.com/charmbracelet/x/ansi"
Expand Down Expand Up @@ -28,11 +29,6 @@ const (
BrightWhite
)

// TerminalColor is a color intended to be rendered in the terminal.
type TerminalColor interface {
ansi.Color
}

var noColor = NoColor{}

// NoColor is used to specify the absence of color styling. When this is active
Expand All @@ -58,8 +54,8 @@ func (n NoColor) RGBA() (r, g, b, a uint32) {
// ansiColor := lipgloss.Color(21)
// hexColor := lipgloss.Color("#0000ff")
// uint32Color := lipgloss.Color(0xff0000)
func Color[T string | int](c T) TerminalColor {
var col TerminalColor = noColor
func Color[T string | int](c T) color.Color {
var col color.Color = noColor
switch c := any(c).(type) {
case string:
if len(c) == 0 {
Expand Down Expand Up @@ -121,7 +117,7 @@ type ANSIColor = ansi.ExtendedColor
// } else {
// fmt.Println("It's light!")
// }
func IsDarkColor(c TerminalColor) bool {
func IsDarkColor(c color.Color) bool {
col, ok := colorful.MakeColor(c)
if !ok {
return true
Expand Down
25 changes: 13 additions & 12 deletions get.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lipgloss

import (
"image/color"
"strings"

"github.com/charmbracelet/x/ansi"
Expand Down Expand Up @@ -49,13 +50,13 @@ func (s Style) GetFaint() bool {

// GetForeground returns the style's foreground color. If no value is set
// NoColor{} is returned.
func (s Style) GetForeground() TerminalColor {
func (s Style) GetForeground() color.Color {
return s.getAsColor(foregroundKey)
}

// GetBackground returns the style's background color. If no value is set
// NoColor{} is returned.
func (s Style) GetBackground() TerminalColor {
func (s Style) GetBackground() color.Color {
return s.getAsColor(backgroundKey)
}

Expand Down Expand Up @@ -241,49 +242,49 @@ func (s Style) GetBorderLeft() bool {

// GetBorderTopForeground returns the style's border top foreground color. If
// no value is set NoColor{} is returned.
func (s Style) GetBorderTopForeground() TerminalColor {
func (s Style) GetBorderTopForeground() color.Color {
return s.getAsColor(borderTopForegroundKey)
}

// GetBorderRightForeground returns the style's border right foreground color.
// If no value is set NoColor{} is returned.
func (s Style) GetBorderRightForeground() TerminalColor {
func (s Style) GetBorderRightForeground() color.Color {
return s.getAsColor(borderRightForegroundKey)
}

// GetBorderBottomForeground returns the style's border bottom foreground
// color. If no value is set NoColor{} is returned.
func (s Style) GetBorderBottomForeground() TerminalColor {
func (s Style) GetBorderBottomForeground() color.Color {
return s.getAsColor(borderBottomForegroundKey)
}

// GetBorderLeftForeground returns the style's border left foreground
// color. If no value is set NoColor{} is returned.
func (s Style) GetBorderLeftForeground() TerminalColor {
func (s Style) GetBorderLeftForeground() color.Color {
return s.getAsColor(borderLeftForegroundKey)
}

// GetBorderTopBackground returns the style's border top background color. If
// no value is set NoColor{} is returned.
func (s Style) GetBorderTopBackground() TerminalColor {
func (s Style) GetBorderTopBackground() color.Color {
return s.getAsColor(borderTopBackgroundKey)
}

// GetBorderRightBackground returns the style's border right background color.
// If no value is set NoColor{} is returned.
func (s Style) GetBorderRightBackground() TerminalColor {
func (s Style) GetBorderRightBackground() color.Color {
return s.getAsColor(borderRightBackgroundKey)
}

// GetBorderBottomBackground returns the style's border bottom background
// color. If no value is set NoColor{} is returned.
func (s Style) GetBorderBottomBackground() TerminalColor {
func (s Style) GetBorderBottomBackground() color.Color {
return s.getAsColor(borderBottomBackgroundKey)
}

// GetBorderLeftBackground returns the style's border left background
// color. If no value is set NoColor{} is returned.
func (s Style) GetBorderLeftBackground() TerminalColor {
func (s Style) GetBorderLeftBackground() color.Color {
return s.getAsColor(borderLeftBackgroundKey)
}

Expand Down Expand Up @@ -426,12 +427,12 @@ func (s Style) getAsBool(k propKey, defaultVal bool) bool {
return s.attrs&int(k) != 0
}

func (s Style) getAsColor(k propKey) TerminalColor {
func (s Style) getAsColor(k propKey) color.Color {
if !s.isSet(k) {
return noColor
}

var c TerminalColor
var c color.Color
switch k { //nolint:exhaustive
case foregroundKey:
c = s.fgColor
Expand Down
34 changes: 18 additions & 16 deletions set.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package lipgloss

import "image/color"

// Set a value on the underlying rules map.
func (s *Style) set(key propKey, value interface{}) {
// We don't allow negative integers on any of our other values, so just keep
Expand Down Expand Up @@ -151,8 +153,8 @@ func (s *Style) setFrom(key propKey, i Style) {
}
}

func colorOrNil(c interface{}) TerminalColor {
if c, ok := c.(TerminalColor); ok {
func colorOrNil(c interface{}) color.Color {
if c, ok := c.(color.Color); ok {
return c
}
return nil
Expand Down Expand Up @@ -212,13 +214,13 @@ func (s Style) Faint(v bool) Style {
//
// // Removes the foreground color
// s.Foreground(lipgloss.NoColor)
func (s Style) Foreground(c TerminalColor) Style {
func (s Style) Foreground(c color.Color) Style {
s.set(foregroundKey, c)
return s
}

// Background sets a background color.
func (s Style) Background(c TerminalColor) Style {
func (s Style) Background(c color.Color) Style {
s.set(backgroundKey, c)
return s
}
Expand Down Expand Up @@ -382,7 +384,7 @@ func (s Style) MarginBottom(i int) Style {
// MarginBackground sets the background color of the margin. Note that this is
// also set when inheriting from a style with a background color. In that case
// the background color on that style will set the margin color on this style.
func (s Style) MarginBackground(c TerminalColor) Style {
func (s Style) MarginBackground(c color.Color) Style {
s.set(marginBackgroundKey, c)
return s
}
Expand Down Expand Up @@ -487,7 +489,7 @@ func (s Style) BorderLeft(v bool) Style {
// top side, followed by the right side, then the bottom, and finally the left.
//
// With more than four arguments nothing will be set.
func (s Style) BorderForeground(c ...TerminalColor) Style {
func (s Style) BorderForeground(c ...color.Color) Style {
if len(c) == 0 {
return s
}
Expand All @@ -506,28 +508,28 @@ func (s Style) BorderForeground(c ...TerminalColor) Style {
}

// BorderTopForeground set the foreground color for the top of the border.
func (s Style) BorderTopForeground(c TerminalColor) Style {
func (s Style) BorderTopForeground(c color.Color) Style {
s.set(borderTopForegroundKey, c)
return s
}

// BorderRightForeground sets the foreground color for the right side of the
// border.
func (s Style) BorderRightForeground(c TerminalColor) Style {
func (s Style) BorderRightForeground(c color.Color) Style {
s.set(borderRightForegroundKey, c)
return s
}

// BorderBottomForeground sets the foreground color for the bottom of the
// border.
func (s Style) BorderBottomForeground(c TerminalColor) Style {
func (s Style) BorderBottomForeground(c color.Color) Style {
s.set(borderBottomForegroundKey, c)
return s
}

// BorderLeftForeground sets the foreground color for the left side of the
// border.
func (s Style) BorderLeftForeground(c TerminalColor) Style {
func (s Style) BorderLeftForeground(c color.Color) Style {
s.set(borderLeftForegroundKey, c)
return s
}
Expand All @@ -547,7 +549,7 @@ func (s Style) BorderLeftForeground(c TerminalColor) Style {
// top side, followed by the right side, then the bottom, and finally the left.
//
// With more than four arguments nothing will be set.
func (s Style) BorderBackground(c ...TerminalColor) Style {
func (s Style) BorderBackground(c ...color.Color) Style {
if len(c) == 0 {
return s
}
Expand All @@ -566,27 +568,27 @@ func (s Style) BorderBackground(c ...TerminalColor) Style {
}

// BorderTopBackground sets the background color of the top of the border.
func (s Style) BorderTopBackground(c TerminalColor) Style {
func (s Style) BorderTopBackground(c color.Color) Style {
s.set(borderTopBackgroundKey, c)
return s
}

// BorderRightBackground sets the background color of right side the border.
func (s Style) BorderRightBackground(c TerminalColor) Style {
func (s Style) BorderRightBackground(c color.Color) Style {
s.set(borderRightBackgroundKey, c)
return s
}

// BorderBottomBackground sets the background color of the bottom of the
// border.
func (s Style) BorderBottomBackground(c TerminalColor) Style {
func (s Style) BorderBottomBackground(c color.Color) Style {
s.set(borderBottomBackgroundKey, c)
return s
}

// BorderLeftBackground set the background color of the left side of the
// border.
func (s Style) BorderLeftBackground(c TerminalColor) Style {
func (s Style) BorderLeftBackground(c color.Color) Style {
s.set(borderLeftBackgroundKey, c)
return s
}
Expand Down Expand Up @@ -761,7 +763,7 @@ func whichSidesBool(i ...bool) (top, right, bottom, left bool, ok bool) {
// whichSidesColor is like whichSides, except it operates on a series of
// boolean values. See the comment on whichSidesInt for details on how this
// works.
func whichSidesColor(i ...TerminalColor) (top, right, bottom, left TerminalColor, ok bool) {
func whichSidesColor(i ...color.Color) (top, right, bottom, left color.Color, ok bool) {
switch len(i) {
case 1:
top = i[0]
Expand Down
23 changes: 12 additions & 11 deletions style.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lipgloss

import (
"image/color"
"strings"
"unicode"

Expand Down Expand Up @@ -110,8 +111,8 @@ type Style struct {
attrs int

// props that have values
fgColor TerminalColor
bgColor TerminalColor
fgColor color.Color
bgColor color.Color

width int
height int
Expand All @@ -128,17 +129,17 @@ type Style struct {
marginRight int
marginBottom int
marginLeft int
marginBgColor TerminalColor
marginBgColor color.Color

borderStyle Border
borderTopFgColor TerminalColor
borderRightFgColor TerminalColor
borderBottomFgColor TerminalColor
borderLeftFgColor TerminalColor
borderTopBgColor TerminalColor
borderRightBgColor TerminalColor
borderBottomBgColor TerminalColor
borderLeftBgColor TerminalColor
borderTopFgColor color.Color
borderRightFgColor color.Color
borderBottomFgColor color.Color
borderLeftFgColor color.Color
borderTopBgColor color.Color
borderRightBgColor color.Color
borderBottomBgColor color.Color
borderLeftBgColor color.Color

maxWidth int
maxHeight int
Expand Down

0 comments on commit 1dbb773

Please sign in to comment.