/*
* [NamedColor.java]
*
* Summary: A Color with a name, possibly "".
*
* Copyright: (c) 2007-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 2007-04-10
*/
package com.mindprod.palette;
import com.mindprod.common18.Misc;
import java.awt.Color;
import java.util.Comparator;
/**
* A Color with a name, possibly "".
*
* @author Roedy Green, Canadian Mind Products
* @version 1.0 2007-04-10
* @since 2007-04-10
*/
public final class NamedColor
{
private final String colorName;
private final float[] resultHolder = new float[ 3 ];
private final int colorRGB;
/**
* constructor.
*
* @param colorRGB 00rrggbb.
* @param colorName name of this colour.
*/
public NamedColor( int colorRGB, String colorName )
{
this.colorRGB = colorRGB;
this.colorName = colorName;
} // end constructor
/**
* constructor.
*
* @param color Color .
* @param colorName name of this colour.
*/
public NamedColor( Color color, String colorName )
{
this.colorRGB = color.getRGB() & 0xffffff;
this.colorName = colorName;
} // end constructor
/**
* constructor.
*
* @param r red 0-255.
* @param g green 0-255.
* @param b blue 0-255.
* @param colorName name of this colour.
*/
@SuppressWarnings( { "SameParameterValue" } )
public NamedColor( int r, int g, int b, String colorName )
{
this.colorRGB = ( r << 16 ) | ( g << 8 ) | b;
this.colorName = colorName;
} // end constructor
/**
* get blue.
*
* @return 0-255
*/
public int getBlue()
{
return colorRGB & 0xff;
}
/**
* get brightness.
*
* @return 0-1.
*/
public float getBrightness()
{
return ( Color.RGBtoHSB( getRed(),
getBlue(),
getGreen(),
resultHolder ) )[ 2 ];
}
/**
* get corresponding vanilla Color object
*
* @return Color object.
*/
public Color getColor()
{
return new Color( colorRGB );
}
/**
* get name of color.
*
* @return e.g. "red"
*/
public String getColorName()
{
return colorName;
}
/**
* get RGB.
*
* @return rrggbb
*/
public int getColorRGB()
{
return colorRGB;
}
/**
* get green.
*
* @return 0-255
*/
public int getGreen()
{
return ( colorRGB >>> 8 ) & 0xff;
}
/**
* get hue.
*
* @return 0-1.
*/
public float getHue()
{
return ( Color.RGBtoHSB( getRed(),
getBlue(),
getGreen(),
resultHolder ) )[ 0 ];
}
/**
* return hex string of form new #ffffff, no quotes included.
*
* @return "#ffffff".
*/
public String getRGBHexString()
{
return '#' + getRawRGBHexString();
} // end getRGBHexString
/**
* return hex string of form new ffffff, no quotes included.
*
* @return "ffffff".
*/
public String getRawRGBHexString()
{
String g = Integer.toString( colorRGB, 16 );
// pad out to 6 chars with lead zeros.
if ( g.length() < 6 )
{
g = "0000000".substring( 0, 6 - g.length() ) + g;
}
return g;
} // end getRawRGBHexString
/**
* get red
*
* @return 0-255
*/
public int getRed()
{
return colorRGB >>> 16;
}
/**
* get saturation.
*
* @return 0-1.
*/
public float getSaturation()
{
return ( Color.RGBtoHSB( getRed(),
getBlue(),
getGreen(),
resultHolder ) )[ 1 ];
}
/**
* show colour name and hex when asked to convert to string
*
* @return 8colour Name
*/
public String toString()
{
return colorName + " : " + getRawRGBHexString();
}
/**
* Sort alphabetically.
*
* Defines an alternate sort order for NamedColor
*/
static class Alphabetically implements Comparator
{
/**
* Sort alphabetically.
* Defines an alternate sort order for NamedColor.
* Compare two NamedColor Objects.
* Compares by getColorName().
* Informally, returns (a-b), or +ve if a is more positive than b.
*
* @param a first NamedColor to compare
* @param b second NamedColor to compare
*
* @return +ve if a>b, 0 if a==b, -ve if a<b
*/
public final int compare( NamedColor a, NamedColor b )
{
return a.getColorName().compareToIgnoreCase( b.getColorName() );
}
}
/**
* Sort by Brightness Hue Saturation.
*
* Defines an alternate sort order for NamedColor.
*
* @version 1.0 2009-05-22 - initial release
* @since 2009-05-22
*/
static class ByBHS implements Comparator
{
/**
* Sort by Brightness Hue Saturation.
* Defines an alternate sort order for NamedColor.
* Compare two NamedColor Objects.
* Compares getBrightness() then getHue() then getSaturation().
* Informally, returns (a-b), or +ve if a is more positive than b.
*
* @param a first NamedColor to compare
* @param b second NamedColor to compare
*
* @return +ve if a>b, 0 if a==b, -ve if a<b
*/
public final int compare( NamedColor a, NamedColor b )
{
float diff = a.getBrightness() - b.getBrightness();
if ( diff != 0 )
{
return Misc.signum( diff );
}
float diff1 = a.getHue() - b.getHue();
if ( diff1 != 0 )
{
return Misc.signum( diff1 );
}
return Misc.signum( a.getSaturation() - b.getSaturation() );
}
}
/**
* Sort by Brightnes Saturation Hue.
*
* Defines an alternate sort order for NamedColor.
*/
static class ByBSH implements Comparator
{
/**
* Sort by Brightnes Saturation Hue.
* Defines an alternate sort order for NamedColor.
* Compare two NamedColor Objects.
* Compares getBrightness() then getSaturation() then getHue().
* Informally, returns (a-b), or +ve if a is more positive than b.
*
* @param a first NamedColor to compare
* @param b second NamedColor to compare
*
* @return +ve if a>b, 0 if a==b, -ve if a<b
*/
public final int compare( NamedColor a, NamedColor b )
{
float diff = a.getBrightness() - b.getBrightness();
if ( diff != 0 )
{
return Misc.signum( diff );
}
float diff1 = a.getSaturation() - b.getSaturation();
if ( diff1 != 0 )
{
return Misc.signum( diff1 );
}
return Misc.signum( a.getHue() - b.getHue() );
}
}
/**
* Sort by Hue Brightnes Saturation.
*
* Defines an alternate sort order for NamedColor.
*/
static class ByHBS implements Comparator
{
/**
* Sort by Hue Brightness Saturation.
* Defines an alternate sort order for NamedColor.
* Compare two NamedColor Objects.
* Compares getHue() then getBrightness() then getSaturation().
* Informally, returns (a-b), or +ve if a is more positive than b.
*
* @param a first NamedColor to compare
* @param b second NamedColor to compare
*
* @return +ve if a>b, 0 if a==b, -ve if a<b
*/
public final int compare( NamedColor a, NamedColor b )
{
float diff = a.getHue() - b.getHue();
if ( diff != 0 )
{
return Misc.signum( diff );
}
float diff1 = a.getBrightness() - b.getBrightness();
if ( diff1 != 0 )
{
return Misc.signum( diff1 );
}
return Misc.signum( a.getSaturation() - b.getSaturation() );
}
}
/**
* Sort by Hue Saturation Brightness.
*
* Defines an alternate sort order for NamedColor.
*/
static class ByHSB implements Comparator
{
/**
* Sort by Hue Saturation Brightness.
* Defines an alternate sort order for NamedColor.
* Compare two NamedColor Objects.
* Compares getHue() then getSaturation() then getBrightness().
* Informally, returns (a-b), or +ve if a is more positive than b.
*
* @param a first NamedColor to compare
* @param b second NamedColor to compare
*
* @return +ve if a>b, 0 if a==b, -ve if a<b
*/
public final int compare( NamedColor a, NamedColor b )
{
float diff = a.getHue() - b.getHue();
if ( diff != 0 )
{
return Misc.signum( diff );
}
float diff1 = a.getSaturation() - b.getSaturation();
if ( diff1 != 0 )
{
return Misc.signum( diff1 );
}
return Misc.signum( a.getBrightness() - b.getBrightness() );
}
}
/**
* Sort by numerically by RGB.
*
* Defines an alternate sort order for NamedColor.
*/
static class ByRGB implements Comparator
{
/**
* Sort by numerically by RGB.
* Defines an alternate sort order for NamedColor.
* Compare two NamedColor Objects.
* Compares getColorRGB().
* Informally, returns (a-b), or +ve if a is more positive than b.
*
* @param a first NamedColor to compare
* @param b second NamedColor to compare
*
* @return +ve if a>b, 0 if a==b, -ve if a<b
*/
public final int compare( NamedColor a, NamedColor b )
{
return a.getColorRGB() - b.getColorRGB();
}
}
/**
* Sort by Saturation, Brightness and Hue.
*
* Defines an alternate sort order for NamedColor.
*
* @version 1.0 2009-05-22 - initial release
* @since 2009-05-22
*/
static class BySBH implements Comparator
{
/**
* Sort by Saturation, Brightness and Hue.
* Defines an alternate sort order for NamedColor.
* Compare two NamedColor Objects.
* Compares getSaturation() then getBrightness() then getHue().
* Informally, returns (a-b), or +ve if a is more positive than b.
*
* @param a first NamedColor to compare
* @param b second NamedColor to compare
*
* @return +ve if a>b, 0 if a==b, -ve if a<b
*/
public final int compare( NamedColor a, NamedColor b )
{
float diff = a.getSaturation() - b.getSaturation();
if ( diff != 0 )
{
return Misc.signum( diff );
}
float diff1 = a.getBrightness() - b.getBrightness();
if ( diff1 != 0 )
{
return Misc.signum( diff1 );
}
return Misc.signum( a.getHue() - b.getHue() );
}
}
/**
* Sort by Saturation, Hue and Brightness.
*
* Defines an alternate sort order for NamedColor.
*
* @version 1.0 2009-05-22 - initial release
* @since 2009-05-22
*/
static class BySHB implements Comparator
{
/**
* Sort by Saturation, Hue and Brightness.
* Defines an alternate sort order for NamedColor.
* Compare two NamedColor Objects.
* Compares getSaturation() then getHue() then getBrightness().
* Informally, returns (a-b), or +ve if a is more positive than b.
*
* @param a first NamedColor to compare
* @param b second NamedColor to compare
*
* @return +ve if a>b, 0 if a==b, -ve if a<b
*/
public final int compare( NamedColor a, NamedColor b )
{
float diff = a.getSaturation() - b.getSaturation();
if ( diff != 0 )
{
return Misc.signum( diff );
}
float diff1 = a.getHue() - b.getHue();
if ( diff1 != 0 )
{
return Misc.signum( diff1 );
}
return Misc.signum( a.getBrightness() - b.getBrightness() );
}
}
}