douyi0219 2019-01-22 13:09
浏览 543
已采纳

Golang使RGBA图像显示奇怪的颜色

So I'm making some rgba images pixel by pixel following a certain pattern and saving them as png later on and noticed that when alpha channel es changed with certain colors it changes the whole pixel color when stored as png.

I made a test to show what is currently happening:

    img := image.NewRGBA(image.Rect(0, 0, 250, 250))
    for y := 0; y < height; y++ {
        for x := 0; x < width; x++ {
            f.Read(b)
            img.SetRGBA(x, y, color.RGBA{
                249,
                214,
                133,
                255,
            })
        }
    }
    var buff bytes.Buffer
    err := png.Encode(&buff, img)
    if err != nil {
        log.Println(err)
        return
    }

This will print an image of color #F9D685. But if I change alpha into 200 it will print another one with #6844BC and transparency instead of printing the original color with it's transparency.

Is there a way to solve this? I believe that it's because I'm missing something but can't really figure it out and didn't find anything similar to what's happening to me on google/here.

  • 写回答

1条回答 默认 最新

  • douyinyi7766 2019-01-22 13:17
    关注

    That one is simple:

    go doc color.RGBA
    

    package color // import "image/color"

    type RGBA struct {
        R, G, B, A uint8
    }
    

    RGBA represents a traditional 32-bit alpha-premultiplied color, having 8 bits for each of red, green, blue and alpha.

    An alpha-premultiplied color component C has been scaled by alpha (A), so has valid values 0 <= C <= A.

    You might be looking for color.NRGBA.

    (Always, really always, consult the documentation of the involved types and functions. Always.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 需要数据库运行的图片
  • ¥15 如何获取vue-video-editor?
  • ¥100 vs2019 mfc程序如何实现64*64/48*48大小的真彩色工具栏
  • ¥15 全志v3s耳机音频输出口怎么外接功放
  • ¥15 华为ensp使用基本ACL限制公司网络访问
  • ¥15 帮我做下照片上的PLC题
  • ¥15 labview2022 使用modbus报缺少依赖?
  • ¥15 谷歌地图是不是不开通结算功能,api会使用不了哦
  • ¥15 unity腾讯云对象存储机型适配
  • ¥15 求全国交通咨询模拟代码,要求如下,可以完全在dev c++运行