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// +build ignore 6 7package draw_test 8 9import ( 10 "fmt" 11 "image" 12 "image/color" 13 "image/draw" 14 "math" 15) 16 17func ExampleDrawer_floydSteinberg() { 18 const width = 130 19 const height = 50 20 21 im := image.NewGray(image.Rectangle{Max: image.Point{X: width, Y: height}}) 22 for x := 0; x < width; x++ { 23 for y := 0; y < height; y++ { 24 dist := math.Sqrt(math.Pow(float64(x-width/2), 2)/3+math.Pow(float64(y-height/2), 2)) / (height / 1.5) * 255 25 var gray uint8 26 if dist > 255 { 27 gray = 255 28 } else { 29 gray = uint8(dist) 30 } 31 im.SetGray(x, y, color.Gray{Y: 255 - gray}) 32 } 33 } 34 pi := image.NewPaletted(im.Bounds(), []color.Color{ 35 color.Gray{Y: 255}, 36 color.Gray{Y: 160}, 37 color.Gray{Y: 70}, 38 color.Gray{Y: 35}, 39 color.Gray{Y: 0}, 40 }) 41 42 draw.FloydSteinberg.Draw(pi, im.Bounds(), im, image.ZP) 43 shade := []string{" ", "░", "▒", "▓", "█"} 44 for i, p := range pi.Pix { 45 fmt.Print(shade[p]) 46 if (i+1)%width == 0 { 47 fmt.Print("\n") 48 } 49 } 50} 51