From 74af10decf5e369c9db8334b346b3b79e5e71b4e Mon Sep 17 00:00:00 2001 From: Jacek Olszak Date: Sun, 13 Aug 2023 15:11:00 +0200 Subject: [PATCH] Rename pi.NewPixMapWithPixels to pi.NewPixMapWithPix pi.PixMap returns pixels using Pix() method. To be consistent with naming I'm renaming NewPixMapWithPixels too. --- .../internal/lib/github_com-elgopher-pi.go | 2 +- pixmap.go | 30 +++++++-------- pixmap_test.go | 38 +++++++++---------- 3 files changed, 35 insertions(+), 35 deletions(-) diff --git a/devtools/internal/lib/github_com-elgopher-pi.go b/devtools/internal/lib/github_com-elgopher-pi.go index 16b336ec..29571c18 100644 --- a/devtools/internal/lib/github_com-elgopher-pi.go +++ b/devtools/internal/lib/github_com-elgopher-pi.go @@ -51,7 +51,7 @@ func init() { "MousePos": reflect.ValueOf(&pi.MousePos).Elem(), "MouseRight": reflect.ValueOf(pi.MouseRight), "NewPixMap": reflect.ValueOf(pi.NewPixMap), - "NewPixMapWithPixels": reflect.ValueOf(pi.NewPixMapWithPixels), + "NewPixMapWithPix": reflect.ValueOf(pi.NewPixMapWithPix), "O": reflect.ValueOf(pi.O), "Palette": reflect.ValueOf(&pi.Palette).Elem(), "Get": reflect.ValueOf(pi.Get), diff --git a/pixmap.go b/pixmap.go index 6ba8f469..50aa122a 100644 --- a/pixmap.go +++ b/pixmap.go @@ -14,7 +14,7 @@ import ( // can be used. PixMap can also be used for maps which not necessary contain pixel colors, such as world map // (as long as only 256 different tiles are used). // -// To create PixMap please use either NewPixMap or NewPixMapWithPixels function. +// To create PixMap please use either NewPixMap or NewPixMapWithPix function. // // All PixMap functions (besides Clear and ClearCol) take into account the clipping region. type PixMap struct { @@ -37,20 +37,20 @@ func NewPixMap(width, height int) PixMap { panic("negative PixMap height") } - pixels := make([]byte, width*height) + pix := make([]byte, width*height) return PixMap{ - pix: pixels, + pix: pix, width: width, height: height, clip: Region{W: width, H: height}, - zeroPix: make([]byte, len(pixels)), + zeroPix: make([]byte, len(pix)), wholeLinePix: make([]byte, width), } } -// NewPixMapWithPixels creates new instance of PixMap using the slice of pixel colors -// as a source. Pixels slice contains colors for the entire PixMap. +// NewPixMapWithPix creates new instance of PixMap using the slice of pixel colors +// as a source. pix slice contains colors for the entire PixMap. // Pixels are organized from left to right, top to bottom. Slice element // number 0 has pixel located in the top-left corner. Slice element number 1 // has pixel color on the right, and so on. @@ -58,33 +58,33 @@ func NewPixMap(width, height int) PixMap { // The lineWidth is the width of PixMap. Height is calculated by dividing pixels // by lineWith. // -// This function is handy when you already have a pixel slice and want to create a PixMap +// This function is handy when you already have a pix slice and want to create a PixMap // out of it. This function does not allocate anything on the heap. -func NewPixMapWithPixels(pixels []byte, lineWidth int) PixMap { +func NewPixMapWithPix(pix []byte, lineWidth int) PixMap { if lineWidth < 0 { panic("PixMap lineWidth cant be negative") } if lineWidth == 0 { - if len(pixels) > 0 { - panic("PixMap lineWidth cant be zero when pixel slice is not empty") + if len(pix) > 0 { + panic("PixMap lineWidth cant be zero when pix slice is not empty") } return PixMap{} } - if len(pixels)%lineWidth != 0 { - panic("invalid pixmap lineWidth. Length of pixels slice must be multiple of lineWidth.") + if len(pix)%lineWidth != 0 { + panic("invalid pixmap lineWidth. Length of pix slice must be multiple of lineWidth.") } - height := len(pixels) / lineWidth + height := len(pix) / lineWidth return PixMap{ - pix: pixels, + pix: pix, width: lineWidth, height: height, clip: Region{W: lineWidth, H: height}, - zeroPix: make([]byte, len(pixels)), + zeroPix: make([]byte, len(pix)), wholeLinePix: make([]byte, lineWidth), } } diff --git a/pixmap_test.go b/pixmap_test.go index e7dc9e24..cf5cb389 100644 --- a/pixmap_test.go +++ b/pixmap_test.go @@ -57,20 +57,20 @@ func TestNewPixMap(t *testing.T) { }) } -func TestNewPixMapWithPixels(t *testing.T) { +func TestNewPixMapWithPix(t *testing.T) { t.Run("should panic when", func(t *testing.T) { tests := map[string]func(){ "length of pixels is not multiple of line width": func() { - pi.NewPixMapWithPixels(make([]byte, 3), 2) + pi.NewPixMapWithPix(make([]byte, 3), 2) }, "pixels slice length is lower than line width": func() { - pi.NewPixMapWithPixels(make([]byte, 1), 2) + pi.NewPixMapWithPix(make([]byte, 1), 2) }, "line width is negative": func() { - pi.NewPixMapWithPixels(make([]byte, 2), -2) + pi.NewPixMapWithPix(make([]byte, 2), -2) }, "line width is zero and pixels slice is not empty": func() { - pi.NewPixMapWithPixels(make([]byte, 1), 0) + pi.NewPixMapWithPix(make([]byte, 1), 0) }, } for name, test := range tests { @@ -83,16 +83,16 @@ func TestNewPixMapWithPixels(t *testing.T) { t.Run("should not panic", func(t *testing.T) { tests := map[string]func(){ "length of pixels is multiple of line width": func() { - pi.NewPixMapWithPixels(make([]byte, 4), 2) + pi.NewPixMapWithPix(make([]byte, 4), 2) }, "line width is equal to pixels slice length": func() { - pi.NewPixMapWithPixels(make([]byte, 2), 2) + pi.NewPixMapWithPix(make([]byte, 2), 2) }, "line width is zero and pixels slice length is 0": func() { - pi.NewPixMapWithPixels(make([]byte, 0), 0) + pi.NewPixMapWithPix(make([]byte, 0), 0) }, "line width is zero and pixels slice is nil": func() { - pi.NewPixMapWithPixels(nil, 0) + pi.NewPixMapWithPix(nil, 0) }, } for name, test := range tests { @@ -103,17 +103,17 @@ func TestNewPixMapWithPixels(t *testing.T) { }) t.Run("should create similar PixMap to NewPixMap", func(t *testing.T) { - pixMap := pi.NewPixMapWithPixels(make([]byte, 6), 3) + pixMap := pi.NewPixMapWithPix(make([]byte, 6), 3) assert.Equal(t, pi.NewPixMap(3, 2), pixMap) }) t.Run("clipping region should cover entire PixMap", func(t *testing.T) { - pixMap := pi.NewPixMapWithPixels(make([]byte, 2*3), 2) + pixMap := pi.NewPixMapWithPix(make([]byte, 2*3), 2) assert.Equal(t, pi.Region{W: 2, H: 3}, pixMap.Clip()) }) t.Run("Width() and Height() should return original dimensions", func(t *testing.T) { - pixMap := pi.NewPixMapWithPixels(make([]byte, 2*3), 2) + pixMap := pi.NewPixMapWithPix(make([]byte, 2*3), 2) pixMap = pixMap.WithClip(1, 1, 1, 1) assert.Equal(t, pixMap.Width(), 2) assert.Equal(t, pixMap.Height(), 3) @@ -121,17 +121,17 @@ func TestNewPixMapWithPixels(t *testing.T) { t.Run("Pix() should return pixel slice passed to constructor", func(t *testing.T) { pix := []byte{1, 2, 3, 4, 5, 6} - pixMap := pi.NewPixMapWithPixels(pix, 2) + pixMap := pi.NewPixMapWithPix(pix, 2) assert.Equal(t, pix, pixMap.Pix()) }) t.Run("should create zero value", func(t *testing.T) { tests := map[string]func() pi.PixMap{ "line width is zero and pixels slice length is 0": func() pi.PixMap { - return pi.NewPixMapWithPixels(make([]byte, 0), 0) + return pi.NewPixMapWithPix(make([]byte, 0), 0) }, "line width is zero and pixels slice is nil": func() pi.PixMap { - return pi.NewPixMapWithPixels(nil, 0) + return pi.NewPixMapWithPix(nil, 0) }, } for name, newPixMap := range tests { @@ -376,7 +376,7 @@ func TestPixMap_Foreach(t *testing.T) { t.Run("should pass trimmed lines", func(t *testing.T) { pix := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8} - pixMap := pi.NewPixMapWithPixels(pix, 3) + pixMap := pi.NewPixMapWithPix(pix, 3) t.Run("x=0", func(t *testing.T) { var lines [][]byte @@ -397,7 +397,7 @@ func TestPixMap_Foreach(t *testing.T) { t.Run("should pass x,y of each line", func(t *testing.T) { pix := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8} - pixMap := pi.NewPixMapWithPixels(pix, 3) + pixMap := pi.NewPixMapWithPix(pix, 3) t.Run("inside clipping range", func(t *testing.T) { var coords []pi.Position @@ -444,7 +444,7 @@ func TestPixMap_Copy(t *testing.T) { func testPixMapCopy(t *testing.T, merge func(pi.PixMap, int, int, int, int, pi.PixMap, int, int)) { t.Run("src bigger than dst", func(t *testing.T) { - src := pi.NewPixMapWithPixels([]byte{ + src := pi.NewPixMapWithPix([]byte{ 1, 2, 3, 4, 5, 6, 7, 8, 9, @@ -495,7 +495,7 @@ func testPixMapCopy(t *testing.T, merge func(pi.PixMap, int, int, int, int, pi.P }) t.Run("src smaller than dst", func(t *testing.T) { - src := pi.NewPixMapWithPixels([]byte{ + src := pi.NewPixMapWithPix([]byte{ 1, 2, 4, 5, 7, 8,