From addb4ad9f27c74693638cf4d169be9e83ac4a752 Mon Sep 17 00:00:00 2001 From: Benjamin Chausse Date: Tue, 17 Dec 2024 13:57:44 -0500 Subject: Copy hex escape sequences directly (#24) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Benjamin Chaussé --- internal/colors/precise.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'internal/colors/precise.go') diff --git a/internal/colors/precise.go b/internal/colors/precise.go index a58d7c5..a84e629 100644 --- a/internal/colors/precise.go +++ b/internal/colors/precise.go @@ -6,6 +6,8 @@ import ( "strings" ) +const esc = "\\X1B" + type ColorSpace interface { ToPrecise() PreciseColor FromPrecise(PreciseColor) ColorSpace @@ -40,3 +42,19 @@ func Hex(cs ColorSpace) string { int(math.Round(p.B*255)), )) } + +func EscapedSeq(cs ColorSpace, fg bool) string { + p := cs.ToPrecise() + mod := 38 // fg by default + if !fg { + mod += 10 + } + + r := int(math.Round(p.R * 255)) + g := int(math.Round(p.G * 255)) + b := int(math.Round(p.B * 255)) + + return fmt.Sprintf("%s[%d;2;%d;%d;%dm", + esc, mod, r, g, b, + ) +} -- cgit v1.2.3