2019-01-10 17:21:34 +03:00
|
|
|
package colormapping
|
|
|
|
|
2019-01-11 12:49:19 +03:00
|
|
|
import (
|
|
|
|
"mapserver/vfs"
|
2019-01-11 13:23:50 +03:00
|
|
|
"bufio"
|
|
|
|
"errors"
|
|
|
|
"bytes"
|
|
|
|
"strings"
|
|
|
|
"strconv"
|
|
|
|
"github.com/sirupsen/logrus"
|
2019-01-11 13:44:17 +03:00
|
|
|
"image/color"
|
2019-01-11 12:49:19 +03:00
|
|
|
)
|
|
|
|
|
2019-01-10 17:21:34 +03:00
|
|
|
type ColorMapping struct {
|
2019-01-11 13:44:17 +03:00
|
|
|
colors map[string]*color.RGBA
|
2019-01-11 13:23:50 +03:00
|
|
|
}
|
|
|
|
|
2019-01-11 13:44:17 +03:00
|
|
|
func (m *ColorMapping) GetColor(name string) *color.RGBA {
|
2019-01-11 13:23:50 +03:00
|
|
|
return m.colors[name]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *ColorMapping) LoadBytes(buffer []byte) error {
|
|
|
|
scanner := bufio.NewScanner(bytes.NewReader(buffer))
|
|
|
|
for scanner.Scan() {
|
|
|
|
txt := strings.Trim(scanner.Text(), " ")
|
|
|
|
|
|
|
|
if len(txt) == 0 {
|
|
|
|
//empty
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(txt, "#") {
|
|
|
|
//comment
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
parts := strings.Fields(txt)
|
|
|
|
|
|
|
|
if len(parts) < 4 {
|
|
|
|
return errors.New("invalid line")
|
|
|
|
}
|
2019-01-10 17:21:34 +03:00
|
|
|
|
2019-01-11 13:23:50 +03:00
|
|
|
if len(parts) >= 4 {
|
|
|
|
r, err := strconv.ParseInt(parts[1], 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
g, err := strconv.ParseInt(parts[2], 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := strconv.ParseInt(parts[3], 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-11 13:44:17 +03:00
|
|
|
c := color.RGBA{uint8(r),uint8(g),uint8(b), 0xFF}
|
|
|
|
m.colors[parts[0]] = &c
|
2019-01-11 13:23:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(parts) >= 5 {
|
|
|
|
//with alpha
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2019-01-10 17:21:34 +03:00
|
|
|
}
|
2019-01-11 12:49:19 +03:00
|
|
|
|
2019-01-11 13:23:50 +03:00
|
|
|
//TODO: colors from fs
|
|
|
|
|
|
|
|
func (m *ColorMapping) LoadVFSColors(useLocal bool, filename string) error {
|
|
|
|
buffer, err := vfs.FSByte(useLocal, "/colors.txt")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.WithFields(logrus.Fields{"size": len(buffer),
|
|
|
|
"filename": filename,
|
|
|
|
"useLocal": useLocal}).Info("Loading local colors file")
|
|
|
|
|
|
|
|
return m.LoadBytes(buffer)
|
2019-01-11 12:49:19 +03:00
|
|
|
}
|
|
|
|
|
2019-01-11 13:44:17 +03:00
|
|
|
func NewColorMapping() *ColorMapping {
|
|
|
|
return &ColorMapping{colors: make(map[string]*color.RGBA)}
|
2019-01-11 12:49:19 +03:00
|
|
|
}
|