mtsatellite/common/colors.go

50 lines
975 B
Go
Raw Normal View History

2014-09-09 15:22:29 +02:00
// Copyright 2014 by Sascha L. Teichmann
// Use of this source code is governed by the MIT license
// that can be found in the LICENSE file.
2014-09-14 00:02:04 +02:00
package common
2014-09-09 15:01:14 +02:00
import (
"bufio"
"fmt"
"image/color"
"os"
"strings"
)
type Colors struct {
Colors []color.RGBA
NameIndex map[string]int32
}
func ParseColors(filename string) (colors *Colors, err error) {
2014-09-09 15:01:14 +02:00
var file *os.File
if file, err = os.Open(filename); err != nil {
return
}
defer file.Close()
nameIndex := make(map[string]int32)
cols := make([]color.RGBA, 0, 2200)
2014-09-09 15:01:14 +02:00
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "#") {
continue
}
c := color.RGBA{A: 0xff}
var name string
if n, _ := fmt.Sscanf(
line, "%s %d %d %d %d", &name, &c.R, &c.G, &c.B, &c.A); n > 0 {
idx := int32(len(cols))
cols = append(cols, c)
nameIndex[name] = idx
}
2014-09-09 15:01:14 +02:00
}
err = scanner.Err()
colors = &Colors{Colors: cols, NameIndex: nameIndex}
2014-09-09 15:01:14 +02:00
return
}