Skip to content

Commit 9e01094

Browse files
committed
Refactor clor_group and ColorUtil
1 parent 7b83c42 commit 9e01094

3 files changed

Lines changed: 42 additions & 14 deletions

File tree

library/color_group/color_group.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
java_import Java::Monkstone::ColorUtil
22

3+
# class for manipulating color
34
class ColorGroup
4-
attr_reader :web_colors, :pcolors
5+
attr_reader :pcolors
56
def initialize(web)
6-
@web_colors = web
77
@pcolors = ColorUtil.web_array(web)
88
end
99

10-
def shuffle
11-
@pcolors = ColorUtil.web_array(web_colors.shuffle)
10+
def self.from_p5cols(p5cols)
11+
return ColorGroup.new(p5cols)
1212
end
1313

1414
def shuffle!
15-
web_colors.shuffle!
16-
@pcolors = ColorUtil.web_array(web_colors)
15+
@pcolors = ColorUtil.shuffle(pcolors)
16+
end
17+
18+
def ruby_string
19+
ColorUtil.rubyString(pcolors)
1720
end
1821
end

src/monkstone/ColorUtil.java

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
22
* This utility allows JRubyArt users to use the processing.org color method
3-
* in their sketches. Includes a method to efficiently convert an array of web
4-
* strings to an array of color int, and another to convert an array of p5 color
5-
* (int) to a string that can be used in ruby code (to generate web color array).
3+
* in their sketches. Includes a method to efficiently convert an cols of web
4+
* strings to an cols of color int, and another to convert an cols of p5 color
5+
* (int) to a string that can be used in ruby code (to generate web color cols).
66
* Copyright (c) 2015-18 Martin Prout.
77
* This utility is free software; you can redistribute it and/or modify
88
* it under the terms of the GNU Lesser General Public License as published by
@@ -13,6 +13,12 @@
1313
*/
1414
package monkstone;
1515

16+
import java.util.ArrayList;
17+
import java.util.Arrays;
18+
import java.util.Collections;
19+
import java.util.List;
20+
import java.util.Random;
21+
1622
/**
1723
*
1824
* @author Martin Prout
@@ -46,7 +52,7 @@ static public int colorString(String hexstring) {
4652
/**
4753
*
4854
* @param web Array of web (hex) String
49-
* @return array of p5 color (int)
55+
* @return cols of p5 color (int)
5056
*/
5157
static public int[] webArray(String[] web) {
5258
int[] result = new int[web.length];
@@ -60,20 +66,39 @@ static public int[] webArray(String[] web) {
6066
* Return a ruby string of the form "%w(a b c)" where a, b, c are raw web
6167
* strings. This string can be used in ruby code.
6268
*
63-
* @param p5colors array of p5 colors (int)
69+
* @param p5colors cols of p5 colors (int)
6470
* @return String for use in ruby
6571
*/
6672
static public String rubyString(int[] p5colors) {
67-
StringBuilder sb = new StringBuilder("%w(");
73+
StringBuilder sb = new StringBuilder("%w[");
6874
for (int p5color : p5colors) {
6975
sb.append(String.format("#%06X", (0xFFFFFF & p5color)));
7076
sb.append(' ');
7177
}
7278
sb.deleteCharAt(sb.length() - 1);
73-
sb.append(")\n");
79+
sb.append("]\n");
7480
return sb.toString();
7581
}
7682

83+
static public String[] p5ToWeb(int[] p5colors) {
84+
List<String> list = new ArrayList<>();
85+
for (int p5color : p5colors) {
86+
list.add(String.format("#%06X", (0xFFFFFF & p5color)));
87+
}
88+
return list.toArray(new String[0]);
89+
}
90+
91+
static public int[] shuffle(int[] cols) {
92+
Random rgen = new Random(); // Random number generator
93+
for (int i = 0; i < cols.length; i++) {
94+
int randomPosition = rgen.nextInt(cols.length);
95+
int temp = cols[i];
96+
cols[i] = cols[randomPosition];
97+
cols[randomPosition] = temp;
98+
}
99+
return cols;
100+
}
101+
77102
/**
78103
*
79104
* @param hex double

test/helper_methods_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def test_hex_color
2929

3030
def test_p52ruby
3131
p5array = [16777215, 16711680, 255]
32-
ruby_string = "%w(#FFFFFF #FF0000 #0000FF)\n"
32+
ruby_string = "%w[#FFFFFF #FF0000 #0000FF]\n"
3333
result = p52ruby(p5array)
3434
assert_equal(result, ruby_string)
3535
end

0 commit comments

Comments
 (0)