📄 colourutilities.java
字号:
/*====================================================================*\ColourUtilities.javaColour utility methods class.------------------------------------------------------------------------This file is part of FuncPlotter, a combined Java application and appletfor plotting explicit functions in one variable.Copyright 2005-2007 Andy Morgan-Richards.FuncPlotter is free software: you can redistribute it and/or modify itunder the terms of the GNU General Public License as published by theFree Software Foundation, either version 3 of the License, or (at youroption) any later version.This program is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public License alongwith this program. If not, see <http://www.gnu.org/licenses/>.\*====================================================================*/// PACKAGEpackage util;//----------------------------------------------------------------------// IMPORTSimport java.awt.Color;//----------------------------------------------------------------------// COLOUR UTILITY METHODS CLASSpublic class ColourUtilities{////////////////////////////////////////////////////////////////////////// Constants//////////////////////////////////////////////////////////////////////// public static final int MIN_RGB_COMPONENT_VALUE = 0; public static final int MAX_RGB_COMPONENT_VALUE = 255; public static final String HEX_FORM_PREFIX = "#";////////////////////////////////////////////////////////////////////////// Constructors//////////////////////////////////////////////////////////////////////// private ColourUtilities( ) { } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Class methods//////////////////////////////////////////////////////////////////////// public static String colourToRgbString( Color colour ) { return new String( colour.getRed( ) + ", " + colour.getGreen( ) + ", " + colour.getBlue( ) ); } //------------------------------------------------------------------ public static String colourToHexString( Color colour ) { Numeric.setHexUpper( ); return new String( HEX_FORM_PREFIX + Numeric.byteToHexString( colour.getRed( ) ) + Numeric.byteToHexString( colour.getGreen( ) ) + Numeric.byteToHexString( colour.getBlue( ) ) ); } //------------------------------------------------------------------ public static Color parseColour( String str ) throws IllegalArgumentException, IndexOutOfBoundsException { Color colour = null; if ( str.startsWith( HEX_FORM_PREFIX ) ) { str = str.substring( 1 ); if ( str.length( ) == 3 ) { char[] chars = new char[6]; for ( int i = 0; i < 3; ++i ) { char ch = str.charAt( i ); chars[2 * i] = ch; chars[2 * i + 1] = ch; } str = new String( chars ); } else if ( str.length( ) != 6 ) throw new IllegalArgumentException( ); int red = Integer.parseInt( str.substring( 0, 2 ), 16 ); int green = Integer.parseInt( str.substring( 2, 4 ), 16 ); int blue = Integer.parseInt( str.substring( 4, 6 ), 16 ); colour = new Color( red, green, blue ); } else { String[] strs = str.split( " *, *", -1 ); if ( strs.length != 3 ) throw new IllegalArgumentException( ); int red = Integer.parseInt( strs[0] ); if ( (red < MIN_RGB_COMPONENT_VALUE) || (red > MAX_RGB_COMPONENT_VALUE) ) throw new IndexOutOfBoundsException( ); int green = Integer.parseInt( strs[1] ); if ( (green < MIN_RGB_COMPONENT_VALUE) || (green > MAX_RGB_COMPONENT_VALUE) ) throw new IndexOutOfBoundsException( ); int blue = Integer.parseInt( strs[2] ); if ( (blue < MIN_RGB_COMPONENT_VALUE) || (blue > MAX_RGB_COMPONENT_VALUE) ) throw new IndexOutOfBoundsException( ); colour = new Color( red, green, blue ); } return colour; } //------------------------------------------------------------------}//----------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -