grid line color/continent number font color should be configurable

This commit is contained in:
Dawid Wysokiński 2020-06-22 17:30:51 +02:00
parent 4d96b7a7be
commit b5421f3c70
2 changed files with 30 additions and 14 deletions

View File

@ -3,7 +3,6 @@ package generator
import (
"fmt"
"image"
"image/color"
"image/png"
"io"
@ -12,8 +11,10 @@ import (
)
const (
defaultBackgroundColor = "#000"
defaultMapSize = 1000
defaultBackgroundColor = "#000"
defaultGridLineColor = "#fff"
defaultContinentNumberColor = "#fff"
defaultMapSize = 1000
)
type Marker struct {
@ -22,18 +23,26 @@ type Marker struct {
}
type Config struct {
Markers []*Marker
Destination io.Writer
MapSize int
ContinentGrid bool
ContinentNumbers bool
BackgroundColor string
Markers []*Marker
Destination io.Writer
MapSize int
ContinentGrid bool
ContinentNumbers bool
BackgroundColor string
GridLineColor string
ContinentNumberColor string
}
func (cfg *Config) init() {
if cfg.BackgroundColor == "" {
cfg.BackgroundColor = defaultBackgroundColor
}
if cfg.GridLineColor == "" {
cfg.GridLineColor = defaultGridLineColor
}
if cfg.ContinentNumberColor == "" {
cfg.ContinentNumberColor = defaultContinentNumberColor
}
if cfg.MapSize <= 0 {
cfg.MapSize = defaultMapSize
}
@ -70,27 +79,35 @@ func Generate(cfg Config) error {
//Continents
if cfg.ContinentGrid {
gridLineColor, err := parseHexColorFast(cfg.GridLineColor)
if err != nil {
return errors.Wrap(err, "map-generator")
}
for y := cfg.MapSize / 10; y < cfg.MapSize; y += cfg.MapSize / 10 {
for x := 0; x < cfg.MapSize; x++ {
img.Set(x, y, color.White)
img.Set(x, y, gridLineColor)
}
}
for x := cfg.MapSize / 10; x < cfg.MapSize; x += cfg.MapSize / 10 {
for y := 0; y < cfg.MapSize; y++ {
img.Set(x, y, color.White)
img.Set(x, y, gridLineColor)
}
}
}
if cfg.ContinentNumbers {
continent := 0
continentNumberColor, err := parseHexColorFast(cfg.ContinentNumberColor)
if err != nil {
return errors.Wrap(err, "map-generator")
}
for y := cfg.MapSize / 10; y <= cfg.MapSize; y += cfg.MapSize / 10 {
for x := cfg.MapSize / 10; x <= cfg.MapSize; x += cfg.MapSize / 10 {
continentStr := fmt.Sprintf("%d", continent)
if continent < 10 {
continentStr = fmt.Sprintf("0%d", continent)
}
drawText(img, x-16, y-3, continentStr)
drawText(img, x-16, y-3, continentNumberColor, continentStr)
continent++
}
}

View File

@ -51,8 +51,7 @@ func parseHexColorFast(s string) (color.RGBA, error) {
return c, err
}
func drawText(img *image.RGBA, x, y int, text string) {
col := color.RGBA{200, 100, 0, 255}
func drawText(img *image.RGBA, x, y int, col color.RGBA, text string) {
point := fixed.Point26_6{fixed.Int26_6(x * 64), fixed.Int26_6(y * 64)}
d := &font.Drawer{