To extend putu's answer, comparing the returned color model to the "prepared" models of the image
package only works if one of those models is used, else all comparison will result in false
. Also it is quite inconvenient to list and compare to all possible models.
Instead to find out a talkative form of the color model, we may use this little trick: try to convert any color using the color model of the image. A concrete color model converts all color values (implementations) to the color type / implementation used by the image. Printing the type of the resulting color will tell you what you are looking for.
Example:
col := color.RGBA{} // This is the "any" color we convert
var img image.Image
img = &image.NRGBA{}
fmt.Printf("%T
", img.ColorModel().Convert(col))
img = &image.Gray16{}
fmt.Printf("%T
", img.ColorModel().Convert(col))
img = &image.NYCbCrA{}
fmt.Printf("%T
", img.ColorModel().Convert(col))
img = &image.Paletted{}
fmt.Printf("%T
", img.ColorModel().Convert(col))
Output (try it on the Go Playground):
color.NRGBA
color.Gray16
color.NYCbCrA
<nil>
As can be seen, an image of type *image.NRGBA
models colors using color.NRGBA
, an image of type *image.Gray16
models colors using color.Gray16
etc. As a last example I used *image.Paletted
, where the result was nil
, because the image's palette was empty.
To quickly fix the nil
palette, let's provide an initial palette:
img = &image.Paletted{Palette: []color.Color{color.Gray16{}}}
fmt.Printf("%T
", img.ColorModel().Convert(col))
Now the output will be (try this on the Go Playground):
color.Gray16