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
1313 */
1414package 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
0 commit comments