1 package org.apache.lucene.search.highlight; 2 17 18 24 public class GradientFormatter implements Formatter 25 { 26 private float maxScore; 27 28 int fgRMin; 29 int fgGMin; 30 int fgBMin; 31 32 int fgRMax; 33 int fgGMax; 34 int fgBMax; 35 36 protected boolean highlightForeground; 37 38 int bgRMin; 39 int bgGMin; 40 int bgBMin; 41 42 int bgRMax; 43 int bgGMax; 44 int bgBMax; 45 46 protected boolean highlightBackground; 47 48 67 public GradientFormatter(float maxScore, String minForegroundColor, 68 String maxForegroundColor, String minBackgroundColor, 69 String maxBackgroundColor) 70 { 71 highlightForeground = (minForegroundColor != null) 72 && (maxForegroundColor != null); 73 if (highlightForeground) 74 { 75 if (minForegroundColor.length() != 7) 76 { 77 throw new IllegalArgumentException ( 78 "minForegroundColor is not 7 bytes long eg a hex " 79 + "RGB value such as #FFFFFF"); 80 } 81 if (maxForegroundColor.length() != 7) 82 { 83 throw new IllegalArgumentException ( 84 "minForegroundColor is not 7 bytes long eg a hex " 85 + "RGB value such as #FFFFFF"); 86 } 87 fgRMin = hexToInt(minForegroundColor.substring(1, 3)); 88 fgGMin = hexToInt(minForegroundColor.substring(3, 5)); 89 fgBMin = hexToInt(minForegroundColor.substring(5, 7)); 90 91 fgRMax = hexToInt(maxForegroundColor.substring(1, 3)); 92 fgGMax = hexToInt(maxForegroundColor.substring(3, 5)); 93 fgBMax = hexToInt(maxForegroundColor.substring(5, 7)); 94 } 95 96 highlightBackground = (minBackgroundColor != null) 97 && (maxBackgroundColor != null); 98 if (highlightBackground) 99 { 100 if (minBackgroundColor.length() != 7) 101 { 102 throw new IllegalArgumentException ( 103 "minBackgroundColor is not 7 bytes long eg a hex " 104 + "RGB value such as #FFFFFF"); 105 } 106 if (maxBackgroundColor.length() != 7) 107 { 108 throw new IllegalArgumentException ( 109 "minBackgroundColor is not 7 bytes long eg a hex " 110 + "RGB value such as #FFFFFF"); 111 } 112 bgRMin = hexToInt(minBackgroundColor.substring(1, 3)); 113 bgGMin = hexToInt(minBackgroundColor.substring(3, 5)); 114 bgBMin = hexToInt(minBackgroundColor.substring(5, 7)); 115 116 bgRMax = hexToInt(maxBackgroundColor.substring(1, 3)); 117 bgGMax = hexToInt(maxBackgroundColor.substring(3, 5)); 118 bgBMax = hexToInt(maxBackgroundColor.substring(5, 7)); 119 } 120 this.maxScore = maxScore; 122 } 124 125 public String highlightTerm(String originalText, TokenGroup tokenGroup) 126 { 127 if (tokenGroup.getTotalScore() == 0) 128 return originalText; 129 float score = tokenGroup.getTotalScore(); 130 if (score == 0) 131 { 132 return originalText; 133 } 134 StringBuffer sb = new StringBuffer (); 135 sb.append("<font "); 136 if (highlightForeground) 137 { 138 sb.append("color=\""); 139 sb.append(getForegroundColorString(score)); 140 sb.append("\" "); 141 } 142 if (highlightBackground) 143 { 144 sb.append("bgcolor=\""); 145 sb.append(getBackgroundColorString(score)); 146 sb.append("\" "); 147 } 148 sb.append(">"); 149 sb.append(originalText); 150 sb.append("</font>"); 151 return sb.toString(); 152 } 153 154 protected String getForegroundColorString(float score) 155 { 156 int rVal = getColorVal(fgRMin, fgRMax, score); 157 int gVal = getColorVal(fgGMin, fgGMax, score); 158 int bVal = getColorVal(fgBMin, fgBMax, score); 159 StringBuffer sb = new StringBuffer (); 160 sb.append("#"); 161 sb.append(intToHex(rVal)); 162 sb.append(intToHex(gVal)); 163 sb.append(intToHex(bVal)); 164 return sb.toString(); 165 } 166 167 protected String getBackgroundColorString(float score) 168 { 169 int rVal = getColorVal(bgRMin, bgRMax, score); 170 int gVal = getColorVal(bgGMin, bgGMax, score); 171 int bVal = getColorVal(bgBMin, bgBMax, score); 172 StringBuffer sb = new StringBuffer (); 173 sb.append("#"); 174 sb.append(intToHex(rVal)); 175 sb.append(intToHex(gVal)); 176 sb.append(intToHex(bVal)); 177 return sb.toString(); 178 } 179 180 private int getColorVal(int colorMin, int colorMax, float score) 181 { 182 if (colorMin == colorMax) 183 { 184 return colorMin; 185 } 186 float scale = Math.abs(colorMin - colorMax); 187 float relScorePercent = Math.min(maxScore, score) / maxScore; 188 float colScore = scale * relScorePercent; 189 return Math.min(colorMin, colorMax) + (int) colScore; 190 } 191 192 private static char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', 193 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; 194 195 private static String intToHex(int i) 196 { 197 return "" + hexDigits[(i & 0xF0) >> 4] + hexDigits[i & 0x0F]; 198 } 199 200 216 public static final int hexToInt(String hex) 217 { 218 int len = hex.length(); 219 if (len > 16) 220 throw new NumberFormatException (); 221 222 int l = 0; 223 for (int i = 0; i < len; i++) 224 { 225 l <<= 4; 226 int c = Character.digit(hex.charAt(i), 16); 227 if (c < 0) 228 throw new NumberFormatException (); 229 l |= c; 230 } 231 return l; 232 } 233 234 } 235 236 | Popular Tags |