Skip to content

Commit af6c444

Browse files
author
monkstone
committed
Add autorun Rakefile
1 parent 13e0d01 commit af6c444

File tree

3 files changed

+82
-96
lines changed

3 files changed

+82
-96
lines changed
Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,40 @@
1-
require 'ruby-processing'
21

3-
class PixelNeighbourDifferencesSketch < Processing::App
42

5-
def setup
6-
@img = load_image 'sunflower.jpg'
7-
@destination = create_image @img.width, @img.height, RGB
8-
end
3+
def setup
4+
size 200, 200
5+
@img = load_image 'sunflower.jpg'
6+
@destination = create_image @img.width, @img.height, RGB
7+
end
98

10-
def draw
11-
# We are going to look at both image's pixels
12-
@img.load_pixels
13-
@destination.load_pixels
14-
15-
# Since we are looking at left neighbors
16-
# We skip the first column
17-
(1...width).each do |x|
18-
(0...height).each do |y|
19-
# Pixel location and color
20-
loc = x + y * @img.width
21-
pix = @img.pixels[loc]
22-
23-
# Pixel to the left location and color
24-
left_loc = (x - 1) + y * @img.width
25-
left_pix = @img.pixels[left_loc]
26-
27-
# New color is difference between pixel and left neighbor
28-
diff = (brightness(pix) - brightness(left_pix)).abs
29-
@destination.pixels[loc] = color(diff)
30-
end
9+
def draw
10+
# We are going to look at both image's pixels
11+
@img.load_pixels
12+
@destination.load_pixels
13+
14+
# Since we are looking at left neighbors
15+
# We skip the first column
16+
(1...width).each do |x|
17+
(0...height).each do |y|
18+
# Pixel location and color
19+
loc = x + y * @img.width
20+
pix = @img.pixels[loc]
21+
22+
# Pixel to the left location and color
23+
left_loc = (x - 1) + y * @img.width
24+
left_pix = @img.pixels[left_loc]
25+
26+
# New color is difference between pixel and left neighbor
27+
diff = (brightness(pix) - brightness(left_pix)).abs
28+
@destination.pixels[loc] = color(diff)
3129
end
32-
33-
# We changed the pixels in @destination
34-
@destination.update_pixels
35-
# Display the @destination
36-
image @destination, 0, 0
3730
end
3831

32+
# We changed the pixels in @destination
33+
@destination.update_pixels
34+
# Display the @destination
35+
image @destination, 0, 0
3936
end
4037

41-
PixelNeighbourDifferencesSketch.new :title => "Pixel Neighbour Differences", :width => 200, :height => 200
38+
4239

4340

chapter_15/14_pointillism.rb

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,29 @@
1-
require 'ruby-processing'
21

3-
class PointillismSketch < Processing::App
4-
5-
def setup
6-
@pointillize = 10
7-
@img = load_image 'sunflower.jpg'
8-
background 255
9-
frame_rate 200 # Let's run this one as fast as we can.
10-
no_stroke
11-
smooth
12-
end
13-
14-
def draw
15-
# Pick a random point
16-
x, y = rand(width), rand(height)
17-
loc = x + y * width
18-
19-
# Look up the RGB color in the source image
20-
load_pixels
21-
pixel = @img.pixels[loc]
22-
r, g, b = red(pixel), green(pixel), blue(pixel)
23-
24-
# Draw an ellipse at that location with that color
25-
fill r, g, b, 100
26-
# Back to shapes! Instead of setting a pixel, we use the color from a pixel to draw a circle.
27-
ellipse x, y, @pointillize, @pointillize
28-
end
2+
def setup
3+
size 200, 200
4+
@pointillize = 10
5+
@img = load_image 'sunflower.jpg'
6+
background 255
7+
frame_rate 200 # Let's run this one as fast as we can.
8+
no_stroke
9+
end
2910

11+
def draw
12+
# Pick a random point
13+
x, y = rand(width), rand(height)
14+
loc = x + y * width
15+
16+
# Look up the RGB color in the source image
17+
load_pixels
18+
pixel = @img.pixels[loc]
19+
r, g, b = red(pixel), green(pixel), blue(pixel)
20+
21+
# Draw an ellipse at that location with that color
22+
fill r, g, b, 100
23+
# Back to shapes! Instead of setting a pixel, we use the color from a pixel to draw a circle.
24+
ellipse x, y, @pointillize, @pointillize
3025
end
3126

32-
PointillismSketch.new :title => "Pointillism", :width => 200, :height => 200
27+
3328

3429

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,37 @@
1-
require 'ruby-processing'
21

3-
class TwoDMappedTo3dSketch < Processing::App
4-
5-
def setup
6-
@cellsize = 2
7-
@img = load_image 'sunflower.jpg' # Load the image
8-
@img.load_pixels
9-
@pixels = @img.pixels
10-
@cols = width / @cellsize # Calculate number of columns
11-
@rows = height / @cellsize # Calculate number of rows
12-
render_mode P3D
13-
no_stroke
14-
rect_mode CENTER
15-
end
16-
17-
def draw
18-
background 255
19-
@cols.times do |i| # Begin loop for columns
20-
x = i * @cellsize + @cellsize / 2 # x position
21-
@rows.times do |j| # Begin loop for rows
22-
y = j * @cellsize + @cellsize / 2 # y position
23-
loc = x + y * width # Pixel array location
24-
c = @pixels[loc] # Grab the color
25-
26-
# Calculate a z position as a function of mouse_x and pixel brightness
27-
z = (mouse_x/width.to_f) * brightness(@pixels[loc]) - 100.0
2+
def setup
3+
size 200, 200, P3D
4+
@cellsize = 2
5+
@img = load_image 'sunflower.jpg' # Load the image
6+
@img.load_pixels
7+
@pixels = @img.pixels
8+
@cols = width / @cellsize # Calculate number of columns
9+
@rows = height / @cellsize # Calculate number of rows
10+
no_stroke
11+
rect_mode CENTER
12+
end
2813

29-
# Translate to the location, set fill and stroke, and draw the rect
30-
push_matrix
31-
translate x, y, z
32-
fill c
33-
rect 0, 0, @cellsize, @cellsize
34-
pop_matrix
35-
end
14+
def draw
15+
background 255
16+
@cols.times do |i| # Begin loop for columns
17+
x = i * @cellsize + @cellsize / 2 # x position
18+
@rows.times do |j| # Begin loop for rows
19+
y = j * @cellsize + @cellsize / 2 # y position
20+
loc = x + y * width # Pixel array location
21+
c = @pixels[loc] # Grab the color
22+
23+
# Calculate a z position as a function of mouse_x and pixel brightness
24+
z = (mouse_x/width.to_f) * brightness(@pixels[loc]) - 100.0
25+
26+
# Translate to the location, set fill and stroke, and draw the rect
27+
push_matrix
28+
translate x, y, z
29+
fill c
30+
rect 0, 0, @cellsize, @cellsize
31+
pop_matrix
3632
end
3733
end
38-
3934
end
4035

41-
TwoDMappedTo3dSketch.new :title => "2d Mapped To 3d", :width => 200, :height => 200
4236

4337

0 commit comments

Comments
 (0)