英文:
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 (
"fmt"
"image"
_ "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
// 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'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 := &MyGray{}
grayColor.FromRGBA(pixel.RGBA())
grayColor.RGBA()
// blablabla
答案2
得分: 1
image/color
包中的类型具有导出字段,因此您可以直接实例化它们。对于您的示例,您可以使用以下代码创建灰色值:
grayColor := color.Gray16{Y: uint16(averaged)}
(red
、green
和blue
值都在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).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论