001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-present, by David Gilbert 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 * ChartColor.java 029 * --------------- 030 * (C) Copyright 2003-present, by Cameron Riley and Contributors. 031 * 032 * Original Author: Cameron Riley; 033 * Contributor(s): David Gilbert; 034 * Yuri Blankenstein; 035 * 036 */ 037 038package org.jfree.chart; 039 040import java.awt.Color; 041import java.awt.Paint; 042 043/** 044 * Class to extend the number of Colors available to the charts. This 045 * extends the java.awt.Color object and extends the number of final 046 * Colors publicly accessible. 047 */ 048public class ChartColor extends Color { 049 050 /** A very dark red color. */ 051 public static final Color VERY_DARK_RED = new Color(0x80, 0x00, 0x00); 052 053 /** A dark red color. */ 054 public static final Color DARK_RED = new Color(0xc0, 0x00, 0x00); 055 056 /** A light red color. */ 057 public static final Color LIGHT_RED = new Color(0xFF, 0x40, 0x40); 058 059 /** A very light red color. */ 060 public static final Color VERY_LIGHT_RED = new Color(0xFF, 0x80, 0x80); 061 062 /** A very dark yellow color. */ 063 public static final Color VERY_DARK_YELLOW = new Color(0x80, 0x80, 0x00); 064 065 /** A dark yellow color. */ 066 public static final Color DARK_YELLOW = new Color(0xC0, 0xC0, 0x00); 067 068 /** A light yellow color. */ 069 public static final Color LIGHT_YELLOW = new Color(0xFF, 0xFF, 0x40); 070 071 /** A very light yellow color. */ 072 public static final Color VERY_LIGHT_YELLOW = new Color(0xFF, 0xFF, 0x80); 073 074 /** A very dark green color. */ 075 public static final Color VERY_DARK_GREEN = new Color(0x00, 0x80, 0x00); 076 077 /** A dark green color. */ 078 public static final Color DARK_GREEN = new Color(0x00, 0xC0, 0x00); 079 080 /** A light green color. */ 081 public static final Color LIGHT_GREEN = new Color(0x40, 0xFF, 0x40); 082 083 /** A very light green color. */ 084 public static final Color VERY_LIGHT_GREEN = new Color(0x80, 0xFF, 0x80); 085 086 /** A very dark cyan color. */ 087 public static final Color VERY_DARK_CYAN = new Color(0x00, 0x80, 0x80); 088 089 /** A dark cyan color. */ 090 public static final Color DARK_CYAN = new Color(0x00, 0xC0, 0xC0); 091 092 /** A light cyan color. */ 093 public static final Color LIGHT_CYAN = new Color(0x40, 0xFF, 0xFF); 094 095 /** Aa very light cyan color. */ 096 public static final Color VERY_LIGHT_CYAN = new Color(0x80, 0xFF, 0xFF); 097 098 /** A very dark blue color. */ 099 public static final Color VERY_DARK_BLUE = new Color(0x00, 0x00, 0x80); 100 101 /** A dark blue color. */ 102 public static final Color DARK_BLUE = new Color(0x00, 0x00, 0xC0); 103 104 /** A light blue color. */ 105 public static final Color LIGHT_BLUE = new Color(0x40, 0x40, 0xFF); 106 107 /** A very light blue color. */ 108 public static final Color VERY_LIGHT_BLUE = new Color(0x80, 0x80, 0xFF); 109 110 /** A very dark magenta/purple color. */ 111 public static final Color VERY_DARK_MAGENTA = new Color(0x80, 0x00, 0x80); 112 113 /** A dark magenta color. */ 114 public static final Color DARK_MAGENTA = new Color(0xC0, 0x00, 0xC0); 115 116 /** A light magenta color. */ 117 public static final Color LIGHT_MAGENTA = new Color(0xFF, 0x40, 0xFF); 118 119 /** A very light magenta color. */ 120 public static final Color VERY_LIGHT_MAGENTA = new Color(0xFF, 0x80, 0xFF); 121 122 /** 123 * Creates a Color with an opaque sRGB with red, green and blue values in 124 * range 0-255. 125 * 126 * @param r the red component in range 0x00-0xFF. 127 * @param g the green component in range 0x00-0xFF. 128 * @param b the blue component in range 0x00-0xFF. 129 */ 130 public ChartColor(int r, int g, int b) { 131 super(r, g, b); 132 } 133 134 /** 135 * Convenience method to return an array of {@code Paint} objects that 136 * represent the pre-defined colors in the {@code Color} and 137 * {@code ChartColor} objects. 138 * 139 * @return An array of objects with the {@code Paint} interface. 140 * @see #createDefaultColorArray() 141 */ 142 public static Paint[] createDefaultPaintArray() { 143 return createDefaultColorArray(); 144 } 145 146 /** 147 * Convenience method to return an array of {@code Color} objects that 148 * represent the pre-defined colors in the {@code Color} and 149 * {@code ChartColor} objects. 150 * 151 * @return An array of objects with the {@code Color} interface. 152 */ 153 public static Color[] createDefaultColorArray() { 154 return new Color[] { 155 new Color(0xFF, 0x55, 0x55), 156 new Color(0x55, 0x55, 0xFF), 157 new Color(0x55, 0xFF, 0x55), 158 new Color(0xFF, 0xFF, 0x55), 159 new Color(0xFF, 0x55, 0xFF), 160 new Color(0x55, 0xFF, 0xFF), 161 Color.PINK, 162 Color.GRAY, 163 ChartColor.DARK_RED, 164 ChartColor.DARK_BLUE, 165 ChartColor.DARK_GREEN, 166 ChartColor.DARK_YELLOW, 167 ChartColor.DARK_MAGENTA, 168 ChartColor.DARK_CYAN, 169 Color.DARK_GRAY, 170 ChartColor.LIGHT_RED, 171 ChartColor.LIGHT_BLUE, 172 ChartColor.LIGHT_GREEN, 173 ChartColor.LIGHT_YELLOW, 174 ChartColor.LIGHT_MAGENTA, 175 ChartColor.LIGHT_CYAN, 176 Color.LIGHT_GRAY, 177 ChartColor.VERY_DARK_RED, 178 ChartColor.VERY_DARK_BLUE, 179 ChartColor.VERY_DARK_GREEN, 180 ChartColor.VERY_DARK_YELLOW, 181 ChartColor.VERY_DARK_MAGENTA, 182 ChartColor.VERY_DARK_CYAN, 183 ChartColor.VERY_LIGHT_RED, 184 ChartColor.VERY_LIGHT_BLUE, 185 ChartColor.VERY_LIGHT_GREEN, 186 ChartColor.VERY_LIGHT_YELLOW, 187 ChartColor.VERY_LIGHT_MAGENTA, 188 ChartColor.VERY_LIGHT_CYAN 189 }; 190 } 191 192 /** 193 * Creates an array of {@link Color#darker() darker} {@code colors} to use 194 * for e.g. borders. 195 * 196 * @param colors original colors 197 * @return a new array containing {@link Color#darker() darker} instances of 198 * the original colors. 199 */ 200 public static Color[] createDarkerColorArray(Color[] colors) { 201 final Color[] result = new Color[colors.length]; 202 for (int i = 0; i < colors.length; i++) { 203 result[i] = colors[i].darker(); 204 } 205 return result; 206 } 207 208 /** 209 * Returns either {@link Color#BLACK black} or {@link Color#WHITE white}, 210 * depending on the provided {@code color} to achieve the best contrast for 211 * e.g. text labels.<br> 212 * The {@code color} is 213 * <a href="https://en.wikipedia.org/wiki/YIQ#From_RGB_to_YIQ">converted 214 * from RGB to YIQ</a> and the luminance value (Y; ≈[0 .. 255]) is 215 * used to determine if {@code color} is closer to either {@link Color#BLACK 216 * black} or {@link Color#WHITE white}. 217 * 218 * @param color the color for which the contrasted color is computed 219 * @return either {@link Color#BLACK black} or {@link Color#WHITE white}, 220 * depending on {@code color} 221 */ 222 public static Color getContrastColor(Color color) { 223 // From Wikipedia: The Y component represents the luma information, and 224 // is the only component used by black-and-white television receivers. 225 final double luminanceY = 0.299 * color.getRed() 226 + 0.587 * color.getGreen() + 0.114 * color.getBlue(); 227 return luminanceY >= 128 ? Color.BLACK : Color.WHITE; 228 } 229}