001 /* 002 * Copyright (C) 2008-2010 by Holger Arndt 003 * 004 * This file is part of the Universal Java Matrix Package (UJMP). 005 * See the NOTICE file distributed with this work for additional 006 * information regarding copyright ownership and licensing. 007 * 008 * UJMP is free software; you can redistribute it and/or modify 009 * it under the terms of the GNU Lesser General Public License as 010 * published by the Free Software Foundation; either version 2 011 * of the License, or (at your option) any later version. 012 * 013 * UJMP is distributed in the hope that it will be useful, 014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 016 * GNU Lesser General Public License for more details. 017 * 018 * You should have received a copy of the GNU Lesser General Public 019 * License along with UJMP; if not, write to the 020 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, 021 * Boston, MA 02110-1301 USA 022 */ 023 024 package org.ujmp.gui.util; 025 026 import java.awt.Color; 027 028 import org.ujmp.core.Matrix; 029 import org.ujmp.gui.colormap.ColorMap; 030 031 public abstract class ColorUtil { 032 public static final Color[] TRACECOLORS = { Color.blue, Color.green, 033 Color.red, Color.black, Color.yellow, Color.cyan }; 034 035 public static final Color contrastBW(Color c) { 036 if ((c.getRed() + c.getGreen() + c.getBlue()) > 200.0) { 037 return (Color.black); 038 } else { 039 return (Color.white); 040 } 041 } 042 043 public static final Color fromRGB(int v) { 044 return new Color(v); 045 } 046 047 public static final Color fromDouble(double v) { 048 // inf = 255 255 0 yellow 049 // 1 = 0 255 0 green 050 // 0 = 0 0 0 black 051 // -1 = 255 0 0 red 052 // -inf = 255 0 255 magenta 053 // nan = 0 255 255 cyan 054 if (v == Double.MIN_VALUE || Double.isNaN(v)) 055 return (Color.MAGENTA); 056 else if (Double.isInfinite(v)) 057 return (Color.CYAN); 058 else if (v > 1.0) 059 return (ColorMap.colorGreenToYellow[(int) (255.0 * Math 060 .tanh((v - 1.0) / 10.0))]); 061 else if (v > 0.0) 062 return (ColorMap.colorBlackToGreen[(int) (255.0 * v)]); 063 else if (v > -1.0) 064 return (ColorMap.colorRedToBlack[(int) (255.0 * (v + 1.0))]); 065 else 066 return (ColorMap.colorRedToMagenta[(int) (255.0 * Math 067 .tanh((-v - 1.0) / 10.0))]); 068 } 069 070 private static Color fromString(String s) { 071 if (s == null) 072 return Color.black; 073 int hc = Math.abs(hash(s.hashCode())); 074 int r = 192 + (hc % 256) / 4; 075 hc = hc / 256; 076 int g = 192 + (hc % 256) / 4; 077 hc = hc / 256; 078 int b = 192 + (hc % 256) / 4; 079 return new Color(r > 255 ? 255 : r, g > 255 ? 255 : g, b > 255 ? 255 080 : b); 081 } 082 083 public static Color fromObject(Object v) { 084 if (v == null) { 085 return Color.black; 086 } 087 if (v instanceof Double) { 088 return fromDouble((Double) v); 089 } 090 if (v instanceof Integer) { 091 return fromRGB((Integer) v); 092 } 093 if (v instanceof Matrix) { 094 if (((Matrix) v).isScalar()) { 095 return fromObject(((Matrix) v).getAsObject(0, 0)); 096 } 097 } 098 try { 099 double d = Double.parseDouble(v.toString()); 100 return fromDouble(d); 101 } catch (Exception e) { 102 } 103 return fromString(v.toString()); 104 } 105 106 private static int hash(int h) { 107 h ^= (h >>> 20) ^ (h >>> 12); 108 return h ^ (h >>> 7) ^ (h >>> 4); 109 } 110 111 }