Skip to content

Commit

Permalink
add + support dynamic determine �number of colors
Browse files Browse the repository at this point in the history
  • Loading branch information
cage1016 committed Aug 12, 2022
1 parent 89d115b commit 33bf5ea
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
27 changes: 24 additions & 3 deletions cmd/paletter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ package cmd

import (
"fmt"
"strconv"
"strings"

"github.com/Baldomo/paletter"
aw "github.com/deanishe/awgo"
"github.com/muesli/clusters"
"github.com/spf13/cobra"

"github.com/cage1016/alfred-paletter/alfred"
Expand All @@ -24,7 +27,14 @@ var paletterCmd = &cobra.Command{
Use: "paletter",
Short: "Extract color from an image",
Run: func(cmd *cobra.Command, args []string) {
q := args[0]
q, r := args[0], ""
match := lib.ReNumberOfColor.FindStringSubmatch(q)
if len(match) > 0 {
index := lib.ReNumberOfColor.FindAllStringSubmatchIndex(q, -1)[0][0]
q, r = strings.TrimSpace(q[:index]), q[index+1:]
} else {
q = strings.TrimSpace(q)
}

var path string
var err error
Expand All @@ -49,10 +59,21 @@ var paletterCmd = &cobra.Command{
// local file
img, err := paletter.OpenImage(q)
if err != nil {
wf.NewItem(fmt.Sprintf("%s", err.Error())).Subtitle("Support png, jpeg, gif, webp, bmp, tiff. Try a different query?").Icon(GaryIcon)
wf.NewItem(err.Error()).Subtitle("Support png, jpeg, gif, webp, bmp, tiff. Try a different query?").Icon(GaryIcon)
} else {
var cs clusters.Clusters
obs := paletter.ImageToObservation(img)
cs, _ := paletter.CalculatePalette(obs, alfred.GetNumberOfColor(wf))
if len(r) > 1 {
nc, err := strconv.Atoi(r)
if err != nil {
wf.NewItem(err.Error()).Subtitle("Support png, jpeg, gif, webp, bmp, tiff. Try a different query?").Icon(GaryIcon)
wf.SendFeedback()
return
}
cs, _ = paletter.CalculatePalette(obs, nc)
} else {
cs, _ = paletter.CalculatePalette(obs, alfred.GetNumberOfColor(wf))
}
colors := paletter.ColorsFromClusters(cs)

uniColors := lib.Unique(colors)
Expand Down
5 changes: 3 additions & 2 deletions lib/paletter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ const (
)

var (
ReUrl = regexp.MustCompile(`(?m)^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$`)
ReB64 = regexp.MustCompile(`(data:image\/[^;]+;base64,.*?)`)
ReUrl = regexp.MustCompile(`(?m)^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$`)
ReB64 = regexp.MustCompile(`(data:image\/[^;]+;base64,.*?)`)
ReNumberOfColor = regexp.MustCompile(`(?m)( \+\d?)`)
)

type HexColor struct {
Expand Down

0 comments on commit 33bf5ea

Please sign in to comment.