/**
 * 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;
      // Display column headings
      out.println( " red grn blu hue sat bri" );
      // show a set of sample RGB colours
      for ( int red=0; red<256; red+=INTERVAL )
         {
         for ( int grn=0; grn<256; grn+=INTERVAL )
            {
            for ( int blu=0; blu<256; blu+=INTERVAL )
               {
               // convert from RGB to HSB
               Color.RGBtoHSB( red, grn, blu, hsb );
               // scale 0 .. 255
               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) );
               } // end for blu
            } // end for grn
         } // end for red
      } // end main

   /**
   * 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 );
      }  // end rightJust

   }  // end class ColorSpaceTest