color mapping

This commit is contained in:
NatureFreshMilk 2019-01-11 11:23:50 +01:00
parent 43896a5ac1
commit a57724d5f0
3 changed files with 110 additions and 7 deletions

View File

@ -2,17 +2,100 @@ package colormapping
import (
"mapserver/vfs"
"bufio"
"errors"
"bytes"
"strings"
"strconv"
"github.com/sirupsen/logrus"
)
type Color struct {
R,G,B,A int
}
type ColorMapping struct {
colors map[string]*Color
}
func (m *ColorMapping) LoadColors(filename string){
//TODO
func (m *ColorMapping) GetColor(name string) *Color {
return m.colors[name]
}
func CreateColorMapping() {
//embedded colors
vfs.FSMustByte(false, "/colors.txt")
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
}
c := Color{}
parts := strings.Fields(txt)
if len(parts) < 4 {
return errors.New("invalid line")
}
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
}
c.R = int(r)
c.G = int(g)
c.B = int(b)
}
if len(parts) >= 5 {
//with alpha
a, err := strconv.ParseInt(parts[4], 10, 32)
if err != nil {
return err
}
c.A = int(a)
}
m.colors[parts[0]] = &c
}
return nil
}
//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)
}
func CreateColorMapping() *ColorMapping {
return &ColorMapping{colors: make(map[string]*Color)}
}

View File

@ -5,5 +5,15 @@ import (
)
func TestNewMapping(t *testing.T) {
CreateColorMapping()
m := CreateColorMapping()
err := m.LoadVFSColors(false, "/colors.txt")
if err != nil {
t.Fatal(err)
}
c := m.GetColor("scifi_nodes:blacktile2")
if c == nil {
panic("no color")
}
}

10
colormapping/logger.go Normal file
View File

@ -0,0 +1,10 @@
package colormapping
import (
"github.com/sirupsen/logrus"
)
var log *logrus.Entry
func init(){
log = logrus.WithFields(logrus.Fields{"prefix": "colormapping"})
}