/*
* [ColourScheme.java]
*
* Summary: Generate a series of colour swatches to display a colour scheme.
*
* Copyright: (c) 2008-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.8 2009-02-06 include go package in ZIP bundle.
*/
package com.mindprod.htmlmacros.macro;
import com.mindprod.common18.Misc;
import com.mindprod.fastcat.FastCat;
import java.awt.Color;
import java.util.Arrays;
import java.util.Comparator;
import static java.lang.System.*;
/**
* Generate a series of colour swatches to display a colour scheme.
*
* @author Roedy Green, Canadian Mind Products
* @version 1.8 2009-02-06 include go package in ZIP bundle.
* @since 2008-09-14
*/
public final class ColourScheme extends Macro
{
/**
* how to use the macro
*/
private static final String USAGE = "\nColourScheme macro needs a list of colours of the form #rrggbb in hex";
/**
* expandNoRef each colour into a td cell
*
* @param colors array of colours to use in creating the swatches.
*
* @return expansion row of colour swatches as a table row
.. |
*/
private static String expand( Color[] colors )
{
final FastCat sb = new FastCat( colors.length * 7 );
for ( Color color : colors )
{
final int r = color.getRed();
final int g = color.getGreen();
final int b = color.getBlue();
final String contrast;
if ( r + g + b > 0x80 * 3 )
{
contrast = "black";
}
else
{
contrast = "white";
}
sb.append( " | " );
sb.append( getRGBHexString( color ) );
sb.append( " | \n" );
}
return sb.toString();
}
/**
* get #rrggbb value of a colour
*
* @param color colour you want to get hex name of
*
* @return #rrggbb
*/
private static String getRGBHexString( Color color )
{
String g = Integer.toString( color.getRGB() & 0xffffff, 16/* radix */ );
// pad out to 6 chars with lead zeros.
if ( g.length() < 6 )
{
g = "0000000".substring( 0, 6 - g.length() ) + g;
}
return '#' + g;
}
/**
* typical use:
*
* @param parms RFC# as a string
* @param quiet true if want output suppressed.
* @param verbose @return expanded macro HTML
*/
public String expandMacro( String[] parms,
final boolean quiet, final boolean verbose )
{
if ( !quiet )
{
out.print( "C" );
}
if ( parms.length < 1 )
{
throw new IllegalArgumentException( USAGE );
}
Color[] colors = new Color[ parms.length ];
int i = 0;
for ( String parm : parms )
{
if ( !( parm.length() == 7 && parm.charAt( 0 ) == '#' ) )
{
throw new IllegalArgumentException( USAGE );
}
try
{
colors[ i++ ] = new Color( Integer.parseInt( parm.substring( 1 ), 16 ) );
}
catch ( NumberFormatException e )
{
throw new IllegalArgumentException( USAGE );
}
} // end for
// sort colours darkest first, brightest last
Arrays.sort( colors, new ColourScheme.ByWhiteness() );
return ( expand( colors ) );
}
/**
* Sort by whiteness.
*
* Defines an alternate sort order for Color.
*
* @version 1.0 2009-05-22 - initial release
* @since 2009-05-22
*/
static class ByWhiteness implements Comparator
{
/**
* needed to hold return values
*/
private static final float[] hsbvals = new float[ 3 ];
/**
* Sort by whiteness.
* Defines an alternate sort order for Color.
* Compare two Color Objects.
* Compares whiteness.
* Informally, returns (a-b), or +ve if a is more positive than b.
*
* @param a first Color to compare
* @param b second Color to compare
*
* @return +ve if a>b, 0 if a==b, -ve if a<b
*/
public final int compare( Color a, Color b )
{
final float[] aHSB = Color.RGBtoHSB(
a.getRed(),
a.getGreen(),
a.getBlue(),
hsbvals );
final float aSat = aHSB[ 1 ];
final float aBright = aHSB[ 2 ];
final float aWhiteness = aBright - aSat * .1f;
final float[] bHSB = Color.RGBtoHSB(
b.getRed(),
b.getGreen(),
b.getBlue(),
hsbvals );
final float bSat = bHSB[ 1 ];
final float bBright = bHSB[ 2 ];
final float bWhiteness = bBright - bSat * .1f;
return Misc.signum( aWhiteness - bWhiteness );
}
}
}