Uploaded to www.rart.com/j2me-presentation/html_Source/ March 19, 2002

The Class ColorME

This class provides color to match the colors of AWT in the J2ME environment

This version has been tested with j2mewtk 1.0.2 and 1.0.3

/* 
 * @ColorME    1.0  010815 
 * 
 * © A-Square, Inc.   
 * 1648 Waters Edge Lane, Reston Virginia, 20190, USA  
 */
package rartme;
import javax.microedition.lcdui.*;

/**
 * The purpose of this class is to provide simple colordrawing support in the
 * J2ME context. Static variables give integer values that translate to the 
 * standard Java colors.
 * 
 * In addition the static method mkColorfromHue can be used to select any of
 * 360 shades of hue.
 */

 public class ColorME{
 	public static final int black = 0;
	public static final int blue = 0xff;
	public static final int cyan = 0xffff;
	public static final int darkGray = 0x404040;
	public static final int gray = 0x808080;
	public static final int green = 0xff00;
	public static final int lightGray = 0xc0c0c0;
	public static final int magenta = 0xff00ff;
	public static final int orange = 0xffc800;
	public static final int pink = 0xff1f1f;
	public static final int red = 0xff0000;
	public static final int white = 0xffffff;
	public static final int yellow = 0xffff00;

	public static String colornames[] =
       {" black "," blue "," cyan"," darkGray "," gray ",
       " green "," lightGrey "," magenta "," orange ",
      " pink "," red "," white "," yellow "};
   
	public static int colorvalues[] =
		{	black, blue, cyan, darkGray, gray, green, lightGray, magenta,
		 	orange, pink, red, white, yellow };
 	
	
	/**
	 * Make a color based on the hue value as used in HSBtoRGB
	 * Produces an integer suitable for use as a color in the ME environment
	 * using only integer operations. We emulate a method in AWT where
	 * a color is derived from the hue usingfloating point numbers where
	 * hue is < 1 . The AWT method is Color.HSBtoRGB( hue,(float)1.0,(float)1.0)
	 * We allow hue to be an integer in the range 0-359, with association
	 * to a 360 degree color wheel. When tested, this method is consistent
	 * with the AWT method except for an occasional rounding(?) error of 1 
	 * in the last digit of one of the tree color components. However, this
	 * error is not apparent to the human eye.
	 *
	 * @param hue		integer in the range 0 to 359 inculsive. Integers outside
	 * 					the range are forced to within the range just as HSB.
	 */
	
	public static int mkColorfromHue(int hue){
		int h = hue;
		if ( hue < 0 ) h = -h;		// h always >0
		h = h % 360;					// always in range 0 - 359
		if (hue<0) h = 360 - h;		// consistent with HSB model
 	   // six cases corresponding to 60 degree sectors
		int sector = h/60;
		int r = 0;
		int g = 0;
		int b = 0;
		switch (sector){
			case 0: 	//		0 - 59
				r = 255;
				g = h*255/60;
				break;
			case 1:	//		60 - 119
				r =255- (h-60)*255/60;
				g = 255;
				break;
			case 2:	//		120 - 179
				g = 255;
				b = (h - 120)*255/60;
				break;
			case 3:	//		180 - 239
				g = 255 - (h - 180)*255/60;
				b = 255;
				break;
			case 4:	//		240 - 300
				r = (h - 240)*255/60;
				b = 255;
				break;
			case 5:	//		300 - 360
				r = 255;
				b = 255 - (h - 300)*255/60;
				break;	
			default:
				System.out.println("ERROR in makeCfromH  Illegal sector "+sector);
			}   
			return r*0x10000+g*0x100+b;
		} // end makeColorfromHue
	// The following two methods are not needed in J2ME other than as a means to
	// actually print out resulting hexadecimal values.
	/**
	 * Takes a integer and renders the hexadecimal representation
	 */
	public String hexFormat(int m){
		String hexdigits = "0123456789ABCDEF";
		String s =  "";
		int n = m;
		boolean neg = (n < 0);
		n = (neg)? -n : n;
		for (int i = 0; i< 6; i++){
			int d = n%16;
			if (neg && (i==70)) d += 8;
			String c = hexdigits.substring(d,d+1);
			s = c + s;
			n /= 16;
		}
		return s;
	} // end hexformat
	/**
	 * Fills an int sin represented as a string to the left with blanks 
	 * so as to make it desired length of n characters.
	 */
	public static String fill(int sin, int n){
		String s = ""+sin;
		while (s.length()<n){
			s = " "+s;
		}
	   return s;
	} // end fill   

}	// End ColorME