GradientFormatterpublic class GradientFormatter extends Object implements FormatterFormats text with different color intensity depending on the score of the
term. |
Fields Summary |
---|
private float | maxScore | int | fgRMin | int | fgGMin | int | fgBMin | int | fgRMax | int | fgGMax | int | fgBMax | protected boolean | highlightForeground | int | bgRMin | int | bgGMin | int | bgBMin | int | bgRMax | int | bgGMax | int | bgBMax | protected boolean | highlightBackground | private static char[] | hexDigits |
Constructors Summary |
---|
public GradientFormatter(float maxScore, String minForegroundColor, String maxForegroundColor, String minBackgroundColor, String maxBackgroundColor)Sets the color range for the IDF scores
highlightForeground = (minForegroundColor != null)
&& (maxForegroundColor != null);
if (highlightForeground)
{
if (minForegroundColor.length() != 7)
{
throw new IllegalArgumentException(
"minForegroundColor is not 7 bytes long eg a hex "
+ "RGB value such as #FFFFFF");
}
if (maxForegroundColor.length() != 7)
{
throw new IllegalArgumentException(
"minForegroundColor is not 7 bytes long eg a hex "
+ "RGB value such as #FFFFFF");
}
fgRMin = hexToInt(minForegroundColor.substring(1, 3));
fgGMin = hexToInt(minForegroundColor.substring(3, 5));
fgBMin = hexToInt(minForegroundColor.substring(5, 7));
fgRMax = hexToInt(maxForegroundColor.substring(1, 3));
fgGMax = hexToInt(maxForegroundColor.substring(3, 5));
fgBMax = hexToInt(maxForegroundColor.substring(5, 7));
}
highlightBackground = (minBackgroundColor != null)
&& (maxBackgroundColor != null);
if (highlightBackground)
{
if (minBackgroundColor.length() != 7)
{
throw new IllegalArgumentException(
"minBackgroundColor is not 7 bytes long eg a hex "
+ "RGB value such as #FFFFFF");
}
if (maxBackgroundColor.length() != 7)
{
throw new IllegalArgumentException(
"minBackgroundColor is not 7 bytes long eg a hex "
+ "RGB value such as #FFFFFF");
}
bgRMin = hexToInt(minBackgroundColor.substring(1, 3));
bgGMin = hexToInt(minBackgroundColor.substring(3, 5));
bgBMin = hexToInt(minBackgroundColor.substring(5, 7));
bgRMax = hexToInt(maxBackgroundColor.substring(1, 3));
bgGMax = hexToInt(maxBackgroundColor.substring(3, 5));
bgBMax = hexToInt(maxBackgroundColor.substring(5, 7));
}
// this.corpusReader = corpusReader;
this.maxScore = maxScore;
// totalNumDocs = corpusReader.numDocs();
|
Methods Summary |
---|
protected java.lang.String | getBackgroundColorString(float score)
int rVal = getColorVal(bgRMin, bgRMax, score);
int gVal = getColorVal(bgGMin, bgGMax, score);
int bVal = getColorVal(bgBMin, bgBMax, score);
StringBuffer sb = new StringBuffer();
sb.append("#");
sb.append(intToHex(rVal));
sb.append(intToHex(gVal));
sb.append(intToHex(bVal));
return sb.toString();
| private int | getColorVal(int colorMin, int colorMax, float score)
if (colorMin == colorMax)
{
return colorMin;
}
float scale = Math.abs(colorMin - colorMax);
float relScorePercent = Math.min(maxScore, score) / maxScore;
float colScore = scale * relScorePercent;
return Math.min(colorMin, colorMax) + (int) colScore;
| protected java.lang.String | getForegroundColorString(float score)
int rVal = getColorVal(fgRMin, fgRMax, score);
int gVal = getColorVal(fgGMin, fgGMax, score);
int bVal = getColorVal(fgBMin, fgBMax, score);
StringBuffer sb = new StringBuffer();
sb.append("#");
sb.append(intToHex(rVal));
sb.append(intToHex(gVal));
sb.append(intToHex(bVal));
return sb.toString();
| public static final int | hexToInt(java.lang.String hex)Converts a hex string into an int. Integer.parseInt(hex, 16) assumes the
input is nonnegative unless there is a preceding minus sign. This method
reads the input as twos complement instead, so if the input is 8 bytes
long, it will correctly restore a negative int produced by
Integer.toHexString() but not neccesarily one produced by
Integer.toString(x,16) since that method will produce a string like '-FF'
for negative integer values.
int len = hex.length();
if (len > 16)
throw new NumberFormatException();
int l = 0;
for (int i = 0; i < len; i++)
{
l <<= 4;
int c = Character.digit(hex.charAt(i), 16);
if (c < 0)
throw new NumberFormatException();
l |= c;
}
return l;
| public java.lang.String | highlightTerm(java.lang.String originalText, TokenGroup tokenGroup)
if (tokenGroup.getTotalScore() == 0)
return originalText;
float score = tokenGroup.getTotalScore();
if (score == 0)
{
return originalText;
}
StringBuffer sb = new StringBuffer();
sb.append("<font ");
if (highlightForeground)
{
sb.append("color=\"");
sb.append(getForegroundColorString(score));
sb.append("\" ");
}
if (highlightBackground)
{
sb.append("bgcolor=\"");
sb.append(getBackgroundColorString(score));
sb.append("\" ");
}
sb.append(">");
sb.append(originalText);
sb.append("</font>");
return sb.toString();
| private static java.lang.String | intToHex(int i)
return "" + hexDigits[(i & 0xF0) >> 4] + hexDigits[i & 0x0F];
|
|