...

Source file src/image/draw/example_test.go

Documentation: image/draw

		 1  // Copyright 2016 The Go Authors. All rights reserved.
		 2  // Use of this source code is governed by a BSD-style
		 3  // license that can be found in the LICENSE file.
		 4  
		 5  package draw_test
		 6  
		 7  import (
		 8  	"fmt"
		 9  	"image"
		10  	"image/color"
		11  	"image/draw"
		12  	"math"
		13  )
		14  
		15  func ExampleDrawer_floydSteinberg() {
		16  	const width = 130
		17  	const height = 50
		18  
		19  	im := image.NewGray(image.Rectangle{Max: image.Point{X: width, Y: height}})
		20  	for x := 0; x < width; x++ {
		21  		for y := 0; y < height; y++ {
		22  			dist := math.Sqrt(math.Pow(float64(x-width/2), 2)/3+math.Pow(float64(y-height/2), 2)) / (height / 1.5) * 255
		23  			var gray uint8
		24  			if dist > 255 {
		25  				gray = 255
		26  			} else {
		27  				gray = uint8(dist)
		28  			}
		29  			im.SetGray(x, y, color.Gray{Y: 255 - gray})
		30  		}
		31  	}
		32  	pi := image.NewPaletted(im.Bounds(), []color.Color{
		33  		color.Gray{Y: 255},
		34  		color.Gray{Y: 160},
		35  		color.Gray{Y: 70},
		36  		color.Gray{Y: 35},
		37  		color.Gray{Y: 0},
		38  	})
		39  
		40  	draw.FloydSteinberg.Draw(pi, im.Bounds(), im, image.ZP)
		41  	shade := []string{" ", "░", "▒", "▓", "█"}
		42  	for i, p := range pi.Pix {
		43  		fmt.Print(shade[p])
		44  		if (i+1)%width == 0 {
		45  			fmt.Print("\n")
		46  		}
		47  	}
		48  }
		49  

View as plain text