    001package org.vishia.gral.ifc;
    002
    003import java.util.Map;
    004import java.util.TreeMap;
    005
    006/**This class defines a data structure to hold color values in a usual but system-independent form
    007 * and defines some standard colors by name.
    008 *
    009 *
    010 * @author Hartmut Schorrig
    011 *
    012 */
    013public class GralColor
    014{
    015
    016
    017  /**Version, history and license.
    018   * <ul>
    019   * <li>2012-10-11 Hartmut new concepts from new ColorChooser with usualNames, starting.
    020   * <li>2012-09-07 Hartmut  bugfix: toString: color with hexa value with leading 00 for example "0x00ff00".
    021   * <li>2012-06-09 Hartmut new: {@link #getColor(String)} now accepts a "0xhexa-Value" for a color name.
    022   *  If the color name does not match, a magenta color is returned. The method returns a color in any case.
    023   * <li>2011-10-01 Hartmut new: color lbk light black darker than dark gray. Change values for gray.
    024   * <li>2011-09-08 Hartmut new: some enhancements, new colors.
    025   * <li>2011-09-04 Hartmut chg: Rename from ColorGui to GralColor
    026   * <li>2011-09-04 Hartmut chg: Move the {@link #getColor(String)} and {@link #getColor(int)} with the colorContainer
    027   *     from gridPanel/PropertiesGUI to this.
    028   * <li>2011-05-14 Hartmut new: Reference to the {@link #colorGuimpl} to get the graphic base system color instance if need.
    029   * <li>2010-00-00 Hartmut created. It had contain only the 3 int red, green, blue
    030   * </ul>
    031   *
    032   * <b>Copyright/Copyleft</b>:<br>
    033   * For this source the LGPL Lesser General Public License,
    034   * published by the Free Software Foundation is valid.
    035   * It means:
    036   * <ol>
    037   * <li> You can use this source without any restriction for any desired purpose.
    038   * <li> You can redistribute copies of this source to everybody.
    039   * <li> Every user of this source, also the user of redistribute copies
    040   *    with or without payment, must accept this license for further using.
    041   * <li> But the LPGL is not appropriate for a whole software product,
    042   *    if this source is only a part of them. It means, the user
    043   *    must publish this part of source,
    044   *    but doesn't need to publish the whole source of the own product.
    045   * <li> You can study and modify (improve) this source
    046   *    for own using or for redistribution, but you have to license the
    047   *    modified sources likewise under this LGPL Lesser General Public License.
    048   *    You mustn't delete this Copyright/Copyleft inscription in this source file.
    049   * </ol>
    050   * If you intent to use this source without publishing its usage, you can get
    051   * a second license subscribing a special contract with the author.
    052   *
    053   * @author Hartmut Schorrig = hartmut.schorrig@vishia.de
    054   */
    055  public final static String version = "2015-10-11";
    056
    057
    058
    059  /**Values 0..255 for the base colors. Note: don't change by any application, read only! Changes are overridden on any usage. */
    060  public int red, green, blue;
    061
    062  /**The rgb value, same value as red, green, blue. */
    063  protected int rgb;
    064
    065  /**The unique short name for this color. */
    066  public final String name;
    067
    068  /**String with one or more usual names for this color, especially HTML names, X11 names.
    069   * If more as one name is given, it is separated by comma.
    070   */
    071  protected String usualNames;
    072
    073  /**The color-instance for the implementation can be set from the implementation with the necessary type
    074   * to minimize the effort in dynamic instances.  */
    075  public Object colorGuimpl;
    076
    077  /**Deprecated
    078   * @param red
    079   * @param green
    080   * @param blue
    081   * @deprecated use {@link #getColor(int, int, int)} because more as one instances for the same color is prevented then
    082   *   and the color have a correct name if the value matches to a existent color.
    083   */
    084  public GralColor(int red, int green, int blue){
    085    this.red = red; this.green = green; this.blue = blue;
    086    this.rgb = ((red << 16) & 0xff0000) | ((green << 8) & 0xff00) | (blue & 0xff);
    087    name = String.format("0x%06X",new Integer(this.rgb));
    088  }
    089
    090  /**
    091   * @param color
    092   * @deprecated only private using, because more as one instances for the same color is prevented then
    093   *   and the color have a correct name if the value matches to a existent color.
    094   */
    095  public GralColor(int color){
    096    this.rgb = color;
    097    this.red = (color >>16) & 0xff; this.green = (color >>8) & 0xff; this.blue = (color >>0) & 0xff;
    098    name = String.format("0x%06X", new Integer(color));
    099  }
    100
    101  /**Deprecated, only private usage. Use
    102   * @param color
    103   * @deprecated only private using, because more as one instances for the same color is prevented then
    104   *   and the color have a correct name if the value matches to a existent color.
    105   */
    106  public GralColor(String name, int color){
    107    this.rgb = color;
    108    this.red = (color >>16) & 0xff; this.green = (color >>8) & 0xff; this.blue = (color >>0) & 0xff;
    109    this.name = name;
    110  }
    111
    112  public int getColorValue(){ return (red & 0xff)<<16 | (green & 0xff)<<8 | (blue & 0xff); }
    113
    114  public int rgb(){ return rgb; }
    115
    116  public String usualNames(){ return usualNames; }
    117
    118  public String getColorName(){ return name; }
    119
    120
    121  /**The color is not used till yet or it is changed.
    122   * @return
    123   */
    124  public boolean notUsed(){ return colorGuimpl == null; }
    125
    126  private static class GralColorContainer
    127  {
    128    Map<String, GralColor> colorsByName = new TreeMap<String, GralColor>();
    129    Map<Integer, GralColor> colorsByValue = new TreeMap<Integer, GralColor>();
    130
    131    public GralColor[][] stdColors = new GralColor[6][19];
    132
    133
    134    GralColorContainer()
    135    {
    136      addColor("white",  0xffffff);
    137      addColor("gray",   0x808080);
    138      addColor("black",  0x000000);
    139      addColor("red",    0xff0000);
    140      addColor("lime",   0x00ff00);
    141      addColor("green",  0x00e000);
    142      addColor("blue",   0x0000ff);
    143      addColor("yellow", 0xffff00);
    144      addColor("magenta",0xff00ff);
    145      addColor("cyan",   0x00ffff);
    146      addColor("brown",  0x600020);
    147      addColor("amber",  0xffe000);
    148      addColor("orange", 0xffa000);
    149      addColor("violet", 0x6000ff);
    150      addColor("mauve",  0xd0d0ff);
    151      addColor("pink",   0xff0080);
    152      addColor("red-purple", 0xff0060);
    153      addColor("purple", 0x800080);
    154
    155      addColor("ye", 0xffff00); //
    156      addColor("ygn", 0x9ACD32); //yellow green
    157      addColor("lm", 0x00ff00); //lime
    158      addColor("gn", 0x00e000);
    159      addColor("g2", 0x00e020);
    160      addColor("cy", 0x00ffff);
    161
    162      //saturated colors
    163      addColor("wh", 0xffffff);
    164      addColor("gr", 0xa0a0a0);
    165      addColor("bk", 0x000000);
    166      addColor("ma", 0xff00ff);
    167      addColor("ma2", 0xff00c0);
    168      addColor("ma3", 0xff0080);
    169      addColor("ma4", 0xff0040);
    170      addColor("rpu", 0xff0040);  //red purple
    171      addColor("pu", 0x800080);  //purple html
    172      addColor("rd", 0xff2020);
    173      addColor("or", 0xffa000);  //html: ffa500
    174      addColor("cc", 0xd2691e);  //chocolate html
    175      addColor("am", 0xffe000);
    176      addColor("sye", 0xc0c000); //saturated yellow
    177      addColor("ol", 0x808000); //olive
    178      addColor("od", 0x6b8e23); //olive drab html
    179      addColor("fgn",  0x228b22);  //forest green html
    180      addColor("sgn", 0x2E8B57); //sea green html
    181      addColor("sg2", 0x00c020); //saturated green-blue
    182      addColor("scy", 0x00c0c0);
    183      addColor("tl", 0x008080);  //teal html
    184      addColor("bl", 0x0000ff);
    185      addColor("ubl", 0x000080); //navi html ultramarin blue
    186      addColor("vi", 0x6000ff);
    187
    188      addColor("bn", 0x600020);
    189      addColor("mv", 0xd0d0ff);
    190      addColor("pk", 0xff0080);
    191
    192      //light colors
    193      addColor("lgr", 0xd0d0d0);
    194      addColor("lrd", 0xff8080);
    195      addColor("lgn", 0x80ff80);
    196      addColor("lbl", 0xa0a0ff);
    197      addColor("lye", 0xffff80);
    198      addColor("lam", 0xfff080);
    199      addColor("lor", 0xffa060);
    200      addColor("lma", 0xff80ff);
    201      addColor("lmv", 0xe0e0ff);
    202      addColor("lcy", 0x00ffff);
    203      addColor("lbk", 0x404040);
    204
    205      //pastel colors, especially for background color
    206      addColor("pgr", 0xf0f0f0);
    207      addColor("prd", 0xffe0e0);
    208      addColor("pgn", 0xe0ffe0);
    209      addColor("pbl", 0xe0e0ff);
    210      addColor("pye", 0xffffc0);
    211      addColor("pam", 0xfff0c0);
    212      addColor("por", 0xffc080);
    213      addColor("pmv", 0xfff0ff);
    214      addColor("pma", 0xffa0ff);
    215      addColor("pcy", 0xa0ffff);
    216
    217      //dark colors for forground
    218      addColor("dgr", 0x606060);
    219      addColor("drd", 0x800000);
    220      addColor("dor", 0xff8c00);  //dark orange html
    221      addColor("dbn", 0x400010);
    222      addColor("dol", 0x556b2f); //dark olive, html
    223      addColor("dgn", 0x008000);
    224      addColor("dbl", 0x000080);
    225      addColor("dye", 0x606000);
    226      addColor("dma", 0x600060);
    227      addColor("dcy", 0x006060);
    228
    229
    230      int iRow = 0;
    231
    232      addColor(iRow, 0, "pMa1", 0xffc0fF);
    233      addColor(iRow, 1, "pMa2", 0xffc0e0);
    234      addColor(iRow, 2, "pRd1", 0xffd0d0);
    235      addColor(iRow, 3, "pRd2", 0xffe0c0);
    236      addColor(iRow, 4, "pRd3", 0xfff0c0);
    237      addColor(iRow, 5, "pYe1", 0xffffd0);
    238      addColor(iRow, 6, "pYe2", 0xe0ffc0);
    239      addColor(iRow, 7, "pYe3", 0xe0ffc0);
    240      addColor(iRow, 8, "pYe4", 0xe0ffc0);
    241      addColor(iRow, 9, "pGn1", 0xd0ffd0);
    242      addColor(iRow,10, "pGn2", 0xc0fff0);
    243      addColor(iRow,11, "pGn3", 0xc0ffe0);
    244      addColor(iRow,12, "pGn4", 0xc0fff0);
    245      addColor(iRow,13, "pCy1", 0xc0ffff);
    246      addColor(iRow,14, "pCy2", 0xd0f0ff);
    247      addColor(iRow,15, "pCy3", 0xc0e0ff);
    248      addColor(iRow,16, "pBl1", 0xd0d0ff);
    249      addColor(iRow,17, "pBl2", 0xe0c0ff);
    250      addColor(iRow,18, "pBl3", 0xf0c0ff);
    251
    252      iRow +=1;
    253      addColor(iRow, 0, "lMa1", 0xff80fF);
    254      addColor(iRow, 1, "lMa2", 0xff80c8);
    255      addColor(iRow, 2, "lRd1", 0xffb8b8);
    256      addColor(iRow, 3, "lRd2", 0xffc8a0);
    257      addColor(iRow, 4, "lRd3", 0xffe090);
    258      addColor(iRow, 5, "lYe1", 0xffff80);
    259      addColor(iRow, 6, "lYe2", 0xe0ff80);
    260      addColor(iRow, 7, "lYe3", 0xc0ff80);
    261      addColor(iRow, 8, "lYe4", 0xa0ff80);
    262      addColor(iRow, 9, "lGn1", 0x70ff70);
    263      addColor(iRow,10, "lGn2", 0x80ffa0);
    264      addColor(iRow,11, "lGn3", 0x80ffc0);
    265      addColor(iRow,12, "lGn4", 0x80ffe0);
    266      addColor(iRow,13, "lCy1", 0x80ffff);
    267      addColor(iRow,14, "lCy2", 0x98e8ff);
    268      addColor(iRow,15, "lCy3", 0xb0d0ff);
    269      addColor(iRow,16, "lBl1", 0xc0c0ff);
    270      addColor(iRow,17, "lBl2", 0xd0b0ff);
    271      addColor(iRow,18, "lBl3", 0xe0a0ff);
    272
    273      iRow +=1;
    274      addColor(iRow, 0, "Ma1", 0xff00fF);
    275      addColor(iRow, 1, "Ma2", 0xff4090);
    276      addColor(iRow, 2, "Rd1", 0xff7070);
    277      addColor(iRow, 3, "Rd2", 0xff9040);
    278      addColor(iRow, 4,  "Am", 0xffe000);
    279      addColor(iRow, 5, "Ye1", 0xffff00);
    280      addColor(iRow, 6, "Ye2", 0xc0ff00);
    281      addColor(iRow, 7, "Ye3", 0x80ff00);
    282      addColor(iRow, 8, "Ye4", 0x40ff00);
    283      addColor(iRow, 9, "Gn1", 0x00ff00);
    284      addColor(iRow,10, "Gn2", 0x00ff40);
    285      addColor(iRow,11, "Gn3", 0x00ff80);
    286      addColor(iRow,12, "Gn4", 0x00ffc0);
    287      addColor(iRow,13, "Cy1", 0x00ffff);
    288      addColor(iRow,14, "Cy2", 0x30d0ff);
    289      addColor(iRow,15, "Cy3", 0x60a0ff);
    290      addColor(iRow,16, "Bl1", 0x8080ff);
    291      addColor(iRow,17, "Bl2", 0xa060ff);
    292      addColor(iRow,18, "Bl3", 0xc040ff);
    293
    294      iRow +=1;
    295      addColor(iRow, 0, "gMa1", 0xd080d0);
    296      addColor(iRow, 1, "gMa2", 0xd080c8);
    297      addColor(iRow, 2, "gRd1", 0xd0b8b8);
    298      addColor(iRow, 3, "gRd2", 0xd0c8a0);
    299      addColor(iRow, 4, "gRd3", 0xd0e090);
    300      addColor(iRow, 5, "gYe1", 0xd0d080);
    301      addColor(iRow, 6, "gYe2", 0xe0d080);
    302      addColor(iRow, 7, "gYe3", 0xc0d080);
    303      addColor(iRow, 8, "gYe4", 0xa0d080);
    304      addColor(iRow, 9, "gGn1", 0x70d070);
    305      addColor(iRow,10, "gGn2", 0x80d0a0);
    306      addColor(iRow,11, "gGn3", 0x80d0c0);
    307      addColor(iRow,12, "gGn4", 0x80d0e0);
    308      addColor(iRow,13, "gCy1", 0x80d0d0);
    309      addColor(iRow,14, "gCy2", 0x98e8d0);
    310      addColor(iRow,15, "gCy3", 0xb0d0d0);
    311      addColor(iRow,16, "gBl1", 0xc0c0d0);
    312      addColor(iRow,17, "gBl2", 0xd0b0d0);
    313      addColor(iRow,18, "gBl3", 0xe0a0d0);
    314
    315      iRow +=1;
    316      addColor(iRow, 0, "sMa1", 0xd000d0);
    317      addColor(iRow, 1, "sMa2", 0xe02080);
    318      addColor(iRow, 2, "sRd1", 0xff0000);
    319      addColor(iRow, 3, "sRd2", 0xffc000);
    320      addColor(iRow, 4, "sRd3", 0xff8000);
    321      addColor(iRow, 5, "sYe1", 0xe8e800);
    322      addColor(iRow, 6, "sYe2", 0xc0c000);
    323      addColor(iRow, 7, "sYe3", 0x80b000);
    324      addColor(iRow, 8, "sYe4", 0x40b000);
    325      addColor(iRow, 9, "sGn1", 0x00c000);
    326      addColor(iRow,10, "sGn2", 0x00b040);
    327      addColor(iRow,11, "sGn3", 0x00b080);
    328      addColor(iRow,12, "sGn4", 0x00b0c0);
    329      addColor(iRow,13, "sCy1", 0x00d0d0);
    330      addColor(iRow,14, "sCy2", 0x0080d0);
    331      addColor(iRow,15, "sCy3", 0x3030ff);
    332      addColor(iRow,16, "sBl1", 0x0000ff);
    333      addColor(iRow,17, "sBl2", 0x8030ff);
    334      addColor(iRow,18, "sBl3", 0xc000ff);
    335
    336      iRow +=1;
    337      addColor(iRow, 0, "dMa1", 0x800080);
    338      addColor(iRow, 1, "dMa2", 0x800000);
    339      addColor(iRow, 2, "dRd1", 0xc00000);
    340      addColor(iRow, 3, "dRd2", 0x808000);
    341      addColor(iRow, 4, "dRd3", 0x800000);
    342      addColor(iRow, 5, "dYe1", 0xc0c000);
    343      addColor(iRow, 6, "dYe2", 0x606000);
    344      addColor(iRow, 7, "dYe3", 0x405000);
    345      addColor(iRow, 8, "dYe4", 0x20000);
    346      addColor(iRow, 9, "dGn1", 0x008000);
    347      addColor(iRow,10, "dGn2", 0x004000);
    348      addColor(iRow,11, "dGn3", 0x005840);
    349      addColor(iRow,12, "dGn4", 0x005860);
    350      addColor(iRow,13, "dCy1", 0x006868);
    351      addColor(iRow,14, "dCy2", 0x004068);
    352      addColor(iRow,15, "dCy3", 0x171780);
    353      addColor(iRow,16, "dBl1", 0x0000c0);
    354      addColor(iRow,17, "dBl2", 0x2000a0);
    355      addColor(iRow,18, "dBl3", 0x200080);
    356
    357}
    358
    359    private void addColor(int i1, int i2, String name, int value)
    360    { GralColor color = new GralColor(name, value);
    361      stdColors[i1][i2] = color;
    362      colorsByName.put(name, color);
    363      colorsByValue.put(new Integer(value), color);
    364
    365    }
    366
    367    private void addColor(String name, int value)
    368    { GralColor color = new GralColor(name, value);
    369      colorsByName.put(name, color);
    370      colorsByValue.put(new Integer(value), color);
    371
    372    }
    373  }
    374
    375  private static GralColorContainer container = new GralColorContainer();
    376
    377  /**Gets a color by its name.
    378   * The following named colors are available:
    379   * <br>Full name simple colors:
    380   * <ul>
    381   * <li>"white",  0xffffff);
    382   * <li>"gray",   0x808080);
    383   * <li>"black",  0x000000);
    384   * <li>"red",    0xff0000);
    385   * <li>"green",  0x00ff00);
    386   * <li>"blue",   0x0000ff);
    387   * <li>"yellow", 0xffff00);
    388   * <li>"magenta",0xff00ff);
    389   * <li>"cyan",   0x00ffff);
    390   * <li>"brown",  0x600020);
    391   * <li>"amber",  0xffe000);
    392   * <li>"orange",  0xffa000);
    393   * <li>"pink",   0xff0080);
    394   * <li>"violet",  0x6000ff);
    395   * <li>"purple",  0xff00ff);
    396   * </ul>
    397
    398   * <br>saturated colors: The same as full name colors. All colors are designated with 2 letters.
    399   * <ul>
    400   * <li>"wh", white, 0xffffff);
    401   * <li>"gr", gray, 0xa0a0a0);
    402   * <li>"bk", black, 0x000000);
    403   * <li>"rd", red, 0xff0000);
    404   * <li>"gn", green, 0x00e000);
    405   * <li>"bl", blue, 0x0000ff);
    406   * <li>"ye", yellow, 0xffff00);
    407   * <li>"ma", magenta, 0xff00ff);
    408   * <li>"cy", cyan, 0x00ffff);
    409   * <li>"bn", brown, 0x600020);
    410   * <li>"am", amber, 0xffe000);
    411   * <li>"or", orange, 0xffa000);
    412   * <li>"pk", orange, 0xff0080);
    413   * <li>"vi", violet 0x6000ff);
    414   * <li>"pu", purple, de: purpurfarben 0xff0060);
    415   * </ul>
    416
    417   * <br>light colors: The more light version is designated with a "l" before the 2-letter color name.
    418   * <ul>
    419   * <li>"lgr", 0xd0d0d0);
    420   * <li>"lrd", 0xffb0b0);
    421   * <li>"lgn", 0xa0ffa0);
    422   * <li>"lbl", 0xa0a0ff);
    423   * <li>"lye", 0xffff80);
    424   * <li>"lma", 0xffc0ff);
    425   * <li>"lcy", 0xa0ffff);
    426   * <li>"lbk", 0x404040);  a black which is lighter, but darker as dark gray
    427   * </ul>
    428
    429   * <br>pastel colors, especially for background color. They are designates with "p" before the 2-letter color name.
    430   * <ul>
    431   * <li>"pgr", 0xf0f0f0);
    432   * <li>"prd", 0xffe0e0);
    433   * <li>"pgn", 0xe0ffe0);
    434   * <li>"pbl", 0xf0f0ff);
    435   * <li>"pye", 0xffffc0);
    436   * <li>"pma", 0xffe0ff);
    437   * <li>"pcy", 0xd0ffff);
    438   * </ul>
    439
    440   * <br>dark colors for lines and fonts: They are designates with "p" before the 2-letter color name.
    441   * <ul>
    442   * <li>"dgr", 0x606060);
    443   * <li>"drd", 0x800000);
    444   * <li>"dbn", 0x400010);
    445   * <li>"dgn", 0x008000);
    446   * <li>"dbl", 0x000080);
    447   * <li>"dye", 0x606000);
    448   * <li>"dma", 0x600060);
    449   * <li>"dcy", 0x006060);
    450   * </ul>
    451   * Additional a hexa value can be given in form "0xffffff" as name.
    452   * @param name The name or 0xhexa
    453   * @return The proper color from the color container. The color is not created as a new instance if it is contained
    454   *   in the container already.
    455   *   If the color name does not match, a magenta color is returned.
    456   */
    457  public static GralColor getColor(String name){
    458    GralColor color;
    459    if(name.startsWith("0x")){
    460      try{
    461        int colorValue = Integer.parseInt(name.substring(2).trim(), 16);
    462        color = getColor(colorValue);
    463      }catch(NumberFormatException exc){
    464        color = container.colorsByName.get("ma");
    465      }
    466    } else {
    467      color = container.colorsByName.get(name);
    468      if(color == null){
    469        color = container.colorsByName.get("ma");
    470      }
    471    }
    472    return color;
    473  }
    474
    475  public static GralColor getStdColor(int color, int brightness){
    476    return container.stdColors[color][brightness];
    477  }
    478
    479
    480  public static GralColor getColor(int value)
    481  { GralColor color = container.colorsByValue.get(new Integer(value));
    482    if(color == null){
    483      color = new GralColor(value);
    484      container.colorsByValue.put(new Integer(value), color);
    485    }
    486    return color;
    487  }
    488
    489  public static GralColor getColor(int red, int green, int blue)
    490  { int value = ((red << 16) & 0xff0000) | ((green << 8) & 0xff00) | (blue & 0xff);
    491    GralColor color = container.colorsByValue.get(new Integer(value));
    492    if(color == null){
    493      color = new GralColor(value);
    494      container.colorsByValue.put(new Integer(value), color);
    495    }
    496    return color;
    497  }
    498
    499  @Override public String toString(){
    500    int color = ((red << 16) & 0xff0000) | ((green << 8) & 0xff00) | (blue & 0xff);
    501    // name = container.colorsByValue.get(color);
    502    //String name = null;
    503    if(name !=null) return name;
    504    else return String.format("0x%06X",new Integer(color));
    505  }
    506}