After being stuck for awhile on some technical issues,
finally I was able to implement a
Colour class w/
name, besides R, G, B.
The idea is that besides getting a color value from
Colour[] using an index,
we could also get it by a color name!
To achieve that, we have to
Array.sort() the
Array storing all the
Colour objects.
Once we got that
Array alphabetically sorted out, we can use
Array.binarySearch() on it,
to look up for a color name
String we so choose.
It then returns an index which corresponds to the
Colour object w/ that particular color
name.
And finally, we can use that found index to
get() the right color from that
Array.
It's a little convoluted and complicated. But with some effort to understand the process, it's a piece of cake.
Any further explanations, just ask here. Check it out:
RGB.txt:
- Black, 0
- Blue, 0, 0, 255
- Cyan, 0, 255, 255
- Gray, 128
- Green, 0, 128, 0
- Magenta, 255, 0, 255
- Red, 255, 0, 0
- White, -1
- Yellow, 255, 255, 0
- Saddle Brown, 140, 70, 20
- Orange, 255, 165, 0
- Tomato, 255, 100, 70
- Pink, 255, 192, 203
- Gold, 255, 215, 0
- Khaki, 240, 230, 140
- Indigo, 75, 0, 130
- Dark Slate Blue, 70, 60, 140
- Olive, 128, 128, 0
- Teal, 0, 128, 128
- Tan, 210, 180, 140
- Dim Gray, 105
/**
* Colour Container II (v2.31)
* by GoToLoop (2013/Sep)
*
* http://forum.processing.org/topic/
* file-of-colors-that-can-be-loaded-for-all-my-programs-
* hope-this-is-a-simple-question
*/
import java.util.Arrays;
final String[] myColors = {
"Tomato", "Gold", "Green", "Saddle Brown", "Teal", "Green", "Tan"
};
Colour[] palette;
final static String NAME = "RGB.txt", SEPARATOR = ",";
void setup() {
size(600, 400);
frameRate(.5);
palette = loadPalette(NAME, SEPARATOR);
println(palette);
println();
}
void draw() {
final int idx = frameCount % myColors.length;
final String name = myColors[idx];
final int search = Arrays.binarySearch(palette, name);
final Colour found = palette[search];
final color c = found.get();
background(c);
println("#" + nf(search, 2) + " " + found);
frame.setTitle("Index: " + idx + "\tColor: "
+ found.name + "\tCode: " + hex(c, 6));
}
Colour[] loadPalette(String path, String delim) {
final String[] lines = loadStrings(path);
final int len = lines.length;
final Colour[] RGBs = new Colour[len];
for (int i = 0; i != len;) {
final String[] units = split(lines[i], delim);
final String name = units[0];
final color[] channels = int( trim(subset(units, 1)) );
RGBs[i++] = new Colour(name, channels);
}
Arrays.sort(RGBs);
return RGBs;
}
class Colour implements Comparable {
short R, G, B;
String name;
Colour(String label, color r, color g, color b) {
name = label;
set(r, g, b);
}
Colour(String label, color c) {
name = label;
set(c);
}
Colour(String label, color[] a) {
name = label;
set(a);
}
void set(color r, color g, color b) {
R = (short) constrain(r, 0, 0xFF);
G = (short) constrain(g, 0, 0xFF);
B = (short) constrain(b, 0, 0xFF);
}
void set(color c) {
if (c >= 0 & c < 0400) {
setGrey(c);
return;
}
R = (short) (c >> 020 & 0xFF);
G = (short) (c >> 010 & 0xFF);
B = (short) (c & 0xFF);
}
void set(color[] a) {
final int len = a.length;
if (len == 1) {
if (a[0] >= 0 & a[0] < 0400) setGrey(a[0]);
else set(a[0]);
return;
}
if (len > 0) R = (short) constrain(a[0], 0, 0xFF);
if (len > 1) G = (short) constrain(a[1], 0, 0xFF);
if (len > 2) B = (short) constrain(a[2], 0, 0xFF);
}
void clear() {
R = G = B = 0;
}
void setGrey(color c) {
R = G = B = (short) constrain(c, 0, 0xFF);
}
color get() {
return color(R, G, B);
}
color[] toArray() {
return new color[] {
R, G, B
};
}
String name() {
return name.toLowerCase().trim();
}
String toString() {
return "\"" + name + "\" \t [ " + R + ", " + G + ", " + B + " ]";
}
int compareTo(Object other) {
return other instanceof Colour ?
name.compareTo( ( (Colour) other ).name ) :
name.compareTo( (String) other );
}
}