20 namespace Lucene.Net.Search.Highlight
27 private float maxScore;
29 protected internal int fgRMin, fgGMin, fgBMin;
30 protected internal int fgRMax, fgGMax, fgBMax;
32 protected internal int bgRMin, bgGMin, bgBMin;
33 protected internal int bgRMax, bgGMax, bgBMax;
57 public GradientFormatter(
float maxScore,
string minForegroundColor,
string maxForegroundColor,
string minBackgroundColor,
string maxBackgroundColor)
59 highlightForeground = (minForegroundColor != null) && (maxForegroundColor != null);
61 if (highlightForeground)
63 if (minForegroundColor.Length != 7)
65 throw new ArgumentException(
"minForegroundColor is not 7 bytes long eg a hex " +
"RGB value such as #FFFFFF");
67 if (maxForegroundColor.Length != 7)
69 throw new ArgumentException(
"minForegroundColor is not 7 bytes long eg a hex " +
"RGB value such as #FFFFFF");
71 fgRMin = HexToInt(minForegroundColor.Substring(1, 2));
72 fgGMin = HexToInt(minForegroundColor.Substring(3, 2));
73 fgBMin = HexToInt(minForegroundColor.Substring(5, 2));
75 fgRMax = HexToInt(maxForegroundColor.Substring(1, 2));
76 fgGMax = HexToInt(maxForegroundColor.Substring(3, 2));
77 fgBMax = HexToInt(maxForegroundColor.Substring(5, 2));
80 highlightBackground = (minBackgroundColor != null) && (maxBackgroundColor != null);
81 if (highlightBackground)
83 if (minBackgroundColor.Length != 7)
85 throw new System.ArgumentException(
"minBackgroundColor is not 7 bytes long eg a hex " +
"RGB value such as #FFFFFF");
87 if (maxBackgroundColor.Length != 7)
89 throw new System.ArgumentException(
"minBackgroundColor is not 7 bytes long eg a hex " +
"RGB value such as #FFFFFF");
91 bgRMin = HexToInt(minBackgroundColor.Substring(1, 2));
92 bgGMin = HexToInt(minBackgroundColor.Substring(3, 2));
93 bgBMin = HexToInt(minBackgroundColor.Substring(5, 2));
95 bgRMax = HexToInt(maxBackgroundColor.Substring(1, 2));
96 bgGMax = HexToInt(maxBackgroundColor.Substring(3, 2));
97 bgBMax = HexToInt(maxBackgroundColor.Substring(5, 2));
100 this.maxScore = maxScore;
104 public virtual string HighlightTerm(
string originalText,
TokenGroup tokenGroup)
114 var sb =
new System.Text.StringBuilder();
116 if (highlightForeground)
118 sb.Append(
"color=\"");
119 sb.Append(GetForegroundColorString(score));
122 if (highlightBackground)
124 sb.Append(
"bgcolor=\"");
125 sb.Append(GetBackgroundColorString(score));
129 sb.Append(originalText);
130 sb.Append(
"</font>");
131 return sb.ToString();
134 protected internal virtual string GetForegroundColorString(
float score)
136 int rVal = GetColorVal(fgRMin, fgRMax, score);
137 int gVal = GetColorVal(fgGMin, fgGMax, score);
138 int bVal = GetColorVal(fgBMin, fgBMax, score);
139 var sb =
new System.Text.StringBuilder();
141 sb.Append(IntToHex(rVal));
142 sb.Append(IntToHex(gVal));
143 sb.Append(IntToHex(bVal));
144 return sb.ToString();
147 protected internal virtual string GetBackgroundColorString(
float score)
149 int rVal = GetColorVal(bgRMin, bgRMax, score);
150 int gVal = GetColorVal(bgGMin, bgGMax, score);
151 int bVal = GetColorVal(bgBMin, bgBMax, score);
152 var sb =
new System.Text.StringBuilder();
154 sb.Append(IntToHex(rVal));
155 sb.Append(IntToHex(gVal));
156 sb.Append(IntToHex(bVal));
157 return sb.ToString();
160 private int GetColorVal(
int colorMin,
int colorMax,
float score)
162 if (colorMin == colorMax)
166 float scale = Math.Abs(colorMin - colorMax);
167 float relScorePercent = Math.Min(maxScore, score) / maxScore;
168 float colScore = scale * relScorePercent;
169 return Math.Min(colorMin, colorMax) + (int) colScore;
172 private static char[] hexDigits =
new char[]{
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'A',
'B',
'C',
'D',
'E',
'F'};
174 private static string IntToHex(
int i)
176 return "" + hexDigits[(i & 0xF0) >> 4] + hexDigits[i & 0x0F];
194 public static int HexToInt(
string hex)
196 int len = hex.Length;
198 throw new FormatException();
201 for (
int i = 0; i < len; i++)
204 int c = (int) System.Char.GetNumericValue(hex[i]);
206 throw new FormatException();