Implement fetaure request issue #17

mtseeder and mtwebmapper got an option to set the background color where no nodes are generated, yet.
This commit is contained in:
Sascha L. Teichmann
2016-04-23 16:45:33 +02:00
parent 0030f7bc02
commit 0db9b519a6
10 changed files with 130 additions and 45 deletions

View File

@ -59,11 +59,13 @@ type BaseTileCreator struct {
baseDir string
update bool
emptyImage []byte
bg color.RGBA
}
func NewBaseTileCreator(
client *RedisClient,
colors *Colors,
bg color.RGBA,
yMin, yMax int16,
transparent bool,
baseDir string,
@ -73,6 +75,7 @@ func NewBaseTileCreator(
return &BaseTileCreator{
client: client,
colors: colors,
bg: bg,
renderer: renderer,
yOrder: NewYOrder(renderer, yOrderCapacity),
yMin: yMin,
@ -141,7 +144,7 @@ func (btc *BaseTileCreator) CreateTile(x, z int16, i, j int) error {
// To avoid redundant encoding cache the resulting empty image.
if btc.emptyImage == nil {
var err error
m := BackgroundImage((tileWidth-2)*16, (tileHeight-2)*16, BackgroundColor)
m := BackgroundImage((tileWidth-2)*16, (tileHeight-2)*16, btc.bg)
if btc.emptyImage, err = EncodeToMem(m); err != nil {
return err
}
@ -152,7 +155,7 @@ func (btc *BaseTileCreator) CreateTile(x, z int16, i, j int) error {
image := btc.renderer.CreateShadedImage(
16, 16, (tileWidth-2)*16, (tileHeight-2)*16,
btc.colors, BackgroundColor)
btc.colors, btc.bg)
log.Printf("Writing (%d, %d) to file %s\n", x, z, path)

View File

@ -10,6 +10,7 @@ import (
"image/color"
"os"
"sort"
"strconv"
"strings"
)
@ -121,3 +122,20 @@ func (colors *Colors) BlendColors(span *Span, col color.RGBA, pos int32) color.R
}
return col
}
func ParseColor(col string) (color.RGBA, error) {
col = strings.TrimLeft(col, "#")
rgb, err := strconv.ParseUint(col, 16, 32)
if err != nil {
return color.RGBA{}, err
}
return color.RGBA{
R: uint8(rgb >> 16),
G: uint8(rgb >> 8),
B: uint8(rgb),
A: 0xff}, nil
}
func ColorToHex(col color.RGBA) string {
return fmt.Sprintf("#%02x%02x%02x", col.R, col.G, col.B)
}

View File

@ -9,6 +9,7 @@ import (
"bytes"
"errors"
"image"
"image/color"
"image/png"
"log"
"os"
@ -94,18 +95,18 @@ func SaveAsPNGAtomic(path string, img image.Image) (err error) {
return os.Rename(tmpPath, path)
}
func LoadPNG(path string) image.Image {
func LoadPNG(path string, bg color.RGBA) image.Image {
var err error
var file *os.File
if file, err = os.Open(path); err != nil {
return image.White
return image.NewUniform(bg)
}
defer file.Close()
reader := bufio.NewReader(file)
var img image.Image
if img, err = png.Decode(reader); err != nil {
log.Printf("WARN: decoding '%s' failed: %s\n", path, err)
return image.White
return image.NewUniform(bg)
}
return img
}