Test and fix ValueColor.RGBA

This commit is contained in:
Sasha Koshka 2024-07-29 15:45:17 -04:00
parent f42dee22f5
commit 90072d8a9e
2 changed files with 24 additions and 7 deletions

View File

@ -0,0 +1,17 @@
package tss
import "testing"
func TestValueColor (test *testing.T) {
testValueColorRGBA(test, 0xFB380CFF, 0xFBFB, 0x3838, 0x0C0C, 0xFFFF)
testValueColorRGBA(test, 0xFB380C00, 0x0000, 0x0000, 0x0000, 0x0000)
}
func testValueColorRGBA (test *testing.T, col ValueColor, r, g, b, a uint32) {
gr, gg, gb, ga := col.RGBA()
test.Logf("testing RGBA for color #%08X", col)
if gr != r { test.Errorf("r component inequal (%04X != %04X)", gr, r) }
if gg != g { test.Errorf("g component inequal (%04X != %04X)", gg, g) }
if gb != b { test.Errorf("b component inequal (%04X != %04X)", gb, b) }
if ga != a { test.Errorf("a component inequal (%04X != %04X)", ga, a) }
}

View File

@ -35,19 +35,19 @@ func (ValueColor) value () { }
func (value ValueColor) RGBA () (r, g, b, a uint32) {
// extract components
bits := uint32(value)
r = (bits & 0xF000) >> 24
g = (bits & 0x0F00) >> 16
b = (bits & 0x00F0) >> 8
a = (bits & 0x000A)
r = (bits & 0xFF000000) >> 24
g = (bits & 0x00FF0000) >> 16
b = (bits & 0x0000FF00) >> 8
a = (bits & 0x000000FF)
// extend to 16 bits per channel
r = r << 8 | r
g = g << 8 | g
b = b << 8 | b
a = a << 8 | a
// alpha premultiply
r = (r * a) / 256
g = (g * a) / 256
b = (b * a) / 256
r = (r * a) / 0xFFFF
g = (g * a) / 0xFFFF
b = (b * a) / 0xFFFF
return
}