mapserver/server/colormapping/colormapping.go

100 lines
1.7 KiB
Go
Raw Normal View History

2019-01-10 17:21:34 +03:00
package colormapping
2019-01-11 12:49:19 +03:00
import (
2019-01-13 18:37:03 +03:00
"bufio"
"bytes"
"errors"
"image/color"
"mapserver/vfs"
"strconv"
"strings"
2019-02-28 22:20:59 +03:00
"github.com/sirupsen/logrus"
2019-01-11 12:49:19 +03:00
)
2019-01-10 17:21:34 +03:00
type ColorMapping struct {
2019-01-13 18:37:03 +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-13 18:37:03 +03:00
return m.colors[name]
2019-01-11 13:23:50 +03:00
}
2019-02-08 09:40:40 +03:00
func (m *ColorMapping) LoadBytes(buffer []byte) (int, error) {
2019-01-13 18:37:03 +03:00
scanner := bufio.NewScanner(bytes.NewReader(buffer))
2019-02-08 09:40:40 +03:00
count := 0
line := 0
2019-01-13 18:37:03 +03:00
for scanner.Scan() {
2019-02-08 09:40:40 +03:00
line++
2019-01-13 18:37:03 +03:00
txt := strings.Trim(scanner.Text(), " ")
2019-01-11 13:23:50 +03:00
2019-01-13 18:37:03 +03:00
if len(txt) == 0 {
//empty
continue
}
2019-01-11 13:23:50 +03:00
2019-01-13 18:37:03 +03:00
if strings.HasPrefix(txt, "#") {
//comment
continue
}
2019-01-11 13:23:50 +03:00
2019-01-13 18:37:03 +03:00
parts := strings.Fields(txt)
2019-01-11 13:23:50 +03:00
2019-01-13 18:37:03 +03:00
if len(parts) < 4 {
2019-02-08 09:40:40 +03:00
return 0, errors.New("invalid line: #" + strconv.Itoa(line))
2019-01-13 18:37:03 +03:00
}
2019-01-10 17:21:34 +03:00
2019-01-13 18:37:03 +03:00
if len(parts) >= 4 {
r, err := strconv.ParseInt(parts[1], 10, 32)
if err != nil {
2019-02-08 09:40:40 +03:00
return 0, err
2019-01-13 18:37:03 +03:00
}
2019-01-11 13:23:50 +03:00
2019-01-13 18:37:03 +03:00
g, err := strconv.ParseInt(parts[2], 10, 32)
if err != nil {
2019-02-08 09:40:40 +03:00
return 0, err
2019-01-13 18:37:03 +03:00
}
2019-01-11 13:23:50 +03:00
2019-01-13 18:37:03 +03:00
b, err := strconv.ParseInt(parts[3], 10, 32)
if err != nil {
2019-02-08 09:40:40 +03:00
return 0, err
2019-01-13 18:37:03 +03:00
}
2019-01-11 13:23:50 +03:00
2019-02-27 18:24:53 +03:00
a := int64(255)
if len(parts) >= 5 {
//with alpha
2019-02-28 22:20:59 +03:00
//a, err = strconv.ParseInt(parts[4], 10, 32)
//if err != nil {
// return 0, err
//}
2019-02-27 18:24:53 +03:00
}
c := color.RGBA{uint8(r), uint8(g), uint8(b), uint8(a)}
2019-01-13 18:37:03 +03:00
m.colors[parts[0]] = &c
2019-02-08 09:40:40 +03:00
count++
2019-01-13 18:37:03 +03:00
}
}
2019-01-11 13:23:50 +03:00
2019-02-08 09:40:40 +03:00
return count, nil
2019-01-10 17:21:34 +03:00
}
2019-01-11 12:49:19 +03:00
2019-02-08 09:40:40 +03:00
func (m *ColorMapping) LoadVFSColors(useLocal bool, filename string) (int, error) {
2019-01-13 18:37:03 +03:00
buffer, err := vfs.FSByte(useLocal, "/colors.txt")
if err != nil {
2019-02-08 09:40:40 +03:00
return 0, err
2019-01-13 18:37:03 +03:00
}
2019-01-11 13:23:50 +03:00
2019-01-13 18:37:03 +03:00
log.WithFields(logrus.Fields{"size": len(buffer),
"filename": filename,
2019-02-08 09:40:40 +03:00
"useLocal": useLocal}).Info("Loading default colors")
2019-01-11 13:23:50 +03:00
2019-01-13 18:37:03 +03:00
return m.LoadBytes(buffer)
2019-01-11 12:49:19 +03:00
}
2019-01-11 13:44:17 +03:00
func NewColorMapping() *ColorMapping {
2019-01-13 18:37:03 +03:00
return &ColorMapping{colors: make(map[string]*color.RGBA)}
2019-01-11 12:49:19 +03:00
}