Java Color from String stringToColor(final String s)

Here you can find the source of stringToColor(final String s)

Description

Converts the given string into a Color object.

License

Open Source License

Declaration

public static Color stringToColor(final String s) 

Method Source Code


//package com.java2s;
/*/*from  w w  w  . ja v  a2  s . c o m*/
 * #%L
 * VisBio application for visualization of multidimensional biological
 * image data.
 * %%
 * Copyright (C) 2002 - 2014 Board of Regents of the University of
 * Wisconsin-Madison.
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 2 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-2.0.html>.
 * #L%
 */

import java.awt.Color;

import java.util.StringTokenizer;

public class Main {
    /** Converts the given string into a Color object. */
    public static Color stringToColor(final String s) {
        final int[] a = s == null ? null : stringToIntArray(s);
        if (a == null || a.length < 3)
            return null;
        if (a.length == 3)
            return new Color(a[0], a[1], a[2]);
        return new Color(a[0], a[1], a[2], a[3]);
    }

    /** Converts the given String into an array of ints. */
    public static int[] stringToIntArray(final String s) {
        if (s == null || s.equals("null"))
            return null;
        final StringTokenizer st = new StringTokenizer(s, ",");
        final int len = st.countTokens();
        final int[] a = new int[len];
        for (int i = 0; i < len; i++)
            a[i] = Integer.parseInt(st.nextToken());
        return a;
    }
}

Related

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