From b048e784b7e165c624246895d58f0fee15b256d4 Mon Sep 17 00:00:00 2001 From: gucio321 Date: Thu, 19 Oct 2023 10:45:45 +0200 Subject: [PATCH] utils: fix ColorToUint as a side-effect I've added an unittest for UintToColor --- Utils.go | 5 ++++- Utils_test.go | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/Utils.go b/Utils.go index 9edcc635..a42e63af 100644 --- a/Utils.go +++ b/Utils.go @@ -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. diff --git a/Utils_test.go b/Utils_test.go index cf29505b..2d89a840 100644 --- a/Utils_test.go +++ b/Utils_test.go @@ -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) + }) + } +}