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;
/**/*from  w w w  . jav  a2s.co m*/
 *  Copyright 2012 Universitat Pompeu Fabra.
 *
 *  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 = 0, g = 0, b = 0;
        try {
            String tmpcolor = color.trim();
            tmpcolor = tmpcolor.substring(1, tmpcolor.length() - 1);
            String rgb[] = tmpcolor.split(",");

            r = Integer.valueOf(rgb[0]).intValue();
            g = Integer.valueOf(rgb[1]).intValue();
            b = Integer.valueOf(rgb[2]).intValue();

            return new Color(r, g, b);
        } catch (Exception e) {
            throw new UnsupportedOperationException(
                    "The color '" + color + "' is malformed. Syntax: [r,g,b] Example: \"[128,12,34]\"", e);
        }
    }
}

Related

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