Creates a new color definition based on hue (0-360), saturation (0-100), and relative value (0-100; usually 75-100). - Java 2D Graphics

Java examples for 2D Graphics:Color

Description

Creates a new color definition based on hue (0-360), saturation (0-100), and relative value (0-100; usually 75-100).

Demo Code

/**/*w ww.j av  a  2  s. c  o m*/
 * <code>ColorHelper</code> has not yet been documented.
 *
 * <p>Copyright (C) 2005 by Ian W. Davis. All rights reserved.
 * <br>Begun on Wed Feb  2 16:52:11 EST 2005
 */
//package com.java2s;

public class Main {
    /** The number of different depth-cueing levels available */
    public static final int COLOR_LEVELS = 16;
    /**
     * The minimum value multiplier on a black background.
     * Smaller numbers mean we fade closer to black before clipping.
     */
    //static final float      BVAL            = 0.36f;
    static final float BVAL = 0.20f;

    /**
     * Creates a new color definition based on hue (0-360), saturation (0-100),
     * and relative value (0-100; usually 75-100).
     */
    static public int[] createHSV(String name, float hue, float blackSat,
            float whiteSat, float blackVal, float whiteVal) {
        if (name == null)
            throw new NullPointerException("Must give paint a name");

        hue /= 360f;
        blackSat /= 100f;
        whiteSat /= 100f;
        blackVal /= 100f;
        whiteVal /= 100f;

        // value decreases going back
        int[] bcolors = new int[COLOR_LEVELS];
        for (int i = 0; i < COLOR_LEVELS; i++) {
            bcolors[i] = getHSB(hue, blackSat,
            //( 0.36f + 0.64f*i/(COLOR_LEVELS-1) )*blackVal );
                    (BVAL + (1 - BVAL) * i / (COLOR_LEVELS - 1)) * blackVal);

            // We only get five bits in RGB on most current PDAs.
            // Rounding rather than just bit-shifting should give better results...
            int red = Math
                    .min(0xff, 8 * (int) Math
                            .round(((bcolors[i] >> 16) & 0xff) / 8.0));
            int grn = Math.min(0xff,
                    8 * (int) Math.round(((bcolors[i] >> 8) & 0xff) / 8.0));
            int blu = Math.min(0xff,
                    8 * (int) Math.round(((bcolors[i] >> 0) & 0xff) / 8.0));
            bcolors[i] = (red << 16) | (grn << 8) | blu;
        }

        // value increases, saturation decreases going back
        /*int[] wcolors = new int[COLOR_LEVELS];
        for(int i = 0; i < COLOR_LEVELS; i++)
        {
            wcolors[i] = getHSB(hue,
                //( 0.36f + 0.64f*i/(COLOR_LEVELS-1) )*whiteSat,
                //Math.min(1f, 1f + (whiteVal-1f)*( 0.40f + 0.60f*i/(COLOR_LEVELS-1) )) ); 
                ( WSAT + (1-WSAT)*i/(COLOR_LEVELS-1) )*whiteSat,
                Math.min(1f, 1f + (whiteVal-1f)*( WVAL + (1-WVAL)*i/(COLOR_LEVELS-1) )) ); 
        }*/

        System.out.print("    static final int[] " + name + " = { ");
        for (int i = 0; i < COLOR_LEVELS; i++) {
            if (i != 0)
                System.out.print(", ");
            System.out.print("0x" + Integer.toHexString(bcolors[i]));
        }
        System.out.println(" };");

        return bcolors;
    }

    static private int getHSB(float hue, float sat, float val) {
        return java.awt.Color.HSBtoRGB(hue, sat, val) & 0xffffff;
    }
}

Related Tutorials