Skip to content

Instantly share code, notes, and snippets.

@flc
Created September 4, 2013 14:17
Show Gist options
  • Save flc/6437570 to your computer and use it in GitHub Desktop.
Save flc/6437570 to your computer and use it in GitHub Desktop.
A Tour of Go - Exercise: Images http://tour.golang.org/#60
package main
import (
"code.google.com/p/go-tour/pic"
"image"
"image/color"
)
type Image struct{
width int
height int
}
func (img Image) ColorModel() color.Model {
return color.RGBAModel
}
func (img Image) Bounds() image.Rectangle {
return image.Rect(0, 0, img.width, img.height)
}
func (img Image) At(x, y int) color.Color {
img_func := func(x, y int) uint8 {
//return uint8(x*y)
//return uint8((x+y) / 2)
return uint8(x^y)
}
v := img_func(x, y)
return color.RGBA{v, v, 255, 255}
}
func main() {
m := Image{256, 64}
pic.ShowImage(m)
}
@nobjohns
Copy link

nobjohns commented Sep 19, 2017

package main

import (
	"golang.org/x/tour/pic"
	"image"
	"image/color"
)

type Image struct{
	w, h int
	colorB, colorA uint8
}

func (i *Image) ColorModel() color.Model {
	return color.RGBAModel
}

func (i *Image) Bounds() image.Rectangle {
	return image.Rect(0, 0, i.w , i.h)
}

func (i *Image) At(x, y int) color.Color {
	return color.RGBA{uint8(x), uint8(y), i.colorB, i.colorA}
}

func main() {
	m := &Image{111, 111, 255, 255}
	pic.ShowImage(m)
}

@SarKerson
Copy link

package main

import (
	"golang.org/x/tour/pic"
	"image"
	"image/color"
)

type Image struct {
	rows, cols int
}

func (m *Image) Bounds() image.Rectangle {
	return image.Rect(0, 0, m.rows, m.cols)
}

func (m *Image) ColorModel() color.Model {
	return color.RGBAModel
}

func (m *Image) At(x, y int) color.Color {
	v := uint8(x ^ y)
	return color.RGBA{v, v, 255, 255}
}

func main() {
	m := Image{64, 64}
	pic.ShowImage(&m)
}

@b4nst
Copy link

b4nst commented Jul 31, 2019

Add swagg

package main

import (
	"image"
	"image/color"

	"golang.org/x/tour/pic"
)

type Image struct{ w, h int }

func (i Image) ColorModel() color.Model {
	return color.RGBAModel
}

func (i Image) Bounds() image.Rectangle {
	return image.Rect(0, 0, i.w, i.h)
}

func (i Image) At(x, y int) color.Color {
	r, g, b := uint8(x*y), uint8(x^y), uint8((x+y)/2)
	return color.RGBA{r, g, b, 255}
}

func main() {
	m := Image{1000,1000}
	pic.ShowImage(m)
}

@Alexplay
Copy link

package main

import (
	"image"
	"image/color"

	"golang.org/x/tour/pic"
)

type Image struct {
	w int
	h int
}

func (i Image) ColorModel() color.Model {
	return color.RGBAModel
}

func (i Image) Bounds() image.Rectangle {
	return image.Rect(0, 0, i.w, i.h)
}

func (i Image) At(x, y int) color.Color {
	v := uint8(2*x ^ -2*x - (y ^ x))
	return color.RGBA{v, v, v ^ v ^ -v, 255}
}

func main() {
	m := Image{500, 500}
	pic.ShowImage(m)
}

@metatronz
Copy link

package main

import (
	"golang.org/x/tour/pic"
	"image"
	"image/color"
)

type MyImage struct {
	w int
	h int
}

func (i MyImage) Bounds() image.Rectangle {
	return image.Rect(0, 0, i.w, i.h)
}
func (i MyImage) ColorModel() color.Model {
	return color.RGBAModel
}
func (i MyImage) At(x, y int) color.Color {
	return color.RGBA{uint8(x*y), uint8((x+y)/2), uint8(x^y), 255}
}

