Skip to content

Commit 4b02b9f

Browse files
committed
more toxiclibs
1 parent 889d1e8 commit 4b02b9f

File tree

2 files changed

+175
-0
lines changed

2 files changed

+175
-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: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#!/usr/bin/env jruby
2+
require 'propane'
3+
require 'toxiclibs'
4+
# ColorTheme demo showing the following:
5+
# - construction of TColor themes via textual descriptions of shades and Colors
6+
# - adding an rand element to the theme
7+
#
8+
# Press SPACE to toggle rendering mode, any other key will re-generate a
9+
# random variation of the color theme
10+
#
11+
# @author Karsten Schmidt <info at postspectacular dot com>
12+
# Copyright (c) 2009 Karsten Schmidt
13+
#
14+
# This demo & 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+
class ColorThemeSketch < Propane::App
30+
31+
include_package 'toxi.color'
32+
include_package 'toxi.util.datatypes'
33+
34+
SWATCH_HEIGHT = 40.0
35+
SWATCH_WIDTH = 5.0
36+
SWATCH_GAP = 1
37+
38+
MAX_SIZE = 150.0
39+
NUM_DISCS = 300
40+
attr_reader :show_discs
41+
42+
def settings
43+
size(1024, 768)
44+
# smooth
45+
end
46+
47+
def setup
48+
sketch_title 'Color Theme'
49+
@show_discs = true
50+
noise_detail(2)
51+
no_loop
52+
end
53+
54+
def draw
55+
# first define our new theme
56+
t = ColorTheme.new('test')
57+
# add different color options, each with their own weight
58+
t.add_range('soft ivory', 0.5)
59+
t.add_range('intense goldenrod', 0.25)
60+
t.add_range('warm saddlebrown', 0.15)
61+
t.add_range('fresh teal', 0.05)
62+
t.add_range('bright yellowgreen', 0.05)
63+
# now add another rand hue which is using only bright shades
64+
t.add_range(ColorRange::BRIGHT, TColor.new_random, rand(0.02..0.05))
65+
# use the TColor theme to create a list of 160 Colors
66+
list = t.get_colors(160)
67+
if show_discs
68+
background(list.get_lightest.toARGB)
69+
discs(list)
70+
else
71+
background(0)
72+
yoff = 32
73+
list.sort_by_distance(false)
74+
swatches(list, 32, yoff)
75+
yoff += SWATCH_HEIGHT + 10
76+
list.sort_by_criteria(AccessCriteria::LUMINANCE, false)
77+
swatches(list, 32, yoff)
78+
yoff += SWATCH_HEIGHT + 10
79+
list.sort_by_criteria(AccessCriteria::BRIGHTNESS, false)
80+
swatches(list, 32, yoff)
81+
yoff += SWATCH_HEIGHT + 10
82+
list.sort_by_criteria(AccessCriteria::SATURATION, false)
83+
swatches(list, 32, yoff)
84+
yoff += SWATCH_HEIGHT + 10
85+
list.sort_by_criteria(AccessCriteria::HUE, false)
86+
swatches(list, 32, yoff)
87+
yoff += SWATCH_HEIGHT + 10
88+
list.sort_by_proximity_to(NamedColor::WHITE, RGBDistanceProxy.new, false)
89+
swatches(list, 32, yoff)
90+
end
91+
# save_frame(format('theme-%s%s', timestamp, '_##.png'))
92+
end
93+
94+
def timestamp
95+
Time.now.strftime('%Y%d%m_%H%M%S')
96+
end
97+
98+
def key_pressed
99+
@show_discs = !show_discs if key == ' '
100+
redraw
101+
end
102+
103+
def swatches(sorted, x, y)
104+
no_stroke
105+
colors = sorted.toARGBArray.to_a
106+
colors.each do |c|
107+
fill(c)
108+
rect(x, y, SWATCH_WIDTH, SWATCH_HEIGHT)
109+
x += SWATCH_WIDTH + SWATCH_GAP
110+
end
111+
end
112+
113+
def discs(list)
114+
no_stroke
115+
colors = list.toARGBArray.to_a
116+
colors.shuffle.each do |c|
117+
fill(c, rand(125.0..255)) # random transparency
118+
r = rand(MAX_SIZE)
119+
ellipse(rand(width), rand(height), r, r)
120+
end
121+
end
122+
end
123+
124+
ColorThemeSketch.new

0 commit comments

Comments
 (0)