|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "Using ColorGroup" |
| 4 | +date: 2019-03-03 00:50:08 +0000 |
| 5 | +categories: vecmath update |
| 6 | +--- |
| 7 | + |
| 8 | +# Example Usage |
| 9 | + |
| 10 | +ColorGroup can be initialized directly with and array of signed ints (corresponding to color integer), or indirectly from an array of "web string" as below. |
| 11 | +The order can be shuffled using `:shuffle!`. The colors are obtained by calling `:colors` on the group. |
| 12 | + |
| 13 | +```ruby |
| 14 | +GEM_HOME = '/home/tux/.gem/ruby/2.6.0' |
| 15 | +require 'vecmath' |
| 16 | + |
| 17 | +PALETTE = %w[#0F4155 #158CA7 #D63826 #F5C03E #152A3B #7EC873 #4B3331].freeze |
| 18 | + |
| 19 | + |
| 20 | + # create a java primitive array of signed int |
| 21 | + # @cols = web_array(PALETTE) |
| 22 | + @group = ColorGroup.from_web_array(PALETTE) |
| 23 | + @colors = @group.colors |
| 24 | + stroke_cap SQUARE |
| 25 | + stroke(0, 200) |
| 26 | + @coloured = true |
| 27 | + |
| 28 | + |
| 29 | +def draw |
| 30 | + arc_pattern |
| 31 | + no_loop |
| 32 | +end |
| 33 | + |
| 34 | +def sep_index(idx, length) |
| 35 | + (idx - (length - 1) * 0.5).abs.floor |
| 36 | +end |
| 37 | + |
| 38 | +def sep_color(idx, number) |
| 39 | + @colors[sep_index(idx - 1, number + 1)] |
| 40 | +end |
| 41 | + |
| 42 | +def arc_pattern |
| 43 | + circ_number = rand(4..10) |
| 44 | + block_size = rand(30..70) |
| 45 | + back_color = @coloured ? @group.last : 255 |
| 46 | + fill(back_color) |
| 47 | + rect(0, 0, width, height) |
| 48 | + half_block = block_size / 2 |
| 49 | + two_block = 2 * block_size |
| 50 | + MathTool::grid(width + two_block, height + two_block, block_size, block_size) do |x, y| |
| 51 | + push_matrix |
| 52 | + translate x, y |
| 53 | + rotate HALF_PI * rand(0..4) |
| 54 | + circ_number.downto(0) do |i| |
| 55 | + diam = two_block * i / (circ_number + 1) |
| 56 | + ccolor = i < 2 || !coloured ? back_color : sep_color(i, circ_number) |
| 57 | + fill ccolor |
| 58 | + arc(-half_block, -half_block, diam, diam, 0, HALF_PI) |
| 59 | + arc(half_block, half_block, diam, diam, PI, PI + HALF_PI) |
| 60 | + end |
| 61 | + pop_matrix |
| 62 | + end |
| 63 | +end |
| 64 | + |
| 65 | +``` |
0 commit comments