skip2 / go-qrcode

:sparkles: QR Code encoder (Go)
http://go-qrcode.appspot.com
MIT License
2.58k stars 335 forks source link

WriteColorFIle does not seem to work with RGBA values #51

Closed lcmps closed 2 years ago

lcmps commented 2 years ago

I've been doing some tests with this library and it's been great so far, but for some reason whenever I try to generate a QR code with RGBA foreground/background colors the function will create a blank PNG whiteout outputting any errors.

For example:

fg := color.RGBA{0, 0, 0, 1} // black
bg := color.RGBA{0, 0, 0, 0} // white

qrcode.WriteColorFile("dummyData", qrcode.Medium, 300, bg, fg, "colored.png")

Will result in a blank 300x300 png

but doing this:

fg := color.CMYK{0, 0, 0, 100} // black
bg := color.CMYK{0, 0, 0, 0} // white

qrcode.WriteColorFile("dummyData", qrcode.Medium, 300, bg, fg, "colored.png")

Will correctly output a valid QR code and non-blank PNG, passing bg := color.White and fg := color.Black as stated in the documentation works too.

Am I doing something wrong with my RGBA values and this behavior is expected or an actual issue with RGBA implementation somewhere in the library?

Cheers

JTSchwartz commented 2 years ago

In RGBA, all values are 8 bits (0 <= n <= 255) and the final value in the constructor is the Alpha channel, ie transparency. So the two values you have provided are instead both black but mostly, if not completely, transparent.

color.RGBA{0, 0, 0, 0} is 100% transparent, and although you are correct that color.RGBA{0, 0, 0, 1} is black, it is also 99.7% transparent.

True black is color.RGBA{0, 0, 0, 255} and white is color.RGBA{255, 255, 255, 255}

lcmps commented 2 years ago

Thanks for the explanation, you're right. I was thinking of RGBA values in terms of CSS, where the '1' value for Alpha is interpreted as 100% opacity

Thank you