001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2017, by Object Refinery Limited and Contributors. 006 * 007 * Project Info: http://www.jfree.org/jfreechart/index.html 008 * 009 * This library is free software; you can redistribute it and/or modify it 010 * under the terms of the GNU Lesser General Public License as published by 011 * the Free Software Foundation; either version 2.1 of the License, or 012 * (at your option) any later version. 013 * 014 * This library is distributed in the hope that it will be useful, but 015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 017 * License for more details. 018 * 019 * You should have received a copy of the GNU Lesser General Public 020 * License along with this library; if not, write to the Free Software 021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 022 * USA. 023 * 024 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 025 * Other names may be trademarks of their respective owners.] 026 * 027 */ 028 029package org.jfree.chart.util; 030 031import java.awt.Color; 032import java.awt.GradientPaint; 033import java.awt.LinearGradientPaint; 034import java.awt.Paint; 035import java.awt.RadialGradientPaint; 036import java.lang.reflect.Field; 037import java.lang.reflect.Modifier; 038import java.util.Arrays; 039 040/** 041 * Utility code that relates to {@code Paint} objects. 042 */ 043public class PaintUtils { 044 045 /** 046 * Private constructor prevents object creation. 047 */ 048 private PaintUtils() { 049 } 050 051 /** 052 * Returns {@code true} if the two {@code Paint} objects are equal 053 * OR both {@code null}. This method handles 054 * {@code GradientPaint}, {@code LinearGradientPaint} and 055 * {@code RadialGradientPaint} as a special cases, since those classes do 056 * not override the {@code equals()} method. 057 * 058 * @param p1 paint 1 ({@code null} permitted). 059 * @param p2 paint 2 ({@code null} permitted). 060 * 061 * @return A boolean. 062 */ 063 public static boolean equal(Paint p1, Paint p2) { 064 if (p1 == p2) { 065 return true; 066 } 067 068 // handle cases where either or both arguments are null 069 if (p1 == null) { 070 return (p2 == null); 071 } 072 if (p2 == null) { 073 return false; 074 } 075 076 // handle GradientPaint as a special case... 077 if (p1 instanceof GradientPaint && p2 instanceof GradientPaint) { 078 GradientPaint gp1 = (GradientPaint) p1; 079 GradientPaint gp2 = (GradientPaint) p2; 080 return gp1.getColor1().equals(gp2.getColor1()) 081 && gp1.getColor2().equals(gp2.getColor2()) 082 && gp1.getPoint1().equals(gp2.getPoint1()) 083 && gp1.getPoint2().equals(gp2.getPoint2()) 084 && gp1.isCyclic() == gp2.isCyclic() 085 && gp1.getTransparency() == gp1.getTransparency(); 086 } else if (p1 instanceof LinearGradientPaint 087 && p2 instanceof LinearGradientPaint) { 088 LinearGradientPaint lgp1 = (LinearGradientPaint) p1; 089 LinearGradientPaint lgp2 = (LinearGradientPaint) p2; 090 return lgp1.getStartPoint().equals(lgp2.getStartPoint()) 091 && lgp1.getEndPoint().equals(lgp2.getEndPoint()) 092 && Arrays.equals(lgp1.getFractions(), lgp2.getFractions()) 093 && Arrays.equals(lgp1.getColors(), lgp2.getColors()) 094 && lgp1.getCycleMethod() == lgp2.getCycleMethod() 095 && lgp1.getColorSpace() == lgp2.getColorSpace() 096 && lgp1.getTransform().equals(lgp2.getTransform()); 097 } else if (p1 instanceof RadialGradientPaint 098 && p2 instanceof RadialGradientPaint) { 099 RadialGradientPaint rgp1 = (RadialGradientPaint) p1; 100 RadialGradientPaint rgp2 = (RadialGradientPaint) p2; 101 return rgp1.getCenterPoint().equals(rgp2.getCenterPoint()) 102 && rgp1.getRadius() == rgp2.getRadius() 103 && rgp1.getFocusPoint().equals(rgp2.getFocusPoint()) 104 && Arrays.equals(rgp1.getFractions(), rgp2.getFractions()) 105 && Arrays.equals(rgp1.getColors(), rgp2.getColors()) 106 && rgp1.getCycleMethod() == rgp2.getCycleMethod() 107 && rgp1.getColorSpace() == rgp2.getColorSpace() 108 && rgp1.getTransform().equals(rgp2.getTransform()); 109 } else { 110 return p1.equals(p2); 111 } 112 } 113 114 /** 115 * Converts a color into a string. If the color is equal to one of the 116 * defined constant colors, that name is returned instead. Otherwise the 117 * color is returned as hex-string. 118 * 119 * @param c the color. 120 * @return the string for this color. 121 */ 122 public static String colorToString(Color c) { 123 try { 124 Field[] fields = Color.class.getFields(); 125 for (int i = 0; i < fields.length; i++) { 126 Field f = fields[i]; 127 if (Modifier.isPublic(f.getModifiers()) 128 && Modifier.isFinal(f.getModifiers()) 129 && Modifier.isStatic(f.getModifiers())) { 130 final String name = f.getName(); 131 final Object oColor = f.get(null); 132 if (oColor instanceof Color) { 133 if (c.equals(oColor)) { 134 return name; 135 } 136 } 137 } 138 } 139 } catch (Exception e) { 140 // 141 } 142 143 // no defined constant color, so this must be a user defined color 144 final String color = Integer.toHexString(c.getRGB() & 0x00ffffff); 145 final StringBuffer retval = new StringBuffer(7); 146 retval.append("#"); 147 148 final int fillUp = 6 - color.length(); 149 for (int i = 0; i < fillUp; i++) { 150 retval.append("0"); 151 } 152 153 retval.append(color); 154 return retval.toString(); 155 } 156 157 /** 158 * Converts a given string into a color. 159 * 160 * @param value the string, either a name or a hex-string. 161 * @return the color. 162 */ 163 public static Color stringToColor(String value) { 164 if (value == null) { 165 return Color.BLACK; 166 } 167 try { 168 // get color by hex or octal value 169 return Color.decode(value); 170 } catch (NumberFormatException nfe) { 171 // if we can't decode lets try to get it by name 172 try { 173 // try to get a color by name using reflection 174 final Field f = Color.class.getField(value); 175 return (Color) f.get(null); 176 } catch (Exception ce) { 177 // if we can't get any color return black 178 return Color.BLACK; 179 } 180 } 181 } 182} 183