to save up a progress
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
package main
|
||||
|
||||
func parseMap(fields []string) (map[string]string, error) {
|
||||
m := map[string]string{}
|
||||
var k, v string
|
||||
|
||||
for i, v := range fields {
|
||||
if v == "=" {
|
||||
return nil, errors.New("bad field")
|
||||
}
|
||||
if len(v) > 1 {
|
||||
// check if "=" is the last or first character
|
||||
if "=" == v[0] {
|
||||
return nil, errors.New("no key")
|
||||
}
|
||||
if "=" == v[len(v)-1] {
|
||||
return nil, errors.New("no value")
|
||||
}
|
||||
}
|
||||
if strings.Contains(v, "=") {
|
||||
splitted := strings.Split(v, "=")
|
||||
k, v = splitted[0], splitted[1]
|
||||
m[k] = v
|
||||
if i == len(fields)-1 {
|
||||
return m, nil
|
||||
}
|
||||
continue
|
||||
}
|
||||
return nil, errors.New("no equality sign in field")
|
||||
}
|
||||
return nil, errors.New("everything is wrong!")
|
||||
}
|
||||
|
||||
type Map map[color.Color]color.Color
|
||||
type repalette struct {
|
||||
image.Image
|
||||
Map
|
||||
}
|
||||
|
||||
func (r repalette) ColorModel() color.Model {
|
||||
return r.Image.ColorModel()
|
||||
}
|
||||
func (r repalette) Bounds() image.Rectangle {
|
||||
return r.Image.Bounds()
|
||||
}
|
||||
func (r repalette) At(x, y int) color.Color {
|
||||
v := r.Image.At(x, y)
|
||||
newColor, ok := r.Map[v]
|
||||
if !ok {
|
||||
return v
|
||||
}
|
||||
return newColor
|
||||
}
|
||||
|
||||
func main() {
|
||||
fields := os.Args[1:]
|
||||
m, err := parseMap(fields)
|
||||
if err != nil {
|
||||
panic("parseMap:", err)
|
||||
}
|
||||
|
||||
errParseHexColor := errors.New("parse hex color:")
|
||||
newPalette, err :=
|
||||
func(m map[string]string) (newMap map[color.Color]color.Color, err error) {
|
||||
for k, v := range m {
|
||||
newKey, err := util.ParseHexColor(k)
|
||||
if err != nil {
|
||||
return nil, errors.Join(
|
||||
errors.New("bad key hex value."),
|
||||
errors.Join(errParseHexColor, err),
|
||||
)
|
||||
}
|
||||
newValue, err := util.ParseHexColor(v)
|
||||
if err != nil {
|
||||
return nil, errors.Join(
|
||||
errors.New("bad value hex value."),
|
||||
errors.Join(errParseHexColor, err),
|
||||
)
|
||||
}
|
||||
newMap[newKey] = newValue
|
||||
}
|
||||
return
|
||||
}(fields)
|
||||
if err != nil {
|
||||
panic(errors.Join(errors.New("palette parsing:"), err))
|
||||
}
|
||||
err = util.ProcessStdio(func(img image.Image) image.Image { return repalette{img, newPalette} })
|
||||
if err != nil {
|
||||
panic(errors.Join(errors.New("process stdio:"), err))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user