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

View File

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