add color canva

This commit is contained in:
Yongwei Xing 2021-03-22 14:58:39 +08:00
parent d27652b576
commit ea6856eb54
6 changed files with 170 additions and 6 deletions

View file

@ -31,6 +31,7 @@
- [Ocean Fish](#ocean-fish) - [Ocean Fish](#ocean-fish)
- [Circle Loop](#circle-loop) - [Circle Loop](#circle-loop)
- [Circle Noise](#circle-noise) - [Circle Noise](#circle-noise)
- [Color Canva](#color-canva)
- [Julia Set](#julia-set) - [Julia Set](#julia-set)
- [Black Hole](#black-hole) - [Black Hole](#black-hole)
- [Silk Sky](#silk-sky) - [Silk Sky](#silk-sky)
@ -424,6 +425,30 @@ func main() {
![](images/circlenoise.png) ![](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 ### Julia Set
```go ```go

66
arts/colorcanva.go Normal file
View file

@ -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()
}

30
common/rect.go Normal file
View file

@ -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,
}
}

View file

@ -34,6 +34,9 @@
- [parameters](#parameters-14) - [parameters](#parameters-14)
- [Black Hole](#black-hole) - [Black Hole](#black-hole)
- [parameters](#parameters-15) - [parameters](#parameters-15)
- [Color Canva](#color-canva)
- [parameters](#parameters-16)
## Color Circle 2 ## Color Circle 2
`Color Circle2` is version 2 of `Color Circle`. It still draws the circle and point cloud. `Color Circle2` is version 2 of `Color Circle`. It still draws the circle and point cloud.
@ -95,6 +98,7 @@ rs := NewRandomShape(150)
- lineNum: It indicates how many lines. - lineNum: It indicates how many lines.
![](../images/contourline.png) ![](../images/contourline.png)
## Silk Sky ## 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.
@ -206,7 +210,6 @@ d := arts.NewDotsWave(300)
- circleNum: The number of the circle in the image. - circleNum: The number of the circle in the image.
```go ```go
cm := arts.NewCircleMove(1000) cm := arts.NewCircleMove(1000)
``` ```
@ -248,3 +251,15 @@ b := arts.NewBlackHole(200, 400, 0.01)
``` ```
![](../images/blackhole.png) ![](../images/blackhole.png)
## Color Canva
### parameters
- seg:
```go
cc := arts.NewColorCanve(5)
```
![](../images/colorcanva.png)

View file

@ -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")
}

BIN
images/colorcanva.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 KiB