Skip to content

Commit f260138

Browse files
committed
Improve color intensity map and support any colors
1 parent a834f0e commit f260138

File tree

5 files changed

+86
-55
lines changed

5 files changed

+86
-55
lines changed

README.md

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1-
# RgbRandomWalk
2-
Creates fractal-like images with three 2D random walks in red, green, and blue.
3-
4-
The intensity of a pixel is proportional (until it reaches the brightest value) to the number of times it is visited. See [Wikipedia](https://en.wikipedia.org/wiki/Random_walk) for more info on random walks.
5-
6-
Made just for fun.
1+
# Random Walk Wallpaper Generator
2+
Generate hazy fractal-like wallpaper images by simulating random walks in different colors.
73

4+
The intensity of a pixel corresponds to the number of times it is visited during each random walk. See [Wikipedia](https://en.wikipedia.org/wiki/Random_walk) for more info on random walks.
85

96
## Usage
10-
`java RgbRandomWalk <imagefilepath> <width> <height> <steps>`
7+
```
8+
javac RgbRandomWalk.java
9+
java RgbRandomWalk <imagefilepath> <width> <height> <steps>
10+
```
11+
12+
The `<imagefilepath>` parameter is the filename of the PNG image to output. Output is in PNG format regardless of specified extension. This can be changed in source file. **Note: will overwrite any existing file. Use caution.**
1113

12-
`imagefilepath` is the image file to write. Output in in PNG format regardless of specified extension. This can be changed in source file. **Note: will overwrite any existing file. Use caution.**
14+
`width` and `height` specify the size of image.
1315

14-
`width` and `height` are the size of image.
16+
`steps` specifies the number of steps to take for each random walk.
1517

16-
`steps` is the number of steps to take for each random walk.
18+
The number of random walks and corresponding colors can be changed by editing the RgbRandomWalk.java file.
1719

18-
## Examples
20+
## Example output
1921

2022
![Example 1](example1.png?raw=true)
2123

RgbRandomWalk.java

+73-44
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,44 @@ private static int step(int pos, int upperBound) {
1717
}
1818
}
1919

20+
// Maps the number of steps into the range 0 to 255.
21+
private static int colorMap(int stepCount) {
22+
int[] multipliers = {8, 8, 4, 2};
23+
int count = 16;
24+
int value = 0;
25+
26+
for (int inc : multipliers) {
27+
if (stepCount > count) {
28+
value += count * inc;
29+
stepCount -= count;
30+
} else {
31+
value += stepCount * inc;
32+
stepCount = 0;
33+
}
34+
}
35+
36+
if (stepCount > 0) {
37+
value += stepCount;
38+
}
39+
value = Math.min(value, 255);
40+
41+
return value;
42+
}
43+
2044
public static void main(String[] args) {
21-
long start = System.currentTimeMillis();
2245
String fileName = "";
23-
int width = 1920, height = 1080;
24-
int increment = 8;
25-
int steps = 6000000;
46+
int width;
47+
int height;
48+
int steps;
49+
50+
// Colors for each of the random paths. Will generate a path for each
51+
// color specified.
52+
// Use RBG format like HTML, e.g. 0xRRGGBB where RR are two hex digits
53+
// specifying the red value, GG specify green, and BB specify blue. Also
54+
// can use builtin colors such as Color.BLUE, see documentation on
55+
// java.awt.Color for details.
56+
Color[] colors = {new Color(0x0000ff), new Color(0x00ff00), new Color(0xff0000)};
57+
2658
String imgFormat = "png";
2759

2860
if (args.length != 4) {
@@ -34,7 +66,6 @@ public static void main(String[] args) {
3466
width = Integer.parseInt(args[1], 10);
3567
height = Integer.parseInt(args[2], 10);
3668
steps = Integer.parseInt(args[3], 10);
37-
3869
} catch (NumberFormatException e) {
3970
System.out.println("Usage: java RgbRandomWalk <imagefilepath> <width> <height> <steps>");
4071
System.out.println("Error: Invalid number.");
@@ -48,54 +79,52 @@ public static void main(String[] args) {
4879
}
4980
}
5081

51-
BufferedImage img;
52-
Color c;
53-
// Good parameters are 1920 x 1080, intensity increment 8, and steps = 6000000.
54-
55-
int rX = width / 2, rY = height / 2;
56-
int gX = width / 2, gY = height / 2;
57-
int bX = width / 2, bY = height / 2;
58-
int r = 0, g = 0, b = 0;
59-
// int sRGB, other;
60-
82+
int[] posX = new int[colors.length];
83+
int[] posY = new int[colors.length];
84+
int[][][] visitCount = new int[width][height][colors.length];
6185

62-
img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
63-
64-
rX = width / 2;
65-
rY = height / 2;
66-
gX = width / 2;
67-
gY = height / 2;
68-
bX = width / 2;
69-
bY = height / 2;
86+
for (int i = 0; i < colors.length; i++) {
87+
posX[i] = width / 2;
88+
posY[i] = height / 2;
89+
}
7090

7191
for (int j = 0; j < steps; j++) {
72-
rX = step(rX, width);
73-
rY = step(rY, height);
74-
gX = step(gX, width);
75-
gY = step(gY, height);
76-
bX = step(bX, width);
77-
bY = step(bY, height);
78-
79-
c = new Color(img.getRGB(rX, rY));
80-
img.setRGB(rX, rY, (c.getRGB() & 0xff00ffff) | (Math.min(c.getRed() + increment, 255) << 16));
92+
for (int i = 0; i < colors.length; i++) {
93+
posX[i] = step(posX[i], width);
94+
posY[i] = step(posY[i], height);
8195

82-
c = new Color(img.getRGB(gX, gY));
83-
img.setRGB(gX, gY, (c.getRGB() & 0xffff00ff) | (Math.min(c.getGreen() + increment, 255) << 8));
84-
85-
c = new Color(img.getRGB(bX, bY));
86-
img.setRGB(bX, bY, (c.getRGB() & 0xffffff00) | (Math.min(c.getBlue() + increment, 255)));
96+
visitCount[posX[i]][posY[i]][i]++;
97+
}
8798
}
99+
int[] pixels = new int[width * height];
100+
101+
for (int i = 0; i < width; i++) {
102+
for (int j = 0; j < height; j++) {
103+
int c = Color.BLACK.getRGB();
104+
float[] rgb = new float[3];
105+
float[] components = new float[3];
106+
for (int k = 0; k < colors.length; k++) {
107+
float value = colorMap(visitCount[i][j][k]) / 256.0f;
108+
colors[k].getColorComponents(components);
109+
for (int m = 0; m < 3; m++) {
110+
rgb[m] += components[m] * value;
111+
}
112+
}
113+
for (int m = 0; m < 3; m++) {
114+
rgb[m] = Math.min(rgb[m], 1.0f);
115+
}
116+
pixels[i + width * j] = new Color(rgb[0], rgb[1], rgb[2]).getRGB();
117+
}
118+
}
119+
120+
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
121+
img.setRGB(0, 0, width, height, pixels, 0, width);
88122

89123
try {
90124
File outputfile = new File(fileName);
91125
ImageIO.write(img, imgFormat, outputfile);
92-
} catch (Exception e) {
93-
System.out.println("Error: cannot open the file.");
94-
System.exit(0);
126+
} catch (IOException e) {
127+
System.err.println("Error: cannot open the file" + fileName);
95128
}
96-
97-
98-
long time = System.currentTimeMillis()-start;
99-
System.out.printf("Time used: %dm, %ds, %dms.%n", time / 60000, (time / 1000) % 60, time % 1000);
100129
}
101130
}

example1.png

-76.9 KB
Loading

example2.png

-25.9 KB
Loading

example3.png

292 KB
Loading

0 commit comments

Comments
 (0)