Skip to content

Commit 593c27c

Browse files
committed
add some library examples
1 parent ac7b70c commit 593c27c

File tree

96 files changed

+4861
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+4861
-0
lines changed

library/control_panel/button.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env jruby -w
2+
require 'picrate'
3+
class SimpleButton < Processing::App
4+
load_library :control_panel
5+
6+
attr_reader :back
7+
def setup
8+
sketch_title 'Simple Button'
9+
control_panel do |c|
10+
c.look_feel 'Nimbus'
11+
c.title 'Control Button'
12+
c.button :color_background # see method below
13+
c.button(:exit!) { exit } # example of a button with a simple block
14+
end
15+
color_mode RGB, 1
16+
@back = [0, 0, 1.0]
17+
end
18+
19+
def color_background
20+
@back = [rand, rand, rand]
21+
end
22+
23+
def draw
24+
background *back
25+
end
26+
27+
def settings
28+
size 300, 300
29+
end
30+
end
31+
32+
SimpleButton.new

library/control_panel/checkbox.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env jruby -w
2+
require 'picrate'
3+
class Checkbox < Processing::App
4+
load_library :control_panel
5+
WARN = 'warning!'.freeze
6+
attr_reader :shouting
7+
8+
def setup
9+
sketch_title 'Simple Checkbox'
10+
control_panel do |c|
11+
c.look_feel 'Nimbus'
12+
c.title 'Checkbox'
13+
c.checkbox :shouting
14+
end
15+
text_font(create_font('mono', 48))
16+
fill(200, 0, 0)
17+
end
18+
19+
def warning
20+
shouting ? WARN.upcase : WARN
21+
end
22+
23+
def draw
24+
background 0
25+
text(warning, 20, 100)
26+
end
27+
28+
def settings
29+
size 300, 300
30+
end
31+
end
32+
33+
Checkbox.new

library/control_panel/menu.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env jruby -w
2+
require 'picrate'
3+
class SimpleMenu < Processing::App
4+
load_library :control_panel
5+
6+
attr_reader :toggle_loop
7+
def setup
8+
sketch_title 'Simple Menu'
9+
control_panel do |c|
10+
c.title 'Menu'
11+
c.menu :choice, %i[one two three]
12+
c.checkbox(:toggle_loop) { toggle_loop ? loop : no_loop}
13+
end
14+
end
15+
16+
def draw
17+
puts @choice
18+
no_loop
19+
end
20+
21+
def settings
22+
size 300, 300
23+
end
24+
end
25+
26+
SimpleMenu.new

library/control_panel/slider.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env jruby -w
2+
require 'picrate'
3+
class SimpleSlider < Processing::App
4+
load_library :control_panel
5+
6+
attr_reader :bluish
7+
def setup
8+
sketch_title 'Simple Slider'
9+
control_panel do |c|
10+
c.title 'Slider'
11+
c.slider :bluish, 0.2..1.0, 0.5
12+
end
13+
color_mode(RGB, 1.0)
14+
end
15+
16+
def draw
17+
background(0, 0, bluish)
18+
end
19+
20+
def settings
21+
size 300, 300
22+
end
23+
end
24+
25+
SimpleSlider.new

