From a27f0706332dc02d6639b4b853120ad645313d1d Mon Sep 17 00:00:00 2001 From: monkstone Date: Sun, 3 Mar 2019 07:37:15 +0000 Subject: [PATCH] color group --- docs/_posts/2019-03-03-Using-ColorGroup.md | 65 ++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 docs/_posts/2019-03-03-Using-ColorGroup.md diff --git a/docs/_posts/2019-03-03-Using-ColorGroup.md b/docs/_posts/2019-03-03-Using-ColorGroup.md new file mode 100644 index 0000000..808b332 --- /dev/null +++ b/docs/_posts/2019-03-03-Using-ColorGroup.md @@ -0,0 +1,65 @@ +--- +layout: post +title: "Using ColorGroup" +date: 2019-03-03 00:50:08 +0000 +categories: vecmath update +--- + +# Example Usage + +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. +The order can be shuffled using `:shuffle!`. The colors are obtained by calling `:colors` on the group. + +```ruby +GEM_HOME = '/home/tux/.gem/ruby/2.6.0' +require 'vecmath' + +PALETTE = %w[#0F4155 #158CA7 #D63826 #F5C03E #152A3B #7EC873 #4B3331].freeze + + + # create a java primitive array of signed int + # @cols = web_array(PALETTE) + @group = ColorGroup.from_web_array(PALETTE) + @colors = @group.colors + stroke_cap SQUARE + stroke(0, 200) + @coloured = true + + +def draw + arc_pattern + no_loop +end + +def sep_index(idx, length) + (idx - (length - 1) * 0.5).abs.floor +end + +def sep_color(idx, number) + @colors[sep_index(idx - 1, number + 1)] +end + +def arc_pattern + circ_number = rand(4..10) + block_size = rand(30..70) + back_color = @coloured ? @group.last : 255 + fill(back_color) + rect(0, 0, width, height) + half_block = block_size / 2 + two_block = 2 * block_size + MathTool::grid(width + two_block, height + two_block, block_size, block_size) do |x, y| + push_matrix + translate x, y + rotate HALF_PI * rand(0..4) + circ_number.downto(0) do |i| + diam = two_block * i / (circ_number + 1) + ccolor = i < 2 || !coloured ? back_color : sep_color(i, circ_number) + fill ccolor + arc(-half_block, -half_block, diam, diam, 0, HALF_PI) + arc(half_block, half_block, diam, diam, PI, PI + HALF_PI) + end + pop_matrix + end +end + +```