Skip to content

Commit

Permalink
feat: conform to color.Color and add RGBColor and color consts
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed May 14, 2024
1 parent d059cfa commit 4de4322
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 30 deletions.
113 changes: 109 additions & 4 deletions color.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,33 @@ package lipgloss

import (
"image/color"
"strconv"

"github.com/charmbracelet/x/ansi"
"github.com/lucasb-eyer/go-colorful"
)

// 4-bit color constants.
const (
Black basicColor = iota
Red
Green
Yellow
Blue
Magenta
Cyan
White

BrightBlack
BrightRed
BrightGreen
BrightYellow
BrightBlue
BrightMagenta
BrightCyan
BrightWhite
)

// TerminalColor is a color intended to be rendered in the terminal.
type TerminalColor interface {
color(p Profile, hasLightBg bool) ansi.Color
Expand Down Expand Up @@ -42,8 +64,63 @@ func (n NoColor) RGBA() (r, g, b, a uint32) {
// hexColor := lipgloss.Color("#0000ff")
type Color string

// Color returns a color.Color from a hex or ANSI value (0-16, 16-255).
func (c Color) Color() (col ansi.Color) {
s := string(c)
if len(s) == 0 {
return noColor
}

if h, err := colorful.Hex(s); err == nil {
tc := uint32(h.R*255)<<16 + uint32(h.G*255)<<8 + uint32(h.B*255)

Check failure on line 75 in color.go

View workflow job for this annotation

GitHub Actions / lint-soft

Magic number: 255, in <argument> detected (gomnd)
col = ansi.TrueColor(tc)

Check failure on line 76 in color.go

View workflow job for this annotation

GitHub Actions / lint

ineffectual assignment to col (ineffassign)
} else if i, err := strconv.Atoi(s); err == nil {
if i < 16 {
col = ansi.BasicColor(i)

Check failure on line 79 in color.go

View workflow job for this annotation

GitHub Actions / lint

ineffectual assignment to col (ineffassign)
} else if i < 256 {
col = ansi.ExtendedColor(i)

Check failure on line 81 in color.go

View workflow job for this annotation

GitHub Actions / lint

ineffectual assignment to col (ineffassign)
} else {
col = ansi.TrueColor(i)

Check failure on line 83 in color.go

View workflow job for this annotation

GitHub Actions / lint

ineffectual assignment to col (ineffassign)
}
}

return noColor
}

// RGBA returns the RGBA value of this color. This satisfies the Go Color
// interface. Note that on error we return black with 100% opacity, or:
//
// Red: 0x0, Green: 0x0, Blue: 0x0, Alpha: 0xFFFF.
func (c Color) RGBA() (r, g, b, a uint32) {
return c.Color().RGBA()
}

func (c Color) color(p Profile, _ bool) ansi.Color {
return p.Color(string(c))
return p.Convert(c)
}

// RGBColor is a color specified by red, green, and blue values.
type RGBColor struct {
R uint8
G uint8
B uint8
}

// Color returns a color.Color from an RGB value.
func (c RGBColor) Color() ansi.Color {
return ansi.TrueColor(uint32(c.R)<<16 + uint32(c.G)<<8 + uint32(c.B))
}

// RGBA returns the RGBA value of this color. This satisfies the Go Color
// interface. Note that on error we return black with 100% opacity, or:
//
// Red: 0x0, Green: 0x0, Blue: 0x0, Alpha: 0xFFFF.
func (c RGBColor) RGBA() (r, g, b, a uint32) {
return c.Color().RGBA()
}

func (c RGBColor) color(p Profile, _ bool) ansi.Color {
return p.Convert(c)
}

// ANSIColor is a color specified by an ANSI256 color value.
Expand All @@ -54,10 +131,38 @@ func (c Color) color(p Profile, _ bool) ansi.Color {
// colorB := lipgloss.ANSIColor(134)
type ANSIColor uint8

// RGBA returns the RGBA value of this color. This satisfies the Go Color
// interface. Note that on error we return black with 100% opacity, or:
//
// Red: 0x0, Green: 0x0, Blue: 0x0, Alpha: 0xFFFF.
func (c ANSIColor) RGBA() (r, g, b, a uint32) {
return ansi.ExtendedColor(c).RGBA()
}

func (ac ANSIColor) color(p Profile, _ bool) ansi.Color {

Check warning on line 142 in color.go

View workflow job for this annotation

GitHub Actions / lint

receiver-naming: receiver name ac should be consistent with previous receiver name c for ANSIColor (revive)
return p.Convert(ansi.ExtendedColor(ac))
}

// basicColor is a color specified by an ANSI 4-bit color value.
type basicColor uint8

// Color returns the color.Color from a basic color value (0-16).
func (bc basicColor) Color() ansi.Color {
return ansi.BasicColor(bc)
}

// RGBA returns the RGBA value of this color. This satisfies the Go Color
// interface. Note that on error we return black with 100% opacity, or:
//
// Red: 0x0, Green: 0x0, Blue: 0x0, Alpha: 0xFFFF.
func (c basicColor) RGBA() (r, g, b, a uint32) {

Check warning on line 158 in color.go

View workflow job for this annotation

GitHub Actions / lint

receiver-naming: receiver name c should be consistent with previous receiver name bc for basicColor (revive)
return c.Color().RGBA()
}

func (bc basicColor) color(p Profile, _ bool) ansi.Color {
return p.Convert(ansi.BasicColor(bc))
}

// AdaptiveColor provides color options for light and dark backgrounds. The
// appropriate color will be returned at runtime based on the darkness of the
// terminal background color.
Expand Down Expand Up @@ -88,11 +193,11 @@ type CompleteColor struct {
func (c CompleteColor) color(p Profile, hasLightBg bool) ansi.Color {
switch p { //nolint:exhaustive
case TrueColor:
return p.Color(c.TrueColor)
return Color(c.TrueColor).color(p, hasLightBg)
case ANSI256:
return p.Color(c.ANSI256)
return Color(c.ANSI256).color(p, hasLightBg)
case ANSI:
return p.Color(c.ANSI)
return Color(c.ANSI).color(p, hasLightBg)
default:
return noColor
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ retract v0.7.0 // v0.7.0 introduces a bug that causes some apps to freeze.
go 1.18

require (
github.com/charmbracelet/x/ansi v0.1.0
github.com/charmbracelet/x/ansi v0.1.1
github.com/charmbracelet/x/term v0.1.1
github.com/lucasb-eyer/go-colorful v1.2.0
github.com/rivo/uniseg v0.4.7
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/charmbracelet/x/ansi v0.1.0 h1:o4NbQQCoVgbLpD5RC1cI687baoLwrLZyCOTGlF0gne4=
github.com/charmbracelet/x/ansi v0.1.0/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=
github.com/charmbracelet/x/ansi v0.1.1 h1:CGAduulr6egay/YVbGc8Hsu8deMg1xZ/bkaXTPi1JDk=
github.com/charmbracelet/x/ansi v0.1.1/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=
github.com/charmbracelet/x/input v0.1.0 h1:TEsGSfZYQyOtp+STIjyBq6tpRaorH0qpwZUj8DavAhQ=
github.com/charmbracelet/x/input v0.1.0/go.mod h1:ZZwaBxPF7IG8gWWzPUVqHEtWhc1+HXJPNuerJGRGZ28=
github.com/charmbracelet/x/term v0.1.1 h1:3cosVAiPOig+EV4X9U+3LDgtwwAoEzJjNdwbXDjF6yI=
Expand Down
25 changes: 0 additions & 25 deletions profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package lipgloss
import (
"image/color"
"math"
"strconv"

"github.com/charmbracelet/x/ansi"
"github.com/lucasb-eyer/go-colorful"
Expand Down Expand Up @@ -76,30 +75,6 @@ func (p Profile) Convert(c ansi.Color) ansi.Color {
return c
}

// Color creates a Color from a string. Valid inputs are hex colors, as well as
// ANSI Color codes (0-15, 16-255).
func (p Profile) Color(s string) ansi.Color {
if len(s) == 0 {
return noColor
}

var c ansi.Color
if h, err := colorful.Hex(s); err == nil {
tc := uint32(h.R*255)<<16 + uint32(h.G*255)<<8 + uint32(h.B*255)
c = ansi.TrueColor(tc)
} else if i, err := strconv.Atoi(s); err == nil {
if i < 16 {
c = ansi.BasicColor(i)
} else {
c = ansi.ExtendedColor(i)
}
} else {
return noColor
}

return p.Convert(c)
}

func hexToANSI256Color(c colorful.Color) ansi.ExtendedColor {
v2ci := func(v float64) int {
if v < 48 {
Expand Down

0 comments on commit 4de4322

Please sign in to comment.