Java RGB Color Create fromRGB(String tag)

Here you can find the source of fromRGB(String tag)

Description

Makes Color objects from the string specified according to [CSS2-color] Fixed currently supported: black|blue|cyan|darkGray|gray|green|lightGray|magenta|orange|pink|red|white|yellow or you can define as rgb format: #rrggbb

License

Open Source License

Declaration

private static Color fromRGB(String tag) 

Method Source Code

//package com.java2s;
/*//from   w  w w  .j a  va2s.  c  om
Copyright (C) 2009 Vasili Gavrilov
    
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.awt.Color;

public class Main {
    /**
     * Makes Color objects from the string specified according to [CSS2-color]
     * Fixed currently supported: black|blue|cyan|darkGray|gray|green|lightGray|magenta|orange|pink|red|white|yellow
     * or you can define as rgb format: #rrggbb
     *//*
        public static Color str2Color(String s){
                
        Color color=null;
                
        if(s.startsWith("#")){
         return fromRGB(s);
        }
        else if(s.equals("black")){
         color=color.black;
        }
        else if(s.equals("blue")){
         color=color.blue;
        }
        else if(s.equals("cyan")){
         color=color.cyan;
        }
        else if(s.equals("darkGray")){
         color=color.darkGray;
        }
        else if(s.equals("gray")){
         color=color.gray;
        }
        else if(s.equals("green")){
         color=color.green;
        }
        else if(s.equals("lightGray")){
         color=color.lightGray;
        }
        else if(s.equals("magenta")){
         color=color.magenta;
        }
        else if(s.equals("orange")){
         color=color.orange;
        }
        else if(s.equals("pink")){
         color=color.pink;
        }
        else if(s.equals("red")){
         color=color.red;
        }
        else if(s.equals("white")){
         color=color.white;
        }
        else if(s.equals("yellow")){
         color=color.yellow;
        }
        return color;
        }
        */
    private static Color fromRGB(String tag) {

        Color color = null;

        try {
            String rgb = tag.substring(1);
            int r = Integer.parseInt(rgb.substring(0, 2), 16);
            int g = Integer.parseInt(rgb.substring(2, 4), 16);
            int b = Integer.parseInt(rgb.substring(4, 6), 16);
            return new Color(r, g, b);
        } catch (Exception e) {
            //LogManager.err(e);
        }
        return color;
    }
}

Related

  1. cmykToRgb(float... cmyk)
  2. deformationToRGB(final double val, final double min, final double max)
  3. fromHtmlRGB(String rgb)
  4. getRGB(Color color)
  5. getRGB(java.awt.Color color)
  6. makeIcon(int width, int height, int[] rgb)
  7. makeLinear_sRGBCM(boolean premult)