Dedup code.

This commit is contained in:
Sascha L. Teichmann 2016-04-23 21:09:23 +02:00
parent 0db9b519a6
commit 607b4de9ec
4 changed files with 14 additions and 21 deletions

View File

@ -7,7 +7,6 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"image/color"
"log" "log"
"bitbucket.org/s_l_teichmann/mtsatellite/common" "bitbucket.org/s_l_teichmann/mtsatellite/common"
@ -68,12 +67,7 @@ func main() {
common.PrintVersionAndExit() common.PrintVersionAndExit()
} }
var bg color.RGBA bg := common.ParseColorDefault(bgColor, common.BackgroundColor)
var err error
if bg, err = common.ParseColor(bgColor); err != nil {
log.Printf("WARN: Cannot parse background color '%s': %s\n", bgColor, err)
bg = common.BackgroundColor
}
if !skipBaseLevel { if !skipBaseLevel {
td := common.Clamp32f(float32(transparentDim/100.0), 0.0, 1.0) td := common.Clamp32f(float32(transparentDim/100.0), 0.0, 1.0)

View File

@ -8,7 +8,6 @@ import (
"flag" "flag"
"fmt" "fmt"
"image" "image"
"image/color"
"log" "log"
"os" "os"
"runtime/pprof" "runtime/pprof"
@ -65,12 +64,7 @@ func main() {
common.PrintVersionAndExit() common.PrintVersionAndExit()
} }
var bg color.RGBA bg := common.ParseColorDefault(bgColor, common.BackgroundColor)
var err error
if bg, err = common.ParseColor(bgColor); err != nil {
log.Printf("WARN: Cannot parse background color '%s': %s\n", bgColor, err)
bg = common.BackgroundColor
}
if cpuProfile != "" { if cpuProfile != "" {
f, err := os.Create(cpuProfile) f, err := os.Create(cpuProfile)
@ -82,6 +76,7 @@ func main() {
} }
var colors *common.Colors var colors *common.Colors
var err error
if colors, err = common.ParseColors(colorsfile); err != nil { if colors, err = common.ParseColors(colorsfile); err != nil {
log.Fatalf("Cannot open color file: %s", err) log.Fatalf("Cannot open color file: %s", err)
} }

View File

@ -7,7 +7,6 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"image/color"
"log" "log"
"net" "net"
"net/http" "net/http"
@ -83,12 +82,7 @@ func main() {
common.PrintVersionAndExit() common.PrintVersionAndExit()
} }
var bg color.RGBA bg := common.ParseColorDefault(bgColor, common.BackgroundColor)
var err error
if bg, err = common.ParseColor(bgColor); err != nil {
log.Printf("WARN: Cannot parse background color '%s': %s\n", bgColor, err)
bg = common.BackgroundColor
}
router := mux.NewRouter() router := mux.NewRouter()

View File

@ -8,6 +8,7 @@ import (
"bufio" "bufio"
"fmt" "fmt"
"image/color" "image/color"
"log"
"os" "os"
"sort" "sort"
"strconv" "strconv"
@ -136,6 +137,15 @@ func ParseColor(col string) (color.RGBA, error) {
A: 0xff}, nil A: 0xff}, nil
} }
func ParseColorDefault(col string, def color.RGBA) color.RGBA {
c, err := ParseColor(col)
if err != nil {
log.Printf("WARN: cannot parse color '%s': %s\n", col, err)
return def
}
return c
}
func ColorToHex(col color.RGBA) string { func ColorToHex(col color.RGBA) string {
return fmt.Sprintf("#%02x%02x%02x", col.R, col.G, col.B) return fmt.Sprintf("#%02x%02x%02x", col.R, col.G, col.B)
} }