Skip to content

Commit

Permalink
add color canva
Browse files Browse the repository at this point in the history
  • Loading branch information
Yongwei Xing committed Mar 22, 2021
1 parent d27652b commit ea6856e
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 6 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
66 changes: 66 additions & 0 deletions arts/colorcanva.go
Original file line number Diff line number Diff line change
@@ -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 changes: 30 additions & 0 deletions common/rect.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
27 changes: 21 additions & 6 deletions docs/doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand All @@ -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)
```
Expand Down Expand Up @@ -206,7 +210,6 @@ d := arts.NewDotsWave(300)

- circleNum: The number of the circle in the image.


```go
cm := arts.NewCircleMove(1000)
```
Expand Down Expand Up @@ -247,4 +250,16 @@ y := arts.NewYarn(2000)
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)
28 changes: 28 additions & 0 deletions example/example_colorcanva.go
Original file line number Diff line number Diff line change
@@ -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")
}
Binary file added images/colorcanva.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit ea6856e

Please sign in to comment.