-
Notifications
You must be signed in to change notification settings - Fork 18.6k
Description
Go version
go version go1.24.6 linux/amd64
Output of go env in your module/workspace:
AR='ar'
CC='gcc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='g++'
GCCGO='gccgo'
GO111MODULE=''
GOAMD64='v1'
GOARCH='amd64'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/home/taco/.cache/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/home/taco/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build556920992=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/home/taco/go/src/github.com/tdewolff/x/go.mod'
GOMODCACHE='/home/taco/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/taco/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/lib/go'
GOSUMDB='sum.golang.org'
GOTELEMETRY='off'
GOTELEMETRYDIR='/home/taco/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/lib/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.24.6'
GOWORK=''
PKG_CONFIG='pkg-config'What did you do?
The colours returned from color.Color.RGBA are pre-multiplied values of red, green. blue by alpha. This means that color.RGBA{64,64,64,128} is really the colour rgb(128,128,128) at half transparency. This should be taken into account when converting to colours without an alpha channel, currently only Gray and Gray16. In the color.GrayModel, besides the JPEG-like conversion from RGB to grey, it should also return the actual colour value, even if it is (half)transparent. Currently, a white rectangle with a fading border would be darker around the edges in the Gray colour space. I believe this to be a bug, but I'd like your opinion on this.
Current implementation:
func grayModel(c Color) Color {
if _, ok := c.(Gray); ok {
return c
}
r, g, b, _ := c.RGBA()
// These coefficients (the fractions 0.299, 0.587 and 0.114) are the same
// as those given by the JFIF specification and used by func RGBToYCbCr in
// ycbcr.go.
//
// Note that 19595 + 38470 + 7471 equals 65536.
//
// The 24 is 16 + 8. The 16 is the same as used in RGBToYCbCr. The 8 is
// because the return value is 8 bit color, not 16 bit color.
y := (19595*r + 38470*g + 7471*b + 1<<15) >> 24
// MISSING: y = (((y<<8) | y) * 0xff / a) >> 8
return Gray{uint8(y)}
}
See also https://go.dev/play/p/vXNDCPPFkNk for the output and expected output.
What did you see happen?
RGBA: {64 64 64 128}
NRGBA: {127 127 127 128}
Gray: {64}
What did you expect to see?
RGBA: {64 64 64 128}
NRGBA: {128 128 128 128}
Gray: {128}
or
RGBA: {64 64 64 128}
NRGBA: {127 127 127 128}
Gray: {127}