Skip to content

Commit

Permalink
add onlycolor option
Browse files Browse the repository at this point in the history
  • Loading branch information
kpym committed May 13, 2023
1 parent 04287c0 commit e7c3709
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
8 changes: 8 additions & 0 deletions cmd/svgpattern/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import (
// quelques variables globales
var version = "dev"

// onlycolor is a flag to only output the color
var onlycolor bool

// Aide affiche l'aide d'utilisation
func help() {
var out = os.Stderr
Expand Down Expand Up @@ -118,6 +121,7 @@ func generatorFromParameters() svgpattern.Generator {
flag.StringVarP(&lightness, "lightness", "l", "", "The lightness variation (using HSL colors). Value format is '[value][~deviation]' or 'min:max'.")
flag.StringVarP(&rotate, "rotate", "r", "", "Rotation angle in degree. Value format is '[value][~deviation]' or 'min:max'.")
flag.StringVarP(&scale, "scale", "s", "", "Scale factor. Value format is '[value][~deviation]' or 'min:max'.")
flag.BoolVar(&onlycolor, "onlycolor", false, "Only output the color.")
//parse the flags
flag.Parse()
// chack if parameters were provided
Expand Down Expand Up @@ -185,6 +189,10 @@ func log(format string, a ...interface{}) (n int, err error) {
// Prints pattern's SVG string with a specific background color
func main() {
g := generatorFromParameters()
if onlycolor {
fmt.Println(g.Color())
return
}
svg, ok := g.Generate()
if !ok {
log("There are some errors : %v", g.Errors())
Expand Down
6 changes: 6 additions & 0 deletions pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type Generator interface {
Options(...Option)
Generate() (svg []byte, ok bool)
Errors() []string
Color() string
}

// Errors provide the error messages generated during the initialization/generation process.
Expand Down Expand Up @@ -106,6 +107,11 @@ func (g *generator) Generate() (svg []byte, ok bool) {
return result.Bytes(), len(g.errors) == 0
}

// Color returns the color used to generate the pattern as hex string.
func (g *generator) Color() string {
return g.color.Hex()
}

// An Option is a function that customize the Generator.
type Option func(*generator)

Expand Down

0 comments on commit e7c3709

Please sign in to comment.