-
Notifications
You must be signed in to change notification settings - Fork 10
/
NightSky.pde
60 lines (52 loc) · 1.45 KB
/
NightSky.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class NightSky extends Routine {
int NUMBER_OF_STARS = 30;
NightStar[] nightStars;
PImage grad;
void setup(PApplet parent) {
super.setup(parent);
color c1 = color(0, 0, 20);
color c2 = color(0, 20, 60);
grad = generateGradient(c1, c2, WIDTH*ZOOM, HEIGHT*ZOOM);
nightStars = new NightStar[NUMBER_OF_STARS];
for (int i=0; i<NUMBER_OF_STARS; i++) {
nightStars[i] = new NightStar();
}
}
void draw() {
background(0);
image(grad, 0, 0);
noStroke();
fill(0, 20, 0);
rect(0, HEIGHT*ZOOM * 0.7, WIDTH*ZOOM, HEIGHT*ZOOM * 0.3);
for (int i=0; i<NUMBER_OF_STARS; i++) {
nightStars[i].draw();
}
if (frameCount - modeFrameStart > FRAMERATE*TYPICAL_MODE_TIME) {
newMode();
}
}
// Generate a vertical gradient image
PImage generateGradient(color top, color bottom, int w, int h) {
int tR = (top >> 16) & 0xFF;
int tG = (top >> 8) & 0xFF;
int tB = top & 0xFF;
int bR = (bottom >> 16) & 0xFF;
int bG = (bottom >> 8) & 0xFF;
int bB = bottom & 0xFF;
PImage bg = createImage(w,h,RGB);
bg.loadPixels();
for(int i=0; i < bg.pixels.length; i++) {
int y = i/bg.width;
float n = y/(float)bg.height;
// for a horizontal gradient:
// float n = x/(float)bg.width;
bg.pixels[i] = color(
lerp(tR,bR,n),
lerp(tG,bG,n),
lerp(tB,bB,n),
255);
}
bg.updatePixels();
return bg;
}
}