doufan6544 2017-10-02 19:30
浏览 59
已采纳

image.Image接口中ColorModel()方法的目的是什么?

Go's image.Image interface has three methods: Bounds (clearly necessary for determining the size of an image), At (which returns the actual color at each pixel), and ColorModel. This last method returns a color.Model, which is capable of converting a color from any model into the representation that this image uses.

Why is ColorModel a part of this interface? How is it used by consumers of the image.Image type? If I have an image img, and I know nothing about its underlying representation, what good does img.ColorModel() do me? I can convert any color into the proper model, but I don't see what I can use this converted color for; the other two ways of interacting with an image, At and Bounds, do not take colors as arguments.

Am I missing something? Do any standard library functions call the ColorModel method of an image.Image, and what for?

  • 写回答

2条回答 默认 最新

  • dqpfkzu360216 2017-10-02 19:55
    关注

    I'm not sure if I understand your question entirely, but I do not think the purpose of ColorModel() is to change the color. It is only to get the color model.

    The standard library uses it mostly when encoding images, like in the png package:

    switch m.ColorModel() {
    case color.GrayModel:
        e.cb = cbG8
    case color.Gray16Model:
        e.cb = cbG16
    case color.RGBAModel, color.NRGBAModel, color.AlphaModel:
        if opaque(m) {
            e.cb = cbTC8
        } else {
            e.cb = cbTCA8
        }
    default:
        if opaque(m) {
            e.cb = cbTC16
        } else {
            e.cb = cbTCA16
        }
    }
    

    Another hint on it's intended use can be found in the jpeg/writer:

    // TODO(wathiede): switch on m.ColorModel() instead of type.
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#人工智能#的问题:(2)设计一个GUI,允许语音和文本实现谣言的检测
  • ¥50 请教 麒麟系统挂载怎么安装
  • ¥15 如何在ns3中实现路径的自由切换
  • ¥20 SpringBoot+Vue3
  • ¥15 IT从业者的调查问卷
  • ¥65 LineageOs-21.0系统编译问题
  • ¥30 关于#c++#的问题,请各位专家解答!
  • ¥15 App的会员连续扣费
  • ¥15 不同数据类型的特征融合应该怎么做
  • ¥15 用proteus软件设计一个基于8086微处理器的简易温度计