-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add whitegold2 pattern * Add newline
- Loading branch information
Showing
10 changed files
with
1,043 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package whitegold2 | ||
|
||
import ( | ||
"io" | ||
"os" | ||
|
||
"github.com/giantswarm/microerror" | ||
"github.com/giantswarm/micrologger" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
const ( | ||
name = "whitegold2" | ||
description = "Draws MarcelMues 'whitegold2'." | ||
) | ||
|
||
type Config struct { | ||
Logger micrologger.Logger | ||
Stderr io.Writer | ||
Stdout io.Writer | ||
} | ||
|
||
func New(config Config) (*cobra.Command, error) { | ||
if config.Logger == nil { | ||
return nil, microerror.Maskf(invalidConfigError, "%T.Logger must not be empty", config) | ||
} | ||
if config.Stderr == nil { | ||
config.Stderr = os.Stderr | ||
} | ||
if config.Stdout == nil { | ||
config.Stdout = os.Stdout | ||
} | ||
|
||
f := &flag{} | ||
|
||
r := &runner{ | ||
flag: f, | ||
logger: config.Logger, | ||
stderr: config.Stderr, | ||
stdout: config.Stdout, | ||
} | ||
|
||
c := &cobra.Command{ | ||
Use: name, | ||
Short: description, | ||
Long: description, | ||
RunE: r.Run, | ||
} | ||
|
||
f.Init(c) | ||
|
||
return c, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package whitegold2 | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/giantswarm/micrologger" | ||
) | ||
|
||
func Test_command(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
c1 string | ||
c2 string | ||
filename string | ||
height int | ||
width int | ||
}{ | ||
{ | ||
name: "case 0: default pattern", | ||
filename: "whitegold2", | ||
}, | ||
{ | ||
name: "case 1: se color pattern", | ||
filename: "whitegold2-se", | ||
c1: "#1e272e", | ||
c2: "#f53b57", | ||
}, | ||
{ | ||
name: "case 2: banner resize", | ||
filename: "whitegold2-wide", | ||
width: 2000, | ||
height: 400, | ||
}, | ||
} | ||
|
||
for i, tc := range testCases { | ||
t.Run(strconv.Itoa(i), func(t *testing.T) { | ||
t.Log(tc.name) | ||
|
||
var err error | ||
|
||
var logger micrologger.Logger | ||
{ | ||
c := micrologger.Config{} | ||
|
||
logger, err = micrologger.New(c) | ||
if err != nil { | ||
t.Fatal("expected", nil, "got", err) | ||
} | ||
} | ||
|
||
c := Config{ | ||
Logger: logger, | ||
} | ||
|
||
testCommand, err := New(c) | ||
if err != nil { | ||
t.Fatal("expected", nil, "got", err) | ||
} | ||
|
||
args := []string{} | ||
if tc.filename != "" { | ||
args = append(args, []string{"--filename", fmt.Sprintf("../../../samples/%s.svg", tc.filename)}...) | ||
} | ||
if tc.c1 != "" { | ||
args = append(args, []string{"--color1", tc.c1}...) | ||
} | ||
if tc.c2 != "" { | ||
args = append(args, []string{"--color2", tc.c2}...) | ||
} | ||
if tc.height != 0 { | ||
args = append(args, []string{"--height", strconv.Itoa(tc.height)}...) | ||
} | ||
if tc.width != 0 { | ||
args = append(args, []string{"--width", strconv.Itoa(tc.width)}...) | ||
} | ||
|
||
testCommand.SetArgs(args) | ||
|
||
err = testCommand.Execute() | ||
if err != nil { | ||
t.Fatal("expected", nil, "got", err) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package whitegold2 | ||
|
||
import "github.com/giantswarm/microerror" | ||
|
||
var invalidConfigError = µerror.Error{ | ||
Kind: "invalidConfigError", | ||
} | ||
|
||
// IsInvalidConfig asserts invalidConfigError. | ||
func IsInvalidConfig(err error) bool { | ||
return microerror.Cause(err) == invalidConfigError | ||
} | ||
|
||
var invalidFlagError = µerror.Error{ | ||
Kind: "invalidFlagError", | ||
} | ||
|
||
// IsInvalidFlag asserts invalidFlagError. | ||
func IsInvalidFlag(err error) bool { | ||
return microerror.Cause(err) == invalidFlagError | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package whitegold2 | ||
|
||
import ( | ||
"github.com/giantswarm/microerror" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/marcelmue/konstrukt/pkg/validate" | ||
) | ||
|
||
const ( | ||
flagFilename = "filename" | ||
|
||
flagHeight = "height" | ||
flagWidth = "width" | ||
|
||
flagColor1 = "color1" | ||
flagColor2 = "color2" | ||
|
||
flagRandomize = "randomize" | ||
) | ||
|
||
type flag struct { | ||
Filename string | ||
|
||
Height int | ||
Width int | ||
|
||
Color1 string | ||
Color2 string | ||
|
||
Randomize bool | ||
} | ||
|
||
func (f *flag) Init(cmd *cobra.Command) { | ||
cmd.Flags().StringVar(&f.Filename, flagFilename, "whitegold2.svg", `Name of the output file.`) | ||
|
||
cmd.Flags().IntVar(&f.Height, flagHeight, 500, `Height of the output file in pixels.`) | ||
cmd.Flags().IntVar(&f.Width, flagWidth, 500, `Width of the output file in pixels.`) | ||
|
||
cmd.Flags().StringVar(&f.Color1, flagColor1, "#95a5a6", `First color.`) | ||
cmd.Flags().StringVar(&f.Color2, flagColor2, "#ecf0f1", `Second color.`) | ||
|
||
cmd.Flags().BoolVar(&f.Randomize, flagRandomize, false, "Randomize all colors in the pattern, ignore other color flags.") | ||
} | ||
|
||
func (f *flag) Validate() error { | ||
if !validate.Color(f.Color1) { | ||
validate.PrintFailure(flagColor1) | ||
return microerror.Mask(invalidFlagError) | ||
} | ||
if !validate.Color(f.Color2) { | ||
validate.PrintFailure(flagColor2) | ||
return microerror.Mask(invalidFlagError) | ||
} | ||
if !validate.Size(f.Height) { | ||
validate.PrintFailure(flagHeight) | ||
return microerror.Mask(invalidFlagError) | ||
} | ||
if !validate.Size(f.Width) { | ||
validate.PrintFailure(flagWidth) | ||
return microerror.Mask(invalidFlagError) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package whitegold2 | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
svg "github.com/ajstarks/svgo" | ||
"github.com/giantswarm/microerror" | ||
"github.com/giantswarm/micrologger" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/marcelmue/konstrukt/pkg/color" | ||
"github.com/marcelmue/konstrukt/pkg/project" | ||
) | ||
|
||
type runner struct { | ||
flag *flag | ||
logger micrologger.Logger | ||
stdout io.Writer | ||
stderr io.Writer | ||
} | ||
|
||
func (r *runner) Run(cmd *cobra.Command, args []string) error { | ||
ctx := context.Background() | ||
|
||
err := r.flag.Validate() | ||
if err != nil { | ||
return microerror.Mask(err) | ||
} | ||
|
||
if r.flag.Randomize { | ||
r.flag.Color1 = color.Random() | ||
r.flag.Color2 = color.Random() | ||
fmt.Printf("Set Color1:%s Color2:%s\n", r.flag.Color1, r.flag.Color2) | ||
} | ||
|
||
err = r.run(ctx, cmd, args) | ||
if err != nil { | ||
return microerror.Mask(err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (r *runner) run(ctx context.Context, cmd *cobra.Command, args []string) error { | ||
f, err := os.Create(r.flag.Filename) | ||
if err != nil { | ||
return microerror.Mask(err) | ||
} | ||
width, height := r.flag.Width, r.flag.Height | ||
c1, c2 := r.flag.Color1, r.flag.Color2 | ||
stylefmt := "stroke:%s;stroke-width:%d;fill:none" | ||
|
||
canvas := svg.New(f) | ||
canvas.Start(width, height) | ||
canvas.Desc(project.PatternDesc()) | ||
canvas.Rect(0, 0, width, height, "fill:"+c1) | ||
|
||
// Polygon. | ||
xp := []int{40, 0, 40, 80} | ||
yp := []int{0, 40, 80, 40} | ||
|
||
// Polyline. | ||
xl := []int{70, 40, 70} | ||
yl := []int{10, 40, 70} | ||
|
||
canvas.Def() | ||
canvas.Gid("unit") | ||
canvas.Polygon(xp, yp, "fill:"+c2) | ||
canvas.Polyline(xl, yl, fmt.Sprintf(stylefmt, c1, 10)) | ||
canvas.Gend() | ||
canvas.Gid("runit") | ||
canvas.ScaleXY(-1, 1) | ||
canvas.Use(0, 0, "#unit") | ||
canvas.Gend() | ||
canvas.Gend() | ||
canvas.DefEnd() | ||
|
||
ypositioncounter := 0 | ||
for y := -100; y < height+200; y += 50 { | ||
for x := -200; x < width+200; x += 100 { | ||
if ypositioncounter%2 == 0 { | ||
canvas.Use(x, y, "#unit") | ||
} | ||
if ypositioncounter%2 == 1 { | ||
canvas.Use(x+30, y, "#runit") | ||
} | ||
} | ||
ypositioncounter++ | ||
} | ||
canvas.End() | ||
|
||
return nil | ||
} |
Oops, something went wrong.