Java Color from String stringToColor(String color)

Here you can find the source of stringToColor(String color)

Description

string To Color

License

Apache License

Declaration

public static Color stringToColor(String color) 

Method Source Code


//package com.java2s;
/*/*w  ww.  j  a v a 2 s.com*/
 * Created on 01.02.2005
 *
 * JDBReport Generator
 * 
 * Copyright (C) 2005-2014 Andrey Kholmanskih
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

import java.awt.*;

public class Main {
    public static Color stringToColor(String color) {
        if (color == null)
            return null;
        int r;
        int g;
        int b;
        int ind1 = color.indexOf(',');
        if (ind1 > 0) {
            r = Integer.parseInt(color.substring(0, ind1 - 1));
            int ind2 = color.indexOf(',', ++ind1);
            g = Integer.parseInt(color.substring(ind1, ind2 - 1));
            b = Integer.parseInt(color.substring(ind2 + 1));
        } else if (color.startsWith("#") || color.startsWith("0x") || color.startsWith("0X")) {
            int c = Integer.decode(color);
            r = (c >> 16) & 0xff;
            g = (c >> 8) & 0xff;
            b = (c) & 0xff;
        } else {
            int c = Integer.parseInt(color);
            b = (c >> 16) & 0xff;
            g = (c >> 8) & 0xff;
            r = (c) & 0xff;
        }
        return new Color(r, g, b);
    }
}

Related

  1. string2color(String str)
  2. string2Color(String stringColor)
  3. stringToColor(final String s)
  4. stringToColor(final String value)
  5. stringToColor(String color)
  6. stringToColor(String colorName)
  7. stringToColor(String colorString)
  8. StringToColor(String name)
  9. stringToColor(String s)