/**
* ColorSpaceTest
* Demonstrates correspondence between RGB and HSB
* appears on http://mindprod.com/jgloss/hsb.html
*/
import java.awt.Color;
public class ColorSpaceTest
{
/**
* The smaller the number the more table entries it will generate.
*/
static final int INTERVAL = 30;
/**
* displays a table of RGB vs HSB values
*/
public static void main ( String[] args)
{
float hsb[]= new float[3];
int hue, sat, bri;
out.println( " red grn blu hue sat bri" );
for ( int red=0; red<256; red+=INTERVAL )
{
for ( int grn=0; grn<256; grn+=INTERVAL )
{
for ( int blu=0; blu<256; blu+=INTERVAL )
{
Color.RGBtoHSB( red, grn, blu, hsb );
hue =(int)( hsb[0] * 255 );
sat =(int)( hsb[1] * 255 );
bri =(int)( hsb[2] * 255 );
out.println(
rightJust( red, 4) +
rightJust( grn, 4) +
rightJust( blu, 4) +
" " +
rightJust( hue, 5) +
rightJust( sat, 5) +
rightJust( bri, 5) );
}
}
}
}
/**
* convert int to String fixed width, right justified.
* @param i the integer to convert.
* @param width the width of the result field in chars.
* Will chop if too wide, pad on left with spaces if too narrow.
*/
static String rightJust( int i, int width )
{
String s = Integer.toString( i );
if ( s.length()< width )
{
return " ".substring( 0 , width-s.length() ) + s;
}
else if ( s.length() == width ) return s;
else return s.substring ( 0, width );
}
}