I made a 256-color class last night that people can use if they want (in case it's the 8-bit challenge):
import java.awt.Color;
import greenfoot.Greenfoot;
/**
* An 8-bit color class.
*/
public class Color8 extends Color {
// The 256-color palette.
public static final Color8[] PALETTE = generatePalette();
// Some colors from the palette (named for easier use).
public static final Color8
BLACK = PALETTE[0],
BLUE = PALETTE[5],
LIGHT_BLUE = PALETTE[23],
GREEN = PALETTE[18],
LIGHT_GREEN = PALETTE[24],
LIME_GREEN = PALETTE[30],
CYAN = PALETTE[71],
PURPLE = PALETTE[77],
BROWN = PALETTE[78],
RED = PALETTE[108],
PINK = PALETTE[185],
ORANGE = PALETTE[196],
LIGHT_PINK = PALETTE[209],
YELLOW = PALETTE[210],
WHITE = PALETTE[215],
TRANSPARENT = PALETTE[255];
/**
* Creates a Color using the given values.
*
* @param r The red value
* @param g The green value
* @param b The blue value
*/
private Color8(int r, int g, int b) {
super(r, g, b);
}
/**
* Creates a Color using the given values.
* Includes an alpha channel
*
* @param r The red value
* @param g The green value
* @param b The blue value
* @param a The alpha value
*/
private Color8(int r, int g, int b, int a) {
super(r, g, b, a);
}
/**
* Returns a random color from the palette.
* (uses only the 0 <= x <= 216 region)
*
* @return A random color in the region PALETTE[0] to PALETTE[216]
*/
public static Color8 randomColor() {
return randomColor(0, 216);
}
/**
* Returns a random color from the given
* range of the palette.
*
* @param min The min (inclusive) index to choose
* @param max The max (exclusive) index to choose
* @return A random color in the region PALETTE[min] to PALETTE[max]
*/
public static Color8 randomColor(int min, int max) {
int val = min + Greenfoot.getRandomNumber(max - min);
return PALETTE[val];
}
/**
* Generates the color palette.
* Uses the "safety palette" from
* http://msdn.microsoft.com/en-us/library/bb250466(VS.85).aspx
* for the first 216 colors. The
* last 40 are subject to change
* (they are to be used for whichever
* colors are needed and not found in
* the default palette).
*
* Here is an image of the generated
* pallete: http://i.imgur.com/KqMpfkx.png
*
* Current custom colors usage:
* - 255: Transparent
*
* @return A 256-color palette
*/
private static Color8[] generatePalette() {
Color8[] palette = new Color8[256];
// Do the first 216 colors
int i = 0;
for(int r = 0; r < 256; r += 51) {
for(int g = 0; g < 256; g += 51) {
for(int b = 0; b < 256; b += 51) {
palette[i] = new Color8(r, g, b);
i++;
}
}
}
// Initialize the rest to black for now
for(; i < 256; i++) {
palette[i] = new Color8(0, 0, 0);
}
// Do any custrom colors here
palette[255] = new Color8(0, 0, 0, 0);
return palette;
}
}
Because it extends Color, you can use it in your code easily. Example:
Code:
import greenfoot.*;
public class TestWorld extends World {
public TestWorld() {
super(200, 200, 1);
GreenfootImage image = new GreenfootImage(200, 200);
image.setColor(Color8.PURPLE);
image.fill();
int size = 20;
for(int x = 0; x < image.getWidth()/size; x++) {
for(int y = 0; y < image.getHeight()/size; y++) {
image.setColor(Color8.randomColor());
image.fillRect(x * size, y * size, size, size);
}
}
setBackground(image);
}
}
Output: