As pointed in Vlad's answer, you are running out of free color slots. One way to get around that would be to cache the colors: whenever you try a RGB combination, the routine should first check if the combination is in the cache; if it is in the cache, then it should use that one instead of creating a new one from scratch; new colors would then only be created if they're not yet in cache.
Here's the implementation I use; it uses XSSF plus Guava's LoadingCache and is geared towards generationg XSSF colors from CSS rgb(r, g, b)
declarations, but it should be relatively trivial to adapt it to HSSF:
private final LoadingCache<String, XSSFColor> colorsFromCSS = CacheBuilder.newBuilder()
.build(new CacheLoader<String, XSSFColor>() {
private final Pattern RGB = Pattern.compile("rgb\\(\\s*(\\d+)\\s*, \\s*(\\d+)\\s*,\\s*(\\d+)\\s*\\)");
@Override
public XSSFColor load(String style) throws Exception {
Matcher mat = RGB.matcher(style);
if (!mat.find()) {
throw new IllegalStateException("Couldn't read CSS color: " + style);
}
return new XSSFColor(new java.awt.Color(
Integer.parseInt(mat.group(1)),
Integer.parseInt(mat.group(2)),
Integer.parseInt(mat.group(3))));
}
});
Perhaps someone else could post a HSSF equivalent? ;)