/* * Copyright (c) 1994-1995 Sun Microsystems, Inc. All Rights Reserved. * * Permission to use, copy, modify, and distribute this software * and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and * without fee is hereby granted. * Please refer to the file http://java.sun.com/copy_trademarks.html * for further important copyright and trademark information and to * http://java.sun.com/licensing.html for further important licensing * information for the Java (tm) Technology. * * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. * * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES"). SUN * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR * HIGH RISK ACTIVITIES. * */ import java.applet.Applet; import java.awt.Color; import java.awt.Image; import java.awt.Graphics; import java.net.URL; import java.net.MalformedURLException; import java.awt.image.ImageFilter; import java.awt.image.RGBImageFilter; import java.awt.image.ImageProducer; import java.awt.image.FilteredImageSource; import java.util.Hashtable; /** * An applet class that generates colored bullets dynamically. * * The source for the bullet is any color GIF image. The colormap of the * image is modified to have a different primary foreground color. * * @author Jim Graham * @version 1.1, 01/23/96 */ public class HueBullet extends Applet { /** * Cache of colors indexed by name or color-spec. */ static Hashtable colorCache = new Hashtable(); /** * Cache of colored bullets indexed by fg,imgurl. */ static Hashtable bulletCache = new Hashtable(); /** * The final bullet image with the appropriate colors. */ Image bulletImage; /** * Initialize the color hash table. */ static { colorCache.put("red", Color.red); colorCache.put("green", Color.green); colorCache.put("blue", Color.blue); colorCache.put("cyan", Color.cyan); colorCache.put("magenta", Color.magenta); colorCache.put("yellow", Color.yellow); colorCache.put("orange", Color.orange); colorCache.put("pink", Color.pink); colorCache.put("white", Color.white); colorCache.put("black", Color.black); } /** * Generate a color from a specification string. The string * can either be a color name which is looked up in the colorCache * hash table, or it can be a 6 digit hex number which is converted * to a color as RRGGBB. */ private synchronized Color lookupColor(String colorName) { String lowerName = colorName.toLowerCase(); Color newcolor = (Color) colorCache.get(lowerName); if (newcolor != null) return newcolor; int colorval; try { colorval = java.lang.Integer.parseInt(lowerName, 16); } catch (NumberFormatException e) { return Color.black; } int r = (colorval >> 16) & 0xff; int g = (colorval >> 8) & 0xff; int b = colorval & 0xff; newcolor = new Color(r, g, b); colorCache.put(lowerName, newcolor); return newcolor; } /** * Create a bullet bitmap from a new foreground color and a color image. */ private static synchronized Image makeBullet(HueBullet app, Color fg, URL url) { String hashName = "" + fg + url; Image bulletImage = (Image) bulletCache.get(hashName); if (bulletImage != null) { return bulletImage; } Image sourceImage = app.getImage(url); ImageFilter imgf = new HueFilter(fg); ImageProducer imgp = new FilteredImageSource(sourceImage.getSource(), imgf); bulletImage = app.createImage(imgp); bulletCache.put(hashName, bulletImage); return bulletImage; } /** * Initialize the applet. Get parameters. */ public void init() { String attr = getParameter("img"); String dir = (attr != null) ? attr : "images/color-ball.gif"; URL url; try { url = new URL(getDocumentBase(), dir); } catch (MalformedURLException e) { return; } attr = getParameter("fg"); Color fg = (attr != null) ? lookupColor(attr) : getForeground(); bulletImage = makeBullet(this, fg, url); } /** * Paint the bullet or error indicator. */ public void paint(Graphics g) { if (bulletImage == null || (checkImage(bulletImage, this) & ERROR) != 0) { g.setColor(Color.red); g.fillRect(0, 0, size().width, size().height); } else { g.drawImage(bulletImage, 0, 0, this); } } } /** * The HueFilter class implements in ImageFilter which recolors * the pixels in an image to have a new primary hue. */ class HueFilter extends RGBImageFilter { /* * A private variable used to hold hue/saturation/brightness * values returned from the static conversion methods in Color. */ private float hsbvals[] = new float[3]; /** * the Hue of the indicated new foreground color. */ float fgHue; /** * the Saturation of the indicated new foreground color. */ float fgSaturation; /** * the Brightness of the indicated new foreground color. */ float fgBrightness; /** * Construct a HueFilter object which performs color modifications * to warp existing image colors to have a new primary hue. */ public HueFilter(Color fg) { Color.RGBtoHSB(fg.getRed(), fg.getGreen(), fg.getBlue(), hsbvals); fgHue = hsbvals[0]; fgSaturation = hsbvals[1]; fgBrightness = hsbvals[2]; canFilterIndexColorModel = true; } /** * Filter an individual pixel in the image by modifying its * hue, saturation, and brightness components to be similar * to the indicated new foreground color. */ public int filterRGB(int x, int y, int rgb) { int alpha = (rgb >> 24) & 0xff; int red = (rgb >> 16) & 0xff; int green = (rgb >> 8) & 0xff; int blue = (rgb ) & 0xff; Color.RGBtoHSB(red, green, blue, hsbvals); float newHue = fgHue; float newSaturation = hsbvals[1] * fgSaturation; float newBrightness = hsbvals[2] * (hsbvals[1] * fgBrightness + (1 - hsbvals[1])); rgb = Color.HSBtoRGB(newHue, newSaturation, newBrightness); return (rgb & 0x00ffffff) | (alpha << 24); } }