Java Color from String stringToColor(String colorName)

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

Description

string To Color

License

Open Source License

Declaration

public static Color stringToColor(String colorName) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.Color;

public class Main {
    public static Color stringToColor(String colorName) {
        colorName = colorName.trim();//  ww  w. ja va 2 s .  c o  m
        if (colorName.startsWith("#")) {
            if (colorName.length() != 7)
                throw new IllegalArgumentException("Invalid #RRGGBB sytnax: " + colorName);
            //RGB format
            int r = Integer.parseInt(colorName.substring(1, 3), 16);
            int g = Integer.parseInt(colorName.substring(3, 5), 16);
            int b = Integer.parseInt(colorName.substring(5, 7), 16);
            return new Color(r, g, b);
        } else {
            //constant format
            java.lang.reflect.Field f;
            try {
                f = Color.class.getField(colorName);
                return (Color) f.get(null);
            } catch (Exception e1) {
                throw new IllegalArgumentException("Unknow Color constant: " + colorName);
            }
        }
    }
}

Related

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