Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provide hints to SVG renderers to prevent antialiased edges of filled blocks causing tiled effect #105

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ _testmain.go
*.DS_Store

*_got
*_expected
*_expected

**/.vscode/
debug
tinygraphs
2 changes: 1 addition & 1 deletion draw/isogrids/gradient.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func RandomGradientColor(w io.Writer, colors, gColors []color.RGBA, gv colors.Gr
}

canvas := svg.New(w)
canvas.Start(width, height)
canvas.Start(width, height, draw.DefaultSvgAttributes()...)
canvas.Def()
canvas.LinearGradient("gradientColors", gv.X1, gv.Y1, gv.X2, gv.Y2, gradientColors)
canvas.DefEnd()
Expand Down
2 changes: 1 addition & 1 deletion draw/isogrids/hexa.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
// Hexa builds an image with lines x lines grids of half diagonals in the form of an hexagon
func Hexa(w io.Writer, key string, colors []color.RGBA, size, lines int) {
canvas := svg.New(w)
canvas.Start(size, size)
canvas.Start(size, size, draw.DefaultSvgAttributes()...)

fringeSize := size / lines
distance := distanceTo3rdPoint(fringeSize)
Expand Down
2 changes: 1 addition & 1 deletion draw/isogrids/hexa16.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
// triangle zero triangle on the center left.
func Hexa16(w io.Writer, key string, colors []color.RGBA, size, lines int) {
canvas := svg.New(w)
canvas.Start(size, size)
canvas.Start(size, size, draw.DefaultSvgAttributes()...)

fringeSize := size / lines
distance := distanceTo3rdPoint(fringeSize)
Expand Down
2 changes: 1 addition & 1 deletion draw/isogrids/isogrids.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
// Isogrids builds an image with 10x10 grids of half diagonals
func Isogrids(w io.Writer, key string, colors []color.RGBA, size, lines int) {
canvas := svg.New(w)
canvas.Start(size, size)
canvas.Start(size, size, draw.DefaultSvgAttributes()...)

fringeSize := size / lines
distance := distanceTo3rdPoint(fringeSize)
Expand Down
6 changes: 3 additions & 3 deletions draw/isogrids/labs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
// Diagonals builds an image with 10x10 grids of diagonals.
func Diagonals(w io.Writer, key string, color1, color2 color.RGBA, size int) {
canvas := svg.New(w)
canvas.Start(size, size)
canvas.Start(size, size, draw.DefaultSvgAttributes()...)

lines := 10
fringeSize := size / lines
Expand Down Expand Up @@ -50,7 +50,7 @@ func Diagonals(w io.Writer, key string, color1, color2 color.RGBA, size int) {
// HalfDiagonals builds an image with 10x10 grids of half diagonals
func HalfDiagonals(w io.Writer, key string, color1, color2 color.RGBA, size int) {
canvas := svg.New(w)
canvas.Start(size, size)
canvas.Start(size, size, draw.DefaultSvgAttributes()...)

lines := 10
fringeSize := size / lines
Expand Down Expand Up @@ -88,7 +88,7 @@ func HalfDiagonals(w io.Writer, key string, color1, color2 color.RGBA, size int)
// Skeleton builds an image with 10x10 grids of half diagonals
func Skeleton(w io.Writer, key string, color1, color2 color.RGBA, size int) {
canvas := svg.New(w)
canvas.Start(size, size)
canvas.Start(size, size, draw.DefaultSvgAttributes()...)

lines := 10
fringeSize := size / lines
Expand Down
6 changes: 3 additions & 3 deletions draw/isogrids/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
// The image is symetric in the middle vertical axis.
func Random(w io.Writer, colors []color.RGBA, width, height, lines int, prob float64) {
canvas := svg.New(w)
canvas.Start(width, height)
canvas.Start(width, height, draw.DefaultSvgAttributes()...)

fringeSize := width / lines
distance := distanceTo3rdPoint(fringeSize)
Expand Down Expand Up @@ -56,7 +56,7 @@ func Random(w io.Writer, colors []color.RGBA, width, height, lines int, prob flo
// from left to right.
func RandomGradient(w io.Writer, colors []color.RGBA, width, height, lines int) {
canvas := svg.New(w)
canvas.Start(width, height)
canvas.Start(width, height, draw.DefaultSvgAttributes()...)

fringeSize := width / lines
distance := distanceTo3rdPoint(fringeSize)
Expand Down Expand Up @@ -100,7 +100,7 @@ func RandomGradient(w io.Writer, colors []color.RGBA, width, height, lines int)
// RandomMirror builds an image with 10x10 grids of half diagonals
func RandomMirror(w io.Writer, colors []color.RGBA, size int, prob float64) {
canvas := svg.New(w)
canvas.Start(size, size)
canvas.Start(size, size, draw.DefaultSvgAttributes()...)

lines := 10
fringeSize := size / lines
Expand Down
2 changes: 1 addition & 1 deletion draw/spaceinvaders/spaceinvaders.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
// argument.
func SpaceInvaders(w io.Writer, key string, colors []color.RGBA, size int) {
canvas := svg.New(w)
canvas.Start(size, size)
canvas.Start(size, size, draw.DefaultSvgAttributes()...)
invader := newInvader(key)
// log.Println(fmt.Sprintf("%+v\n", invader)) // for debug
squares := 11
Expand Down
2 changes: 1 addition & 1 deletion draw/squares/gradient.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func RandomGradientColorSVG(w io.Writer, colors, gColors []color.RGBA, gv colors
}

canvas := svg.New(w)
canvas.Start(width, height)
canvas.Start(width, height, draw.DefaultSvgAttributes()...)
canvas.Def()
canvas.LinearGradient("gradientColors", gv.X1, gv.Y1, gv.X2, gv.Y2, gradientColors)
canvas.DefEnd()
Expand Down
2 changes: 1 addition & 1 deletion draw/squares/palette.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func Palette(m *image.RGBA, theme []color.RGBA) {
// PaletteSVG builds an SVG image with all the colors present in the theme color array.
func PaletteSVG(w io.Writer, theme []color.RGBA, width, height int) {
canvas := svg.New(w)
canvas.Start(width, height)
canvas.Start(width, height, draw.DefaultSvgAttributes()...)

numColors := len(theme)
quadrant := width / numColors
Expand Down
4 changes: 2 additions & 2 deletions draw/squares/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func RandomGrid(m *image.RGBA, colors []color.RGBA, xSquares int, prob float64)
// RandomGridSVG builds a grid image with with x colors selected at random for each quadrant.
func RandomGridSVG(w io.Writer, colors []color.RGBA, width, height, xSquares int, prob float64) {
canvas := svg.New(w)
canvas.Start(width, height)
canvas.Start(width, height, draw.DefaultSvgAttributes()...)
squares := xSquares
quadrantSize := width / squares
colorMap := make(map[int]color.RGBA)
Expand Down Expand Up @@ -79,7 +79,7 @@ func RandomGradientGrid(m *image.RGBA, colors []color.RGBA, xSquares int) {
// RandomGradientGridSVG builds a grid image with with x colors selected at random for each quadrant.
func RandomGradientGridSVG(w io.Writer, colors []color.RGBA, width, height, xSquares int) {
canvas := svg.New(w)
canvas.Start(width, height)
canvas.Start(width, height, draw.DefaultSvgAttributes()...)
squares := xSquares
quadrantSize := width / squares
colorMap := make(map[int]color.RGBA)
Expand Down
4 changes: 2 additions & 2 deletions draw/squares/squares.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func Grid(m *image.RGBA, color1, color2 color.RGBA) {
// GridSVG builds an image with 6 by 6 quadrants of alternate colors.
func GridSVG(w io.Writer, color1, color2 color.RGBA, size int) {
canvas := svg.New(w)
canvas.Start(size, size)
canvas.Start(size, size, draw.DefaultSvgAttributes()...)
squares := 6
quadrantSize := size / squares
colorMap := make(map[int]color.RGBA)
Expand Down Expand Up @@ -88,7 +88,7 @@ func Image(m *image.RGBA, key string, colors []color.RGBA) {
// SVG builds an svg image with 6 by 6 quadrants of alternate colors.
func SVG(w io.Writer, key string, colors []color.RGBA, size int) {
canvas := svg.New(w)
canvas.Start(size, size)
canvas.Start(size, size, draw.DefaultSvgAttributes()...)

squares := 6
quadrantSize := size / squares
Expand Down
8 changes: 8 additions & 0 deletions draw/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,11 @@ func PickIndex(key string, n int, index int) int {
func RGBToHex(r, g, b uint8) string {
return fmt.Sprintf("#%02X%02X%02X", r, g, b)
}

// DefaultSvgAttributes returns the default attributes to apply to SVG canvases
func DefaultSvgAttributes() []string {
// shape-rendering provides hints to the renderer about how to render the image
// crispedges and optimizeSpeed prevent the edges of generated blocks from being
// antialiased causing a tiled effect (visible lines between each block)
return []string{"shape-rendering = \"optimizeSpeed\""}
}