Java examples for 2D Graphics:Color RGB
Re-conversion of the human readable RGB representation back to a Color : if the given text matches the expected pattern 'rgb(X, X, X)', the represented Color will be returned, if the given text equals 'none', null will be returned, otherwise the provided defaultValue will be returned.
/*/*from w w w . j a v a 2 s . c o m*/ 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/>. */ //package com.java2s; 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; } }