Created
September 4, 2013 14:17
-
-
Save flc/6437570 to your computer and use it in GitHub Desktop.
A Tour of Go - Exercise: Images
http://tour.golang.org/#60
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
Alexplay
commented
Aug 24, 2019
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)
}
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)
}
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)
}
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)
}
i just wasnt familiar enough with this lib to know what was going on here.
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)
}
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