package picker import ( "github.com/ChausseBenjamin/golorpicker/internal/slider" "github.com/charmbracelet/bubbles/list" tea "github.com/charmbracelet/bubbletea" ) const ( rgbTitle string = "RGB" ) // A picker is a list of sliders allowing the user to select a color // It must therefore be capable of exporting a HEX value for previewing // as well as a type RGBPicker struct { sliders list.Model r, g, b slider.Slider loaded bool } func NewRGB() *RGBPicker { r := slider.New("R", 0, 255) g := slider.New("G", 0, 255) b := slider.New("B", 0, 255) sliders := list.New( []list.Item{*r, *g, *b}, list.NewDefaultDelegate(), 80, 6, ) sliders.Title = rgbTitle sliders.SetShowHelp(false) sliders.SetShowFilter(false) sliders.DisableQuitKeybindings() return &RGBPicker{ sliders: sliders, r: *r, g: *g, b: *b, loaded: false, } } func (p RGBPicker) toHex() string { const txt = "0123456789ABCDEF" rgb := string([]byte{ '#', txt[(p.r.Val()>>4)&0x0F], txt[p.r.Val()&0x0F], txt[(p.g.Val()>>4)&0x0F], txt[p.g.Val()&0x0F], txt[(p.b.Val()>>4)&0x0F], txt[p.b.Val()&0x0F], }) return rgb } func (p RGBPicker) Init() tea.Cmd { return nil } func (p RGBPicker) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) { case tea.WindowSizeMsg: if p.sliders.Width() != msg.Width && !p.loaded { p.sliders.SetWidth(msg.Width - 6) p.loaded = true } case tea.KeyMsg: key := msg.String() for _, k := range slider.Keystrokes() { if key == k { m := p.sliders.Items()[p.sliders.Index()].(slider.Slider) return m.Update(msg) } } } var cmd tea.Cmd p.sliders, cmd = p.sliders.Update(msg) return p, cmd } func (p RGBPicker) View() string { return p.sliders.View() }