Java Color Create toColor(final String text, final Color defaultValue)

Here you can find the source of toColor(final String text, final Color defaultValue)

Description

Re-conversion of the human readable RGB representation back to a Color :
  1. if the given text matches the expected pattern 'rgb(X, X, X)', the represented Color will be returned,
  2. if the given text equals 'none', null will be returned,
  3. otherwise the provided defaultValue will be returned.

License

Open Source License

Parameter

Parameter Description
text the text to interpret as color 'rgb(X, X, X)' ('none' will return null )
defaultValue value to return if the given text is null or otherwise invalid

Return

interpreted value (can be null )

Declaration

public static Color toColor(final String text, final Color defaultValue) 

Method Source Code


//package com.java2s;
/*/*from  ww w .  j a  v a 2 s.  com*/
   Copyright (C) 2016 HermeneutiX.org
    
   This file is part of SciToS.
    
   SciToS 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 3 of the License, or
   (at your option) any later version.
    
   SciToS 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 SciToS. If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.Color;

public class Main {
    /**
     * Re-conversion of the human readable RGB representation back to a {@link Color}:
     * <ol>
     * <li>if the given {@code text} matches the expected pattern 'rgb(X, X, X)', the represented {@link Color} will be returned,</li>
     * <li>if the given {@code text} equals 'none', {@code null} will be returned,</li>
     * <li>otherwise the provided {@code defaultValue} will be returned.</li>
     * </ol>
     *
     * @param text
     *            the text to interpret as color 'rgb(X, X, X)' ('none' will return {@code null})
     * @param defaultValue
     *            value to return if the given {@code text} is {@code null} or otherwise invalid
     * @return interpreted {@link Color} value (can be {@code null})
     * @see #toString(Color)
     */
    public static Color toColor(final String text, final Color defaultValue) {
        if ("none".equals(text)) {
            return null;
        }
        if (text != null) {
            final String[] colorValues = text.replaceAll("[^0-9]+", " ").trim().split(" ");
            if (colorValues.length == 3) {
                final int red = Integer.valueOf(colorValues[0]).intValue();
                final int green = Integer.valueOf(colorValues[1]).intValue();
                final int blue = Integer.valueOf(colorValues[2]).intValue();
                return new Color(red, green, blue);
            }
        }
        return defaultValue;
    }
}

Related

  1. generateVisuallyDistinctColors(int ncolors, float minComponent, float maxComponent)
  2. toColor(byte red, byte green, byte blue)
  3. toColor(byte[] bytes)
  4. toColor(final String hexColor)
  5. toColor(final String hexString)
  6. toColor(float[] color)
  7. toColor(int rgba)
  8. toColor(int x)
  9. toColor(Node n)