summaryrefslogtreecommitdiff
path: root/internal/switcher/misc.go
blob: d34a3bc0a2de138fb47225110b2574ee6a5fd2fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package switcher

import (
	"log/slog"

	"github.com/ChausseBenjamin/termpicker/internal/colors"
	"github.com/ChausseBenjamin/termpicker/internal/parse"
	"github.com/ChausseBenjamin/termpicker/internal/util"
)

func (m Model) copyColor(format string) string {
	pc := m.pickers[m.active].GetColor().ToPrecise()
	switch format {
	case cpHex:
		return util.Copy(colors.Hex(m.pickers[m.active].GetColor()))
	case cpRGB:
		rgb := colors.RGB{}.FromPrecise(pc).(colors.RGB)
		return util.Copy(rgb.String())
	case cpHSL:
		hsl := colors.HSL{}.FromPrecise(pc).(colors.HSL)
		return util.Copy(hsl.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"
	}
}

func (m *Model) SetColorFromText(colorStr string) string {
	color, err := parse.Color(colorStr)
	if err != nil {
		slog.Error("Failed to parse color", util.ErrKey, err)
		return err.Error()
	} else {
		pc := color.ToPrecise()
		switch color.(type) {
		case colors.RGB:
			m.UpdatePicker(IndexRgb, pc)
			m.SetActive(IndexRgb)
		case colors.CMYK:
			m.UpdatePicker(IndexCmyk, pc)
			m.SetActive(IndexCmyk)
		case colors.HSL:
			m.UpdatePicker(IndexHsl, pc)
			m.SetActive(IndexHsl)
		}
		return "Color set to " + colorStr
	}
}