add color circlr, update readme

This commit is contained in:
jdxyw 2021-03-06 23:17:26 +08:00
parent 98938954b1
commit fb7fc70a2f
5 changed files with 169 additions and 45 deletions

100
README.md
View file

@ -7,7 +7,7 @@
`generativeart` is a `Go` package to generate many kinds of generative art. The goal is to collect some excellent generative art (implemented in `R` or `Processing`), and rewrite them in `Go` again. I would paste the original link at the end of this README(If I remember. You can also submit a PR if you found I miss something.). Currently, it supports the following type.
This package is still working in progress, more types would be added. Welcome anyone interested in this filed to submit your PR.
This package is still working in progress, more types would be added. Welcome anyone interested in this field to submit your PR.
- Maze
- Julia Set
@ -23,6 +23,7 @@ This package is still working in progress, more types would be added. Welcome an
- Point Ribbon
- Janus
- Random Shapes
- Color Circle
For these kinds of art, the package provides as many as parameters to control the appearance.
@ -49,6 +50,7 @@ NewDotLine(n int, ras, canv float64, randColor bool)
NewPointRibbon(r float64)
NewJanus(n int, decay float64)
NewRandomShape(shapeNum int)
NewColorCircle(circleNum int)
```
## General Options
@ -70,25 +72,22 @@ The `Options` is a global option for the whole `canva`. It includes those genera
For those parameters specified for different kinds of art type, they have their own `struct`.
## Usage and example
### Silk Smoke
### Junas
```go
func main() {
rand.Seed(time.Now().Unix())
c := generativeart.NewCanva(500, 500)
c.SetBackground(generativeart.Black)
c.SetLineWidth(1.0)
c.SetLineColor(generativeart.MediumAquamarine)
c.SetAlpha(30)
c.SetColorSchema(generativeart.Plasma)
c.SetIterations(4)
c.FillBackground()
c.Draw(generativeart.NewSilkSmoke(400, 20, 0.2, 2, 10, 30, false))
c.ToPNG("silksmoke.png")
c.SetColorSchema(generativeart.DarkRed)
c.SetForeground(generativeart.LightPink)
c.Draw(generativeart.NewJanus(10, 0.2))
c.ToPNG("janus.png")
}
```
![](images/silksmoke.png)
![](images/janus.png)
### Random Shapes
@ -112,6 +111,52 @@ func main() {
![](images/randomshape.png)
### Color Circle
```go
func main() {
rand.Seed(time.Now().Unix())
colors := []color.RGBA{
{0xFF, 0xC6, 0x18, 0xFF},
{0xF4, 0x25, 0x39, 0xFF},
{0x41, 0x78, 0xF4, 0xFF},
{0xFE, 0x84, 0xFE, 0xFF},
{0xFF, 0x81, 0x19, 0xFF},
{0x56, 0xAC, 0x51, 0xFF},
{0x98, 0x19, 0xFA, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF},
}
c := generativeart.NewCanva(1000, 1000)
c.SetBackground(generativeart.White)
c.FillBackground()
c.SetColorSchema(colors)
c.Draw(generativeart.NewColorCircle(500))
c.ToPNG("colorcircle.png")
}
```
![](images/colorcircle.png)
### Silk Smoke
```go
func main() {
rand.Seed(time.Now().Unix())
c := generativeart.NewCanva(500, 500)
c.SetBackground(generativeart.Black)
c.SetLineWidth(1.0)
c.SetLineColor(generativeart.MediumAquamarine)
c.SetAlpha(30)
c.SetColorSchema(generativeart.Plasma)
c.SetIterations(4)
c.FillBackground()
c.Draw(generativeart.NewSilkSmoke(400, 20, 0.2, 2, 10, 30, false))
c.ToPNG("silksmoke.png")
}
```
![](images/silksmoke.png)
### Spiral Square
```go
@ -168,24 +213,6 @@ func main() {
![](images/dotline.png)
### Junas
```go
func main() {
rand.Seed(time.Now().Unix())
c := generativeart.NewCanva(500, 500)
c.SetBackground(generativeart.Black)
c.FillBackground()
c.SetColorSchema(generativeart.DarkRed)
c.SetForeground(generativeart.LightPink)
c.Draw(generativeart.NewJanus(10, 0.2))
c.ToPNG("janus.png")
}
```
![](images/janus.png)
### Julia Set
```go
@ -223,23 +250,6 @@ func main() {
![](images/silksky.png)
### Point Ribbon
```go
func main() {
rand.Seed(time.Now().Unix())
c := generativeart.NewCanva(500, 500)
c.SetBackground(generativeart.Lavender)
c.SetLineWidth(2)
c.SetIterations(150000)
c.FillBackground()
c.Draw(generativeart.NewPointRibbon(50))
c.ToPNG("pointribbon.png")
}
```
![](images/pointribbon.png)
### Maze
```go

86
colorCircle.go Normal file
View file

@ -0,0 +1,86 @@
package generativeart
import (
"github.com/fogleman/gg"
"math"
"math/rand"
)
type colorCircle struct {
circleNum int
}
func NewColorCircle(circleNum int) *colorCircle {
return &colorCircle{
circleNum: circleNum,
}
}
// Generative draws a color circle images.
func (cc *colorCircle) Generative(c *canva) {
ctex := gg.NewContextForRGBA(c.img)
for i := 0; i < cc.circleNum; i++ {
rnd := rand.Intn(3)
x := RandomRangeFloat64(-0.1, 1.1) * float64(c.width)
y := RandomRangeFloat64(-0.1, 1.1) * float64(c.height)
s := RandomRangeFloat64(0, RandomRangeFloat64(0, float64(c.width/2))) + 10
if rnd == 2 {
rnd = rand.Intn(3)
}
switch rnd {
case 0:
cc.drawCircleV1(ctex, c, x, y, s)
case 1:
ctex.SetLineWidth(RandomRangeFloat64(0, 1))
ctex.SetColor(c.opts.colorSchema[rand.Intn(len(c.opts.colorSchema))])
ctex.DrawCircle(x, y, RandomRangeFloat64(0, s)/2)
ctex.Stroke()
case 2:
cc.drawCircleV2(ctex, c, x, y, s)
}
}
}
func (cc *colorCircle) drawCircleV1(ctex *gg.Context, c *canva, x, y, s float64) {
n := RandomRangeInt(4, 30)
cs := RandomRangeFloat64(2, 8)
ctex.SetColor(c.opts.colorSchema[rand.Intn(len(c.opts.colorSchema))])
ctex.Push()
ctex.Translate(x, y)
for a := 0.0; a < math.Pi*2.0; a += math.Pi * 2.0 / float64(n) {
ctex.DrawEllipse(s*0.5*math.Cos(a), s*0.5*math.Sin(a), cs/2, cs/2)
ctex.Fill()
}
ctex.Pop()
}
func (cc *colorCircle) drawCircleV2(ctex *gg.Context, c *canva, x, y, s float64) {
cl := c.opts.colorSchema[rand.Intn(len(c.opts.colorSchema))]
ctex.SetLineWidth(1.0)
sx := s * RandomRangeFloat64(0.1, 0.55)
for j := 0.0001; j < sx; j++ {
dd := s + j*2.0
alpha := int(255 * sx / j)
if alpha > 255 {
alpha = 255
}
if alpha < 0 {
alpha = 0
}
//alpha := RandomRangeInt(30, 150)
cl.A = uint8(alpha)
ctex.SetColor(cl)
for i := 0; i < 200; i++ {
theta := RandomRangeFloat64(0, math.Pi*2)
xx := x + dd*0.3*math.Cos(theta)
yy := y + dd*0.3*math.Sin(theta)
//ctex.DrawLine(xx, yy, xx, yy)
ctex.DrawPoint(xx, yy, 0.6)
ctex.Stroke()
}
}
}

View file

@ -0,0 +1,28 @@
package main
import (
"github.com/jdxyw/generativeart"
"image/color"
"math/rand"
"time"
)
func main() {
rand.Seed(time.Now().Unix())
colors := []color.RGBA{
{0xFF, 0xC6, 0x18, 0xFF},
{0xF4, 0x25, 0x39, 0xFF},
{0x41, 0x78, 0xF4, 0xFF},
{0xFE, 0x84, 0xFE, 0xFF},
{0xFF, 0x81, 0x19, 0xFF},
{0x56, 0xAC, 0x51, 0xFF},
{0x98, 0x19, 0xFA, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF},
}
c := generativeart.NewCanva(1000, 1000)
c.SetBackground(generativeart.White)
c.FillBackground()
c.SetColorSchema(colors)
c.Draw(generativeart.NewColorCircle(500))
c.ToPNG("colorcircle.png")
}

BIN
images/colorcircle.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 564 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 165 KiB