FAQ

On Wed, Apr 29, 2015 at 6:02 AM, simon place wrote:
But i can't make a colorModel that makes sense and why does it
exist in the interface, if its not required?
We seem to be talking in circles. I'll be blunt. You are trying to use
the image.Image interface in a different way from how it is designed
-- an image's color model is a property of the image overall, not
per-pixel. Your code may compile, and other code that calls the
Image.ColorModel method may be rare enough that nothing obviously
breaks for you, and you may argue that the "Image is a finite
rectangular grid of color.Color values taken from a color model" doc
comment at http://golang.org/pkg/image/#Image is insufficiently clear,
but still, you are trying to use the image.Image interface in a
different way from how it is designed. If something actually does
break, you get to keep the pieces.

As for having a color model that makes sense, as I said, using a
color.RGBAModel makes sense, and would play nicely with any image
encoding library that uses the image's color model to determine the
on-disk or on-wire format. Yes, that means that you have to convert
your color.Gray values to color.RGBA. That is how the image.Image
interface is designed, and is what other code that takes image.Image
arguments can assume. This isn't a stdlib only thing. A third party
image encode for the foobar format can make the same assumptions.

Alternatively you could define a hybrid color model that allows for
both gray and RGBA colors:

func (hybrid) Convert(c color.Color) color.Color {
   if x, ok := c.(color.Gray); ok {
     return x
   }
   return color.RGBAModel.Convert(c)
}

In hindsight, it may have been a better design for the ColorModel
method to be optional, the way that some but not all io.Reader
implementations also implement io.ByteReader, but the image.Image
interface is frozen for the Go 1.x releases.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 8 of 12 | next ›
Discussion Overview
groupgolang-nuts @
categoriesgo
postedApr 25, '15 at 8:20p
activeMay 8, '15 at 11:52p
posts12
users2
websitegolang.org

2 users in discussion

Simon place: 7 posts Nigel Tao: 5 posts

People

Translate

site design / logo © 2021 Grokbase