Skip to content

Commit

Permalink
Rename pi.NewPixMapWithPixels to pi.NewPixMapWithPix
Browse files Browse the repository at this point in the history
pi.PixMap returns pixels using Pix() method. To be consistent with naming I'm renaming NewPixMapWithPixels too.
  • Loading branch information
elgopher committed Aug 13, 2023
1 parent 4da9dda commit 74af10d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 35 deletions.
2 changes: 1 addition & 1 deletion devtools/internal/lib/github_com-elgopher-pi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 15 additions & 15 deletions pixmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -37,54 +37,54 @@ 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.
//
// 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),
}
}
Expand Down
38 changes: 19 additions & 19 deletions pixmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -103,35 +103,35 @@ 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)
})

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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 74af10d

Please sign in to comment.