Skip to content

Commit dd419b7

Browse files
committed
ColorUtil
1 parent 32f94f0 commit dd419b7

File tree

2 files changed

+155
-4
lines changed

2 files changed

+155
-4
lines changed

docs/_posts/2019-03-01-Using-grid-method.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,24 @@ categories: vecmath update
88
# Example Usage
99

1010
```ruby
11-
# ===== Default : Default
1211
GEM_HOME = '/home/tux/.gem/ruby/2.6.0'
1312
require 'vecmath'
14-
13+
PALETTE = %w[#a11220 #884444].freeze
14+
@colors = ColorUtil.webArray(PALETTE.to_java(:string))
1515
rect_mode(CORNER)
1616
no_stroke
1717
frame_rate(1) # set the frame rate to 1 draw call per second
1818

19+
1920
def draw
2021
background(180) # clear the screen to grey
2122
grid_size = 5 # rand(3..12) # select a rand number of squares each frame
2223
gap = 5 # rand(5..50) # select a rand gap between each square
2324
# calculate the size of each square for the given number of squares and gap between them
2425
cellsize = (width - (grid_size + 1) * gap) / grid_size
2526
position = -> (count) { gap * (count + 1) + cellsize * count + rand(-5..5) }
26-
grid(grid_size, grid_size) do |x, y|
27-
rand(0..5) > 4 ? fill(color('#a11220'), 180.0) : fill(color('#884444'), 180.0)
27+
MathTool::grid(grid_size, grid_size) do |x, y|
28+
rand(0..5) > 4 ? fill(color(@colors[0]), 180.0) : fill(color(@colors[1]), 180.0)
2829
rect(position.call(x), position.call(y), cellsize, cellsize)
2930
end
3031
end

src/monkstone/ColorUtil.java

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
/**
2+
* This utility allows JRubyArt users to use the processing.org color method
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).
6+
* Copyright (c) 2015-18 Martin Prout.
7+
* This utility is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU Lesser General Public License as published by
9+
* the Free Software Foundation; either version 2.1 of the License, or (at
10+
* your option) any later version.
11+
*
12+
* Obtain a copy of the license at http://www.gnu.org/licenses/lgpl-2.1.html
13+
*/
14+
package monkstone;
15+
16+
import java.util.ArrayList;
17+
import java.util.List;
18+
import java.util.Random;
19+
20+
/**
21+
*
22+
* @author Martin Prout
23+
*/
24+
public class ColorUtil {
25+
26+
static final String TOO_BIG = "produces a line too long a line for code";
27+
28+
/**
29+
* Returns hex long as a positive int unless greater than Integer.MAX_VALUE
30+
* else return the complement as a negative integer or something like that
31+
*
32+
* @param hexlong long
33+
* @return rgb int
34+
*/
35+
static final int hexLong(long hexlong) {
36+
long SPLIT = Integer.MAX_VALUE + 1;
37+
if (hexlong < SPLIT) {
38+
return (int) hexlong;
39+
} else {
40+
return (int) (hexlong - SPLIT * 2L);
41+
}
42+
}
43+
44+
/**
45+
* @param hexstring String
46+
* @return rgb int
47+
*/
48+
static public int colorString(String hexstring) {
49+
return java.awt.Color.decode(hexstring).getRGB();
50+
}
51+
52+
/**
53+
*
54+
* @param web Array of web (hex) String
55+
* @return cols of p5 color (int)
56+
*/
57+
static public int[] webArray(String... web) {
58+
int[] result = new int[web.length];
59+
for (int i = 0; i < web.length; i++) {
60+
result[i] = java.awt.Color.decode(web[i]).getRGB();
61+
}
62+
return result;
63+
}
64+
65+
/**
66+
* Return a ruby string of the form "%w[a b c]" where a, b, c are raw web
67+
* strings. This string can be used in ruby code.
68+
*
69+
* @param p5colors cols of p5 colors (int)
70+
* @return String for use in ruby
71+
*/
72+
static public String rubyString(int[] p5colors) {
73+
if (p5colors.length > 8){ return TOO_BIG;}
74+
StringBuilder sb = new StringBuilder("%w[");
75+
for (int p5color : p5colors) {
76+
sb.append(String.format("#%06X", (0xFFFFFF & p5color)));
77+
sb.append(' ');
78+
}
79+
sb.deleteCharAt(sb.length() - 1);
80+
sb.append("]\n");
81+
return sb.toString();
82+
}
83+
84+
/**
85+
*
86+
* @param p5colors
87+
* @return array of web color as String
88+
*/
89+
static public String[] p5ToWeb(int[] p5colors) {
90+
List<String> list = new ArrayList<>();
91+
for (int p5color : p5colors) {
92+
list.add(String.format("#%06X", (0xFFFFFF & p5color)));
93+
}
94+
return list.toArray(new String[0]);
95+
}
96+
97+
/**
98+
*
99+
* @param cols
100+
* @return array of color int
101+
*/
102+
static public int[] shuffle(int[] cols) {
103+
Random rgen = new Random(); // Random number generator
104+
for (int i = 0; i < cols.length; i++) {
105+
int randomPosition = rgen.nextInt(cols.length);
106+
int temp = cols[i];
107+
cols[i] = cols[randomPosition];
108+
cols[randomPosition] = temp;
109+
}
110+
return cols;
111+
}
112+
113+
/**
114+
*
115+
* @param hex double
116+
* @return hex float
117+
*/
118+
static public float colorLong(double hex) {
119+
return (float) hex;
120+
}
121+
122+
/**
123+
*
124+
* @param hexlong long
125+
* @return hexlong int
126+
*/
127+
static public int colorLong(long hexlong) {
128+
return hexLong(hexlong);
129+
}
130+
131+
/**
132+
*
133+
* @param hex double
134+
* @return hex float
135+
*/
136+
static public float colorDouble(double hex) {
137+
return (float) hex;
138+
}
139+
140+
/**
141+
*
142+
* @param hue
143+
* @param sat
144+
* @param brightness
145+
* @return color int
146+
*/
147+
static public int hsbToRgB(double hue, double sat, double brightness) {
148+
return java.awt.Color.HSBtoRGB((float) hue, (float) sat, (float) brightness);
149+
}
150+
}

0 commit comments

Comments
 (0)