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"
|
|
|
|
)
|
|
|
|
|
2014-09-09 23:33:53 +02:00
|
|
|
type Colors struct {
|
|
|
|
Colors []color.RGBA
|
2014-09-10 23:49:27 +02:00
|
|
|
NameIndex map[string]int32
|
2014-09-09 23:33:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
2014-09-10 23:49:27 +02:00
|
|
|
nameIndex := make(map[string]int32)
|
2014-09-09 23:33:53 +02:00
|
|
|
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
|
2014-09-09 17:48:39 +02:00
|
|
|
if n, _ := fmt.Sscanf(
|
|
|
|
line, "%s %d %d %d %d", &name, &c.R, &c.G, &c.B, &c.A); n > 0 {
|
2014-09-10 23:49:27 +02:00
|
|
|
idx := int32(len(cols))
|
2014-09-09 23:33:53 +02:00
|
|
|
cols = append(cols, c)
|
|
|
|
nameIndex[name] = idx
|
2014-09-09 17:48:39 +02:00
|
|
|
}
|
2014-09-09 15:01:14 +02:00
|
|
|
}
|
|
|
|
err = scanner.Err()
|
2014-09-09 23:33:53 +02:00
|
|
|
colors = &Colors{Colors: cols, NameIndex: nameIndex}
|
2014-09-09 15:01:14 +02:00
|
|
|
return
|
|
|
|
}
|