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

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

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

问题

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

  1. package main
  2. import (
  3. "fmt"
  4. "image"
  5. "image/color"
  6. _ "image/gif"
  7. _ "image/jpeg"
  8. _ "image/png"
  9. "os"
  10. )
  11. func main() {
  12. reader, err := os.Open("test-image.jpg")
  13. if err != nil {
  14. fmt.Fprintf(os.Stderr, "%v\n", err)
  15. }
  16. image, _, err := image.Decode(reader)
  17. if err != nil {
  18. fmt.Fprintf(os.Stderr, "%s", err)
  19. }
  20. bounds := image.Bounds()
  21. for i := 0; i <= bounds.Max.X; i++ {
  22. for j := 0; j <= bounds.Max.Y; j++ {
  23. pixel := image.At(i, j)
  24. if i == 0 && j == 0 {
  25. red, green, blue, _ := pixel.RGBA()
  26. averaged := (red + green + blue) / 3
  27. grayColor := color.RGBA{
  28. R: uint8(averaged >> 8),
  29. G: uint8(averaged >> 8),
  30. B: uint8(averaged >> 8),
  31. A: 255,
  32. }
  33. // Then you can do something like:
  34. grayColor.RGBA() // This would work since it's a type color.RGBA.
  35. }
  36. }
  37. }
  38. }

在上面的代码中,我们使用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/


  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;image&quot;
  5. _ &quot;image/gif&quot;
  6. _ &quot;image/jpeg&quot;
  7. _ &quot;image/png&quot;
  8. &quot;os&quot;
  9. )
  10. func main() {
  11. reader, err := os.Open(&quot;test-image.jpg&quot;)
  12. if err != nil {
  13. fmt.Fprintf(os.Stderr, &quot;%v\n&quot;, err)
  14. }
  15. image, _, err := image.Decode(reader)
  16. if err != nil {
  17. fmt.Fprintf(os.Stderr, &quot;%s&quot;, err)
  18. }
  19. bounds := image.Bounds()
  20. for i := 0; i &lt;= bounds.Max.X; i++ {
  21. for j := 0; j &lt;= bounds.Max.Y; j++ {
  22. pixel := image.At(i, j)
  23. if i == 0 &amp;&amp; j == 0 {
  24. red, green, blue, _ := pixel.RGBA()
  25. averaged := (red + green + blue) / 3
  26. // This FromRGBA function DOES NOT EXIST!
  27. grayColor := Color.FromRGBA(averaged, averaged, averaged, 1)
  28. // Then I could do something like:
  29. grayColor.RGBA() // This would work since it&#39;s a type Color.
  30. }
  31. }
  32. }
  33. }

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:

  1. grayColor := image.Gray{averaged}

或者你自己定义的grayColor:

  1. type MyGray struct {
  2. y uint32
  3. }
  4. func (gray *MyGray) FromRGBA(r, g, b, a uint32) {
  5. gray.y = (r + g + b) / 3
  6. }
  7. func (gray *MyGray) RGBA() (r, g, b, a uint32) { // 满足image.Color接口
  8. return gray.y, gray.y, gray.y, 1
  9. }
  10. grayColor := &MyGray{}
  11. grayColor.FromRGBA(pixel.RGBA())
  12. grayColor.RGBA()
  13. // 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:

  1. grayColor := image.Gray{averaged}

or your own grayColor:

  1. type MyGray struct {
  2. y uint32
  3. }
  4. func (gray *MyGray) FromRGBA(r, g, b, a uint32) {
  5. gray.y = (r + g + b) / 3
  6. }
  7. func (gray *MyGray) RGBA() (r, g, b, a uint32) { // to satisfy image.Color
  8. return gray.y, gray.y, gray.y, 1
  9. }
  10. grayColor := &amp;MyGray{}
  11. grayColor.FromRGBA(pixel.RGBA())
  12. grayColor.RGBA()
  13. // blablabla

答案2

得分: 1

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

  1. 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:

  1. 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:

确定