/*
 * [TestIndexColorModel.java]
 *
 * Summary: Demonstrate the use of IndexColorModel with a map of possible colours.
 *
 * Copyright: (c) 2009-2017 Roedy Green, Canadian Mind Products, http://mindprod.com
 *
 * Licence: This software may be copied and used freely for any purpose but military.
 *          http://mindprod.com/contact/nonmil.html
 *
 * Requires: JDK 1.8+
 *
 * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/
 *
 * Version History:
 *  1.0 2006-03-02
 *  1.1 2011-01-11 add comments.
 */
package com.mindprod.example;

import javax.imageio.ImageIO;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.DataBuffer;
import java.awt.image.IndexColorModel;
import java.io.File;
import java.io.IOException;

/**
 * Demonstrate the use of IndexColorModel with a map of possible colours.
 * <p/>
 * Directly generate a png file with a palette limited to 256 colours.
 * Draws three nested rectangles, lawn green, tomato, transparent,
 * but what you get is two nested rectangles, lawn green and hot pink.
 * Such images are 1/4 the size of images encoded with 32-bit colours.
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.1 2011-01-11 add comments
 * @since 2006-03-02
 */
public class TestIndexColorModel
    {
    /**
     * height of image in pixels
     */
    private static final int height = 50;

    /**
     * width of image in pixels
     */
    private static final int width = 50;

    private static final Color LAWN_GREEN = new Color( 0x7c, 0xfc, 0x00 );

    private static final Color TOMATO = new Color( 0xff, 0x63, 0x47 );

    private static final Color TRANSPARENT = new Color( 0, 0, 0, 0 );

    /**
     * map of encoding to full colour
     */
    private static final int[] colourMap
            = {
            0x00000000, /* TRANSPARENT */
            0xff000000, /* black */
            0xffffffff, /* white */
            0xffff0000, /* red */
            0xffffff00, /* yellow */
            0xff00ff00, /* green */
            0xff0000ff, /* blue */
            0xff00ffff, /* cyan */
            0xffff00ff, /* fuschia */
            0xff7cfc00, /* LAWN_GREEN */
            0xffff69b4, /* hotPink */
            0xffff1493, /* deepPink */
            0xffffb6c1, /* lightPink */
            // could be up to 256 long.
    };

    /**
     * Create an IndexColorModel Image and write it to disk.
     *
     * @param args not used
     *
     * @throws java.io.IOException if problem writing png file.
     */
    public static void main( String args[] ) throws IOException
        {
        IndexColorModel colorModel = new IndexColorModel( 8
                /* bits */,
                colourMap.length,
                colourMap,
                0
                /* start offset */,
                true
                /* using alpha transparency */,
                0
                /* code
             for fully TRANSPARENT */,
                DataBuffer.TYPE_BYTE );
        // paint on a non-visible buffer.
        BufferedImage image = new BufferedImage( width, height,
                BufferedImage.TYPE_BYTE_INDEXED, colorModel );
        Graphics2D g = image.createGraphics();
        // We specify colours with Color objects, not encoded bytes.
        // Graphics will look up the 32-bit colors in the map
        // and convert them to the 8-bit encoding.
        // draw a lawn green rectangle.
        g.setBackground( LAWN_GREEN );
        g.clearRect( 0, 0, width, height );
        // Draw a TOMATO rectangle inside that.
        // Tomato is not a colour in the map so item will show up as the nearest colour
        // that is, namely hot pink.
        g.setColor( TOMATO );
        g.fillRect( 10, 10, width - 20, height - 20 );
        // Draw a TRANSPARENT rectangle nested in that.
        // Drawing in TRANSPARENT ink is like drawing with water. It has no effect.
        // It won't cover anything already there.
        g.setColor( TRANSPARENT );
        g.fillRect( 20, 20, width - 40, height - 40 );
        // dump the buffer to an png image file on disk.
        ImageIO.write( image, "PNG", new File( "C:/temp/indexcolormodelexample.png" ) );
        // view the generated image with a browser or paint program.
        }
    }