1 44 45 package org.jfree.util; 46 47 import java.awt.GradientPaint ; 48 import java.awt.Paint ; 49 import java.awt.Color ; 50 import java.lang.reflect.Field ; 51 import java.lang.reflect.Modifier ; 52 53 58 public class PaintUtilities { 59 60 63 private PaintUtilities() { 64 } 65 66 76 public static boolean equal(final Paint p1, final Paint p2) { 77 78 if (p1 == null) { 80 return (p2 == null); 81 } 82 if (p2 == null) { 83 return false; 84 } 85 86 boolean result = false; 87 if (p1 instanceof GradientPaint && p2 instanceof GradientPaint ) { 89 final GradientPaint gp1 = (GradientPaint ) p1; 90 final GradientPaint gp2 = (GradientPaint ) p2; 91 result = gp1.getColor1().equals(gp2.getColor1()) 92 && gp1.getColor2().equals(gp2.getColor2()) 93 && gp1.getPoint1().equals(gp2.getPoint1()) 94 && gp1.getPoint2().equals(gp2.getPoint2()) 95 && gp1.isCyclic() == gp2.isCyclic() 96 && gp1.getTransparency() == gp1.getTransparency(); 97 } 98 else { 99 result = p1.equals(p2); 100 } 101 return result; 102 103 } 104 105 113 public static String colorToString (final Color c) 114 { 115 try { 116 final Field [] fields = Color .class.getFields(); 117 for (int i = 0; i < fields.length; i++) { 118 final Field f = fields[i]; 119 if (Modifier.isPublic(f.getModifiers()) 120 && Modifier.isFinal(f.getModifiers()) 121 && Modifier.isStatic(f.getModifiers())) { 122 final String name = f.getName(); 123 final Object oColor = f.get(null); 124 if (oColor instanceof Color ) { 125 if (c.equals(oColor)) { 126 return name; 127 } 128 } 129 } 130 } 131 } 132 catch (Exception e) { 133 } 135 136 final String color = Integer.toHexString(c.getRGB() & 0x00ffffff); 138 final StringBuffer retval = new StringBuffer (7); 139 retval.append("#"); 140 141 final int fillUp = 6 - color.length(); 142 for (int i = 0; i < fillUp; i++) { 143 retval.append("0"); 144 } 145 146 retval.append(color); 147 return retval.toString(); 148 } 149 150 156 public static Color stringToColor (final String value) 157 { 158 if (value == null) { 159 return Color.black; 160 } 161 try { 162 return Color.decode(value); 164 } 165 catch (NumberFormatException nfe) { 166 try { 168 final Field f = Color .class.getField(value); 170 171 return (Color ) f.get(null); 172 } 173 catch (Exception ce) { 174 Log.info("No such Color : " + value); 175 return Color.black; 177 } 178 } 179 } 180 } 181 | Popular Tags |