/* * ColorOrganizer.java - Organizes color objects. * * Copyright (c) 1996 Chuck McManis, All Rights Reserved. * * Permission to use, copy, modify, and distribute this software * and its documentation for NON-COMMERCIAL purposes and without * fee is hereby granted provided that this copyright notice * appears in all copies. * * CHUCK MCMANIS 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. CHUCK MCMANIS * SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT * OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. */ import java.awt.Color; import java.util.Dictionary; /** * This class "adapts" the binary search tree to use Color objects * as keys. */ public class ColorOrganizer extends ContainerOrganizer { public boolean verifyType(Object o, boolean b) { if (o instanceof ColorItem) return true; if (b) throw new RuntimeException("Invalid Type"); return false; } float HSBValues[] = new float[3]; /** * This determines how we will compare our ColorItem objects, * in this example we are comparing them by hue. */ public int compareKeys(Object k1, Object k2) { Color a = ((ColorItem) k1).toColor(); Color b = ((ColorItem) k2).toColor(); float aHue, bHue; Color.RGBtoHSB(a.getRed(), a.getGreen(), a.getBlue(), HSBValues); aHue = HSBValues[0]; Color.RGBtoHSB(b.getRed(), b.getGreen(), b.getBlue(), HSBValues); bHue = HSBValues[0]; if (aHue < bHue) { return -1; } else if (aHue > bHue) { return 1; } else { String s1 = ((ColorItem) k1).nameOf(); String s2 = ((ColorItem) k2).nameOf(); return (s1.compareTo(s2)); } } public Object keyForObject(Object s) { if (! (s instanceof ColorItem)) { throw new RuntimeException("Invalid key type."); } return s; } public ColorItem fetch(ColorItem key) { if (localContainer == null) return null; return (ColorItem) localContainer.get(key); } }