Compare commits
5 commits
a2c7aca3d5
...
d148b27426
Author | SHA1 | Date | |
---|---|---|---|
Cat /dev/Nulo | d148b27426 | ||
50049f1530 | |||
2364c6e4e4 | |||
45b66f9f18 | |||
2b7d135f55 |
|
@ -13,4 +13,4 @@ If you add new art, please add a new example under the folder `example`. And wri
|
|||
## Code
|
||||
|
||||
- Add concise and detailed comments.
|
||||
- The code style shold follow the [Go Code Review Comments](https://code.google.com/p/go-wiki/wiki/CodeReviewComments).
|
||||
- The code style should follow the [Go Code Review Comments](https://code.google.com/p/go-wiki/wiki/CodeReviewComments).
|
||||
|
|
|
@ -1,21 +1,24 @@
|
|||
package arts
|
||||
|
||||
import (
|
||||
"math"
|
||||
"math/rand"
|
||||
|
||||
"github.com/fogleman/gg"
|
||||
"github.com/jdxyw/generativeart"
|
||||
"github.com/jdxyw/generativeart/common"
|
||||
"math"
|
||||
"math/rand"
|
||||
)
|
||||
|
||||
type contourLine struct {
|
||||
lineNum int
|
||||
lineNum int
|
||||
lineSize float64
|
||||
}
|
||||
|
||||
// NewContourLine returns a contourLine object.
|
||||
func NewContourLine(lineNum int) *contourLine {
|
||||
func NewContourLine(lineNum int, lineSize float64) *contourLine {
|
||||
return &contourLine{
|
||||
lineNum: lineNum,
|
||||
lineNum: lineNum,
|
||||
lineSize: lineSize,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -35,7 +38,7 @@ func (cl *contourLine) Generative(c *generativeart.Canva) {
|
|||
y += math.Sin(theta) * 0.4
|
||||
|
||||
ctex.SetColor(cls)
|
||||
ctex.DrawEllipse(x, y, 2, 2)
|
||||
ctex.DrawEllipse(x, y, cl.lineSize, cl.lineSize)
|
||||
ctex.Fill()
|
||||
|
||||
if x > float64(c.Width()) || x < 0 || y > float64(c.Height()) || y < 0 || rand.Float64() < 0.001 {
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
package common
|
||||
|
||||
import "image/color"
|
||||
import (
|
||||
"fmt"
|
||||
"image/color"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
MistyRose = color.RGBA{R: 0xFF, G: 0xE4, B: 0xE1, A: 0xFF}
|
||||
|
@ -114,3 +118,56 @@ var (
|
|||
{R: 0xD2, G: 0xFD, B: 0xFF, A: 0xFF},
|
||||
}
|
||||
)
|
||||
|
||||
// ParseHexColor parses color string likes #FFFFFF or #2398EFFF
|
||||
func ParseHexColor(s string) (color.RGBA, error) {
|
||||
s = strings.TrimSpace(strings.ToLower(s))
|
||||
|
||||
if !strings.HasPrefix(s, "#") {
|
||||
return Black, fmt.Errorf("invalid hex color string %v", s)
|
||||
}
|
||||
|
||||
if c, ok := parseHex(s[1:]); ok {
|
||||
return c, nil
|
||||
}
|
||||
return Black, fmt.Errorf("invalid hex color string %v", s)
|
||||
}
|
||||
|
||||
// parseHex returns a color.RGBA by parsing a hex string
|
||||
// Reference: https://stackoverflow.com/questions/54197913/parse-hex-string-to-image-color
|
||||
func parseHex(s string) (color.RGBA, bool) {
|
||||
c := color.RGBA{}
|
||||
c.A = 255
|
||||
ok := true
|
||||
|
||||
hexToByte := func(b byte) byte {
|
||||
switch {
|
||||
case b >= '0' && b <= '9':
|
||||
return b - '0'
|
||||
case b >= 'a' && b <= 'f':
|
||||
return b - 'a' + 10
|
||||
}
|
||||
ok = false
|
||||
return 0
|
||||
}
|
||||
|
||||
n := len(s)
|
||||
if n == 6 || n == 8 {
|
||||
c.R = hexToByte(s[0])<<4+hexToByte(s[1])
|
||||
c.G = hexToByte(s[2])<<4+hexToByte(s[3])
|
||||
c.B = hexToByte(s[4])<<4+hexToByte(s[5])
|
||||
if n == 8 {
|
||||
c.A = hexToByte(s[6])<<4+hexToByte(s[7])
|
||||
}
|
||||
} else if n == 3 || n == 4 {
|
||||
c.R = hexToByte(s[0])*17
|
||||
c.G = hexToByte(s[1])*17
|
||||
c.B = hexToByte(s[2])*17
|
||||
if n == 4 {
|
||||
c.A = hexToByte(s[3])*17
|
||||
}
|
||||
} else {
|
||||
ok = false
|
||||
}
|
||||
return c, ok
|
||||
}
|
30
common/color_test.go
Normal file
30
common/color_test.go
Normal file
|
@ -0,0 +1,30 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"image/color"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestParseHexColor(t *testing.T) {
|
||||
tests := []struct{
|
||||
name string
|
||||
s string
|
||||
wanterr error
|
||||
want color.RGBA
|
||||
}{
|
||||
{name: "testcase1", s: "#112233", wanterr: nil, want: color.RGBA{R: 17, G: 34, B: 51, A: 255}},
|
||||
{name: "testcase2", s: "#123", wanterr: nil, want: color.RGBA{R: 17, G: 34, B: 51, A: 255}},
|
||||
{name: "testcase3", s: "#000233", wanterr: nil, want: color.RGBA{R: 0, G: 2, B: 51, A: 255}},
|
||||
{name: "testcase4", s: "#FFFFFFFF", wanterr: nil, want: color.RGBA{R: 255, G: 255, B: 255, A: 255}},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := ParseHexColor(tt.s)
|
||||
assert.Equal(t, tt.wanterr, err)
|
||||
assert.Equal(t, tt.want, got)
|
||||
})
|
||||
}
|
||||
}
|
4
go.mod
4
go.mod
|
@ -3,8 +3,8 @@ module github.com/jdxyw/generativeart
|
|||
go 1.16
|
||||
|
||||
require (
|
||||
github.com/aquilax/go-perlin v1.0.0
|
||||
github.com/fogleman/gg v1.3.0
|
||||
github.com/llgcode/draw2d v0.0.0-20200930101115-bfaf5d914d1e
|
||||
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
|
||||
github.com/stretchr/testify v1.7.0 // indirect
|
||||
golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb // indirect
|
||||
)
|
||||
|
|
20
go.sum
20
go.sum
|
@ -1,17 +1,17 @@
|
|||
github.com/aquilax/go-perlin v1.0.0 h1:7KBttX3KwqipwhmIVE/B2cEZVYiOZpoE/q8HsS6HBoQ=
|
||||
github.com/aquilax/go-perlin v1.0.0/go.mod h1:z9Rl7EM4BZY0Ikp2fEN1I5mKSOJ26HQpk0O2TBdN2HE=
|
||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/fogleman/gg v1.3.0 h1:/7zJX8F6AaYQc57WQCyN9cAIz+4bCJGO9B+dyW29am8=
|
||||
github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
|
||||
github.com/go-gl/gl v0.0.0-20180407155706-68e253793080/go.mod h1:482civXOzJJCPzJ4ZOX/pwvXBWSnzD4OKMdH4ClKGbk=
|
||||
github.com/go-gl/glfw v0.0.0-20180426074136-46a8d530c326/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
|
||||
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
|
||||
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
|
||||
github.com/jung-kurt/gofpdf v1.0.0/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=
|
||||
github.com/llgcode/draw2d v0.0.0-20200930101115-bfaf5d914d1e h1:YRRazju3DMGuZTSWEj0nE2SCRcK3DW/qdHQ4UQx7sgs=
|
||||
github.com/llgcode/draw2d v0.0.0-20200930101115-bfaf5d914d1e/go.mod h1:mVa0dA29Db2S4LVqDYLlsePDzRJLDfdhVZiI15uY0FA=
|
||||
github.com/llgcode/ps v0.0.0-20150911083025-f1443b32eedb h1:61ndUreYSlWFeCY44JxDDkngVoI7/1MVhEl98Nm0KOk=
|
||||
github.com/llgcode/ps v0.0.0-20150911083025-f1443b32eedb/go.mod h1:1l8ky+Ew27CMX29uG+a2hNOKpeNYEQjjtiALiBlFQbY=
|
||||
golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81/go.mod h1:ux5Hcp/YLpHSI86hEcLt0YII63i6oz57MZXIpbrjZUs=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb h1:fqpd0EBDzlHRCjiphRR5Zo/RSWWQlWv34418dnEixWk=
|
||||
golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
|
Loading…
Reference in a new issue