Skip to content

Commit

Permalink
utils: fix ColorToUint
Browse files Browse the repository at this point in the history
as a side-effect I've added an unittest for UintToColor
  • Loading branch information
gucio321 committed Oct 19, 2023
1 parent 65bce2a commit b048e78
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,10 @@ func ColorToUint(col color.Color) uint32 {
r, g, b, a := col.RGBA()
mask := uint32(0xff)

return r&mask<<24 + g&mask<<16 + b&mask<<8 + a&mask
return r&mask +
g&mask<<8 +
b&mask<<16 +
a&mask<<24
}

// UintToColor converts uint32 of form 0xRRGGBB into color.RGBA.
Expand Down
17 changes: 17 additions & 0 deletions Utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,20 @@ func Test_Assert(t *testing.T) {
})
}
}

func TestUintToColor(t *testing.T) {
tests := []struct {
name string
col uint32
want *color.RGBA
}{
{"full red full alpha", 0xFF0000FF, &color.RGBA{R: 255, G: 0, B: 0, A: 255}},
{"full red 0 alpha", 0xFF000000, &color.RGBA{R: 255, G: 0, B: 0, A: 0}},
{"full green", 0x00FF00FF, &color.RGBA{R: 0, G: 255, B: 0, A: 255}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, UintToColor(tt.col), "UintToColor(%v)", tt.col)
})
}
}

0 comments on commit b048e78

Please sign in to comment.