|
| 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