diff --git a/README.md b/README.md index ae511d4..2956134 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,7 @@ - [Ocean Fish](#ocean-fish) - [Circle Loop](#circle-loop) - [Circle Noise](#circle-noise) + - [Color Canva](#color-canva) - [Julia Set](#julia-set) - [Black Hole](#black-hole) - [Silk Sky](#silk-sky) @@ -424,6 +425,30 @@ func main() { ![](images/circlenoise.png) +### Color Canva + +```go +func main() { + rand.Seed(time.Now().Unix()) + colors := []color.RGBA{ + {0xF9, 0xC8, 0x0E, 0xFF}, + {0xF8, 0x66, 0x24, 0xFF}, + {0xEA, 0x35, 0x46, 0xFF}, + {0x66, 0x2E, 0x9B, 0xFF}, + {0x43, 0xBC, 0xCD, 0xFF}, + } + c := generativeart.NewCanva(500, 500) + c.SetBackground(common.Black) + c.FillBackground() + c.SetLineWidth(8) + c.SetColorSchema(colors) + c.Draw(arts.NewColorCanve(5)) + c.ToPNG("colorcanva.png") +} +``` + +![](images/colorcanva.png) + ### Julia Set ```go diff --git a/arts/colorcanva.go b/arts/colorcanva.go new file mode 100644 index 0000000..3a07785 --- /dev/null +++ b/arts/colorcanva.go @@ -0,0 +1,66 @@ +package arts + +import ( + "github.com/fogleman/gg" + "github.com/jdxyw/generativeart" + "github.com/jdxyw/generativeart/common" + "math/rand" +) + +type colorCanva struct { + seg float64 +} + +func NewColorCanve(seg float64) *colorCanva { + return &colorCanva{seg: seg} +} + +// Generative returns a color canva image. +func (cc *colorCanva) Generative(c *generativeart.Canva) { + ctex := gg.NewContextForRGBA(c.Img()) + ctex.SetLineWidth(c.Opts().LineWidth()) + + rects := make([]*common.Rect, 0) + w := float64(c.Width()) / cc.seg + for i := 0.0; i < cc.seg; i += 1.0 { + for j := 0.0; j < cc.seg; j += 1.0 { + x := i*w + w/2 + y := j*w + w/2 + rects = append(rects, common.NewRect(x, y, w, w)) + } + } + + rand.Shuffle(len(rects), func(i, j int) { + rects[i], rects[j] = rects[j], rects[i] + }) + + ctex.Translate(float64(c.Width())/2, float64(c.Height())/2) + ctex.Scale(0.6, 0.6) + ctex.Translate(-float64(c.Width())/2, -float64(c.Height())/2) + + for i := 0; i < len(rects); i++ { + cc.draw(c, ctex, rects[i]) + cc.draw(c, ctex, rects[i]) + } +} + +func (cc *colorCanva) draw(c *generativeart.Canva, ctex *gg.Context, rect *common.Rect) { + rnd := rand.Intn(4) + ww := (rect.W() / 5) * float64(rand.Intn(int(cc.seg)*2)+1) + hh := (rect.H() / 5) * float64(rand.Intn(int(cc.seg)*2)+1) + + switch rnd { + case 0: + ctex.DrawRectangle(rect.X()-ww/2+rect.W()/2, rect.Y()+hh/2+rect.H()/2, ww, hh) + case 1: + ctex.DrawRectangle(rect.X()-ww/2-rect.W()/2, rect.Y()+hh/2+rect.H()/2, ww, hh) + case 2: + ctex.DrawRectangle(rect.X()-ww/2+rect.W()/2, rect.Y()+hh/2-rect.H()/2, ww, hh) + case 3: + ctex.DrawRectangle(rect.X()-ww/2-rect.W()/2, rect.Y()+hh/2-rect.H()/2, ww, hh) + } + ctex.SetColor(common.Black) + ctex.StrokePreserve() + ctex.SetColor(c.Opts().ColorSchema()[rand.Intn(len(c.Opts().ColorSchema()))]) + ctex.Fill() +} diff --git a/common/rect.go b/common/rect.go new file mode 100644 index 0000000..ddfad59 --- /dev/null +++ b/common/rect.go @@ -0,0 +1,30 @@ +package common + +type Rect struct { + x, y, w, h float64 +} + +func (r Rect) X() float64 { + return r.x +} + +func (r Rect) Y() float64 { + return r.y +} + +func (r Rect) W() float64 { + return r.w +} + +func (r Rect) H() float64 { + return r.h +} + +func NewRect(x, y, w, h float64) *Rect { + return &Rect{ + x: x, + y: y, + w: w, + h: h, + } +} diff --git a/docs/doc.md b/docs/doc.md index 5fb0650..2281f9d 100644 --- a/docs/doc.md +++ b/docs/doc.md @@ -34,6 +34,9 @@ - [parameters](#parameters-14) - [Black Hole](#black-hole) - [parameters](#parameters-15) + - [Color Canva](#color-canva) + - [parameters](#parameters-16) + ## Color Circle 2 `Color Circle2` is version 2 of `Color Circle`. It still draws the circle and point cloud. @@ -95,9 +98,10 @@ rs := NewRandomShape(150) - lineNum: It indicates how many lines. ![](../images/contourline.png) + ## Silk Sky -`Silk Sky` would draw an image with multiple circles converge to one point or one circle. +`Silk Sky` would draw an image with multiple circles converge to one point or one circle. ### parameters @@ -122,9 +126,9 @@ silkSky := NewSilkSky(circleNum int, sunRadius float64) ```go func julia1(z complex128) complex128 { - c := complex(-0.1, 0.651) - z = z*z + c - return z + c := complex(-0.1, 0.651) + z = z*z + c + return z } julia := arts.NewJulia(julia1, 40, 1.5, 1.5) ``` @@ -206,7 +210,6 @@ d := arts.NewDotsWave(300) - circleNum: The number of the circle in the image. - ```go cm := arts.NewCircleMove(1000) ``` @@ -247,4 +250,16 @@ y := arts.NewYarn(2000) b := arts.NewBlackHole(200, 400, 0.01) ``` -![](../images/blackhole.png) \ No newline at end of file +![](../images/blackhole.png) + +## Color Canva + +### parameters + +- seg: + +```go +cc := arts.NewColorCanve(5) +``` + +![](../images/colorcanva.png) diff --git a/example/example_colorcanva.go b/example/example_colorcanva.go new file mode 100644 index 0000000..3ad6d66 --- /dev/null +++ b/example/example_colorcanva.go @@ -0,0 +1,28 @@ +package main + +import ( + "github.com/jdxyw/generativeart" + "github.com/jdxyw/generativeart/arts" + "github.com/jdxyw/generativeart/common" + "image/color" + "math/rand" + "time" +) + +func main() { + rand.Seed(time.Now().Unix()) + colors := []color.RGBA{ + {0xF9, 0xC8, 0x0E, 0xFF}, + {0xF8, 0x66, 0x24, 0xFF}, + {0xEA, 0x35, 0x46, 0xFF}, + {0x66, 0x2E, 0x9B, 0xFF}, + {0x43, 0xBC, 0xCD, 0xFF}, + } + c := generativeart.NewCanva(500, 500) + c.SetBackground(common.Black) + c.FillBackground() + c.SetLineWidth(8) + c.SetColorSchema(colors) + c.Draw(arts.NewColorCanve(5)) + c.ToPNG("colorcanva.png") +} diff --git a/images/colorcanva.png b/images/colorcanva.png new file mode 100644 index 0000000..789347e Binary files /dev/null and b/images/colorcanva.png differ