001 /*--------------------------------------------------------------------------+ 002 $Id: ECCSMColor.java 26283 2010-02-18 11:18:57Z juergens $ 003 | | 004 | Copyright 2005-2010 Technische Universitaet Muenchen | 005 | | 006 | Licensed under the Apache License, Version 2.0 (the "License"); | 007 | you may not use this file except in compliance with the License. | 008 | You may obtain a copy of the License at | 009 | | 010 | http://www.apache.org/licenses/LICENSE-2.0 | 011 | | 012 | Unless required by applicable law or agreed to in writing, software | 013 | distributed under the License is distributed on an "AS IS" BASIS, | 014 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 015 | See the License for the specific language governing permissions and | 016 | limitations under the License. | 017 +--------------------------------------------------------------------------*/ 018 package edu.tum.cs.commons.color; 019 020 import java.awt.Color; 021 import java.awt.Graphics2D; 022 import java.awt.geom.Rectangle2D; 023 import java.awt.image.BufferedImage; 024 025 import edu.tum.cs.commons.string.StringUtils; 026 027 /** 028 * Colors of the CCSM CI. 029 * 030 * @author Florian Deissenboeck 031 * @author $Author: juergens $ 032 * @version $Rev: 26283 $ 033 * @levd.rating GREEN Hash: BBF7FFF92CC252EB7D7E4BE16C822AD2 034 */ 035 public enum ECCSMColor implements IColor { 036 037 /** Light blue */ 038 LIGHT_BLUE(205, 222, 239), 039 040 /** Blue */ 041 BLUE(99, 156, 206), 042 043 /** Purple */ 044 PURPLE(50, 50, 102), 045 046 /** Dark Blue */ 047 DARK_BLUE(0, 117, 204), 048 049 /** Green */ 050 GREEN(102, 204, 102), 051 052 /** Yellow */ 053 YELLOW(255, 255, 51), 054 055 /** Red */ 056 RED(255, 102, 51), 057 058 /** Dark red */ 059 DARK_RED(204, 51, 51), 060 061 /** Light Gray */ 062 LIGHT_GRAY(204, 204, 204), 063 064 /** Dark Gray */ 065 DARK_GRAY(102, 102, 102); 066 067 /** Red value. */ 068 private final int red; 069 070 /** Green value. */ 071 private final int green; 072 073 /** Blue value. */ 074 private final int blue; 075 076 /** AWT color. */ 077 private Color color; 078 079 /** Create new color. */ 080 private ECCSMColor(int red, int green, int blue) { 081 check(red); 082 check(green); 083 check(blue); 084 this.red = red; 085 this.green = green; 086 this.blue = blue; 087 } 088 089 /** Check if the color value is legal. */ 090 private void check(int colorValue) { 091 if (colorValue < 0 || colorValue > 255) { 092 throw new IllegalArgumentException( 093 "Value must be between 0 and 255"); 094 } 095 } 096 097 /** {@inheritDoc} */ 098 public String getHTMLColorCode() { 099 return String.format("#%02X%02X%02X", red, green, blue); 100 } 101 102 /** {@inheritDoc} */ 103 public Color getColor() { 104 if (color == null) { 105 color = new Color(red, green, blue); 106 } 107 return color; 108 } 109 110 /** Get string representation. */ 111 @Override 112 public String toString() { 113 return name() + "; " + getHTMLColorCode() + "; " 114 + String.format("%03d, %03d, %03d", red, green, blue); 115 116 } 117 118 /** Get table of all defined colors. */ 119 public static String getColorTable() { 120 StringBuilder result = new StringBuilder(); 121 122 result.append(" HTML | r g b | name"); 123 result.append(StringUtils.CR); 124 125 for (ECCSMColor color : values()) { 126 result.append(color.getHTMLColorCode()); 127 result.append(" | "); 128 result.append(String.format("%03d, %03d, %03d", color.red, 129 color.green, color.blue)); 130 result.append(" | "); 131 result.append(color.name()); 132 result.append(StringUtils.CR); 133 } 134 135 return result.toString(); 136 } 137 138 /** 139 * Get image showing all defined colors. If image height is sufficient, 140 * color information text is shown. 141 * 142 * @param width 143 * image width 144 * @param height 145 * image height 146 */ 147 public static BufferedImage getColorChart(int width, int height) { 148 149 BufferedImage image = new BufferedImage(width, height, 150 BufferedImage.TYPE_INT_RGB); 151 152 Graphics2D graphics = image.createGraphics(); 153 154 float barHeight = (float) height / values().length; 155 boolean drawName = graphics.getFontMetrics().getHeight() < barHeight; 156 157 float y = 0; 158 for (ECCSMColor color : values()) { 159 graphics.setColor(color.getColor()); 160 161 Rectangle2D rect = new Rectangle2D.Float(0, y, width, barHeight); 162 graphics.fill(rect); 163 164 y += barHeight; 165 166 if (drawName) { 167 graphics.setColor(Color.black); 168 graphics.drawString(color.toString(), 5f, y - 3); 169 } 170 } 171 172 return image; 173 } 174 175 }