func main() {
	m := MyImage{256, 256}
	pic.ShowImage(m)
}

@magicparty
Copy link

magicparty commented Jan 20, 2020

package main

import (
	"golang.org/x/tour/pic"
	"image"
	"image/color"
)

type Image struct{
	w, h int
}

func (i *Image) ColorModel() color.Model {
	return color.RGBAModel
}

func (i *Image) Bounds() image.Rectangle {
	return image.Rect(0, 0, i.w , i.h)
}

func (i *Image) At(x, y int) color.Color {
	v := x*x*y*y
	return color.RGBA{uint8(v), uint8(v), 255, 255}
}

func main() {
	m := &Image{300, 300}
	pic.ShowImage(m)
}

@rickygao
Copy link

rickygao commented Jul 18, 2020

package main

import (
	"image"
	"image/color"
	"math"

	"golang.org/x/tour/pic"
)

type Image struct {
	w, h int
}

func (*Image) ColorModel() color.Model {
	return color.RGBAModel
}

func (m *Image) Bounds() image.Rectangle {
	return image.Rect(0, 0, m.w, m.h)
}

func (m *Image) At(x, y int) color.Color {
	fx := (float64(x) + .5) / float64(m.w)
	fy := (float64(y) + .5) / float64(m.h)
	r := uint8(math.Round(255 * fx))
	g := uint8(math.Round(255 * fy))
	return color.RGBA{r, g, 255, 255}
}

func main() {
	m := &Image{w: 400, h: 400}
	pic.ShowImage(m)
}

@Karllzy
Copy link

Karllzy commented Jul 29, 2021

package main

import (
	"image"
	"image/color"

	"golang.org/x/tour/pic"
)

type Image struct {
	width, height int
}

func (self Image) Bounds() image.Rectangle {
	return image.Rect(0, 0, self.width, self.height)
}

func (self Image) ColorModel() color.Model {
	return color.RGBAModel
}

func (self Image) At(x, y int) color.Color {
	v := uint8(x ^ y)
	return color.RGBA{v, v, 255, 255}

}

func main() {
	m := Image{300, 300}
	pic.ShowImage(m)
}

@eoinahern
Copy link

i just wasnt familiar enough with this lib to know what was going on here.

@vojtaripa
Copy link

vojtaripa commented Nov 4, 2022

package main

import (
  "golang.org/x/tour/pic"
  "image"
  "image/color"
)

//HERE IS THE STRUCTURE OF AN IMAGE:
type Image struct{
  width, height int
  color uint8
}


//THESE 3 methods are used to generate the image called by ShowImage method
//follow this structure: https://pkg.go.dev/image#Image

//FUNCTION ColorModel -> color Model
func (i Image) ColorModel() color.Model {
  return color.RGBAModel
}

//FUNCTION Bounds -> returns Rectangle
func (i Image) Bounds() image.Rectangle {
  return image.Rect(0, 0, i.width, i.height)
}


//FUNCTION AT -> color
func (i Image) At(x, y int) color.Color {
  return color.RGBA{
      uint8(x), 
      uint8(y), 
      255, 
      255}
}

// GENERATE RGB COLORS
func main() {
    
    //Define image size -> Pass in width, hight, and 8 bit int8 for color (0to 255)
    m := Image{200, 200, 120}
  
    //SHOW IMAGE is 
    pic.ShowImage(m)
}

@krapie
Copy link

krapie commented Dec 9, 2022

package main

import (
	"golang.org/x/tour/pic"
	"image"
	"image/color"
)

type Image struct {
	width, height int
}

func (i Image) ColorModel() color.Model {
	return color.RGBAModel
}

func (i Image) Bounds() image.Rectangle {
	return image.Rect(0, 0, i.width, i.height)
}

func (i Image) At(x, y int) color.Color {
	return color.RGBA{interpretCoordinate(x,y),interpretCoordinate(x,y), 255, 255}
}

func interpretCoordinate(x, y int) uint8 {
	n := x^y
	
	return uint8(n)
}

func main() {
	m := Image{100, 100}
	pic.ShowImage(m)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment