@@ -5,20 +5,20 @@ import (
55 "image/color"
66 "strings"
77
8- "github.com/lucasb-eyer/go-colorful "
8+ "charm.land/lipgloss/v2 "
99 "github.com/rivo/uniseg"
1010)
1111
1212// ForegroundGrad returns a slice of strings representing the input string
1313// rendered with a horizontal gradient foreground from color1 to color2. Each
1414// string in the returned slice corresponds to a grapheme cluster in the input
1515// string. If bold is true, the rendered strings will be bolded.
16- func ForegroundGrad (t * Styles , input string , bold bool , color1 , color2 color.Color ) []string {
16+ func ForegroundGrad (base lipgloss. Style , input string , bold bool , color1 , color2 color.Color ) []string {
1717 if input == "" {
1818 return []string {"" }
1919 }
2020 if len (input ) == 1 {
21- style := t . Base .Foreground (color1 )
21+ style := base .Foreground (color1 )
2222 if bold {
2323 style .Bold (true )
2424 }
@@ -30,9 +30,9 @@ func ForegroundGrad(t *Styles, input string, bold bool, color1, color2 color.Col
3030 clusters = append (clusters , string (gr .Runes ()))
3131 }
3232
33- ramp := blendColors (len (clusters ), color1 , color2 )
33+ ramp := lipgloss . Blend1D (len (clusters ), color1 , color2 )
3434 for i , c := range ramp {
35- style := t . Base .Foreground (c )
35+ style := base .Foreground (c )
3636 if bold {
3737 style .Bold (true )
3838 }
@@ -43,12 +43,12 @@ func ForegroundGrad(t *Styles, input string, bold bool, color1, color2 color.Col
4343
4444// ApplyForegroundGrad renders a given string with a horizontal gradient
4545// foreground.
46- func ApplyForegroundGrad (t * Styles , input string , color1 , color2 color.Color ) string {
46+ func ApplyForegroundGrad (base lipgloss. Style , input string , color1 , color2 color.Color ) string {
4747 if input == "" {
4848 return ""
4949 }
5050 var o strings.Builder
51- clusters := ForegroundGrad (t , input , false , color1 , color2 )
51+ clusters := ForegroundGrad (base , input , false , color1 , color2 )
5252 for _ , c := range clusters {
5353 fmt .Fprint (& o , c )
5454 }
@@ -57,61 +57,14 @@ func ApplyForegroundGrad(t *Styles, input string, color1, color2 color.Color) st
5757
5858// ApplyBoldForegroundGrad renders a given string with a horizontal gradient
5959// foreground.
60- func ApplyBoldForegroundGrad (t * Styles , input string , color1 , color2 color.Color ) string {
60+ func ApplyBoldForegroundGrad (base lipgloss. Style , input string , color1 , color2 color.Color ) string {
6161 if input == "" {
6262 return ""
6363 }
6464 var o strings.Builder
65- clusters := ForegroundGrad (t , input , true , color1 , color2 )
65+ clusters := ForegroundGrad (base , input , true , color1 , color2 )
6666 for _ , c := range clusters {
6767 fmt .Fprint (& o , c )
6868 }
6969 return o .String ()
7070}
71-
72- // blendColors returns a slice of colors blended between the given keys.
73- // Blending is done in Hcl to stay in gamut.
74- func blendColors (size int , stops ... color.Color ) []color.Color {
75- if len (stops ) < 2 {
76- return nil
77- }
78-
79- stopsPrime := make ([]colorful.Color , len (stops ))
80- for i , k := range stops {
81- stopsPrime [i ], _ = colorful .MakeColor (k )
82- }
83-
84- numSegments := len (stopsPrime ) - 1
85- blended := make ([]color.Color , 0 , size )
86-
87- // Calculate how many colors each segment should have.
88- segmentSizes := make ([]int , numSegments )
89- baseSize := size / numSegments
90- remainder := size % numSegments
91-
92- // Distribute the remainder across segments.
93- for i := range numSegments {
94- segmentSizes [i ] = baseSize
95- if i < remainder {
96- segmentSizes [i ]++
97- }
98- }
99-
100- // Generate colors for each segment.
101- for i := range numSegments {
102- c1 := stopsPrime [i ]
103- c2 := stopsPrime [i + 1 ]
104- segmentSize := segmentSizes [i ]
105-
106- for j := range segmentSize {
107- var t float64
108- if segmentSize > 1 {
109- t = float64 (j ) / float64 (segmentSize - 1 )
110- }
111- c := c1 .BlendHcl (c2 , t )
112- blended = append (blended , c )
113- }
114- }
115-
116- return blended
117- }
0 commit comments