github.com/as/shiny@v0.8.2/imageutil/imageutil.go (about) 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 imageutil implements some image utility functions. 6 package imageutil 7 8 import ( 9 "image" 10 ) 11 12 // TODO: move Border into the standard library's package image? 13 14 // Border returns four rectangles that together contain those points between r 15 // and r.Inset(inset). Visually: 16 // 17 // 00000000 18 // 00000000 19 // 11....22 20 // 11....22 21 // 11....22 22 // 33333333 23 // 33333333 24 // 25 // The inset may be negative, in which case the points will be outside r. 26 // 27 // Some of the returned rectangles may be empty. None of the returned 28 // rectangles will overlap. 29 func Border(r image.Rectangle, inset int) [4]image.Rectangle { 30 if inset == 0 { 31 return [4]image.Rectangle{} 32 } 33 if r.Dx() <= 2*inset || r.Dy() <= 2*inset { 34 return [4]image.Rectangle{r} 35 } 36 37 x := [4]int{ 38 r.Min.X, 39 r.Min.X + inset, 40 r.Max.X - inset, 41 r.Max.X, 42 } 43 y := [4]int{ 44 r.Min.Y, 45 r.Min.Y + inset, 46 r.Max.Y - inset, 47 r.Max.Y, 48 } 49 if inset < 0 { 50 x[0], x[1] = x[1], x[0] 51 x[2], x[3] = x[3], x[2] 52 y[0], y[1] = y[1], y[0] 53 y[2], y[3] = y[3], y[2] 54 } 55 56 // The top and bottom sections are responsible for filling the corners. 57 // The top and bottom sections go from x[0] to x[3], across the y's. 58 // The left and right sections go from y[1] to y[2], across the x's. 59 60 return [4]image.Rectangle{{ 61 // Top section. 62 Min: image.Point{ 63 X: x[0], 64 Y: y[0], 65 }, 66 Max: image.Point{ 67 X: x[3], 68 Y: y[1], 69 }, 70 }, { 71 // Left section. 72 Min: image.Point{ 73 X: x[0], 74 Y: y[1], 75 }, 76 Max: image.Point{ 77 X: x[1], 78 Y: y[2], 79 }, 80 }, { 81 // Right section. 82 Min: image.Point{ 83 X: x[2], 84 Y: y[1], 85 }, 86 Max: image.Point{ 87 X: x[3], 88 Y: y[2], 89 }, 90 }, { 91 // Bottom section. 92 Min: image.Point{ 93 X: x[0], 94 Y: y[2], 95 }, 96 Max: image.Point{ 97 X: x[3], 98 Y: y[3], 99 }, 100 }} 101 }