Java Color Create toColor(String str)

Here you can find the source of toColor(String str)

Description

Can be: (255, 0, 0) 255, 0, 0 #FF0000 #F00 red

License

Apache License

Declaration

public static Color toColor(String str) 

Method Source Code


//package com.java2s;
/*/*w ww  . j av  a  2  s  .c  om*/
 * Copyright 2005 Patrick Gotthardt
 *
 * 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.*;
import java.util.Map;

public class Main {
    private static int[] colorShorthandTable = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA,
            0xBB, 0xCC, 0xDD, 0xEE, 0xFF };
    private static Map colorNamesMap;

    /**
     * Can be:
     * (255, 0, 0)
     * 255, 0, 0
     * #FF0000
     * #F00
     * red
     */
    public static Color toColor(String str) {
        switch (str.charAt(0)) {
        case '(':
            int red, green, blue;
            int index;

            red = nextColorInt(str, 1);

            index = str.indexOf(',');
            green = nextColorInt(str, index + 1);

            index = str.indexOf(',', index + 1);
            blue = nextColorInt(str, index + 1);

            return new Color(red, green, blue);
        case '#':
            // Shorthand?
            if (str.length() == 4) {
                return new Color(getShorthandValue(str.charAt(1)), getShorthandValue(str.charAt(2)),
                        getShorthandValue(str.charAt(3)));
            } else {
                return new Color(Integer.parseInt(str.substring(1), 16));
            }
        default:
            if (Character.isDigit(str.charAt(0))) {
                red = nextColorInt(str, 0);

                index = str.indexOf(',');
                green = nextColorInt(str, index + 1);

                index = str.indexOf(',', index + 1);
                blue = nextColorInt(str, index + 1);

                return new Color(red, green, blue);
            }
            return (Color) colorNamesMap.get(str);
        }
    }

    private static int nextColorInt(String str, int index) {
        // start with adjusting the start index
        while (index < str.length()) {
            char c = str.charAt(index);
            // a digit?
            if ('0' <= c && c <= '9') {
                break;
            } else {
                index++;
            }
        }
        // that's only the maximum limit!
        int colorLength = index;
        for (; colorLength < index + 3; colorLength++) {
            char c = str.charAt(colorLength);
            // not a digit?
            if (c < '0' || '9' < c) {
                break;
            }
        }
        return Integer.parseInt(str.substring(index, colorLength));
    }

    private static int getShorthandValue(char c) {
        c = Character.toUpperCase(c);
        if ('A' <= c && c <= 'F') {
            return colorShorthandTable[c - 'A' + 10];
        }
        return colorShorthandTable[c - '0'];
    }
}

Related

  1. toColor(Node n)
  2. toColor(short rgb565)
  3. toColor(String background)
  4. toColor(String hexString)
  5. toColor(String pString)
  6. toColors(boolean hasAlpha, int... colors)