Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Align colors in SDL package to Go specification #581

Merged
merged 6 commits into from
Nov 25, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
sdl/surface: handle colors correctly in Surface.Set method
mokiat committed Nov 21, 2023
commit 795e4620139b69428a616cfcc6d140fce686baf1
173 changes: 79 additions & 94 deletions sdl/surface.go
Original file line number Diff line number Diff line change
@@ -653,165 +653,150 @@ func (surface *Surface) At(x, y int) color.Color {
return color.RGBA{r, g, b, a}
}

// Set the color of the pixel at (x, y) using this surface's color model to
// convert c to the appropriate color. This method is required for the
// Set the color of the pixel at (x, y) using this surface's color format to
// convert c to the appropriate byte sequence. This method is required for the
// draw.Image interface. The surface may require locking before calling Set.
func (surface *Surface) Set(x, y int, c color.Color) {
// All sdl2 colors are a subset of NRGBA so it is safe precision-wise to
// convert to NRGBA and use the color components from there.
nrgbaColor := color.NRGBAModel.Convert(c).(color.NRGBA)
colR, colG, colB, colA := nrgbaColor.R, nrgbaColor.G, nrgbaColor.B, nrgbaColor.A

pix := surface.Pixels()
i := int32(y)*surface.Pitch + int32(x)*int32(surface.Format.BytesPerPixel)
switch surface.Format.Format {
case PIXELFORMAT_ARGB8888:
col := surface.ColorModel().Convert(c).(ARGB8888)
pix[i+3] = col.A
pix[i+2] = col.R
pix[i+1] = col.G
pix[i+0] = col.B
pix[i+3] = colA
pix[i+2] = colR
pix[i+1] = colG
pix[i+0] = colB
case PIXELFORMAT_ABGR8888:
col := surface.ColorModel().Convert(c).(ABGR8888)
pix[i+3] = col.A
pix[i+2] = col.B
pix[i+1] = col.G
pix[i+0] = col.R
pix[i+3] = colA
pix[i+2] = colB
pix[i+1] = colG
pix[i+0] = colR
case PIXELFORMAT_RGB24, PIXELFORMAT_RGB888:
col := surface.ColorModel().Convert(c).(RGB888)
pix[i+2] = col.R
pix[i+1] = col.G
pix[i+0] = col.B
pix[i+2] = colR
pix[i+1] = colG
pix[i+0] = colB
case PIXELFORMAT_BGR24, PIXELFORMAT_BGR888:
col := surface.ColorModel().Convert(c).(BGR888)
pix[i+2] = col.B
pix[i+1] = col.G
pix[i+0] = col.R
pix[i+2] = colB
pix[i+1] = colG
pix[i+0] = colR
case PIXELFORMAT_RGB444:
col := surface.ColorModel().Convert(c).(RGB444)
buf := (*uint32)(unsafe.Pointer(&pix[i]))
r := uint32(col.R) >> 4 & 0x0F
g := uint32(col.G) >> 4 & 0x0F
b := uint32(col.B) >> 4 & 0x0F
r := uint32(colR) >> 4 & 0x0F
g := uint32(colG) >> 4 & 0x0F
b := uint32(colB) >> 4 & 0x0F
*buf = r<<8 | g<<4 | b
case PIXELFORMAT_RGB332:
col := surface.ColorModel().Convert(c).(RGB332)
buf := (*uint32)(unsafe.Pointer(&pix[i]))
r := uint32(col.R) >> 5 & 0x0F
g := uint32(col.G) >> 5 & 0x0F
b := uint32(col.B) >> 6 & 0x0F
r := uint32(colR) >> 5 & 0x0F
g := uint32(colG) >> 5 & 0x0F
b := uint32(colB) >> 6 & 0x0F
*buf = r<<5 | g<<2 | b
case PIXELFORMAT_RGB565:
col := surface.ColorModel().Convert(c).(RGB565)
buf := (*uint32)(unsafe.Pointer(&pix[i]))
r := uint32(col.R) >> 3 & 0xFF
g := uint32(col.G) >> 2 & 0xFF
b := uint32(col.B) >> 3 & 0xFF
r := uint32(colR) >> 3 & 0xFF
g := uint32(colG) >> 2 & 0xFF
b := uint32(colB) >> 3 & 0xFF
*buf = r<<11 | g<<5 | b
case PIXELFORMAT_RGB555:
col := surface.ColorModel().Convert(c).(RGB555)
buf := (*uint32)(unsafe.Pointer(&pix[i]))
r := uint32(col.R) >> 3 & 0xFF
g := uint32(col.G) >> 3 & 0xFF
b := uint32(col.B) >> 3 & 0xFF
r := uint32(colR) >> 3 & 0xFF
g := uint32(colG) >> 3 & 0xFF
b := uint32(colB) >> 3 & 0xFF
*buf = r<<10 | g<<5 | b
case PIXELFORMAT_BGR565:
col := surface.ColorModel().Convert(c).(BGR565)
buf := (*uint32)(unsafe.Pointer(&pix[i]))
r := uint32(col.R) >> 3 & 0xFF
g := uint32(col.G) >> 2 & 0xFF
b := uint32(col.B) >> 3 & 0xFF
r := uint32(colR) >> 3 & 0xFF
g := uint32(colG) >> 2 & 0xFF
b := uint32(colB) >> 3 & 0xFF
*buf = b<<11 | g<<5 | r
case PIXELFORMAT_BGR555:
col := surface.ColorModel().Convert(c).(BGR555)
buf := (*uint32)(unsafe.Pointer(&pix[i]))
r := uint32(col.R) >> 3 & 0xFF
g := uint32(col.G) >> 3 & 0xFF
b := uint32(col.B) >> 3 & 0xFF
r := uint32(colR) >> 3 & 0xFF
g := uint32(colG) >> 3 & 0xFF
b := uint32(colB) >> 3 & 0xFF
*buf = b<<10 | g<<5 | r
case PIXELFORMAT_ARGB4444:
col := surface.ColorModel().Convert(c).(ARGB4444)
buf := (*uint32)(unsafe.Pointer(&pix[i]))
a := uint32(col.A) >> 4 & 0x0F
r := uint32(col.R) >> 4 & 0x0F
g := uint32(col.G) >> 4 & 0x0F
b := uint32(col.B) >> 4 & 0x0F
a := uint32(colA) >> 4 & 0x0F
r := uint32(colR) >> 4 & 0x0F
g := uint32(colG) >> 4 & 0x0F
b := uint32(colB) >> 4 & 0x0F
*buf = a<<12 | r<<8 | g<<4 | b
case PIXELFORMAT_ABGR4444:
col := surface.ColorModel().Convert(c).(ABGR4444)
buf := (*uint32)(unsafe.Pointer(&pix[i]))
a := uint32(col.A) >> 4 & 0x0F
r := uint32(col.R) >> 4 & 0x0F
g := uint32(col.G) >> 4 & 0x0F
b := uint32(col.B) >> 4 & 0x0F
a := uint32(colA) >> 4 & 0x0F
r := uint32(colR) >> 4 & 0x0F
g := uint32(colG) >> 4 & 0x0F
b := uint32(colB) >> 4 & 0x0F
*buf = a<<12 | b<<8 | g<<4 | r
case PIXELFORMAT_RGBA4444:
col := surface.ColorModel().Convert(c).(RGBA4444)
buf := (*uint32)(unsafe.Pointer(&pix[i]))
r := uint32(col.R) >> 4 & 0x0F
g := uint32(col.G) >> 4 & 0x0F
b := uint32(col.B) >> 4 & 0x0F
a := uint32(col.A) >> 4 & 0x0F
r := uint32(colR) >> 4 & 0x0F
g := uint32(colG) >> 4 & 0x0F
b := uint32(colB) >> 4 & 0x0F
a := uint32(colA) >> 4 & 0x0F
*buf = r<<12 | g<<8 | b<<4 | a
case PIXELFORMAT_BGRA4444:
col := surface.ColorModel().Convert(c).(BGRA4444)
buf := (*uint32)(unsafe.Pointer(&pix[i]))
r := uint32(col.R) >> 4 & 0x0F
g := uint32(col.G) >> 4 & 0x0F
b := uint32(col.B) >> 4 & 0x0F
a := uint32(col.A) >> 4 & 0x0F
r := uint32(colR) >> 4 & 0x0F
g := uint32(colG) >> 4 & 0x0F
b := uint32(colB) >> 4 & 0x0F
a := uint32(colA) >> 4 & 0x0F
*buf = b<<12 | g<<8 | r<<4 | a
case PIXELFORMAT_ARGB1555:
col := surface.ColorModel().Convert(c).(ARGB1555)
buf := (*uint32)(unsafe.Pointer(&pix[i]))
r := uint32(col.R) >> 3 & 0xFF
g := uint32(col.G) >> 3 & 0xFF
b := uint32(col.B) >> 3 & 0xFF
r := uint32(colR) >> 3 & 0xFF
g := uint32(colG) >> 3 & 0xFF
b := uint32(colB) >> 3 & 0xFF
a := uint32(0)
if col.A > 0 {
if colA > 0 {
a = 1
}
*buf = a<<15 | r<<10 | g<<5 | b
case PIXELFORMAT_RGBA5551:
col := surface.ColorModel().Convert(c).(RGBA5551)
buf := (*uint32)(unsafe.Pointer(&pix[i]))
r := uint32(col.R) >> 3 & 0xFF
g := uint32(col.G) >> 3 & 0xFF
b := uint32(col.B) >> 3 & 0xFF
r := uint32(colR) >> 3 & 0xFF
g := uint32(colG) >> 3 & 0xFF
b := uint32(colB) >> 3 & 0xFF
a := uint32(0)
if col.A > 0 {
if colA > 0 {
a = 1
}
*buf = r<<11 | g<<6 | b<<1 | a
case PIXELFORMAT_ABGR1555:
col := surface.ColorModel().Convert(c).(ABGR1555)
buf := (*uint32)(unsafe.Pointer(&pix[i]))
r := uint32(col.R) >> 3 & 0xFF
g := uint32(col.G) >> 3 & 0xFF
b := uint32(col.B) >> 3 & 0xFF
r := uint32(colR) >> 3 & 0xFF
g := uint32(colG) >> 3 & 0xFF
b := uint32(colB) >> 3 & 0xFF
a := uint32(0)
if col.A > 0 {
if colA > 0 {
a = 1
}
*buf = a<<15 | b<<10 | g<<5 | r
case PIXELFORMAT_BGRA5551:
col := surface.ColorModel().Convert(c).(BGRA5551)
buf := (*uint32)(unsafe.Pointer(&pix[i]))
r := uint32(col.R) >> 3 & 0xFF
g := uint32(col.G) >> 3 & 0xFF
b := uint32(col.B) >> 3 & 0xFF
r := uint32(colR) >> 3 & 0xFF
g := uint32(colG) >> 3 & 0xFF
b := uint32(colB) >> 3 & 0xFF
a := uint32(0)
if col.A > 0 {
if colA > 0 {
a = 1
}
*buf = b<<11 | g<<6 | r<<1 | a
case PIXELFORMAT_RGBA8888:
col := surface.ColorModel().Convert(c).(RGBA8888)
pix[i+3] = col.R
pix[i+2] = col.G
pix[i+1] = col.B
pix[i+0] = col.A
pix[i+3] = colR
pix[i+2] = colG
pix[i+1] = colB
pix[i+0] = colA
case PIXELFORMAT_BGRA8888:
col := surface.ColorModel().Convert(c).(BGRA8888)
pix[i+3] = col.B
pix[i+2] = col.G
pix[i+1] = col.R
pix[i+0] = col.A
pix[i+3] = colB
pix[i+2] = colG
pix[i+1] = colR
pix[i+0] = colA
default:
panic("Unknown pixel format!")
}