1 /* Copyright (C) 2005-2011 Fabio Riccardi */
2 
3 package com.lightcrafts.ui.swing;
4 
5 import java.awt.*;
6 import java.awt.geom.Rectangle2D;
7 
8 import com.lightcrafts.jai.JAIContext;
9 import com.lightcrafts.jai.utils.Functions;
10 
11 /**
12  * A <code>RangeSelectorZoneTrack</code> paints a 16-zone track for a
13  * {@link RangeSelector}.
14  *
15  * @author Paul J. Lucas [paul@lightcrafts.com]
16  */
17 public class RangeSelectorZoneTrack
18     extends CommonRangeSelectorTrack implements RangeSelector.Track {
19 
20     ////////// public /////////////////////////////////////////////////////////
21 
22     /**
23      * {@inheritDoc}
24      */
paintTrack( RangeSelector selector, Rectangle r, Graphics2D g2d )25     public void paintTrack( RangeSelector selector, Rectangle r,
26                             Graphics2D g2d ) {
27         final int w = r.width - 2;      // -2 for inset border
28         final int h = r.height - 2;
29         final int y = r.y + 1;
30         final float zoneWidth = (float)w / ZONES;
31         final float[] colorArray = new float[3];
32         final Rectangle2D r2d = new Rectangle2D.Float();
33 
34         for ( int z = 0; z < ZONES; ++z ) {
35             final float[] c = zoneToColor( z, colorArray );
36             g2d.setColor( new Color( c[0], c[1], c[2] ) );
37             final float x = r.x + 1 + z * zoneWidth;
38             r2d.setRect( x, y, zoneWidth, h );
39             g2d.fill( r2d );
40         }
41 
42         paintBorder( r, g2d );
43     }
44 
45     /**
46      * Convert a zone to a color.
47      *
48      * @param zone The zone in the range [0,15].
49      * @param rgb An array to use for temporary storage or <code>null</code> if
50      * none.
51      * @return Returns the RGB values (in the range [0,1]) for the zone.
52      */
zoneToColor( int zone, float[] rgb )53     public static float[] zoneToColor( int zone, float[] rgb ) {
54         assert zone >= 0 && zone < ZONES;
55         if ( rgb == null )
56             rgb = new float[3];
57 
58         final float c =
59             (float)((Math.pow( 2, zone * 8.0 / (ZONES - 1) ) - 1) / 255.);
60         rgb[0] = rgb[1] = rgb[2] = c;
61 
62         return Functions.fromLinearToCS( JAIContext.systemColorSpace, rgb );
63     }
64 
65     ////////// private ////////////////////////////////////////////////////////
66 
67     private static final int ZONES = 16;
68 }
69 /* vim:set et sw=4 ts=4: */
70