mapserver/server/colormapping/colormapping.go
NatureFreshMilk 90d902e99b cleanup
2019-02-15 09:08:22 +01:00

94 lines
1.6 KiB
Go

package colormapping
import (
"bufio"
"bytes"
"errors"
"github.com/sirupsen/logrus"
"image/color"
"mapserver/vfs"
"strconv"
"strings"
)
type ColorMapping struct {
colors map[string]*color.RGBA
}
func (m *ColorMapping) GetColor(name string) *color.RGBA {
return m.colors[name]
}
func (m *ColorMapping) LoadBytes(buffer []byte) (int, error) {
scanner := bufio.NewScanner(bytes.NewReader(buffer))
count := 0
line := 0
for scanner.Scan() {
line++
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 0, errors.New("invalid line: #" + strconv.Itoa(line))
}
if len(parts) >= 4 {
r, err := strconv.ParseInt(parts[1], 10, 32)
if err != nil {
return 0, err
}
g, err := strconv.ParseInt(parts[2], 10, 32)
if err != nil {
return 0, err
}
b, err := strconv.ParseInt(parts[3], 10, 32)
if err != nil {
return 0, err
}
c := color.RGBA{uint8(r), uint8(g), uint8(b), 0xFF}
m.colors[parts[0]] = &c
count++
}
if len(parts) >= 5 {
//with alpha
}
}
return count, nil
}
func (m *ColorMapping) LoadVFSColors(useLocal bool, filename string) (int, error) {
buffer, err := vfs.FSByte(useLocal, "/colors.txt")
if err != nil {
return 0, err
}
log.WithFields(logrus.Fields{"size": len(buffer),
"filename": filename,
"useLocal": useLocal}).Info("Loading default colors")
return m.LoadBytes(buffer)
}
func NewColorMapping() *ColorMapping {
return &ColorMapping{colors: make(map[string]*color.RGBA)}
}