From adaf56c51797bf08ed25eee6f81d70d07dcfb707 Mon Sep 17 00:00:00 2001 From: BuckarooBanzay Date: Fri, 20 Jan 2023 17:43:46 +0100 Subject: [PATCH] use colormapping repo --- colormapping/colormapping.go | 139 ------ colormapping/colormapping_test.go | 67 --- colormapping/palette.go | 45 -- colormapping/palette_test.go | 30 -- colormapping/testdata/palette_nodes.txt | 1 - colormapping/testdata/testcolor.txt | 4 - .../testdata/unifieddyes_palette_extended.png | Bin 290 -> 0 bytes go.mod | 7 +- go.sum | 11 + isorenderer.go | 2 +- isorenderer_test.go | 9 +- maprenderer.go | 2 +- maprenderer_test.go | 10 +- testdata/mtg.txt | 430 ------------------ 14 files changed, 22 insertions(+), 735 deletions(-) delete mode 100644 colormapping/colormapping.go delete mode 100644 colormapping/colormapping_test.go delete mode 100644 colormapping/palette.go delete mode 100644 colormapping/palette_test.go delete mode 100644 colormapping/testdata/palette_nodes.txt delete mode 100644 colormapping/testdata/testcolor.txt delete mode 100644 colormapping/testdata/unifieddyes_palette_extended.png delete mode 100644 testdata/mtg.txt diff --git a/colormapping/colormapping.go b/colormapping/colormapping.go deleted file mode 100644 index 0de5ffe..0000000 --- a/colormapping/colormapping.go +++ /dev/null @@ -1,139 +0,0 @@ -package colormapping - -import ( - "bufio" - "bytes" - "errors" - "image/color" - "io" - "io/ioutil" - "strconv" - "strings" -) - -type ColorMapping struct { - colors map[string]*color.RGBA - extendedpaletteblock map[string]bool - extendedpalette *Palette -} - -func (m *ColorMapping) GetColor(name string, param2 int) *color.RGBA { - //TODO: list of node->palette - if m.extendedpaletteblock[name] { - // param2 coloring - return m.extendedpalette.GetColor(param2) - } - - return m.colors[name] -} - -func (m *ColorMapping) GetColors() map[string]*color.RGBA { - return m.colors -} - -func (m *ColorMapping) ReadColorMapping(r io.Reader) (int, error) { - b, err := ioutil.ReadAll(r) - if err != nil { - return 0, err - } - - return m.LoadColorMapping(b) -} - -func (m *ColorMapping) LoadColorMapping(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 - } - - a := int64(255) - - /* - if len(parts) >= 5 { - //with alpha - //a, err = strconv.ParseInt(parts[4], 10, 32) - //if err != nil { - // return 0, err - //} - } - */ - - c := color.RGBA{uint8(r), uint8(g), uint8(b), uint8(a)} - m.colors[parts[0]] = &c - count++ - } - } - - return count, nil -} - -func (m *ColorMapping) LoadPalette(palettefile, nodelistfile []byte) error { - palette, err := NewPalette(palettefile) - if err != nil { - return err - } - - scanner := bufio.NewScanner(bytes.NewReader(nodelistfile)) - paletteblock := make(map[string]bool) - - if err != nil { - return err - } - - for scanner.Scan() { - txt := strings.Trim(scanner.Text(), " ") - - if len(txt) == 0 { - //empty - continue - } - - paletteblock[txt] = true - } - - m.extendedpaletteblock = paletteblock - m.extendedpalette = palette - return nil -} - -func NewColorMapping() *ColorMapping { - return &ColorMapping{ - colors: make(map[string]*color.RGBA), - } -} diff --git a/colormapping/colormapping_test.go b/colormapping/colormapping_test.go deleted file mode 100644 index a555d91..0000000 --- a/colormapping/colormapping_test.go +++ /dev/null @@ -1,67 +0,0 @@ -package colormapping - -import ( - "os" - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestNewMapping(t *testing.T) { - m := NewColorMapping() - assert.NotNil(t, m) - - data, err := os.Open("testdata/testcolor.txt") - assert.NoError(t, err) - assert.NotNil(t, data) - - // empty mapping - assert.Equal(t, 0, len(m.colors)) - - // no colors loaded - c := m.GetColor("default:river_water_flowing", 0) - assert.Nil(t, c) - - //load testcolors - count, err := m.ReadColorMapping(data) - assert.NoError(t, err) - assert.Equal(t, 2, count) - - // test colors loaded - c = m.GetColor("default:river_water_flowing", 0) - assert.NotNil(t, c) - assert.Equal(t, uint8(0), c.R) - assert.Equal(t, uint8(0), c.G) - assert.Equal(t, uint8(100), c.B) - assert.Equal(t, uint8(255), c.A) - c = m.GetColor("scifi_nodes:blacktile2", 0) - assert.NotNil(t, c) - assert.Equal(t, uint8(10), c.R) - assert.Equal(t, uint8(20), c.G) - assert.Equal(t, uint8(30), c.B) - assert.Equal(t, uint8(255), c.A) - - // load param2 palette - palette_data, err := os.ReadFile("testdata/unifieddyes_palette_extended.png") - assert.NoError(t, err) - assert.NotNil(t, data) - palette_nodes, err := os.ReadFile("testdata/palette_nodes.txt") - assert.NoError(t, err) - assert.NotNil(t, data) - err = m.LoadPalette(palette_data, palette_nodes) - assert.NoError(t, err) - - // test node with extended palette - c = m.GetColor("unifiedbricks:brickblock_multicolor_dark", 50) - assert.NotNil(t, c) - assert.Equal(t, uint8(255), c.R) - assert.Equal(t, uint8(191), c.G) - assert.Equal(t, uint8(129), c.B) - assert.Equal(t, uint8(255), c.A) - c = m.GetColor("unifiedbricks:brickblock_multicolor_dark", 20) - assert.NotNil(t, c) - assert.Equal(t, uint8(255), c.R) - assert.Equal(t, uint8(191), c.G) - assert.Equal(t, uint8(255), c.B) - assert.Equal(t, uint8(255), c.A) -} diff --git a/colormapping/palette.go b/colormapping/palette.go deleted file mode 100644 index 736c7d2..0000000 --- a/colormapping/palette.go +++ /dev/null @@ -1,45 +0,0 @@ -package colormapping - -import ( - "bytes" - "image/color" - "image/png" -) - -type Palette struct { - colors map[int]*color.RGBA -} - -func NewPalette(imagefile []byte) (*Palette, error) { - palette := &Palette{ - colors: make(map[int]*color.RGBA), - } - - reader := bytes.NewReader(imagefile) - img, err := png.Decode(reader) - - if err != nil { - return nil, err - } - - bounds := img.Bounds() - - index := 0 - for y := bounds.Min.Y; y < bounds.Max.Y; y++ { - for x := bounds.Min.X; x < bounds.Max.X; x++ { - c := img.At(x, y) - r, g, b, a := c.RGBA() - - //fmt.Println("x ", x, " y ", y, " Index: ", index, " Color ", c) - palette.colors[index] = &color.RGBA{uint8(r), uint8(g), uint8(b), uint8(a)} - - index++ - } - } - - return palette, nil -} - -func (m *Palette) GetColor(param2 int) *color.RGBA { - return m.colors[param2] -} diff --git a/colormapping/palette_test.go b/colormapping/palette_test.go deleted file mode 100644 index 8dc1f17..0000000 --- a/colormapping/palette_test.go +++ /dev/null @@ -1,30 +0,0 @@ -package colormapping - -import ( - "fmt" - "io/ioutil" - "testing" -) - -func TestNewPalette(t *testing.T) { - data, err := ioutil.ReadFile("./testdata/unifieddyes_palette_extended.png") - - if err != nil { - t.Fatal(err) - } - - palette, err := NewPalette(data) - - if err != nil { - t.Fatal(err) - } - - color := palette.GetColor(0) - - if color == nil { - t.Fatal("color not found!") - } - - fmt.Println(color) - -} diff --git a/colormapping/testdata/palette_nodes.txt b/colormapping/testdata/palette_nodes.txt deleted file mode 100644 index cec8106..0000000 --- a/colormapping/testdata/palette_nodes.txt +++ /dev/null @@ -1 +0,0 @@ -unifiedbricks:brickblock_multicolor_dark \ No newline at end of file diff --git a/colormapping/testdata/testcolor.txt b/colormapping/testdata/testcolor.txt deleted file mode 100644 index 99bd792..0000000 --- a/colormapping/testdata/testcolor.txt +++ /dev/null @@ -1,4 +0,0 @@ -# comment - -default:river_water_flowing 0 0 100 -scifi_nodes:blacktile2 10 20 30 \ No newline at end of file diff --git a/colormapping/testdata/unifieddyes_palette_extended.png b/colormapping/testdata/unifieddyes_palette_extended.png deleted file mode 100644 index 2ff0323aadaa03a49133702f67c0d4a47d37a158..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 290 zcmV+-0p0$IP)Q~yD0002#Nkl