From: "Dr. Tobias Quathamer" Date: Fri, 24 Oct 2025 21:06:22 +0200 Subject: Add patch to revert commit 6aa93cd20570c3f73ab32881f77e1b29ca59c22e In that commit, the package exp/color has been removed. However, it is still needed by other packages in this library, so we add it again for now. Forwarded: not-needed --- exp/color/blend.go | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ exp/color/go.mod | 5 ++++ exp/color/go.sum | 2 ++ 3 files changed, 76 insertions(+) create mode 100644 exp/color/blend.go create mode 100644 exp/color/go.mod create mode 100644 exp/color/go.sum diff --git a/exp/color/blend.go b/exp/color/blend.go new file mode 100644 index 0000000..cee2c7f --- /dev/null +++ b/exp/color/blend.go @@ -0,0 +1,69 @@ +// Package color contains utilities for working with colors. +package color + +import ( + "image/color" + + "github.com/lucasb-eyer/go-colorful" +) + +// Blend returns a slice of colors blended between the given +// colors. Blending is done as Hcl to stay in gamut. +// +// Example: +// +// red := color.RGBA{R: 0xff, G: 0x00, B: 0x00, A: 0xff} +// blue := color.RGBA{R: 0x00, G: 0x00, B: 0xff, A: 0xff} +// +// blend := Blend(10, red, blue) +// var b strings.Builder +// +// for _, c := range blend { +// b.WriteString(lipgloss.NewStyle().Background(c).Render(" ")) +// } +// +// lipgloss.Println(b.String()) +func Blend(size int, points ...color.Color) []color.Color { + if size <= 0 || len(points) < 2 { + return nil + } + if size == 1 { + return []color.Color{points[0]} + } + + stops := make([]colorful.Color, len(points)) + for i, c := range points { + stops[i], _ = colorful.MakeColor(c) + } + + numSegments := len(stops) - 1 + blended := make([]color.Color, 0, size) + + // Calculate how many colors each segment should have. + segmentSizes := make([]int, numSegments) + baseSize := size / numSegments + remainder := size % numSegments + + // Distribute the remainder across segments. + for i := range numSegments { + segmentSizes[i] = baseSize + if i < remainder { + segmentSizes[i]++ + } + } + + // Generate colors for each segment. + for i := range numSegments { + c1 := stops[i] + c2 := stops[i+1] + segmentSize := segmentSizes[i] + + for j := range segmentSize { + t := float64(j) / float64(segmentSize) + c := c1.BlendHcl(c2, t) + blended = append(blended, c) + } + } + + return blended +} diff --git a/exp/color/go.mod b/exp/color/go.mod new file mode 100644 index 0000000..414e6c4 --- /dev/null +++ b/exp/color/go.mod @@ -0,0 +1,5 @@ +module github.com/charmbracelet/x/exp/color + +go 1.24.0 + +require github.com/lucasb-eyer/go-colorful v1.3.0 diff --git a/exp/color/go.sum b/exp/color/go.sum new file mode 100644 index 0000000..7e7b637 --- /dev/null +++ b/exp/color/go.sum @@ -0,0 +1,2 @@ +github.com/lucasb-eyer/go-colorful v1.3.0 h1:2/yBRLdWBZKrf7gB40FoiKfAWYQ0lqNcbuQwVHXptag= +github.com/lucasb-eyer/go-colorful v1.3.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=