library/file_chooser/README.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
### Obtaining and Using contextfreeart ###
2+
3+
For debian users it should be as simple as `sudo apt-get install cfdg` installs commandline version.
4+
5+
For others check [contextfreeart website][download], if you get the command line version you could easily automate running the cfdg program, once the files have been created.
6+
7+
### Black and White Demo
8+
9+
```ruby
10+
#!/usr/bin/env jruby -v -W2
11+
# frozen_string_literal: true
12+
require 'picrate'
13+
# Creates files that you can use with context free art (Gray Scale)
14+
class GrayScale < Processing::App
15+
load_library :file_chooser
16+
attr_reader :img, :data, :skip, :invert
17+
18+
def settings
19+
size 500, 500
20+
end
21+
22+
def setup
23+
sketch_title 'Pixellator for CheChe'
24+
color_mode(HSB, 360, 1.0, 1.0)
25+
fill 0, 0, 200
26+
text('Click Window to Load Image', 10, 100)
27+
@skip = 5 # controls apparent resolution
28+
@data = []
29+
@invert = true
30+
end
31+
32+
def draw
33+
unless img.nil?
34+
img.filter(INVERT) if invert
35+
@invert = false
36+
image(img, 0, 0)
37+
end
38+
end
39+
40+
def write_data(name, data)
41+
df = " %s [x %d y %d s %0.2f hue 0 sat 0.7 brightness 0]\n"
42+
open(data_path('data.cfdg'), 'w') do |pw|
43+
pw.puts format("shape %s{\n", name)
44+
data.each do |row|
45+
pw.puts format(df, *row)
46+
end
47+
pw.puts "}\n"
48+
end
49+
end
50+
51+
def write_start(start, data)
52+
open(data_path(format('%s.cfdg', start)), 'w') do |pw|
53+
pw.puts 'CF::Background = [b 1]'
54+
pw.puts format("startshape %s\n", start)
55+
pw.puts "shape dot{CIRCLE[]}\n"
56+
pw.puts "import data.cfdg\n"
57+
end
58+
write_data start, data
59+
end
60+
61+
def file_selected(selection)
62+
if selection.nil?
63+
puts 'Nothing Chosen'
64+
else
65+
@img = load_image(selection.get_absolute_path)
66+
surface.set_size(img.width, img.height)
67+
end
68+
end
69+
70+
def mouse_clicked
71+
@img = nil
72+
# java_signature 'void selectInput(String, String)'
73+
select_input('Select Image File', 'file_selected')
74+
end
75+
76+
def key_pressed
77+
case key
78+
when 'p', 'P'
79+
export = Thread.new do
80+
pixellate
81+
end
82+
export.join
83+
puts 'done'
84+
when 's', 'S'
85+
save_frame(data_path('original.png'))
86+
else
87+
puts format('key %s was pressed', key)
88+
end
89+
end
90+
91+
def pixellate
92+
load_pixels
93+
shp = 'dot'
94+
(skip...img.width).step(skip) do |x|
95+
(skip...img.height).step(skip) do |y|
96+
pix = pixels[x + y * width]
97+
sz = brightness(pix) * skip
98+
data << [
99+
shp, -width / 2 + x, height / 2 - y, sz.round(2)
100+
] if sz > 0.4
101+
end
102+
end
103+
write_start 'cheche', data
104+
end
105+
end
106+
107+
GrayScale.new
108+
```
109+
110+
If for example the chosen sketch was 590 * 600 pixels you might use the following to generate the pixellated image using the `cli` assuming `haddock.cfdg` is in the local folder. Windows users might need to use different escapes for path, but then they've got a GUI to use if they want.
111+
112+
```bash
113+
cfdg haddock.cfdg -w 1180 -h 1200 -o phil.png
114+
```
115+
116+
But there is much more you can do (look up che che for a much more sophisticated cfdg file).
117+
118+
119+
NB: You could add a grayscale filter if you start with a coloured image. Here we set hue to 0 ie red colour 20 is yellow etc, read more from contextfreeart tutorial (plus have a go at creating a standalone cfdg sketch it is a lot of fun).
120+
121+
122+
[download]:http://www.contextfreeart.org/mediawiki/index.php/Download_page

library/file_chooser/gray.rb

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#!/usr/bin/env jruby -v -W2
2+
# frozen_string_literal: true
3+
require 'picrate'
4+
# Creates files that you can use with context free art (Gray Scale)
5+
class GrayScale < Processing::App
6+
load_library :file_chooser
7+
attr_reader :img, :data, :skip, :invert
8+
9+
def settings
10+
size 500, 500
11+
end
12+
13+
def setup
14+
sketch_title 'Pixellator for CheChe'
15+
color_mode(HSB, 360, 1.0, 1.0)
16+
fill 0, 0, 200
17+
text('Click Window to Load Image', 10, 100)
18+
@skip = 5 # controls apparent resolution
19+
@data = []
20+
@invert = true
21+
end
22+
23+
def draw
24+
unless img.nil?
25+
img.filter(INVERT) if invert
26+
@invert = false
27+
image(img, 0, 0)
28+
end
29+
end
30+
31+
def write_data(name, data)
32+
df = " %s [x %d y %d s %0.2f hue 0 sat 0.7 brightness 0]\n"
33+
open(data_path('data.cfdg'), 'w') do |pw|
34+
pw.puts format("shape %s{\n", name)
35+
data.each do |row|
36+
pw.puts format(df, *row)
37+
end
38+
pw.puts "}\n"
39+
end
40+
end
41+
42+
def write_start(start, data)
43+
open(data_path(format('%s.cfdg', start)), 'w') do |pw|
44+
pw.puts 'CF::Background = [b 1]'
45+
pw.puts format("startshape %s\n", start)
46+
pw.puts "shape dot{CIRCLE[]}\n"
47+
pw.puts "import data.cfdg\n"
48+
end
49+
write_data start, data
50+
end
51+
52+
def file_selected(selection)
53+
if selection.nil?
54+
puts 'Nothing Chosen'
55+
else
56+
@img = load_image(selection.get_absolute_path)
57+
surface.set_size(img.width, img.height)
58+
end
59+
end
60+
61+
def mouse_clicked
62+
@img = nil
63+
# java_signature 'void selectInput(String, String)'
64+
select_input('Select Image File', 'file_selected')
65+
end
66+
67+
def key_pressed
68+
case key
69+
when 'p', 'P'
70+
export = Thread.new do
71+
pixellate
72+
end
73+
export.join
74+
puts 'done'
75+
when 's', 'S'
76+
save_frame(data_path('original.png'))
77+
else
78+
puts format('key %s was pressed', key)
79+
end
80+
end
81+
82+
def pixellate
83+
load_pixels
84+
shp = 'dot'
85+
(skip...img.width).step(skip) do |x|
86+
(skip...img.height).step(skip) do |y|
87+
pix = pixels[x + y * width]
88+
sz = brightness(pix) * skip
89+
data << [
90+
shp, -width / 2 + x, height / 2 - y, sz.round(2)
91+
] if sz > 0.4
92+
end
93+
end
94+
write_start 'cheche', data
95+
end
96+
end
97+
98+
GrayScale.new

0 commit comments

Comments
 (0)