Skip to content

Commit b1e15fe

Browse files
committed
more toxi examples
1 parent 6bdcdb8 commit b1e15fe

File tree

3 files changed

+256
-0
lines changed

3 files changed

+256
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env jruby
2+
require 'propane'
3+
4+
class ColorPresets < Propane::App
5+
6+
load_library :hype
7+
include_package 'hype'
8+
java_import 'hype.extended.layout.HGridLayout'
9+
# string color array
10+
PALETTE = %w(
11+
CLEAR
12+
WHITE
13+
BLACK
14+
LGREY
15+
GREY
16+
DGREY
17+
RED
18+
GREEN
19+
BLUE
20+
CYAN
21+
YELLOW
22+
MAGENTA
23+
).freeze
24+
# format string see fill below
25+
FSTRING = 'Java::HypeInterfaces::HConstants::%s'.freeze
26+
27+
def settings
28+
size(640, 640)
29+
end
30+
31+
def setup
32+
sketch_title 'Color Presets'
33+
H.init(self)
34+
H.background(color('#242424'))
35+
grid = HGridLayout.new(3).spacing(210, 157).start_loc(10, 10)
36+
PALETTE.each do |col|
37+
H.add(HRect.new(200, 147))
38+
.stroke_weight(3)
39+
.stroke(H::BLACK, 150.0) # Note we call hype constants
40+
.fill(instance_eval(format(FSTRING, col))) # and using eval
41+
.loc(grid.get_next_point)
42+
end
43+
end
44+
45+
def draw
46+
H.draw_stage
47+
no_loop
48+
end
49+
end
50+
51+
ColorPresets.new
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# ColorTheme demo showing the following:
2+
# - construction of TColor themes via textual descriptions of shades and Colors
3+
# - adding an rand element to the theme
4+
#
5+
# Press SPACE to toggle rendering mode, any other key will re-generate a
6+
# random variation of the color theme
7+
#
8+
# @author Karsten Schmidt <info at postspectacular dot com>
9+
# Copyright (c) 2009 Karsten Schmidt
10+
#
11+
# This demo & library is free software you can redistribute it and/or
12+
# modify it under the terms of the GNU Lesser General Public
13+
# License as published by the Free Software Foundation either
14+
# version 2.1 of the License, or (at your option) any later version.
15+
#
16+
# http://creativecommons.org/licenses/LGPL/2.1/
17+
#
18+
# This library is distributed in the hope that it will be useful,
19+
# but WITHOUT ANY WARRANTY without even the implied warranty of
20+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21+
# Lesser General Public License for more details.
22+
#
23+
# You should have received a copy of the GNU Lesser General Public
24+
# License along with this library if not, write to the Free Software
25+
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26+
27+
require 'toxiclibs'
28+
include_package 'toxi.color'
29+
include_package 'toxi.util.datatypes'
30+
31+
SWATCH_HEIGHT = 40.0
32+
SWATCH_WIDTH = 5.0
33+
SWATCH_GAP = 1
34+
35+
MAX_SIZE = 150.0
36+
NUM_DISCS = 300
37+
attr_reader :show_discs
38+
39+
def settings
40+
size(1024, 768)
41+
# smooth
42+
end
43+
44+
def setup
45+
sketch_title 'Color Theme'
46+
@show_discs = true
47+
noise_detail(2)
48+
no_loop
49+
end
50+
51+
def draw
52+
# first define our new theme
53+
t = ColorTheme.new('test')
54+
# add different color options, each with their own weight
55+
t.add_range('soft ivory', 0.5)
56+
t.add_range('intense goldenrod', 0.25)
57+
t.add_range('warm saddlebrown', 0.15)
58+
t.add_range('fresh teal', 0.05)
59+
t.add_range('bright yellowgreen', 0.05)
60+
# now add another rand hue which is using only bright shades
61+
t.add_range(ColorRange::BRIGHT, TColor.new_random, rand(0.02..0.05))
62+
# use the TColor theme to create a list of 160 Colors
63+
list = t.get_colors(160)
64+
if show_discs
65+
background(list.get_lightest.toARGB)
66+
discs(list)
67+
else
68+
background(0)
69+
yoff = 32
70+
list.sort_by_distance(false)
71+
swatches(list, 32, yoff)
72+
yoff += SWATCH_HEIGHT + 10
73+
list.sort_by_criteria(AccessCriteria::LUMINANCE, false)
74+
swatches(list, 32, yoff)
75+
yoff += SWATCH_HEIGHT + 10
76+
list.sort_by_criteria(AccessCriteria::BRIGHTNESS, false)
77+
swatches(list, 32, yoff)
78+
yoff += SWATCH_HEIGHT + 10
79+
list.sort_by_criteria(AccessCriteria::SATURATION, false)
80+
swatches(list, 32, yoff)
81+
yoff += SWATCH_HEIGHT + 10
82+
list.sort_by_criteria(AccessCriteria::HUE, false)
83+
swatches(list, 32, yoff)
84+
yoff += SWATCH_HEIGHT + 10
85+
list.sort_by_proximity_to(NamedColor::WHITE, RGBDistanceProxy.new, false)
86+
swatches(list, 32, yoff)
87+
end
88+
# save_frame(format('theme-%s%s', timestamp, '_##.png'))
89+
end
90+
91+
def timestamp
92+
Time.now.strftime('%Y%d%m_%H%M%S')
93+
end
94+
95+
def key_pressed
96+
@show_discs = !show_discs if key == ' '
97+
redraw
98+
end
99+
100+
def swatches(sorted, x, y)
101+
no_stroke
102+
colors = sorted.toARGBArray.to_a
103+
colors.each do |c|
104+
fill(c)
105+
rect(x, y, SWATCH_WIDTH, SWATCH_HEIGHT)
106+
x += SWATCH_WIDTH + SWATCH_GAP
107+
end
108+
end
109+
110+
def discs(list)
111+
no_stroke
112+
colors = list.toARGBArray.to_a
113+
colors.shuffle.each do |c|
114+
fill(c, rand(125.0..255)) # random transparency
115+
r = rand(MAX_SIZE)
116+
ellipse(rand(width), rand(height), r, r)
117+
end
118+
end
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# <p>Much like the GameOfLife example, this de_mo shows the basic usage
2+
# pattern for the 2D cellular automata imple_mentation, this time however
3+
# utilizing cell aging and using a tone map to render its current state.
4+
# The CA simulation can be configured with birth and survival rules to
5+
# create all the complete set of rules with a 3x3 cell evaluation kernel.</p>
6+
#
7+
# <p><strong>Usage:</strong><ul>
8+
# <li>click + drag mouse to disturb the CA matrix</li>
9+
# <li>press 'r' to restart simulation</li>
10+
# </ul></p>
11+
#
12+
# Copyright (c) 2011 Karsten Schmidt
13+
#
14+
# This de_mo & library is free software you can redistribute it and/or
15+
# modify it under the terms of the GNU Lesser General Public
16+
# License as published by the Free Software Foundation either
17+
# version 2.1 of the License, or (at your option) any later version.
18+
#
19+
# http://creativecommons.org/licenses/LGPL/2.1/
20+
#
21+
# This library is distributed in the hope that it will be useful,
22+
# but WITHOUT ANY WARRANTY without even the implied warranty of
23+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24+
# Lesser General Public License for more details.
25+
#
26+
# You should have received a copy of the GNU Lesser General Public
27+
# License along with this library if not, write to the Free Software
28+
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29+
30+
require 'toxiclibs'
31+
include_package 'toxi.sim.automata'
32+
include_package 'toxi.math'
33+
include_package 'toxi.color'
34+
attr_reader :ca, :tone_map
35+
36+
def settings
37+
size(680, 382, P2D)
38+
end
39+
40+
def setup
41+
sketch_title 'CA Ornament'
42+
# the birth rules specify options for when a cell becomes active
43+
# the numbers refer to the amount of ACTIVE neighbour cells allowed,
44+
# their order is irrelevant
45+
birth_rules = [1, 5, 7].to_java Java::byte
46+
# survival rules specify the possible numbers of allowed or required
47+
# ACTIVE neighbour cells in order for a cell to stay alive
48+
survival_rules = [0, 3, 5, 6, 7, 8].to_java Java::byte
49+
# setup cellular automata matrix
50+
@ca = CAMatrix.new(width, height)
51+
# unlike traditional CA's only supporting binary cell states
52+
# this implementation supports a flexible number of states (cell age)
53+
# in this demo cell states reach from 0 - 255
54+
rule = CARule2D.new(birth_rules, survival_rules, 256, false)
55+
# we also want cells to automatically die when they've reached their
56+
# maximum age
57+
rule.set_auto_expire(true)
58+
# finally assign the rules to the CAMatrix
59+
ca.set_rule(rule)
60+
# create initial seed pattern
61+
ca.draw_box_at(0, height / 2, 5, 1)
62+
# create a gradient for rendering/shading the CA
63+
grad = ColorGradient.new
64+
# NamedColors are preset colors, but any TColor can be added
65+
# see javadocs for list of names:
66+
# http://toxiclibs.org/docs/colorutils/toxi/color/NamedColor::html
67+
grad.add_color_at(0, NamedColor::BLACK)
68+
grad.add_color_at(64, NamedColor::CYAN)
69+
grad.add_color_at(128, NamedColor::YELLOW)
70+
grad.add_color_at(192, NamedColor::WHITE)
71+
grad.add_color_at(255, NamedColor::BLACK)
72+
# the tone map will map cell states/ages to a gradient color
73+
@tone_map = ToneMap.new(0, rule.get_state_count - 1, grad)
74+
end
75+
76+
def draw
77+
load_pixels
78+
ca.draw_box_at(mouse_x, mouse_y, 5, 1) if mouse_pressed?
79+
ca.update
80+
tone_map.get_tone_mapped_array(ca.get_matrix, pixels)
81+
update_pixels
82+
end
83+
84+
def key_pressed
85+
return unless key == 'r'
86+
ca.reset
87+
end

0 commit comments

Comments
 (0)