I have been working towards a render-image
hook that lazyloads images, with a low-resolution placeholder embedded as base64. (see related code)
What I want to do, is to detect transparency in an image resource. Is this even possible?
I wonder if any Hugo wizards know of a way to determine whether an image resource has opacity (either full, or alpha). Digging into the documentation, imageConfig
’s .ColorModel
property seemed promising, but I am getting inconsistent results and do not know how to read the output.
I wish to avoid the placeholder for images that have transparency, because the fully loaded image won’t be able to accurately cover its background. I also wish to avoid using JavaScript to replace the src
attribute. So far this approach works great for opaque images.
Trying ColorModel
, in the case of PNG8 files with a limited palette, sets of what appears to be RGBA values are returned (e.g. {0 0 0 255} …
) but the A is always opaque (255). In the case of PNG24 and JPEG files, I get a single hex value, such as 0xd72140
which I have no idea what it represents.