Skip to content

Commit 6662f14

Browse files
committed
refactor color_group
1 parent 9e01094 commit 6662f14

4 files changed

Lines changed: 43 additions & 12 deletions

File tree

Rakefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ task :test do
3131
sh 'jruby test/helper_methods_test.rb'
3232
sh 'jruby test/aabb_spec_test.rb'
3333
sh 'jruby test/create_test.rb'
34+
sh 'jruby test/color_group_test.rb'
3435
home = File.expand_path('~')
3536
config = File.exist?(format('%s/.jruby_art/config.yml', home))
3637
if config

library/color_group/color_group.rb

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
java_import Java::Monkstone::ColorUtil
22

3-
# class for manipulating color
3+
# class wraps a java color array, supports shuffle!, last and ruby_string
4+
# as well as ability to initialize with an array of "web" color string
45
class ColorGroup
56
attr_reader :pcolors
6-
def initialize(web)
7-
@pcolors = ColorUtil.web_array(web)
7+
def initialize(p5cols)
8+
@pcolors = p5cols
89
end
910

10-
def self.from_p5cols(p5cols)
11-
return ColorGroup.new(p5cols)
11+
def self.from_web_array(web)
12+
ColorGroup.new(ColorUtil.web_array(web))
1213
end
1314

1415
def shuffle!
@@ -18,4 +19,8 @@ def shuffle!
1819
def ruby_string
1920
ColorUtil.rubyString(pcolors)
2021
end
22+
23+
def last
24+
pcolors[0]
25+
end
2126
end

test/color_group_test.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# frozen_string_literal: true
2+
require_relative 'test_helper'
3+
require_relative '../lib/rpextras'
4+
require_relative '../library/color_group/color_group'
5+
6+
Java::Monkstone::JRLibrary.load(JRuby.runtime)
7+
java_import Java::Monkstone::ColorUtil
8+
9+
Dir.chdir(File.dirname(__FILE__))
10+
11+
PALETTE = %w[#FFFFFF #FF0000 #0000FF].freeze
12+
COLORS = [16777215, 16711680, 255].to_java(:int)
13+
14+
class ColorGroupTest < Minitest::Test
15+
def test_new
16+
group = ColorGroup.new(COLORS)
17+
assert group.kind_of? ColorGroup
18+
end
19+
20+
def test_web_array
21+
group = ColorGroup.from_web_array(PALETTE)
22+
assert group.kind_of? ColorGroup
23+
end
24+
25+
def test_ruby_string
26+
p5array = [16777215, 16711680, 255]
27+
group = ColorGroup.new(COLORS)
28+
ruby_string = "%w[#FFFFFF #FF0000 #0000FF]\n"
29+
result = group.ruby_string
30+
assert_equal(result, ruby_string)
31+
end
32+
end

test/helper_methods_test.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,6 @@ def test_hex_color
2727
end
2828
end
2929

30-
def test_p52ruby
31-
p5array = [16777215, 16711680, 255]
32-
ruby_string = "%w[#FFFFFF #FF0000 #0000FF]\n"
33-
result = p52ruby(p5array)
34-
assert_equal(result, ruby_string)
35-
end
36-
3730
def test_dist
3831
ax, ay, bx, by = 0, 0, 1.0, 1.0
3932
assert_in_epsilon(dist(ax, ay, bx, by), Math.sqrt(2), epsilon = 0.0001, msg = '2D distance')

0 commit comments

Comments
 (0)