Skip to content

Commit 5305c50

Browse files
committed
add rainbow vine example
1 parent f1af161 commit 5305c50

File tree

6 files changed

+82
-21
lines changed

6 files changed

+82
-21
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,14 @@ An example with heavy use of the ScratchPen. It also plays an sound file in the
243243

244244
![pipes example](web/assets/pipes.gif)
245245

246+
### Rainbow Vine
247+
248+
Source Code: https://github.com/mikebarkmin/processing-library-scratch/tree/master/examples/RainbowVine
249+
250+
An example which makes use a mouse events.
251+
252+
![rainbow vine example](web/assets/rainbow_vine.gif)
253+
246254
## Missing
247255

248256
* blocks which are not listed above are currently missing

examples/RainbowVine/RainbowVine.pde

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,48 @@ import eu.barkmin.processing.scratch.*;
22

33
ScratchStage stage;
44

5-
ArrayList<ScratchSprite> sprites = new ArrayList();
65
VineSprite vine;
76

87
void setup() {
98
size(800, 600);
109
ScratchStage.init(this);
1110
stage = ScratchStage.getInstance();
1211
stage.setColor(0, 0, 0);
12+
1313
vine = new VineSprite();
14-
sprites.add(vine);
1514
}
1615

1716
void draw() {
18-
for(int i = 0; i < sprites.size(); i++) {
19-
sprites.get(i).draw();
20-
}
21-
sprites.add(new LeafSprite(vine));
17+
vine.draw();
2218
}
2319

2420
class VineSprite extends ScratchSprite {
21+
22+
ArrayList<LeafSprite> leafs = new ArrayList();
23+
int lastSpawnTime;
24+
2525
VineSprite() {
2626
super("vine", "sprites/vine.png");
27+
this.lastSpawnTime = millis();
2728
this.getPen().down();
2829
this.getPen().setSize(3);
30+
this.getPen().setColor(120);
2931
this.hide();
3032
}
3133

3234
void draw() {
3335
super.draw();
3436
this.setPosition(ScratchStage.parent.mouseX, ScratchStage.parent.mouseY);
35-
this.getPen().changeColor(10);
3637
this.turnRight(5);
38+
39+
for(int i = 0; i < leafs.size(); i++) {
40+
leafs.get(i).draw();
41+
}
42+
43+
if ((millis() - lastSpawnTime) > 20) {
44+
leafs.add(new LeafSprite(vine));
45+
lastSpawnTime = millis();
46+
}
3747
}
3848
}
3949

@@ -48,7 +58,10 @@ class LeafSprite extends ScratchSprite {
4858
this.startMillis = millis();
4959
this.vine = vine;
5060
this.setRotation(vine.getRotation());
61+
// use current color from vine for pen
5162
this.getPen().setColor(vine.getPen().getColor());
63+
// update vine color with every leaf spawn
64+
vine.getPen().changeColor(2);
5265
this.setPosition((int) vine.getX(), (int) vine.getY());
5366
this.hide();
5467
}
@@ -57,9 +70,13 @@ class LeafSprite extends ScratchSprite {
5770
super.draw();
5871
this.turnRight(5);
5972
this.move(10);
73+
74+
// slowly increase pen size
6075
this.getPen().changeSize(1);
61-
if ((millis() - this.startMillis) / 1000.0 >= 0.2) {
62-
sprites.remove(this);
76+
77+
// remove leaf after 200ms
78+
if ((millis() - this.startMillis) >= 200) {
79+
this.vine.leafs.remove(this);
6380
}
6481
}
6582
}

src/eu/barkmin/processing/scratch/ScratchColor.java

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ public class ScratchColor {
88
private float g = 255;
99
private float b = 255;
1010

11+
private float h = 255;
12+
private float s = 255;
13+
private float l = 255;
14+
1115
public ScratchColor() {
1216
}
1317

@@ -20,6 +24,10 @@ public ScratchColor(ScratchColor c) {
2024
this.r = c.r;
2125
this.g = c.g;
2226
this.b = c.b;
27+
28+
this.h = c.h;
29+
this.s = c.s;
30+
this.l = c.l;
2331
}
2432

2533
/**
@@ -28,8 +36,7 @@ public ScratchColor(ScratchColor c) {
2836
* @return hue value [0...255]
2937
*/
3038
public float getHSB() {
31-
float[] hsb = ScratchColor.RGBtoHSB(this.r, this.b, this.g);
32-
return hsb[0] * 255;
39+
return this.h;
3340
}
3441

3542
/**
@@ -38,11 +45,7 @@ public float getHSB() {
3845
* @param h A hue value [0...255]
3946
*/
4047
public void setHSB(float h) {
41-
h = (h % 255) / 255.0f;
42-
float[] rgb = ScratchColor.HSBtoRGB(h, 1.0f, 1.0f);
43-
this.r = rgb[0];
44-
this.g = rgb[1];
45-
this.b = rgb[2];
48+
this.setHSB(h, this.s, this.l);
4649
}
4750

4851
/**
@@ -53,9 +56,24 @@ public void setHSB(float h) {
5356
* @param l A luminosity value [0...255]
5457
*/
5558
public void setHSB(float h, float s, float l) {
56-
h = (h % 255) / 255.0f;
57-
s = (s % 255) / 255.0f;
58-
l = (l % 255) / 255.0f;
59+
while (h > 255) {
60+
h -= 255;
61+
}
62+
while (s > 255) {
63+
s-= 255;
64+
}
65+
while (l > 255) {
66+
l -= 255;
67+
}
68+
69+
this.h = h;
70+
this.s = s;
71+
this.l = l;
72+
73+
h = h / 255.0f;
74+
s = s / 255.0f;
75+
l = l / 255.0f;
76+
5977
float[] rgb = ScratchColor.HSBtoRGB(h, s, l);
6078
this.r = rgb[0];
6179
this.g = rgb[1];
@@ -73,6 +91,12 @@ public void setRGB(float r, float g, float b) {
7391
this.r = r;
7492
this.g = g;
7593
this.b = b;
94+
95+
float[] hsb = ScratchColor.RGBtoHSB(r, g, b);
96+
97+
this.h = hsb[0] * 255;
98+
this.s = hsb[1] * 255;
99+
this.l = hsb[2] * 255;
76100
}
77101

78102
/**
@@ -81,7 +105,7 @@ public void setRGB(float r, float g, float b) {
81105
* @param h A hue value. Could be any positive or negative number.
82106
*/
83107
public void changeColor(float h) {
84-
float newH = (this.getHSB() + h) % 255;
108+
float newH = this.getHSB() + h;
85109
this.setHSB(newH);
86110
}
87111

@@ -144,4 +168,15 @@ public float getBlue() {
144168
}
145169

146170

171+
public float getH() {
172+
return h;
173+
}
174+
175+
public float getS() {
176+
return s;
177+
}
178+
179+
public float getL() {
180+
return l;
181+
}
147182
}

src/eu/barkmin/processing/scratch/ScratchPen.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void changeColor(float c) {
6363
}
6464

6565
public void changeColor(double c) {
66-
this.changeColor((float) c);
66+
this.color.changeColor((float) c);
6767
}
6868

6969
/**

src/eu/barkmin/processing/scratch/ScratchSprite.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ public void setOnEdgeBounce(boolean b) {
274274
public void setPosition(int x, int y) {
275275
this.x = x;
276276
this.y = y;
277+
this.getPen().setPosition(x, y);
277278
}
278279

279280
/**

web/assets/rainbow_vine.gif

386 KB
Loading

0 commit comments

Comments
 (0)