Here you can find the source of toColor(final String text, final Color defaultValue)
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 |
public static Color toColor(final String text, final Color defaultValue)
//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; } }