diff options
-rw-r--r-- | internal/colors/precise.go | 18 | ||||
-rw-r--r-- | internal/switcher/keys.go | 15 | ||||
-rw-r--r-- | internal/switcher/misc.go | 8 |
3 files changed, 31 insertions, 10 deletions
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, + ) +} diff --git a/internal/switcher/keys.go b/internal/switcher/keys.go index 73ba710..0bf7111 100644 --- a/internal/switcher/keys.go +++ b/internal/switcher/keys.go @@ -7,10 +7,12 @@ import ( ) const ( - cpHex = "x" - cpRGB = "r" - cpHSL = "s" - cpCMYK = "c" + cpHex = "x" + cpRGB = "r" + cpHSL = "s" + cpCMYK = "c" + cpEscFG = "f" + cpEscBG = "b" ) type keybinds struct { @@ -18,6 +20,7 @@ type keybinds struct { } func newKeybinds() keybinds { + cpKeys := []string{cpHex, cpRGB, cpHSL, cpCMYK, cpEscBG, cpEscFG} return keybinds{ next: key.NewBinding( key.WithKeys("tab"), @@ -28,9 +31,9 @@ func newKeybinds() keybinds { key.WithHelp("shift+tab", "prev picker"), ), copy: key.NewBinding( - key.WithKeys(cpHex, cpRGB, cpHSL, cpCMYK), + key.WithKeys(cpKeys...), key.WithHelp( - strings.Join([]string{cpHex, cpRGB, cpHSL, cpCMYK}, "/"), + strings.Join(cpKeys, "/"), "copy color", ), ), diff --git a/internal/switcher/misc.go b/internal/switcher/misc.go index e87f219..d34a3bc 100644 --- a/internal/switcher/misc.go +++ b/internal/switcher/misc.go @@ -8,10 +8,6 @@ import ( "github.com/ChausseBenjamin/termpicker/internal/util" ) -const ( - okCpMsg = "Copied %s to clipboard as %s" -) - func (m Model) copyColor(format string) string { pc := m.pickers[m.active].GetColor().ToPrecise() switch format { @@ -26,6 +22,10 @@ func (m Model) copyColor(format string) string { case cpCMYK: cmyk := colors.CMYK{}.FromPrecise(pc).(colors.CMYK) return util.Copy(cmyk.String()) + case cpEscFG: + return util.Copy(colors.EscapedSeq(m.pickers[m.active].GetColor(), true)) + case cpEscBG: + return util.Copy(colors.EscapedSeq(m.pickers[m.active].GetColor(), false)) default: return "Copy format not supported" } |