

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | 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; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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); } } |