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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| package theme
import ( "cmp" "slices"
"github.com/muesli/gamut" "github.com/muesli/gamut/palette" "github.com/pkg/errors" "github.com/samber/lo"
"{{{ .Package }}}/app/util" )
func PaletteTheme(pal string, key string) (*Theme, error) { p, err := getPalette(pal) if err != nil { return nil, err } matched := p.Filter(key) if len(matched) == 0 { return nil, errors.Errorf("no color available in palette [%s] with key [%s]", pal, key) } return ColorTheme(matched[0].Name, matched[0].Color), nil }
func PaletteThemes(pal string) (Themes, error) { p, err := getPalette(pal) if err != nil { return nil, err } colors := paletteColors(*p) return lo.Map(colors, func(c gamut.Color, _ int) *Theme { return ColorTheme(c.Name, c.Color) }), nil }
func PaletteRandomThemes(pal string, num int) (Themes, error) { p, err := getPalette(pal) if err != nil { return nil, err } pc := p.Colors() var ret Themes = lo.Times(num, func(_ int) *Theme { return randomTheme(pc) }) return ret, nil }
func getPalette(pal string) (*gamut.Palette, error) { switch pal { case "crayola", "": return &palette.Crayola, nil case "css": return &palette.CSS, nil case "ral": return &palette.RAL, nil case "resene": return &palette.Resene, nil case "wikipedia": return &palette.Wikipedia, nil default: return nil, errors.Errorf("no palette available with key [%s]", pal) } }
func paletteColors(p gamut.Palette) gamut.Colors { ret := p.Colors() slices.SortFunc(ret, func(l gamut.Color, r gamut.Color) int { return cmp.Compare(l.Name, r.Name) }) return ret }
func randomTheme(colors gamut.Colors) *Theme { x := colors[util.RandomInt(len(colors))] return ColorTheme(x.Name, x.Color) }
|