Java Color Decode decodeColor(final String value)

Here you can find the source of decodeColor(final String value)

Description

Interprets a string as a color value.

License

Apache License

Parameter

Parameter Description
value One of the following forms: <ul> <li>0xdddddddd - 8 hexadecimal digits, specifying 8 bits each of red, green, and blue, followed by 8 bits of alpha.</li> <li>#dddddd - 6 hexadecimal digits, specifying 8 bits each of red, green, and blue. <li>Any of the names of the static colors in the Java Color class. </ul>

Exception

Parameter Description
NumberFormatException if the value in the first two cases containsillegal hexadecimal digits.
IllegalArgumentException if the value is not in one of the formats listed above.

Return

A on successful decoding

Declaration

public static Color decodeColor(final String value) throws NumberFormatException 

Method Source Code


//package com.java2s;
/*/*from ww  w . jav  a 2  s  .  c  o m*/
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to you 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.Color;

import java.util.Locale;

public class Main {
    /**
     * Interprets a string as a color value.
     * @param value One of the following forms:
     * <ul>
     * <li>0xdddddddd - 8 hexadecimal digits, specifying 8 bits each of
     * red, green, and blue, followed by 8 bits of alpha.</li>
     * <li>#dddddd - 6 hexadecimal digits, specifying 8 bits each of
     * red, green, and blue.
     * <li>Any of the names of the static colors in the Java {@link Color} class.
      * </ul>
     * @return A {@link Color} on successful decoding
     * @throws NumberFormatException if the value in the first two cases contains
     * illegal hexadecimal digits.
     * @throws IllegalArgumentException if the value is not in one of the formats listed above.
     */
    public static Color decodeColor(final String value) throws NumberFormatException {
        if (value == null) {
            throw new IllegalArgumentException("Cannot decode a null String.");
        }

        String valueLowercase = value.toLowerCase(Locale.ENGLISH);

        Color color;
        if (valueLowercase.startsWith("0x")) {
            valueLowercase = valueLowercase.substring(2);
            if (valueLowercase.length() != 8) {
                throw new IllegalArgumentException(
                        "Incorrect Color format.  Expecting exactly 8 digits after the '0x' prefix.");
            }

            int rgb = Integer.parseInt(valueLowercase.substring(0, 6), 16);
            float alpha = Integer.parseInt(valueLowercase.substring(6, 8), 16) / 255f;

            color = getColor(rgb, alpha);
        } else if (valueLowercase.startsWith("#")) {
            valueLowercase = valueLowercase.substring(1);
            if (valueLowercase.length() != 6) {
                throw new IllegalArgumentException(
                        "Incorrect Color format.  Expecting exactly 6 digits after the '#' prefix.");
            }

            int rgb = Integer.parseInt(valueLowercase, 16);
            float alpha = 1.0f;

            color = getColor(rgb, alpha);
        } else {
            try {
                color = (Color) Color.class.getDeclaredField(valueLowercase).get(null);
            } catch (Exception exception) {
                throw new IllegalArgumentException("\"" + valueLowercase + "\" is not a valid color constant.");
            }
        }

        return color;
    }

    public static Color getColor(int rgb, float alpha) {
        float red = ((rgb >> 16) & 0xff) / 255f;
        float green = ((rgb >> 8) & 0xff) / 255f;
        float blue = (rgb >> 0 & 0xff) / 255f;

        return new Color(red, green, blue, alpha);
    }
}

Related

  1. decode(String color)
  2. decodeColor(final double v)
  3. decodeColor(int color)
  4. decodeColor(int value)
  5. decodeColor(String color, Color defaultColor)
  6. decodeColor(String string)