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.)

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

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效