使用Color包从RGB值创建新的颜色?

huangapple go评论148阅读模式
英文:

Using the Color package to create new Color from RGB value?

问题

你可以使用color.RGBA结构体来创建一个新的颜色对象,该结构体具有红、绿、蓝和透明度四个成员变量。你可以通过设置这些成员变量的值来创建一个新的颜色对象。下面是修改后的代码示例:

package main

import (
	"fmt"
	"image"
	"image/color"
	_ "image/gif"
	_ "image/jpeg"
	_ "image/png"
	"os"
)

func main() {
	reader, err := os.Open("test-image.jpg")
	if err != nil {
		fmt.Fprintf(os.Stderr, "%v\n", err)
	}

	image, _, err := image.Decode(reader)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s", err)
	}

	bounds := image.Bounds()

	for i := 0; i <= bounds.Max.X; i++ {
		for j := 0; j <= bounds.Max.Y; j++ {
			pixel := image.At(i, j)
			if i == 0 && j == 0 {
				red, green, blue, _ := pixel.RGBA()
				averaged := (red + green + blue) / 3

				grayColor := color.RGBA{
					R: uint8(averaged >> 8),
					G: uint8(averaged >> 8),
					B: uint8(averaged >> 8),
					A: 255,
				}

				// Then you can do something like:
				grayColor.RGBA() // This would work since it's a type color.RGBA.

			}
		}
	}
}

在上面的代码中,我们使用color.RGBA结构体来创建一个新的颜色对象grayColor,并设置其红、绿、蓝和透明度成员变量的值。请注意,我们需要将averaged的值进行适当的位移和类型转换,以确保它适合uint8类型的成员变量。

英文:

I'm trying to create a new Color object using RGB values I have in variables:

http://golang.org/pkg/image/color/


package main

import (
	&quot;fmt&quot;
	&quot;image&quot;
	_ &quot;image/gif&quot;
	_ &quot;image/jpeg&quot;
	_ &quot;image/png&quot;
	&quot;os&quot;
)

func main() {
	reader, err := os.Open(&quot;test-image.jpg&quot;)
	if err != nil {
		fmt.Fprintf(os.Stderr, &quot;%v\n&quot;, err)
	}

	image, _, err := image.Decode(reader)
	if err != nil {
		fmt.Fprintf(os.Stderr, &quot;%s&quot;, err)
	}

	bounds := image.Bounds()

	for i := 0; i &lt;= bounds.Max.X; i++ {
		for j := 0; j &lt;= bounds.Max.Y; j++ {
			pixel := image.At(i, j)
			if i == 0 &amp;&amp; j == 0 {
				red, green, blue, _ := pixel.RGBA()
				averaged := (red + green + blue) / 3
               
                            // This FromRGBA function DOES NOT EXIST!
				grayColor := Color.FromRGBA(averaged, averaged, averaged, 1)

				// Then I could do something like:
				grayColor.RGBA() // This would work since it&#39;s a type Color.

			}
		}
	}
}

I can't seem to find any package Function that generates a new Color object given rgba values.

Any recommendations?

答案1

得分: 2

image.Color实际上是一个接口。你可以使用任何满足该接口的结构体,甚至是你自己定义的结构体。

例如,你可以使用image.Gray:

grayColor := image.Gray{averaged}

或者你自己定义的grayColor:

type MyGray struct {
    y uint32
}

func (gray *MyGray) FromRGBA(r, g, b, a uint32) {
    gray.y = (r + g + b) / 3
}

func (gray *MyGray) RGBA() (r, g, b, a uint32) { // 满足image.Color接口
    return gray.y, gray.y, gray.y, 1
}

grayColor := &MyGray{}
grayColor.FromRGBA(pixel.RGBA())
grayColor.RGBA()
// blablabla
英文:

The image.Color actually is an interface. You can use any structure which satisfies it. Even your own structures.

For example, you could use image.Gary:

grayColor := image.Gray{averaged}

or your own grayColor:

type MyGray struct {
    y uint32
}

func (gray *MyGray) FromRGBA(r, g, b, a uint32) {
    gray.y = (r + g + b) / 3
}

func (gray *MyGray) RGBA() (r, g, b, a uint32) { // to satisfy image.Color
    return gray.y, gray.y, gray.y, 1
}

grayColor := &amp;MyGray{}
grayColor.FromRGBA(pixel.RGBA())
grayColor.RGBA()
// blablabla

答案2

得分: 1

image/color包中的类型具有导出字段,因此您可以直接实例化它们。对于您的示例,您可以使用以下代码创建灰色值:

grayColor := color.Gray16{Y: uint16(averaged)}

redgreenblue值都在0..0xffff范围内,因此使用16位灰色实现似乎是合适的)。

英文:

The types in the image/color package have exported fields, so you can instantiate them directly. For your example, you could create the colour value with:

grayColor := color.Gray16{Y: uint16(averaged)}

(The red, green and blue values are all in the 0..0xffff range, so the 16-bit Gray colour implementation seems appropriate).

huangapple
  • 本文由 发表于 2014年2月28日 08:04:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/22083460.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定